-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (100 loc) · 4.6 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
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
import env from "dotenv"
env.config();
const app = express();
const port = 3000;
const db = new pg.Client({
user: process.env.user_Name,
host:"localhost",
database:"Book Hub",
password: process.env.database_password,
port:5432
});
db.connect();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
let books = [
{ id: 1, title: "The Particular Sadness of Lemon Cake by Aimee Bender", isbn:"9780385533225", rating: 9.5, review:"The Particular Sadness of Lemon Cake by Aimee Bender is a poignant novel that explores the complexities of family and personal identity through the lens of magical realism. The story follows Rose Edelstein, a young girl who discovers she can taste the emotions of the person who made her food, beginning with her mother's lemon cake imbued with deep sadness. This unique ability forces Rose to confront the hidden emotional lives of her family members,Bender's lyrical prose and imaginative premise offer a compelling exploration of emotional isolation, connection, and self-discovery. Though the book received mixed reviews for its plot resolution, it is celebrated for its rich character development and emotional depth.revealing layers of secrecy and personal struggles.",
reviewDate:'2024-07-30'
},
{ id: 2, title: "The Particular Sadness of Lemon Cake by Aimee Bender", isbn:"0385472579", rating: 9.5, review:"The Particular Sadness of Lemon Cake by Aimee Bender is a poignant novel that explores the complexities of family and personal identity through the lens of magical realism. The story follows Rose Edelstein, a young girl who discovers she can taste the emotions of the person who made her food, beginning with her mother's lemon cake imbued with deep sadness. This unique ability forces Rose to confront the hidden emotional lives of her family members,Bender's lyrical prose and imaginative premise offer a compelling exploration of emotional isolation, connection, and self-discovery. Though the book received mixed reviews for its plot resolution, it is celebrated for its rich character development and emotional depth.revealing layers of secrecy and personal struggles.",
reviewDate:'2024-07-30'
}
];
app.get("/", async(req, res)=>{
const result = await db.query("select * from books");
books = result.rows;
res.render("index.ejs", {
listItems:books,
});
})
app.get("/new", (req, res)=>{
res.render("addBook.ejs");
})
app.get("/back", (req, res)=>{
res.render("index.ejs", {
listItems:books
});
})
app.get("/viewReview/:id", (req, res) => {
const bookId = parseInt(req.params.id);
const book = books.find(b => b.id === bookId);
if (book) {
res.render("viewReview.ejs", { book });
} else {
res.status(404).send("Book not found");
}
});
app.get("/detail/:id", (req, res)=>{
const bookId = parseInt(req.params.id);
const book = books.find(b => b.id === bookId);
if(book){
res.render("searchDetails.ejs", {book});
} else {
res.status(404).send("Book not found");
}
});
app.post("/addBook", async (req, res)=>{
const inputTitle = req.body.title;
const inputIsbn = req.body.isbn;
const inputRating = req.body.rating;
const inputReview = req.body.review;
const inputDate = req.body.date;
await db.query("insert into books (title, isbn, rating, review, reviewdate) values($1, $2, $3, $4, $5);",[inputTitle, inputIsbn, inputRating, inputReview, inputDate]);
res.redirect("/");
});
app.post("/edit", async(req, res)=>{
const bookId = req.body.bookid;
const updatedReview = req.body.updatedReview;
await db.query("update books set review = $1 where id=$2;",[updatedReview, bookId]);
res.redirect("/");
});
app.get("/suggest", async(req, res)=>{
const searchTerm = req.query.q; // Get the search term from the query string
console.log(searchTerm);
try {
const results = await db.query(
'SELECT title, isbn, id FROM books WHERE title ILIKE $1 LIMIT 10',
[`%${searchTerm}%`]
);
res.json(results.rows); // Send the matching results as JSON
} catch (err) {
console.error(err);
res.status(500).send('Error querying database');
}
});
app.post("/sort-criteria", async(req, res)=>{
const criteria = req.body.criteria;
console.log(criteria);
const result = await db.query(`select * from books order by ${criteria} asc`);
books = result.rows;
res.render("index.ejs",{
listItems: books
});
})
app.listen(port, ()=>{
console.log("sever running on port 3000");
})