-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
298 lines (206 loc) · 6.97 KB
/
Copy pathapp.js
File metadata and controls
298 lines (206 loc) · 6.97 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
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
// Requiring module
const express = require('express');
require('dotenv').config();
const path = require('path');
const fs = require("fs");
var mysql = require('mysql2');
const helmet = require('helmet');
const cors = require('cors');
// connect the database server to the backend server
var con = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
ssl: {
rejectUnauthorized: true,
}
});
// Creating express object
const app = express();
// Serve static files from the public directory
// app.use(express.static(path.join(__dirname, 'public')));
// set up ejs
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
// middle ware to proccess json
app.use(express.json());
app.use(express.urlencoded( { extended: true }));
// app.use(express.static(path.join(__dirname, "public")));
// allow cors
app.use(cors());
// middleware for saftey
app.use(helmet());
// Handling GET request
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
})
app.get("/contact", (req, res) => {
res.send("Contact page is working!");
})
app.get("/about", (req, res) => {
res.sendFile(path.join(__dirname, "public", "about.html"));
})
// end point to handle azure health check requests - idk why they send these so often lol
app.get('/admin/host/ping', (req, res) => {
// Logic to check the health status
const healthStatus = {
status: 'healthy',
timestamp: new Date().toISOString()
};
res.json(healthStatus);
});
// end point to check status
app.get('/admin/host/status', (req, res) => {
const healthStatus = {
status: 'healthy',
timestamp: new Date().toISOString()
};
res.json(healthStatus);
});
// get request - dynamically created book page
app.get("/bookpage", (req, res) => {
const title = req.query.title;
const genre = req.query.genre;
const author = req.query.author;
const date = req.query.date;
const isbn = req.query.isbn;
const path = req.query.path;
const desc = req.query.desc;
console.log(title, genre, author, date, path, desc);
res.render("bookpage", { title, genre, author, date, isbn, path, desc});
})
// get info from database
app.get("/buttons", (req, res) => {
con.query("SELECT * FROM Inventory", function (err, result, fields) {
if (err) {
res.status(500).send(err);
return;
}
console.log(result);
res.json(result);
});
})
// search all criteria from database
app.get("/buttons/search/all", (req, res) => {
let criteria = req.query.search;
let sql = `SELECT * FROM Inventory WHERE Title LIKE ? OR Genre LIKE ? OR Date LIKE ? OR Path LIKE ? OR Description LIKE ? OR Author LIKE ?`;
let values = [`%${criteria}%`,`%${criteria}%`, `%${criteria}%`, `%${criteria}%`, `%${criteria}%`, `%${criteria}%`, `%${criteria}%`];
con.query(sql, values, function(err, result, fields) {
if (err) throw err;
res.json(result);
});
})
// search by title
app.get("/buttons/search/title", (req, res) => {
let title = req.query.name;
console.log("the title is", title);
let sql = "SELECT * FROM Inventory WHERE Title LIKE ?";
let value = `%${title}%`;
con.query(sql, [value], function(err, result, fields) {
if (err) throw err;
console.log("searching all book that match title");
res.json(result);
})
})
// search by genre
app.get("/buttons/search/genre", (req, res) => {
let genre = req.query.name;
let value = `%${genre}%`;
console.log(genre);
let sql = "SELECT * FROM Inventory WHERE Genre LIKE ?";
con.query(sql, [value], function(err, result, fields) {
if (err) throw err;
console.log("searching all books with matching genre");
res.json(result);
})
})
// search by date
app.get("/buttons/search/date", (req, res) => {
let date = req.query.name;
let value = `%${date}%`;
console.log(date);
let sql = `SELECT * FROM Inventory WHERE Date LIKE ?`;
con.query(sql, [value], function(err, result, fields) {
if (err) throw err;
console.log("searching all books with matching date");
res.json(result);
})
})
// search by author
app.get("/buttons/search/author", (req, res) => {
let author = req.query.name;
let sql = `SELECT * FROM INVENTORY WHERE Author LIKE ?`;
let value = `%${author}%`;
con.query(sql, [value], function(err, result, fields) {
if (err) throw err;
console.log("searching for all matching authors");
res.json(result);
})
})
// search by isbn
app.get("/buttons/search/isbn", (req, res) => {
let isbn = req.query.name;
let sql = `SELECT * FROM Inventory WHERE ISBN LIKE ?`;
let value = `%${isbn}%`;
con.query(sql, [value], function(err, result, fields) {
if (err) throw err;
console.log("searching for all matching ISBN's");
res.json(result);
})
})
// insert into database
app.post("/buttons", (req, res) => {
console.log(req.body);
let entryId = req.body["EntryId"];
let title = req.body["Title"];
let genre = req.body["Genre"];
let date = req.body["Date"];
let isbn = req.body["ISBN"];
console.log(date);
let sql = "INSERT INTO Inventory (Title, Genre, Date, ISBN) VALUES (?, ?, ?, ?)";
con.query(sql, [title, genre, date, isbn], function (err, result, fields) {
if (err) throw err;
console.log("1 record inserted");
})
})
// replace info in the database
app.put("/buttons", (req, res) => {
console.log(req.body);
let title = req.body["Title"];
let genre = req.body["Genre"];
let date = req.body["Date"];
let isbn = req.body["ISBN"];
let entryId = req.body["EntryId"]
console.log(isbn);
if (err) throw err;
console.log("shitter");
let sql = "UPDATE Inventory SET Title = ?, Genre = ?, Date = ?, ISBN = ? WHERE EntryId = ?";
con.query(sql, [title, genre, date, isbn, entryId], function (err, result, fields) {
if (err) throw err;
console.log("1 record edited");
})
})
// delete info in the database
app.delete("/buttons", (req, res) => {
console.log("back end got the request");
let id = req.body["id"];
if (!id) {
return res.status(400).send("ID is required to delete the record.");
}
console.log("shitter");
let sql = "DELETE FROM Inventory WHERE EntryId = ?";
con.query(sql, [id], function (err, result, fields) {
if (err) throw err;
console.log("1 record removed");
})
})
// Handles leftover non caught requests
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
})
// Port Number
const PORT = process.env.PORT ||5001;
// Server Setup
app.listen(PORT,console.log(`Server started on port ${PORT}`));