This repository was archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
340 lines (296 loc) · 12.9 KB
/
Copy pathserver.js
File metadata and controls
340 lines (296 loc) · 12.9 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
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
331
332
333
334
335
336
337
338
339
340
/**
* @file server.js
* @brief This file implements a server using Express.js framework to handle HTTP requests and interact with a database.
* The server provides various routes for different website pages and performs CRUD operations on the database tables.
* Author: Nils Streedain (https://github.com/nilsstreedain)
*/
// Import required libraries and dependencies
const express = require('express');
const exphbs = require('express-handlebars');
// Database connector module
var db = require('./db-connector')
// Define port number from environment variables, or use 6784 as default
const PORT = process.env.PORT || 6784;
// Define website pages and their routes
const pages = [
{title: 'Home', url: '/'},
{title: 'Customers', url: '/customers'},
{title: 'Orders', url: '/orders'},
{title: 'OrderBooks', url: '/orderbooks'},
{title: 'Books', url: '/books'},
{title: 'BookGenres', url: '/bookgenres'},
{title: 'Genres', url: '/genres'}
];
// Define foreign key and key information for database tables
const IDs = {
'Customers': {
"keys": ['customerID'],
"foriegnKeys": [{"name": "favoriteGenre", "key": "genreID", "refColumn": "name"}]
},
'Orders': {
"keys": ['orderID'],
"foriegnKeys": [{"key": "customerID", "name": "customerID", "refColumn": "name"}]
},
'OrderBooks': {
"keys": [],
"foriegnKeys": [
{"key": "orderID", "name": "orderID", "refColumn": "orderID"},
{"key": "bookID", "name": "bookID", "refColumn": "title"}
]
},
'Books': {
"keys": ['bookID'],
"foriegnKeys": []
},
'BookGenres': {
"keys": [],
"foriegnKeys": [
{"key": "bookID", "name": "bookID", "refColumn": "title"},
{"key": "genreID", "name": "genreID", "refColumn": "name"}
]
},
'Genres': {
"keys": ['genreID'],
"foriegnKeys": []
}
};
// Initialize an Express application
const app = express();
// Initialize an instance of express-handlebars
const hbs = exphbs.create({});
// Register handlebars helper function for checking equality
hbs.handlebars.registerHelper('isEqual', function(a, b, options) {
// Check if a is equal to b and return the appropriate template block
return a === b ? options.fn(this) : options.inverse(this);
});
// Register handlebars helper function for retrieving entry keys
hbs.handlebars.registerHelper('entryKeys', function(a, b) {
// Check if the number of keys for the specified entry is 1 and Return the value of the single key
if (IDs[b].keys.length == 1)
return a[IDs[b].keys[0]];
else
// Return a concatenated string of two foreign keys
return a[IDs[b].foriegnKeys[0].key] + "_" + a[IDs[b].foriegnKeys[1].key];
});
// Register handlebars helper function for checking if a key is the primary key of an entity
hbs.handlebars.registerHelper('entityKey', function(a, b) {
// Check if the entity has a single primary key and if it matches the provided key
if (IDs[a].keys.length === 1 && IDs[a].keys[0] === b)
// Return true if the key matches the primary key
return true;
// Return false if the key doesn't match the primary key or there are multiple primary keys
return false;
});
// Register handlebars helper function for checking if a key is a multiple choice key
hbs.handlebars.registerHelper('multipleChoice', function(a, b) {
// Loop through the foreign keys of the specified entity
for (let i = 0; i < IDs[a].foriegnKeys.length; i++)
// Check if the name of the foreign key matches the provided key
if (IDs[a].foriegnKeys[i].name == b)
// Return true if the key is a multiple choice key
return true;
// Return false if the key is not a multiple choice key
return false;
});
// Register handlebars helper function for looking up and iterating over an array of items
hbs.handlebars.registerHelper('lookupAndEach', function(context, key, options) {
// Retrieve the array of items from the context using the specified key
const items = context[key];
// Initialize an empty string to store the rendered output
let out = "";
// Check if the items is an array
if(Array.isArray(items))
// Loop through each item in the array and render the block of the template for each item
for(let i = 0; i < items.length; i++)
out += options.fn(items[i]);
// Return the rendered output
return out;
});
// Set 'hbs' as the templating engine for the Express app
app.engine('hbs', exphbs.engine({extname: '.hbs'}));
app.set('view engine', 'hbs');
// Middleware for parsing JSON bodies from HTTP requests
app.use(express.json());
// Serve static files from the 'public' directory
app.use(express.static('public'));
// Function to handle database modifying queries (Insert, Update, Delete)
function modQuery(sql, params, res, url) {
console.log(sql);
db.pool.query(sql, params, (error, results) => {
if (error) res.status(500).json({error: error.sqlMessage});
else res.redirect(url);
});
}
// Function to render the page with data fetched from the database
function renderPage(res, title, tables, tableResults, error) {
res.render('table', {
title: title,
data: tableResults,
tables: tables ? tables : "",
error: error ? error.sqlMessage : "",
pages
});
}
// Function to handle SQL statements for Update and Delete operations
const handleSQLStatement = (action, req, res, url, title) => {
// Retrieve the keys of the request body object
const keys = Object.keys(req.body);
// Generate the SQL SET clause by mapping the keys and values of the request body
const sqlSet = keys.slice(IDs[title].keys.length).map(key => `${key} = '${req.body[key]}'`).join(', ');
let sqlConditions = "";
// Check if the entity has a single primary key
if (IDs[title].keys.length == 1) {
// Handle SQL conditions for single primary key
if (action === 'UPDATE')
sqlConditions = IDs[title].keys[0] + " = " + req.body[IDs[title].keys[0]];
else if (action === 'DELETE FROM')
sqlConditions = IDs[title].keys[0] + " = " + req.body.id;
} else {
// Handle SQL conditions for multiple primary keys
if (action === 'UPDATE')
sqlConditions = IDs[title].foriegnKeys[0].key + " = " + req.body[IDs[title].foriegnKeys[0].key] + " AND " + IDs[title].foriegnKeys[1].key + " = " + req.body[IDs[title].foriegnKeys[1].key];
else if (action === 'DELETE FROM')
sqlConditions = IDs[title].foriegnKeys[0].key + " = " + req.body.id.split("_")[0] + " AND " + IDs[title].foriegnKeys[1].key + " = " + req.body.id.split("_")[1];
}
// Generate the complete SQL statement
const sql = `${action} ${title} ${action === 'UPDATE' ? 'SET ' + sqlSet : ''} WHERE ${sqlConditions}`;
// Call the modQuery function with the SQL statement, transformed values, and response object
modQuery(sql, Object.values(req.body).toString().split("_").map(Number), res, url);
}
// Create routes for each page
pages.forEach(({title, url}) => {
if (title == 'Home') {
// Route for the home page
app.get(url, (req, res) => {
// Render the home template with the title and pages data
res.render('home', {
title: title,
pages
});
});
} else {
// Route for other pages
app.get(url, (req, res) => {
// Execute a SQL query to select all data from the specified table
db.pool.query(`SELECT * FROM ${title}`, (error, tableResults) => {
if (error)
console.log(error.sqlMessage);
// Query the INFORMATION_SCHEMA to retrieve foreign key information
db.pool.query(`SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = ? AND REFERENCED_TABLE_NAME IS NOT NULL`, [title], (error, results) => {
// If the table doesn't have foreign keys, render the page without foreign key data
if (results.length === 0)
return renderPage(res, title, null, tableResults, error);
// Retrieve foreign key data for each foreign key reference
let foreignKeyPromises = results.map(result => {
let foreignTableName = result.REFERENCED_TABLE_NAME;
let columnName = result.COLUMN_NAME;
// Query the foreign table
return new Promise((resolve, reject) => {
db.pool.query(`SELECT * FROM ${foreignTableName}`, function(error, foreignTableResults) {
if (error) {
reject(error);
} else {
// Transform the results into a 2D array
let fks = foreignTableResults.map(row => Object.values(row));
let foreignKeyData = { [columnName]: fks };
resolve(foreignKeyData);
}
});
});
});
// Retrieve all foreign key data asynchronously
Promise.all(foreignKeyPromises)
.then(allForeignKeys => {
let tables = Object.assign({}, ...allForeignKeys); // Combine all key-value pairs into one object
// Format date column if it exists
if (tableResults[0].date)
for (let i = 0; i < tableResults.length; i++)
if (tableResults[i].date instanceof Date)
tableResults[i].date = tableResults[i].date.toISOString();
// Render the page with foreign key data
renderPage(res, title, tables, tableResults, error);
})
.catch(error => console.log(error));
});
});
});
}
// CREATE
app.post(url + '/create', (req, res) => {
// Retrieve the keys and values from the request body
const keys = Object.keys(req.body);
const values = Object.values(req.body);
const sql = `INSERT INTO ${title} (${keys.join(',')}) VALUES (${keys.map(() => '?').join(',')})`;
// Execute the modQuery function to perform the SQL insert operation
modQuery(sql, values, res, url);
});
// UPDATE
app.post(url + '/update', (req, res) => {
// Handle the SQL update statement for the specified table
handleSQLStatement('UPDATE', req, res, url, title);
});
// DELETE
app.post(url + '/delete/:id', (req, res) => {
// Set the id parameter in the request body
req.body.id = req.params.id;
// Handle the SQL delete statement for the specified table
handleSQLStatement('DELETE FROM', req, res, url, title);
});
});
// SEARCH Books By Genre
app.post('/books/search', (req, res) => {
// Retrieve the genreID from the request body
let genreID = req.body.genreID;
let sql = '';
let params = [];
// Check if genreID is provided
if (genreID) {
// Wrap the genre name with '%' for SQL's LIKE
genreID = '%' + genreID + '%';
sql = `SELECT * FROM Books
JOIN BookGenres ON Books.bookID = BookGenres.bookID
JOIN Genres ON BookGenres.genreID = Genres.genreID
WHERE Genres.name LIKE ?`;
params = [genreID];
} else {
sql = `SELECT * FROM Books`;
}
// Execute the SQL query with the provided parameters
db.pool.query(sql, [genreID], (error, results) => {
if (error)
res.status(500).json({ error: error.sqlMessage });
else
res.json(results);
});
});
// SEARCH Customers By Name
app.post('/customers/search', (req, res) => {
// Retrieve the name from the request body
let name = req.body.name;
let sql = '';
let params = [];
// Check if name is provided
if (name) {
// Wrap the name with '%' for SQL's LIKE
name = '%' + name + '%';
sql = `SELECT * FROM Customers
WHERE Customers.name LIKE ?`;
params = [name];
} else {
sql = `SELECT * FROM Customers`;
}
// Execute the SQL query with the provided parameters
db.pool.query(sql, [name], (error, results) => {
if (error)
res.status(500).json({ error: error.sqlMessage });
else
res.json(results);
});
});
// Start the server and listen on the specified port
app.listen(PORT, function (err) {
if (err) throw err;
console.log(`Server running on port ${PORT}`);
});