From 0c7bb17b8c955254c2db61a5fe9c8240ea1357e2 Mon Sep 17 00:00:00 2001 From: Matthew Strapp Date: Fri, 16 Apr 2021 21:37:26 -0500 Subject: finish hw6 --- csci4131/hw6/strap012_hw6/api/utilities.js | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'csci4131/hw6/strap012_hw6/api') diff --git a/csci4131/hw6/strap012_hw6/api/utilities.js b/csci4131/hw6/strap012_hw6/api/utilities.js index 8036c29..65a087f 100644 --- a/csci4131/hw6/strap012_hw6/api/utilities.js +++ b/csci4131/hw6/strap012_hw6/api/utilities.js @@ -4,13 +4,11 @@ const router = express.Router() router.use(express.urlencoded({ extended: true })) router.get('/contacts', function (req, res) { - // TODO: Implement code to fetch contacts from the database - table = db.getContacts(); - res.send("AAAAAAAAAAAA"); + db.getContacts().then(function(table) { + res.send(table) + }); }); - -// TODO: Add implementation for other necessary end-points router.post('/login', async function(req, res) { var loginInfo = req.body; var login = loginInfo.login; @@ -18,31 +16,33 @@ router.post('/login', async function(req, res) { let rows = []; // Query the database tbl_login with login and hashed password - try { - rows = await db.query(login, pwd); - } - finally { + 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' }); - res.send("SUCC"); - } else - res.send("FAIL"); - res.json({ status: 'fail' }); - } + } else { + res.json({ status: 'fail' }); + } + }); + }); router.get('/logout', function(req, res) { - if(!req.session.value) { + if(!req.session.user) { res.send('Session not started, can not logout!'); } else { - console.log ("Successfully Destroyed Session!"); req.session.destroy(); - res.send("Session Complete!"); res.redirect('/login'); } }); +router.post('/addContact', function(req, res) { + var contact = req.body; + db.addContact(contact).then(function() { + res.redirect('/contacts'); + }); +}); + module.exports = router; -- cgit v1.2.3