-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (149 loc) · 4.94 KB
/
index.js
File metadata and controls
174 lines (149 loc) · 4.94 KB
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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
import pg from "pg";
import 'dotenv/config';
import methodOverride from 'method-override';
const app = express();
app.use(methodOverride('_method')); // Add this before defining any routes
const port = 3000;
const db= new pg.Client({
user:"postgres",
host:"localhost",
database: process.env.id,
password: process.env.pass,
port: process.env.port,
});
db.connect();
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true}));
async function getBookCover(title) {
try {
const encodedTitle = encodeURIComponent(title);
const coverUrl = `https://covers.openlibrary.org/b/title/${encodedTitle}-M.jpg`;
// Check if the URL is valid
const response = await axios.get(coverUrl, { responseType: "arraybuffer" });
if (response.status === 200) {
return coverUrl; // Valid cover URL
}
} catch (error) {
console.error("Error fetching cover image:", error);
}
// Return a default cover URL if no cover is found
return "https://via.placeholder.com/128x192?text=No+Cover+Available";
}
app.get("/", async (req, res) => {
const sortOpt = req.query.sort;
let sortQuery = "ORDER BY id ASC"; // default sorting
//sort query based on user selection
switch (sortOpt) {
case "rating_desc":
sortQuery = "ORDER BY rating DESC";
break;
case "rating_asc":
sortQuery = "ORDER BY rating ASC";
break;
case "date_desc":
sortQuery = "ORDER BY date_read DESC";
break;
case "date_asc":
sortQuery = "ORDER BY date_read ASC";
break;
}
try {
const result = await db.query(`SELECT * FROM books ${sortQuery}`);
res.render("index.ejs", {
books: result.rows,
});
} catch (error) {
console.error('Error rendering the page:', error);
res.status(500).send('Internal Server Error');
}
});
app.post("/books/add", async(req, res)=>{
const newBook ={
title: req.body.title,
author: req.body.author,
rating: req.body.rating,
notes: req.body.notes,
date_read: req.body.date_read,
}
const url = await getBookCover(req.body.title);
try{
await db.query(
"INSERT INTO books (title, author, cover_url, rating, date_read, notes) VALUES ($1, $2, $3, $4, $5, $6)",
[newBook.title, newBook.author, url, newBook.rating, newBook.date_read, newBook.notes]
);
res.redirect("/");
} catch (err){
console.log(err);
}
});
//This route fetches the book details from the database and renders the edit form.
app.get('/books/edit/:id', async(req, res)=>{
const { id } = req.params;
try{
const result = await db.query("SELECT * FROM books WHERE id = $1", [id]);
if(result.rows.length > 0){
const books = result.rows[0];
res.render("edit.ejs", {book: books}); // render injes. ejs file
} else {
res.status(404).send("Book not found");
}
} catch(err){
console.log(err);
res.status(500).send("Server error");
}
});
app.post("/books/edit/:id", async (req, res) => {
const { id } = req.params;
const { title, author, rating, date_read, notes } = req.body;
try {
await db.query(
"UPDATE books SET title = $1, author = $2, rating = $3, date_read = $4, notes = $5 WHERE id = $6",
[title, author, rating, date_read || null, notes, id]
);
res.redirect("/"); // Redirect to the homepage after updating
} catch (err) {
console.error(err);
res.status(500).send("Server error");
}
});
app.delete("/books/delete/:id", async (req, res)=>{
const { id } = req.params;
console.log("Delete request received for ID:", id);
try{
const result = await db.query(
"DELETE FROM books WHERE id = $1",
[id]
);
console.log("Deletion result:", result.rowCount);
if(result.rowCount === 0){
console.log(`No book found with ID: ${id}`);
res.status(404).send("Book not found");
} else {
console.log(`Successfully deleted book with ID: ${id}`);
res.redirect("/");
}
} catch(err){
console.log(err);
res.status(500).send("Server error");
}
});
app.get("/books/details/:id", async(req,res)=>{
const { id } = req.params;
try{
const result = await db.query("SELECT * FROM books WHERE id = $1", [id]);
if(result.rows.length > 0){
const books = result.rows[0];
res.render("bookDetails.ejs", {book: books});
} else {
res.status(404).send("Book not found");
}
} catch(err){
console.log(err);
}
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});