diff options
Diffstat (limited to 'csci4131')
-rw-r--r-- | csci4131/hw7/api/utilities.js | 22 | ||||
-rw-r--r-- | csci4131/hw7/dbio.js | 64 | ||||
-rw-r--r-- | csci4131/hw7/insert_into_accounts_table.js | 6 | ||||
-rw-r--r-- | csci4131/hw7/package.json | 1 |
4 files changed, 79 insertions, 14 deletions
diff --git a/csci4131/hw7/api/utilities.js b/csci4131/hw7/api/utilities.js index 65a087f..12e06aa 100644 --- a/csci4131/hw7/api/utilities.js +++ b/csci4131/hw7/api/utilities.js @@ -40,9 +40,25 @@ router.get('/logout', function(req, res) { router.post('/addContact', function(req, res) {
var contact = req.body;
- db.addContact(contact).then(function() {
- res.redirect('/contacts');
- });
+ 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;
diff --git a/csci4131/hw7/dbio.js b/csci4131/hw7/dbio.js index dcc7386..96faf93 100644 --- a/csci4131/hw7/dbio.js +++ b/csci4131/hw7/dbio.js @@ -53,9 +53,7 @@ function getContacts() { return new Promise (function(resolve, reject) { let conTab = [] connection.query('SELECT * FROM tbl_contacts', function (err, rows, fields) { - if (err) { - return reject(err); - } + if (err) throw err; resolve(rows); }); }); @@ -68,16 +66,64 @@ function addContacts(contact) { location: contact.location, contact_info: contact.contact, email: contact.email, - website_url: contact.website_name, + website: contact.website, } - return new Promise(function(resolve, reject) { - connection.query('INSERT tbl_contacts SET ?', newCon, function (err, result) { //Parameterized insert + return new Promise (function (resolve, reject) { + connection.query('SELECT * FROM tbl_contacts where name=?', contact.name, function(err, rows, fields) { if (err) throw err; - console.log("Values inserted"); - resolve(); + console.log("Table found") + if (rows.length > 0) { + // Duplicate + console.log("found Duplicate name!"); + resolve(false); + } else { + connection.query('INSERT tbl_contacts SET ?', newCon, function (err, result) { //Parameterized insert + if (err) throw err; + console.log("Values inserted."); + resolve(true); + }); + } }); }); +} + + + +function editContact(contact) { + let edit = { + name: contact.name, + category: contact.category, + location: contact.location, + contact_info: contact.contact, + email: contact.email, + website: contact.website, + } + return new Promise(function (resolve, reject) { + connection.query('SELECT * FROM tbl_contacts where name=?', contact.name, function (err, rows, fields) { + if (err) throw err; + if (rows.length == 0) { + console.log("Name Changed!"); + resolve(false); + } else { + connection.query('UPDATE tbl_contacts SET ? WHERE name=?', [edit, edit.name], function(err, result) { + if (err) throw err; + console.log("Value edited successfully?") + resolve(true); + }); + } + }); + }); +} + +function deleteContact(contact) { + return new Promise(function(resolve, reject) { + connection.query('DELETE FROM tbl_contacts WHERE name=?', contact, function (err, result) { + if (err) throw err; + console.log("Row deleted!") + resolve(); + }) + }) } @@ -86,3 +132,5 @@ function addContacts(contact) { exports.addContact = addContacts; exports.query = passcheck; exports.getContacts = getContacts; +exports.deleteContact = deleteContact; +exports.editContact = editContact; diff --git a/csci4131/hw7/insert_into_accounts_table.js b/csci4131/hw7/insert_into_accounts_table.js index 3157ce3..0be14ce 100644 --- a/csci4131/hw7/insert_into_accounts_table.js +++ b/csci4131/hw7/insert_into_accounts_table.js @@ -24,12 +24,12 @@ dbCon.connect(function (err) { console.log("Connected to database!"); const saltRounds = 10; - const myPlaintextPassword = 'tango'; // replace with password chosen by you OR retain the same value + const myPlaintextPassword = 'admin%'; // replace with password chosen by you OR retain the same value const passwordHash = bcrypt.hashSync(myPlaintextPassword, saltRounds); const rowToBeInserted = { - acc_name: 'charlie', // replace with acc_name chosen by you OR retain the same value - acc_login: 'charlie', // replace with acc_login chosen by you OR retain the same value + acc_name: 'admin$', // replace with acc_name chosen by you OR retain the same value + acc_login: 'admin$', // replace with acc_login chosen by you OR retain the same value acc_password: passwordHash }; diff --git a/csci4131/hw7/package.json b/csci4131/hw7/package.json index 543fc7f..0598cc4 100644 --- a/csci4131/hw7/package.json +++ b/csci4131/hw7/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "start": "node index.js", "test": "node index.js" }, "author": "", |