-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
199 lines (165 loc) · 4.46 KB
/
index.js
File metadata and controls
199 lines (165 loc) · 4.46 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
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
import express from 'express'
import axios from 'axios'
import pg from 'pg'
import bodyparser from 'body-parser'
const app = express();
const port = 3000;
const API_URL= 'https://covers.openlibrary.org/b/isbn/';
app.use(express.static('public'));
app.use(bodyparser.urlencoded({extended: true}));
const db = new pg.Client({
user: 'postgres',
host: 'localhost',
database: 'book_notes',
password: 'post2g',
port: 5432,
});
db.connect();
var currentBook;
async function getBooks() {
try{
const result = await db.query("SELECT id,title,rating,to_char(read_time,'DD/MM/YYYY') as time,description,coverurl FROM BOOKS");
return result.rows;
}catch(err)
{
console.log(err.message);
}
}
async function addbook(ISBN,title,rating,description,date) {
try
{
const coverurl = API_URL+ISBN+'-L.jpg';
await db.query('INSERT INTO BOOKS VALUES ($1,$2,$3,$4,$5,$6)',[ISBN,title,rating,date,description,coverurl]);
}catch(error)
{
console.log(error);
}
}
async function editbook(previousId,ISBN,title,rating,description,date) {
try
{
const coverurl = API_URL+ISBN+'-L.jpg';
await db.query('update books set id=$1 where id=$2',[ISBN,previousId]);
await db.query('update books set title=$1 where id=$2',[title,previousId]);
await db.query('update books set rating=$1 where id=$2',[rating,previousId]);
await db.query('update books set description=$1 where id=$2',[description,previousId]);
await db.query('update books set read_time=$1 where id=$2',[date,previousId]);
await db.query('update books set coverurl=$1 where id=$2',[coverurl,previousId]);
}catch(error)
{
console.log(error);
}
}
async function deleteBook(id) {
try{
await db.query('delete from books where id=$1',[id]);
}
catch(error)
{
console.log(error);
}
}
async function getNotes(bookid) {
try{
const result = await db.query('select * from notes where bookid = $1',[bookid]);
return result.rows
}catch(err)
{
console.log(err.message);
}
}
async function addNote(content) {
try
{
await db.query('insert into notes(content,bookid) values($1,$2) ',[content,currentBook]);
}catch(error)
{
console.log(error);
}
}
async function editNote(note,content) {
try
{
await db.query('update notes set content=$1 where id=$2',[content,note]);
}catch(error)
{
console.log(error);
}
}
async function deleteNote(id) {
try{
await db.query('delete from notes where id=$1',[id]);
}
catch(error)
{
console.log(error);
}
}
app.get('/',async(req,res)=>
{
const books = await getBooks();
res.render('index.ejs',{
books: books
})
});
app.post('/addbook',async(req,res)=>{
const ISBN = req.body.bookid;
const title = req.body.bookTitle;
const description = req.body.bookDescription;
const date = req.body.readTime;
const rating = req.body.rating;
console.log(req);
await addbook(ISBN,title,rating,description,date);
res.redirect('/');
});
app.post('/edit',async(req,res)=>
{
const previousId = req.body.book;
const ISBN = req.body.bookid;
const title = req.body.bookTitle;
const description = req.body.bookDescription;
const date = req.body.readTime;
const rating = req.body.rating;
await editbook(previousId,ISBN,title,rating,description,date);
res.redirect('/');
});
app.post('/delete',async(req,res)=>{
const id = req.body.book;
await deleteBook(id);
res.redirect('/');
})
app.post('/view',async(req,res)=>
{
currentBook = parseInt(req.body.book);
res.redirect('/view');
});
app.get('/view',async(req,res)=>
{
const notes = await getNotes(currentBook);
res.render('notes.ejs',{
notes: notes
})
});
app.post('/addnote',async(req,res)=>
{
const content = req.body.content;
await addNote(content);
res.redirect('/view');
});
app.post('/editnote',async(req,res)=>
{
const note = req.body.note;
const content = req.body.Editedcontent;
await editNote(note,content);
res.redirect('/view');
});
app.post('/deletenote',async(req,res)=>
{
const note = req.body.note;
await deleteNote(note);
res.redirect('/view');
})
app.listen(port,()=>
{
console.log(`listening to port ${port}`);
})