-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbServices.js
330 lines (303 loc) · 11.7 KB
/
dbServices.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const mysql = require('mysql');
const dotenv = require('dotenv');
let instance = null;
dotenv.config();
const connection = mysql.createConnection({
host: 'localhost',
user: 'AMA',
password: 'AMA2023',
database: 'bookwise',
port: 3306
});
connection.connect((err) => {
if (err) {
console.log(err.message);
}
console.log('db ' + connection.state);
});
class DbService {
static getDbServiceInstance() {
//return new DbService();
return instance ? instance : new DbService();
}
async addNewBook(title, description, categoryId, coverPhoto, publishDate, buyUrl, authorName, userId) {
try {
const response = await new Promise((resolve, reject) => {
const query = "INSERT INTO books (title, description, category_id, cover_photo, publication_date, buy_link, author_name, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
connection.query(query, [title, description, categoryId, coverPhoto, publishDate, buyUrl, authorName, userId], (err, results) => {
if (err) reject(new Error(err.message));
else {
console.log('New book added:', results);
resolve(results);
}
});
});
return response;
} catch (error) {
throw error;
}
}
async getAllBooksPagination(page, limit) {
//console.log('db service');
try {
const offset = (page - 1) * limit;
const query = `
SELECT title, book_id, LEFT(description, 120) AS short_description, cover_photo, author_name
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY category_id ORDER BY book_id) AS rn
FROM books
) AS ranked_books
WHERE rn <= 1
ORDER BY category_id, book_id
LIMIT 8 OFFSET ?;
`;
const results = await new Promise((resolve, reject) => {
connection.query(query, [offset], (err, results) => {
if (err) reject(new Error(err.message));
else resolve(results);
});
});
return results;
} catch (error) {
throw error;
}
}
async getBooksByCategoryWithPagination(categoryId, page, limit) {
//console.log('db service');
try {
const offset = (page - 1) * limit;
const query = `
SELECT title, LEFT(description, 120) AS short_description, author_name, cover_photo, book_id
FROM books
WHERE category_id = (SELECT category_id FROM categories WHERE category_name= ?)
ORDER BY book_id
LIMIT ? OFFSET ?
`;
const results = await new Promise((resolve, reject) => {
connection.query(query, [categoryId, limit, offset], (err, results) => {
if (err) reject(new Error(err.message));
else resolve(results);
});
});
return results;
} catch (error) {
throw error;
}
}
async getBookInfo(bookId) {
//console.log('db service');
try {
const query = `
SELECT title, book_id, description, cover_photo, author_name, category_id, buy_link FROM books WHERE book_id = ?; `;
const results = await new Promise((resolve, reject) => {
connection.query(query, [bookId], (err, results) => {
if (err) reject(new Error(err.message));
else resolve(results);
});
});
//console.log(results);
return results;
} catch (error) {
throw error;
}
}
async likeBook(userId, bookId) {
try {
const query = "INSERT INTO rates (book_id, user_id, `like`) VALUES (?, ?, '1')";
const results = await new Promise((resolve, reject) => {
connection.query(query, [bookId, userId], (err, results) => {
if (err) {
reject(new Error(err.message));
} else {
resolve(results);
}
});
});
return results;
} catch (error) {
throw error;
}
}
async checkLikeBook(userId, bookId) {
try {
const query = "SELECT `like` FROM rates WHERE book_id=? AND user_id=?;";
const results = await new Promise((resolve, reject) => {
connection.query(query, [bookId, userId], (err, results) => {
if (err) {
reject(new Error(err.message));
} else {
resolve(results);
}
});
});
return results;
} catch (error) {
throw error;
}
}
async getFavorite(userId) {
try {
const query = "SELECT b.title, LEFT(b.description, 120) AS short_description, b.author_name, b.cover_photo, b.book_id FROM books b JOIN rates r ON b.book_id = r.book_id WHERE r.user_id = ? AND r.`like` = 1; ";
const results = await new Promise((resolve, reject) => {
connection.query(query, [userId], (err, results) => {
if (err) {
reject(new Error(err.message));
} else {
resolve(results);
}
});
});
console.log('fav',results);
return results;
} catch (error) {
throw error;
}
}
async totalBooksCount() {
return new Promise((resolve, reject) => {
connection.query('SELECT COUNT(*) AS total_books FROM books', function (error, results, fields) {
if (error) {
console.error(error);
reject(error);
} else {
const totalBooksCount = results[0].total_books;
resolve(totalBooksCount);
}
});
});
}
async totalBooksCountByCategory(categoryName) {
try {
const query = `SELECT COUNT(*) AS total_books FROM books WHERE category_name ?;`;
const results = await new Promise((resolve, reject) => {
connection.query(query,[categoryName], (err, results) => {
if (err) reject(new Error(err.message));
else resolve(results[0].total_books);
});
});
return results;
} catch (error) {
throw error;
}
}
async findEmail(inputEmail) {
try {
const response = await new Promise((resolve, reject) => {
const query = "SELECT email FROM users WHERE email=?";
connection.query(query, [inputEmail], (err, results) => {
if (err) {
reject(new Error(err.message));
} else {
if (results.length > 0) {
resolve(false); //indicates that the email already exist
} else {
resolve(true); // indicates that email is new to the database
}
}
});
});
// console.log(response);
return response;
// // Check if the response contains any rows (emails)
// // if (response.length > 0) {
// // console.log("Email found in the database");
// // // You can return a custom object or value to indicate the email was found
// // return { data: response };
// // } else {
// // console.log("Email not found in the database");
// // // You can return a custom object or value to indicate the email was not found
// // return false;
// // }
} catch (error) {
//console.log(error);
// You can throw the error to be caught by the caller
throw error;
}
}
// async verifyPassword(inputEmail,inputPassword){
// try {
// const response = await new Promise((resolve, reject) => {
// const realPasswordQuery = "SELECT password FROM users WHERE email=?";
// connection.query(realPasswordQuery, [inputEmail], (err, results) => {
// if (err) {
// reject(new Error(err.message));
// } else {
// if (results.length > 0) {
// const realPassword = results[0].password;
// if (realPassword === inputPassword) {
// resolve(true); // Password is correct
// } else {
// resolve(false); // Password is incorrect
// }
// } else {
// resolve(false); // User not found or password not retrieved
// }
// }
// });
// });
// return response;
// } catch (error) {
// //console.log(error);
// // You can throw the error to be caught by the caller
// throw error;
// }
// }
async verifyUser(email,password){
try{
const response = await new Promise((resolve, reject) => {
const query = "SELECT user_id,first_name FROM users WHERE email=? AND password=?;";
connection.query(query, [email,password], (err, results) => {
if (err) reject(new Error(err.message));
else{
console.log('User logged:',results);
resolve(results);
}
})
});
return response;
}
catch (error) {
//console.log(error);
// You can throw the error to be caught by the caller
throw error;
}
}
async addUser(email, password, firstName, lastName){
try{
const response = await new Promise((resolve, reject) => {
const query = "INSERT INTO users (email, password, first_name, last_name) VALUES (?, ?, ?, ?)";
connection.query(query, [email,password,firstName,lastName], (err, results) => {
if (err) reject(new Error(err.message));
else{
console.log('User Added:',results);
resolve(results);
}
})
});
return response;
}
catch (error) {
//console.log(error);
// You can throw the error to be caught by the caller
throw error;
}
}
async getUserEssInfo(email){
try {
const response = await new Promise((resolve, reject) => {
const query = "SELECT user_id,first_name FROM users WHERE email=?;";
connection.query(query, [email], (err, results) => {
if (err) reject(new Error(err.message));
resolve(results);
})
});
// console.log(response);
return response;
} catch (error) {
//console.log(error);
// You can throw the error to be caught by the caller
throw error;
}
}
}
module.exports = DbService;