-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
259 lines (211 loc) · 8.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import pg from "pg";
import pixels from 'image-pixels';
dotenv.config();
const app = express();
const port = process.env.PORT; // set up a port config on your .env file
// PostgreSQL db config
const db = new pg.Client({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
ssl: process.env.SSL === 'true' ? { rejectUnauthorized: false } : false,
})
db.connect() // to connect to DATABASE
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
// this URL is used for searching for all the informations of the book.
const searchURL = "https://openlibrary.org/search.json";
// to verify if book cover image exists
async function checkImageUrl(isbn){
const imageUrl = `https://covers.openlibrary.org/b/isbn/${isbn}-L.jpg`;
const fallbackImage = 'images/image.png';
const { width , height } = await pixels(imageUrl); // return width,height in PX
if(width < 100){
return fallbackImage;
}
return imageUrl
}
// SEARCH for books and their info from the Open Library using their API
app.post("/search", async (req, res) => {
const book = req.body.input.trim();// used to eleminate unessecary/accidental spacing
if (!book || (typeof book !== 'string' && typeof book !== 'number')) { // the make sure the input isn't null
return res.status(400).send("Invalid input. Please enter a valid book title.");
}
console.log("Book name: ", book);
try {
const searchResponse = await axios.get(`${searchURL}?q=${encodeURIComponent(book)}&limit=1`); // to get all the infos of a book only from the it's title
//here I Mapped the first book's data to a new object with only the fields I need which will enhance performace and response speed a LOT compared to getting the full response from OL API
const firstBook = searchResponse.data?.docs?.[0] && {
title: searchResponse.data.docs[0].title,
author_name: searchResponse.data.docs[0].author_name?.[0], // get first author
author_key: searchResponse.data.docs[0].author_key?.[0], // get first author key for author picture
first_publish_year: searchResponse.data.docs[0].first_publish_year,
isbn: searchResponse.data.docs[0].isbn?.[0], // get fourth ISBN for book cover
subject: searchResponse.data.docs[0].subject?.[0], // get first subject which is the genre
first_sentence: searchResponse.data.docs[0].first_sentence?.[0], // get first sentence of the book
};
console.log("book isbn: ", firstBook.isbn);
if (!firstBook) {
return res.status(404).render("booksInfo.ejs", { error: "No book found by that entry. Please try again with a different book." });
}
// Get valid image URL or fallback image
const imageUrl = await checkImageUrl(firstBook.isbn);
res.render("booksInfo.ejs", {
searchResult: firstBook ? { ...firstBook, imageUrl } : null, // to verify if the first book exists or not
error: firstBook ? null : "No book found by that entry. Please try again.",
});
console.log("Data sent to EJS:", { searchResult: firstBook });
} catch (err) {
console.error("Error fetching books data:", err);
res.status(500).render("booksInfo.ejs", { error: "No book found by that entry. Please try again with a different book." });
}
})
let error = "";
async function getRead() { // get only read books by latest date
const result = await db.query(
`SELECT id, title, author, rating, read_at, genre, isbn
FROM books
WHERE is_read = TRUE
ORDER BY read_at DESC;`
);
return result.rows;
}
app.get("/", async (req, res) => { // get only read books by latest date
try {
const readbooks = await getRead();
console.log("books :", readbooks);
res.render("index.ejs", {
books: readbooks,
error: error
})
} catch (err) {
console.error("Error fetching books:", err);
res.render("index.ejs", {
books: [],
error: "An error occurred while fetching books."
});
}
})
async function getRating() { // get rating of read books and order by descending
const result = await db.query(`SELECT id, title, author, rating, read_at, genre, isbn
FROM books
WHERE is_read = TRUE
ORDER BY rating DESC;`);
return result.rows;
}
app.get("/byRating", async (req, res) => { // get rating of read books and order by descending
try {
const ratedBooks = await getRating();
console.log("Rated books :", ratedBooks);
res.render("index.ejs", {
books: ratedBooks,
error: error
})
} catch (err) {
console.error("Error fetching books:", err);
res.render("index.ejs", {
books: [],
error: "An error occurred while fetching books."
});
}
})
async function getTitle() { // get titles of read books by Alphabetical order
const result = await db.query(`SELECT id, title, author, rating, read_at, genre, isbn
FROM books
WHERE is_read = TRUE
ORDER BY title ASC;`);
return result.rows;
}
app.get("/byTitle", async (req, res) => { // get titles of read books by Alphabetical order
try {
const orderByTitle = await getTitle();
console.log("Books ordered by title ASC :", orderByTitle);
res.render("index.ejs", {
books: orderByTitle,
error: error
})
} catch (err) {
console.error("Error fetching books:", err);
res.render("index.ejs", {
books: [],
error: "An error occurred while fetching books."
});
}
})
async function getWatchList() { // get books that are on watch list
const result = await db.query(`SELECT id, title, author, rating, genre, isbn
FROM books
WHERE is_in_watchlist = TRUE
ORDER BY title ASC;`);
return result.rows;
}
app.get("/byWatchList", async (req, res) => { // get books that are on watch list
try {
const watchList = await getWatchList();
console.log("Books that are on watch-list :", watchList);
res.render("index.ejs", {
books: watchList,
error: error
})
} catch (err) {
console.error("Error fetching books:", err);
res.render("index.ejs", {
books: [],
error: "An error occurred while fetching books."
});
}
})
app.get("/genre", async (req, res) => { // get books by genre and display them by extracting queries
try {
const selectedGenre = req.query.genre; // extracting the genre from the query params because we are using get request
const result = await db.query(`SELECT id, title, author, rating, genre, isbn
FROM books
WHERE genre = $1
ORDER BY title ASC;`, [selectedGenre]); // passing the genre as a param to prevent SQL injection as a good practice
const books = result.rows;
console.log("Books by selected genre:", books);
res.render("index.ejs", {
books: books,
error: null
});
} catch (err) {
console.error("Error fetching books:", err);
res.render("index.ejs", {
books: [],
error: "Sorry I haven't read any book from this Genre."
});
}
})
app.get("/opinion", async (req, res) => {
try {
const selectedId = req.query.id; // extracting the book id from the query params to help identify the book and search for the users opinion on the db.
const result = await db.query(`SELECT id, title, author, rating, genre, isbn, opinion
FROM books
WHERE id = $1;`, [selectedId]); // passing the book id as a param to prevent SQL injection as a good practice
const opinionResults = result.rows;
console.log("Book opinion data is : ", opinionResults)
res.render("bookReview.ejs", {
book: opinionResults[0],// to access the first book of the array so the data can be displayed on my EJS file. PS: i'm still trying to figure out why i should use the [0] even tho it's only returning one book.
error: null
});
} catch (err) {
console.error("Error fetching books:", err);
res.render("index.ejs", {
books: [],
error: "Sorry I haven't written an opinion for this book just yet."
});
}
})
app.use(async (req, res) => { // When a user searchs for a route that doesn't exist this error message will be displayed
res.status(404).send("Could not find what you are looking for.")
})
app.listen(port, () => {
console.log(`Server is running on port: ${port}`)
});