-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbfunctions.js
569 lines (342 loc) · 14.9 KB
/
dbfunctions.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
let db = require('./connect.js');
db = db.connect("projetodam");
let dateformat = require("dateformat");
let moment = require("moment");
const { query } = require('express');
let getUserData = (req, res) =>{
/*
Create a promise to read the data that is being fetch asynchronously
*/
let promise = new Promise((resolve, reject) =>{
let query = `SELECT * FROM user WHERE EMAIL= "${req.params.email}"`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
})
})
promise.then(rows =>{
/*
send the data to the page
*/
res.send(rows);
}).catch(err => setImmediate(()=>{throw err}))
}
let checkUserData = (req, res) =>{
/*
Create a promise to read the data that is being fetch asynchronously
*/
let promise = new Promise((resolve, reject) =>{
let query = `SELECT * FROM user WHERE EMAIL= "${req.params.email}" and palavra_passe = "${req.params.password}"`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
})
})
promise.then(rows =>{
/*
send the data to the page
*/
if(Object.keys(rows).length === 0){
res.send([false]);
}else{
res.send([true,rows[0].EMAIL, rows[0].NOME]);
}
}).catch(err => setImmediate(()=>{throw err}))
}
let getLocation = (req, res) =>{
/*
Create a promise to read the data that is being fetch asynchronously
*/
let promise = new Promise((resolve, reject) =>{
let query = `SELECT * FROM localizacao where ID_LOCATION = ${req.params.id}`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
})
})
promise.then(rows =>{
/*
send the data to the page
*/
res.send(rows);
}).catch(err => setImmediate(()=>{throw err}))
}
let getAllLocations = (req, res) =>{
/*
Create a promise to read the data that is being fetch asynchronously
*/
let promise = new Promise((resolve, reject) =>{
let query = `SELECT DISTRITO FROM localizacao`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
})
})
promise.then(rows =>{
/*
send the data to the page
*/
newRows = [];
for(var i = 0; i< rows.length; i++){
newRows.push(rows[i].DISTRITO)
}
res.send(newRows);
}).catch(err => setImmediate(()=>{throw err}))
}
let register = (req, res) =>{
let registered = true;
let promise = new Promise((resolve, reject)=>{
let query = `SELECT email FROM user where email = "${req.body.email}" `;
db.query(query, (err, result, fields) =>{
if (err) return reject(err);
resolve(result)
});
});
promise.then( (rows) =>{
if(Object.keys(rows).length === 0){
registered = !registered;
doRegisterUser(req, res);
}
else{
res.send([false]);
console.log("account already exists");
}
}).catch(err => setImmediate(()=>{throw err}));
}
let doRegisterUser = (req, res) =>{
let promise = new Promise((resolve, reject)=>{
let date = moment(req.body.dataDeNascimento, "DD/MM/YYYY").toDate();
req.body.dataDeNascimento = dateformat(date, "isoDate");
let query = `Insert into user (email, nome, palavra_passe, local, data_de_nascimento)
values( "${req.body.email}" , "${req.body.name}" , "${req.body.password}", "${req.body.distrito}", "${req.body.dataDeNascimento}")`;
db.query(query, (err, result, fields) =>{
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
res.send([true, req.body.email, req.body.name]);
}).catch(err => setImmediate(()=>{throw err}));
}
let getElections = (req, res)=>{
let promise = new Promise((resolve, reject) =>{
let query = `SELECT Local FROM user WHERE EMAIL = "${req.body.email}"`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
});
});
promise.then(rows =>{
let local = rows[0].Local;
getPossibleElections(local, req.body.email, res);
}).catch(err => setImmediate(()=>{throw err}));
}
let getPossibleElections = (local, email, res)=>{
let promise = new Promise((resolve, reject) =>{
let day = new Date();
let today = moment(day, "DD-MM-YYYY").toDate();
today = dateformat(today, "isoDate");
let query = `SELECT DISTINCT el.ID
FROM eleicao el
LEFT OUTER JOIN voto vo
ON el.id = vo.ELEICAO
LEFT OUTER JOIN user us
ON vo.EMAIL = us.EMAIL
WHERE (el.ID_LOCATION = "${local}" OR el.ID_LOCATION = "Nacional")
and el.DATA_INICIO < "${today}" and el.DATA_FIM > "${today}"
and EL.ID not in (SELECT DISTINCT EL.ID
FROM ELEICAO el
LEFT JOIN voto v
ON el.id = v.ELEICAO
where v.EMAIL like "${email}"
UNION
SELECT DISTINCT EL.ID
FROM ELEICAO el
RIGHT JOIN voto v
ON el.id = v.ELEICAO
where v.EMAIL like "${email}")
or vo.EMAIL is null`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
});
});
promise.then(rows =>{
console.log(rows);
selectCandidates(rows, res);
// res.send(rows);
}).catch(err => setImmediate(()=>{throw err}));
}
let selectCandidates = (rows, res) =>{
let promise = new Promise((resolve, reject) =>{
let idList = []
let q = `SELECT * FROM candidato_eleicao ce, candidato c
WHERE ce.ID_CANDIDATO = c.ID_CANDIDATO and ce.ID_ELEICAO = ?;`
let finalQuerry = "";
for(let i = 0; i < rows.length; i++){
finalQuerry += q;
idList.push(rows[i].ID);
}
db.query(finalQuerry, idList ,function (err, result, fields) {
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
console.log(rows);
res.send(rows);
}).catch(err => setImmediate(()=>{res.send([0])}));
}
let getPastElections = (req, res)=>{
let promise = new Promise((resolve, reject) =>{
let query = `SELECT Local FROM user WHERE EMAIL = "${req.body.email}"`;
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
});
});
promise.then(rows =>{
let local = rows[0].Local;
getPossiblePast(local, req.body.email, res);
}).catch(err => setImmediate(()=>{throw err}));
}
let getPossiblePast = (local, email, res) =>{
let promise = new Promise((resolve, reject) =>{
let day = new Date();
let today = moment(day, "DD-MM-YYYY").toDate();
today = dateformat(today, "isoDate");
let query = `SELECT DISTINCT el.ID
FROM eleicao el
LEFT OUTER JOIN voto vo
ON el.id = vo.ELEICAO
LEFT OUTER JOIN user us
ON vo.EMAIL = us.EMAIL
WHERE (el.ID_LOCATION = "${local}" OR el.ID_LOCATION = "Nacional")
and el.DATA_FIM < "${today}"
`;
console.log(query);
db.query(query ,function (err, result, fields) {
if (err) return reject(err);
resolve(result)
});
});
promise.then(rows =>{
console.log(rows);
selectCandidates(rows, res);
// res.send(rows);
}).catch(err => setImmediate(()=>{throw err}));
}
let getElectionData = (req, res)=>{
let idList = req.body;
let promise = new Promise((resolve, reject) =>{
let q = `SELECT * FROM eleicao e
WHERE e.ID = ?;`
let finalQuerry = "";
for(let i = 0; i < req.body.length; i++){
finalQuerry += q;
}
db.query(finalQuerry, idList ,function (err, result, fields) {
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
console.log(rows);
res.send(rows);
}).catch(err => setImmediate(()=>{throw err}));
}
let castVote = (req, res) =>{
let promise = new Promise((resolve, reject)=>{
let Insertquery = `INSERT INTO voto(eleicao, ID_CANDIDATO, EMAIL) values(${req.body.election}, ${req.body.vote}, "${req.body.email}");`;
let candidateVotesQuery = `UPDATE candidato_eleicao
SET NUMERO_VOTOS = (SELECT COUNT(*)
FROM VOTO
WHERE ID_CANDIDATO = ${req.body.vote} and eleicao = ${req.body.election})
WHERE ID_CANDIDATO = ${req.body.vote} and ID_ELEICAO = ${req.body.election};`;
let finalQuerry = Insertquery + candidateVotesQuery;
db.query(finalQuerry, (err, result, fields)=>{
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
res.send([1]);
}).catch(err => setImmediate(()=>{throw err}));
}
let updateVotes = (req,res)=>{
let promise = new Promise((resolve, reject)=>{
let candidateVotesQuery = `UPDATE candidato_eleicao
SET NUMERO_VOTOS = (SELECT COUNT(*)
FROM VOTO
WHERE ID_CANDIDATO = ${req.body.vote} and eleicao = ${req.body.election})
WHERE ID_CANDIDATO = ${req.body.vote} and ID_ELEICAO = ${req.body.election};`;
db.query(candidateVotesQuery, (err, result, fields)=>{
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
}).catch(err => setImmediate(()=>{throw err}));
}
let updateWinner = () =>{
let promise = new Promise((resolve, reject)=>{
let day = new Date();
let today = moment(day, "DD-MM-YYYY").toDate();
today = dateformat(today, "isoDate");
let updateTotalVotes = `UPDATE eleicao SET NUMERO_DE_VOTOS = (SELECT COUNT(*) from voto v where v.eleicao = eleicao.id );`;
let updateWinner = `UPDATE eleicao
SET VENCEDOR =(SELECT ID_CANDIDATO FROM VOTO v where v.ELEICAO = eleicao.ID having count(*) ORDER BY COUNT(*) DESC LIMIT 1)
WHERE data_fim < "${today}"`;
let finalQuerry = updateTotalVotes + updateWinner;
console.log(finalQuerry);
db.query(finalQuerry, (err, result, fields)=>{
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
}).catch(err => setImmediate(()=>{throw err}));
}
let getPastElectionData = (req, res)=>{
let promise = new Promise((resolve, reject)=>{
let q = `SElECT ce.ID_CANDIDATO, ce.NUMERO_VOTOS, c.NOME, c.PARTIDO
FROM candidato_eleicao ce, candidato c
where ce.ID_CANDIDATO = c.ID_CANDIDATO and ce.ID_ELEICAO = ${req.body.id}
ORDER BY NUMERO_VOTOS DESC`;
db.query(q, (err, result, fields)=>{
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
res.send(rows);
}).catch(err => setImmediate(()=>{throw err}));
}
let getElectionVotesByArea =(req, res)=>{
let promise = new Promise((resolve, reject)=>{
let query = `SELECT v.eleicao, c.NOME, u.Local, count(*) as NumberOfVotesPerLocal
FROM voto v, user u, candidato c
where v.email = u.EMAIL and v.eleicao = ${req.body.id} and c.ID_CANDIDATO = v.ID_CANDIDATO
GROUP BY LOCAL, v.ID_CANDIDATO`;
db.query(query, (err, result, fields)=>{
if (err) return reject(err);
resolve(result);
});
});
promise.then(rows =>{
res.send(rows);
}).catch(err => setImmediate(()=>{throw err}));
}
exports.getUserData = getUserData;
exports.checkUserData = checkUserData;
exports.getLocation = getLocation;
exports.getAllLocations = getAllLocations;
exports.register = register;
exports.getElections = getElections;
exports.getPastElections = getPastElections;
exports.getElectionData = getElectionData;
exports.castVote = castVote;
exports.updateWinner = updateWinner;
exports.getPastElectionData = getPastElectionData;
exports.getElectionVotesByArea = getElectionVotesByArea;
exports.updateVotes = updateVotes;