forked from emmy-rich/448_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
386 lines (358 loc) · 16.2 KB
/
database.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
const sqlite3 = require('sqlite3').verbose();
//Function that creates the database tables
function createTables() {
//Creates a new database connection
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
db.serialize(() => {
//Creates the user_info table within the database with a primary key for the user_id so that it can be connected to other tables, unique username, and text and int fields for password, email, zipcode, latitude, longitude and the local weather station.
db.run('CREATE TABLE IF NOT EXISTS user_info (user_id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL UNIQUE, password TEXT NOT NULL, email TEXT NOT NULL UNIQUE, zipcode INT(5) NOT NULL, lat INT, long INT, station VARCHAR(3))')
//this allows for the use of foreign/primary keys in order to connect the user_id within all three tables
.run('PRAGMA foreign_keys = ON;')
//Creates a closet table within the database that holds the user_id, and a row for each clothing items supported; long-sleeve shirt, sweater, jacket, jeans, coat, tank top, sweats, summer hat, t-shirt, skirt, winter hat, and winter skirt. This table keeps track of all of the user's clothing.
.run('CREATE TABLE IF NOT EXISTS closet (user_id INT UNIQUE, long_sleeve_shirt INT NOT NULL DEFAULT 0, sweater INT NOT NULL DEFAULT 0, jacket INT NOT NULL DEFAULT 0, jeans INT NOT NULL DEFAULT 0, coat INT NOT NULL DEFAULT 0, tank_top INT NOT NULL DEFAULT 0, sweats INT NOT NULL DEFAULT 0, summer_hat INT NOT NULL DEFAULT 0, t_shirt INT NOT NULL DEFAULT 0, skirt INT NOT NULL DEFAULT 0, winter_hat INT NOT NULL DEFAULT 0, winter_skirt INT NOT NULL DEFAULT 0, FOREIGN KEY(user_id) REFERENCES user_info(user_id))')
//Creates a laundry table within the database that holds the user_id, and a row for each of the clothing items supported; long-sleeve shirt, sweater, jacket, jeans, coat, tank top, sweats, summer hat, t-shirt, skirt, winter hat, and winter skirt. This table keeps track of the user's clothing that has been worn previously.
.run('CREATE TABLE IF NOT EXISTS laundry (user_id INT UNIQUE, long_sleeve_shirt INT NOT NULL DEFAULT 0, sweater INT NOT NULL DEFAULT 0, jacket INT NOT NULL DEFAULT 0, jeans INT NOT NULL DEFAULT 0, coat INT NOT NULL DEFAULT 0, tank_top INT NOT NULL DEFAULT 0, sweats INT NOT NULL DEFAULT 0, summer_hat INT NOT NULL DEFAULT 0, t_shirt INT NOT NULL DEFAULT 0, skirt INT NOT NULL DEFAULT 0, winter_hat INT NOT NULL DEFAULT 0, winter_skirt INT NOT NULL DEFAULT 0, FOREIGN KEY(user_id) REFERENCES user_info(user_id))')
});
//close the database connection
db.close((err) => {
if (err) {
return console.error(err.message);
}
});
}
//Function that takes an article of clothing and the username of the user, that
function wear(article, username) {
//Creates a new database connection
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
db.serialize(() => {
//match and select each item in the closet table to the specific username given as a parameter
db.each(`SELECT closet.${article} FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
throw err;
}
//take the returned data row and stringify it, then transform it into an integer value
db.serialize(() => {
let closetValueString = JSON.stringify(row);
let closetValue = parseInt(closetValueString.replace(/[^0-9]*/g, ''));
//decrement the value in the closet if possible
if (closetValue > 0) {
closetValue--;
//update the value for the given article parameter within the specific user's closet table to reflect the new value calculated above
db.run(`UPDATE closet SET ${article} = ${closetValue} WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}')`)
//match and select each item in the laundry table to the specific username given as a parameter
db.each(`SELECT laundry.${article} FROM laundry INNER JOIN user_info ON laundry.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
throw err;
}
//take the returned data row and stringify it, then transform it into an integer value and increments it by 1 to reflect the new value
db.serialize(() => {
let laundryValueString = JSON.stringify(row);
let laundryValue = parseInt(laundryValueString.replace(/[^0-9]*/g, '')) + 1;
// update the value for the given article parameter within the specific user's laundry table to reflect the new value
db.run(`UPDATE laundry SET ${article} = ${laundryValue} WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}')`)
//close the database connection
db.close();
})
})
}
else {
//close the database connection if the closet value is less than 0
db.close();
}
})
})
});
}
//removes all articles in the laundry and adds them to the closet
function clean(article, username) {
//creates a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
//gets the article from the launrdy row that is connected to the user id for the inputted username
db.serialize(() => {
db.each(`SELECT laundry.${article} FROM laundry INNER JOIN user_info ON laundry.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
throw err;
}
//gets the value of the article in the laundry
db.serialize(() => {
let laundryValueString = JSON.stringify(row);
let laundryValue = parseInt(laundryValueString.replace(/[^0-9]*/g, ''));
//gets the article in the closet that is assocated with the same user id
db.each(`SELECT closet.${article} FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
throw err;
}
db.serialize(() => {
//gets the value for the article in closet
let closetValueString = JSON.stringify(row);
let closetValue = parseInt(closetValueString.replace(/[^0-9]*/g, ''));
//sets the value of closet to be its current value plus the value of the same article in laundry
closetValue += laundryValue;
//updates the value of the article in laundry to 0
db.run(`UPDATE laundry SET ${article} = 0 WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}')`)
//updates the value of the article in closet to be the closet and laundry values combined
db.run(`UPDATE closet SET ${article} = ${closetValue} WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}')`)
//closes the connection to the database
db.close();
})
})
})
})
})
}
//removes a specific article from the users closet
function remove(article, username) {
//creates a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
//gets the article related to the id for the inputed username
db.serialize(() => {
db.each(`SELECT closet.${article} FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
throw err;
}
db.serialize(() => {
//gets the current value for the article
let closetValueString = JSON.stringify(row);
//decrements the value of that article by 1
let closetValue = parseInt(closetValueString.replace(/[^0-9]*/g, '')) - 1;
if (closetValue >= 0) {
//updates the value for the article in the closet table
db.run(`UPDATE closet SET ${article} = ${closetValue} WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}')`)
}
})
//closes the connection to the database
db.close();
})
});
}
//returns what is in the closet for the inputed user
function getCloset(username) {
//creates a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
//selects every type of article from the closet for that user
db.serialize(() => {
db.each(`SELECT long_sleeve_shirt, sweater, jacket, jeans, coat, tank_top, sweats, summer_hat, t_shirt, skirt, winter_hat, winter_skirt FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username ='${username}'`, (err, row) => {
if (err) {
throw err;
}
//prints the closet data to the console
console.log("closet: ");
console.log(row);
})
//close connection to database
db.close();
})
}
//returns what the launrdy for that user looks like
function getLaundry(username) {
//creates a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
//selects every type of article in the laundry
db.serialize(() => {
db.each(`SELECT long_sleeve_shirt, sweater, jacket, jeans, coat, tank_top, sweats, summer_hat, t_shirt, skirt, winter_hat, winter_skirt FROM laundry INNER JOIN user_info ON laundry.user_id = user_info.user_id WHERE user_info.username ='${username}'`, (err, row) => {
if (err) {
throw err;
}
//prints the laundry information to the console
console.log("laundry: ");
console.log(row);
})
//closes the connection to the database
db.close();
})
}
//checks to make sure there is data for that specific articl
function checkData(article, username) {
//create a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
//creates a promise, and looks through the closet for a specific article
//of clothing for a specific username
return new Promise((resolve, reject) => {
db.serialize(() => {
db.all(`SELECT ${article} FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
reject(err);
} //if there is no error then the amount of that article of clothing gets
//returned from the database
console.log("The row is: " + row);
let countString = JSON.stringify(row);
console.log("This is the count of string: " + countString);
if(countString.length != 0){
count = parseInt(countString.replace(/[^0-9]*/g, ''));
row = count;
resolve(row);
}
else {
console.log("uh oh");
}
});
//close the connection to the database
db.close();
})
})
}
//adds an article to a specific user's closet
function add(article, username) {
//creates connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
//gets the article from the closet associated with the id for the user
db.serialize(() => {
db.each(`SELECT closet.${article} FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
throw err;
}
//gets the current value for the article
db.serialize(() => {
let closetValueString = JSON.stringify(row);
let closetValue = parseInt(closetValueString.replace(/[^0-9]*/g, '')) + 1;
//makes sure there is at least 0 or more articles and updates the articles value by 1
if (closetValue >= 0) {
db.run(`UPDATE closet SET ${article} = ${closetValue} WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}')`)
}
})
//closes the connection to the database
db.close();
})
});
}
//adds a new user with their given input to be stored in the database
function addUser(username, password, email, zipcode, lat, long, station) {
//connects to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message + "error here at 271");
}
});
//Inserts all of the users information into the user information table, which makes a new user
db.serialize(() => {
db.run(`INSERT INTO user_info(username, password, email, zipcode, lat, long, station) VALUES('${username}', '${password}', '${email}', ${zipcode}, ${lat}, ${long}, '${station}')`)
//gets the user id correlating with the username
// try{
db.each(`SELECT user_id FROM user_info WHERE username = '${username}'`, (err, row) => {
if (err) {
return console.error("Username or email is already taken. Try again \n");
}
else {
db.serialize(() => {
let id = JSON.stringify(row);
id = parseInt(id.replace(/[^0-9]*/g, ''));
db.run(`INSERT INTO closet(user_id)
VALUES(${id})`)
db.run(`INSERT INTO laundry(user_id)
VALUES(${id})`)
//closes the connection to the database
db.close();
})
}
})
//creates a tables for the closet and laundry to be associated with the new user id
})
}
//returns 0 if username and password combo is not in database
//and 1 if username and password combo is in database
function validateLogin(username, password) {
//create a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
return new Promise((resolve, reject) => {
db.serialize(() => {
//returns the number of users with the username and password entered in by the user
db.all(`SELECT COUNT(*) FROM user_info WHERE user_id IN (SELECT user_id FROM user_info WHERE username = '${username}' AND password = '${password}')`, (err, row) => {
if (err) {
reject(err);
}
//turn the output into a string and parse the integer from it
let countString = JSON.stringify(row);
count = parseInt(countString.replace(/[^0-9]*/g, ''));
row = count;
//return either 0 or 1
resolve(row);
});
//close the connection to the database
db.close();
})
})
}
function getInfo(username, info) {
//create a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
return new Promise((resolve, reject) => {
db.serialize(() => {
//returns the number of users with the username and password entered in by the user
db.each(`SELECT ${info} FROM user_info WHERE username = '${username}'`, (err, row) => {
if (err) {
reject(err);
}
//turn the output into a string
row = JSON.stringify(row);
resolve(row);
});
//close the connection to the database
db.close();
})
})
}
function getArticle(username, article) {
//create a connection to the database
let db = new sqlite3.Database('reelcoloset', (err) => {
if (err) {
return console.error(err.message);
}
});
return new Promise((resolve, reject) => {
db.serialize(() => {
//returns the number of users with the username and password entered in by the user
db.each(`SELECT ${article} FROM closet INNER JOIN user_info ON closet.user_id = user_info.user_id WHERE user_info.username = '${username}'`, (err, row) => {
if (err) {
reject(err);
}
//turn the output into a string
row = JSON.stringify(row);
resolve(row);
console.log(row);
});
//close the connection to the database
db.close();
})
})
}
module.exports = { createTables, wear, clean, add, remove, getCloset, getLaundry, addUser, validateLogin, checkData, getInfo, getArticle };