aboutsummaryrefslogtreecommitdiffstats
path: root/csci4131/hw7/api/utilities.js
blob: 12e06aa32e54621d3e365371993895ad405464f1 (plain) (blame)
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
const express = require('express')
const db = require ('../dbio')
const router = express.Router()
router.use(express.urlencoded({ extended: true }))

router.get('/contacts', function (req, res) {
    db.getContacts().then(function(table) {
		res.send(table)
	});
});

router.post('/login', async function(req, res) {
	var loginInfo = req.body;
	var login = loginInfo.login;
	var pwd = loginInfo.password;
	let rows = [];
	
	// Query the database tbl_login with login and hashed password
	db.query(login, pwd).then(function(rows) {
		// Provided there is no error, and the results set is assigned to a variable named rows:
		if (rows.length >= 1) {// the length should be 0 or 1, but this will work for now 
			//success, set the session, return success
			req.session.user = login;
			res.json({ status: 'success' });
		} else {
			res.json({ status: 'fail' });
		}
	}); 
		
});

router.get('/logout', function(req, res) {
	if(!req.session.user) {
		res.send('Session not started, can not logout!');
	} else {
		req.session.destroy();
		res.redirect('/login');
	}
});

router.post('/addContact', function(req, res) {
	var contact = req.body;
	db.addContact(contact).then(function(r) {
		res.send({flag: r});
	}).catch(function() {

	})
});

router.post('/updateContact', function(req, res) {
	var edit = req.body;
	db.editContact(edit).then(function(f) {
		res.send({flag:f});
	});
})

router.post('/deleteContact', function(req, res) {
	var contact = req.body.name;
	db.deleteContact(contact).then(function(r) {
		res.send({flag: r});
	})
})

module.exports = router;