From 9148fa6e2fad9d54e3451a4478e03f55f0a9fa3c Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Sun, 16 May 2021 21:38:59 -0500 Subject: Rearrange files --- OLD/csci4131/hw7/api/utilities.js | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 OLD/csci4131/hw7/api/utilities.js (limited to 'OLD/csci4131/hw7/api') diff --git a/OLD/csci4131/hw7/api/utilities.js b/OLD/csci4131/hw7/api/utilities.js new file mode 100644 index 0000000..12e06aa --- /dev/null +++ b/OLD/csci4131/hw7/api/utilities.js @@ -0,0 +1,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; -- cgit v1.2.3