forked from cs4241-19a/a5-databases-and-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.improved.js
More file actions
323 lines (254 loc) · 9.56 KB
/
Copy pathserver.improved.js
File metadata and controls
323 lines (254 loc) · 9.56 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
let currentID; //this will keep track of who is signed in
let userData;
let productData;
let objectID;
const http = require( 'http' ),
fs = require( 'fs' ),
express = require('express'),
bodyParser = require('body-parser'),
mongodb = require('mongodb'),
app = express(),
session = require('express-session'),
mime = require( 'mime' ),
dir = 'public/',
port = 3000
app.use(express.static("public"));
app.use(session({ secret: "secretsInTheSky", resave: false,
saveUninitialized: true,
cookie: { secure: true } }));
app.use( bodyParser.json() );
const uri = 'mongodb+srv://'+process.env.USER+':'+process.env.PASS+'@'+process.env.HOST+'/'+process.env.DB
const client = new mongodb.MongoClient( uri, { useNewUrlParser: true, useUnifiedTopology:true })
let collection = null;
let productList = null;
client.connect()
.then( () => {
// will only create collection if it doesn't exist
return client.db( 'users' ).createCollection( 'accounts' )
})
.then( __collection => {
// store reference to collection
collection = __collection
// blank query returns all documents
//return collection.find({ }).toArray()
})
//.then( console.log )
//.catch(err=>console.log(err))
client.connect().then( () => {
// will only create collection if it doesn't exist
return client.db( 'users' ).createCollection( 'products' )
})
.then( __collection => {
// store reference to collection
productList = __collection
// blank query returns all documents
//return productList.find({ }).toArray()
})
//.then( console.log )
//.catch(err=>console.log(err))
const createUserData = async function(){
await collection.find({}).toArray()
.then(result => {
userData=[];
result.forEach(function(user) {
userData.push(JSON.parse(JSON.stringify({id: user._id,
firstName : user.firstName,
lastName:user.lastName,
username: user.username,
password:user.password
})))
})
})
}
const createProductData = async function(){
await productList.find({}).toArray()
.then(result => {
productData=[];
result.forEach(function(product) {
productData.push(JSON.parse(JSON.stringify({id:product._id,
accountID: product.accountID,
productName: product.productName,
numProducts: product.numProducts,
price: product.price
})))
})
})
}
const updateProductData = async function(productID,num){
await productList.updateOne({ _id:mongodb.ObjectID(productID)},//id needs to be the products unique ID
{ $set: { numProducts: num } })
}
const deleteProductData = async function(productID,productName){
await productList.deleteOne({ _id:mongodb.ObjectID(productID) })
}
app.use( (req,res,next) => {
if( collection !== null ) {
next()
}else{
res.status( 503 ).send()
}
})
app.post('/createAccount', function(req,res){
let inputData = "";
currentID = undefined; //this is to restart the CurrentID in use so that a new one can be added later
req.on( 'data', function( data ) {
inputData = JSON.parse( data);
})
req.on( 'end', function() {
collection.insertOne({
"firstName": inputData.firstName,
"lastName": inputData.lastName,
"username": inputData.username,
"password": inputData.password
})
})
res.send(JSON.stringify(inputData))
})
app.get('/', function(request, response) {
sendFile( response, 'public/index.html' )
});
app.post('/submit', function(request, response) {
handlePost( request, response )
})
app.delete('/delete', function(request,response){
handleDelete(request,response)
})
app.get('/getrequest', function(request,response){
handleGet(request,response)
})
app.post('/login', function(request,response){
//response.send('POST request to homepage');
verify(request,response)
})
const verify = async function(request,response){
await createUserData();
let inputData = "";
request.on( 'data', function( data ) {
inputData = JSON.parse( data);
})
request.on( 'end', function() {
let uname = inputData.username
let pass = inputData.password
let unameCheck = userData.filter(x => x.username===uname) //checks if username exists
let passwordCheck = unameCheck.filter(x=> x.password ===pass) //checks if username and password combo exists using the array created by finding the username
if (passwordCheck.length === 0){ //if username does not exist or password is incorrect, this array will have length of 0
response.send({message:"User or password not found"})
}else{
response.send({message:"User and Password matched"})
currentID = passwordCheck[0].id
//console.log(currentID)
}
})
}
const handleGet = function( request, response ) {
const filename = dir + request.url.slice( 1 )
if( request.url === '/' ) {
sendFile( response, 'public/index.html' )
}else if (request.url === '/getrequest'){
handleGetRequest(request, response)
}
else{
sendFile( response, filename )
}
}
const handlePost = function( request, response ) {
let inputData = "";
request.on( 'data', function( data ) {
inputData = JSON.parse( data);
})
request.on( 'end', async function() {
if(currentID === undefined){
await createUserData();
let newUserID = userData.slice(-1)[0].id //gets the newly created userID
currentID = newUserID
//console.log("post request: "+currentID)
productList.insertOne({
accountID: currentID,
productName: inputData.productName,
numProducts: inputData.numProducts,
price: inputData.price
})
}
else{
productList.insertOne({
accountID: currentID,
productName: inputData.productName,
numProducts: inputData.numProducts,
price: inputData.price
})
//console.log("post request using currentID: "+currentID)
}
})
response.send(JSON.stringify(inputData) )
}
const handleGetRequest = async function( request, response ) {
await createProductData();
request.on( 'data', function( data ) {
})
//uses a similar method to the verify function, need to match the currentID to accountID
//this will then display all the user's products in his list
request.on( 'end', async function() {
if(currentID === undefined){
await createUserData();
let newUserID = userData.slice(-1)[0].id //gets the newly created userID
currentID = newUserID
let newProductList = productData.filter(x => x.accountID===currentID)
//console.log(currentID)
//console.log(newProductList)
response.send(newProductList)
//console.log('New userID was created for the new registering Account')
}
else{
let currentProductList = productData.filter(x => x.accountID===currentID)
//console.log(currentID)
//console.log('An existing userID was used')
//console.log(currentProductList)
response.send(currentProductList)
}
})
}
//this will use updateOne and deleteOne in mongoDB
//if input makes numproduct greater than zero, use updateOne
//if input mkaes numproduct less than or equal to zero, use deleteOne
const handleDelete = async function( request, response ) {
await createProductData();
let dataVar ={}
request.on( 'data', function( data ) {
dataVar = JSON.parse( data);
})
request.on( 'end', async function() {
let currentProductList = productData.filter(x => x.accountID===currentID)
let exactProduct = currentProductList.filter(x =>x.productName === dataVar.productName)//only one product
let numProd = parseInt(exactProduct[0].numProducts) //made into a number because it is originally a string
let inputNumProd = parseInt(dataVar.numProducts)
//update the number of products in the user's list if the quantity in the list is greater than the inputted value
if(numProd > inputNumProd){
numProd = numProd - inputNumProd
objectID = exactProduct[0].id
//console.log(objectID)
await updateProductData(objectID,numProd);
}
//if the input is greater than or equal to the quantity stored than delete the whole entry/product
else if (numProd <= inputNumProd){
objectID = exactProduct[0].id
await deleteProductData(objectID);
}
response.send(dataVar)
})
}
const sendFile = function( response, filename ) {
const type = mime.getType( filename )
fs.readFile( filename, function( err, content ) {
// if the error = null, then we've loaded the file successfully
if( err === null ) {
// status code: https://httpstatuses.com
response.writeHeader( 200, { 'Content-Type': type })
response.end( content )
}else{
// file not found, error code 404
response.writeHeader( 404 )
response.end( '404 Error: File Not Found' )
}
})
}
app.listen(process.env.PORT || port)