-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (212 loc) · 9.38 KB
/
index.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
const { performance, PerformanceObserver } = require("perf_hooks")
const express = require('express')
const fs = require('fs')
const path = require('path')
var favicon = require('serve-favicon')
const sha256 = require('js-sha256')
const MongoClient = require('mongodb').MongoClient
const PORT = process.env.PORT || 5001
mainApp = express()
// performance hook obeserver of measurements
let latency;
const perfObserver = new PerformanceObserver((items) => {
items.getEntries().forEach((entry) => {
latency = entry.duration
console.log(entry)
})
})
perfObserver.observe({ entryTypes: ["measure"], buffer: true })
// Database definition and connection
const DATABASE = "Assignment1DB"
const COLLECTION = "Collection1"
const uri = "mongodb+srv://127.0.0.1:5301/Assignment1DB?retryWrites=true&w=majority";
const mongodbClient = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
async function runDB() {
try {
await mongodbClient.connect();
console.log("Connection to DB was successful.");
// await listDatabases(mongodbClient);
} finally {
// await mongodbClient.close();
}
}
runDB().catch(console.error);
// Main back-end of application
mainApp
.use(express.static(path.join(__dirname, 'public')))
.use(favicon(path.join(__dirname, 'public', 'favicon.ico')))
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/', (req, res) => res.render('pages/index'))
.get('/assignment1', (req, res) => res.render('pages/assignment01'))
.post('/assignment1/createUserAccount/:fname/:lname/:dateOfBirth/:city', async (req, res) => {
const response = await createListings(mongodbClient, req.params.fname, req.params.lname, req.params.dateOfBirth, req.params.city);
// console.log(response);
res.setHeader('content-type', 'application/json');
res.end(response.message);
})
.get('/assignment1/getAccount/:fname/:lname/:dateOfBirth/:city', async (req, res) => {
const response = await findListingsByName(mongodbClient, req.params.fname, req.params.lname, req.params.dateOfBirth, req.params.city);
// console.log(response);
res.setHeader('content-type', 'application/json');
res.end(response.message);
})
.delete('/assignment1/deleteAccountById/:person_num', async (req, res) => {
const response = await deleteListings(mongodbClient, req.params.person_num);
// console.log(response);
res.setHeader('content-type', 'application/json');
res.end(response.message);
})
.get('/assignment1/allPersonNums', async (req, res) => {
const response = await seeAllPersonNums(mongodbClient);
// console.log(response);
res.setHeader('content-type', 'application/json');
res.send(response);
})
.put('/assignment1/updateAccount/:person_num/:lname', async (req, res) => {
const response = await updateClientsLastName(mongodbClient, req.params.person_num, req.params.lname);
// console.log(response);
res.setHeader('content-type', 'application/json');
res.send(response.message);
})
// .get('/ajaxCall.txt', (req, res) => res.sendFile(path.join(__dirname + '/views/pages/resources/ajaxCall.txt')))
// .get('/exercise2', (req,res) => res.render('pages/exercise2'))
// .get('/exercise3', (req,res) => res.render('pages/exercise3'))
// .get('/exercise4', (req,res) => res.render('pages/exercise4'))
// .get('/cool', (req, res) => res.send(cool()))
// .get('/times', (req,res) => res.send(showTimes()))
.listen(PORT, () => console.log(`Listening on ${PORT}`))
showTimes = () => {
let result = ''
const times = process.env.TIMES || 5
for (i = 0; i < times; i++) {
result += i + ' '
}
return result
}
async function listDatabases(client) {
databasesList = await client.db().admin().listDatabases();
console.log("Databases: ")
databasesList.databases.forEach(db => console.log(` - ${db.name}`));
}
async function createListings(client, firstName, lastName, dateOfBirth, city) {
performance.mark('createListing_3');
const account_number = Math.floor(1000000 + Math.random() * 9999999);
const person_num = sha256(firstName.toLowerCase() + lastName.toLowerCase() + dateOfBirth + city.toLowerCase());
const dateOfBirthUnix = new Date(Date.parse(dateOfBirth))
newListings = {
"personal_number": person_num,
"account_number": account_number,
"first_name": firstName,
"last_name": lastName,
"date_of_birth": dateOfBirthUnix,
"city": city,
"creation_date": Date.now()
}
const finding = await client.db(DATABASE).collection(COLLECTION)
.findOne({ personal_number: person_num })
.catch(console.error);
if (finding) {
console.log(`User already exists.`);
return { "message": `<div class="response"> Client ${firstName} ${lastName} already has an account - ${finding.account_number}</div>` };
}
else {
performance.mark('createListing_4');
performance.measure("createListing 3-4", "createListing_3", "createListing_4")
const result = client.db(DATABASE).collection(COLLECTION)
.insertOne(newListings)
.then(result => {
console.log(`A new record with id ${result.insertedId} was inserted to DB ${DATABASE} and table ${COLLECTION}.`);
})
.catch(console.error);
return { "message": `<div class="response"> An account for ${firstName} ${lastName} was created. <br>Account number: ${account_number} <br>Personal number: ${person_num} <div class="latency"> Latency for this request: ${latency} </div></div>` };
}
}
async function findListingsByName(client, firstName, lastName, dateOfBirth, city) {
performance.mark('findListing_3');
const person_num = sha256(firstName.toLowerCase() + lastName.toLowerCase() + dateOfBirth + city.toLowerCase());
query = { personal_number: person_num };
const result = await client
.db(DATABASE)
.collection(COLLECTION)
.find(query)
.toArray()
.catch(console.error);
if (result.length > 0) {
performance.mark('findListing_4');
performance.measure("findListing 3-4", "findListing_3", "findListing_4")
console.log(`${result.length} record(s) exist in DB:`);
console.log(result);
return { "message": `<div class="response"> Client with matching details found in DB! <br> Account number: ${result[0].account_number} <br> Personal number: ${result[0].personal_number} <div class="latency"> Latency for this request: ${latency} </div></div>` }
}
else {
console.log("No record found in DB.");
return { "message": `<div class="response"> No account exists for ${firstName}/${lastName}/${dateOfBirth}/${city} combination.</div>` }
}
}
async function deleteListings(client, person_num) {
performance.mark('deleteListing_3');
query = { personal_number: person_num }
const result = await client
.db(DATABASE)
.collection(COLLECTION)
.deleteOne(query)
.catch(console.error);
if (result.deletedCount > 0) {
performance.mark('deleteListing_4');
performance.measure("deleteListing 3-4", "deleteListing_3", "deleteListing_4")
console.log(`${result.deletedCount} client(s) with personal number ${person_num} was/were deleted.`);
return { "message": `<div class="response"> ${result.deletedCount} Deleted client(s) with personal number: <br> ${person_num}<div class="latency"> Latency for this request: ${latency} </div></div>` }
}
else {
console.log(`No client with personal number ${person_num} exists in DB.`);
return { "message": `<div class="response"> Could not find client in DB with with personal number: <br> ${person_num}</div>` }
}
}
async function updateClientsLastName(client, person_num, lastName) {
performance.mark('updateName_3');
const query = { personal_number: person_num }
const update = { last_name: lastName }
const result = await client
.db(DATABASE)
.collection(COLLECTION)
.updateOne(query, { $set: update })
.catch(console.error);
if (result.matchedCount > 0) {
if (result.modifiedCount > 0) {
performance.mark('updateName_4');
performance.measure("updateName 3-4", "updateName_3", "updateName_4")
console.log(`We have found ${result.matchedCount} records with such parameter. ${result.modifiedCount} - number of modified records.`);
return { "message": `<div class="response"> Last name of client with Personal number: ${person_num} <br>was updated to "${lastName}". <div class="latency"> Latency for this request: ${latency}</div></div>` };
}
else {
console.log(`${result.modifiedCount} updated records. ${result.matchedCount} found objects in DB with such criteria.`);
return { "message": `<div class="response">${result.modifiedCount} updated records. ${result.matchedCount} found objects in DB with such criteria.</div>` };
}
}
else {
console.log(`${result.modifiedCount} updated records. ${result.matchedCount} found objects in DB with such criteria.`);
return { "message": `<div class="response"> ${result.modifiedCount} updated records. ${result.matchedCount} found objects in DB with such criteria.</div>` };
}
}
async function seeAllPersonNums(client) {
const result = await client
.db(DATABASE)
.collection(COLLECTION)
.find()
.toArray()
.catch(console.error);
if (result.length > 0) {
console.log(`I have found many documents in the collection.`);
const person_nums = []
result.forEach(element => {
person_nums.push(element.personal_number)
})
// console.log(person_nums);
return person_nums
}
else {
console.log("No record found in DB.");
return "No record found in DB.";
}
}