diff options
-rw-r--r-- | csci4131/hw6/strap012_hw6/api/utilities.js | 34 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/dbio.js | 79 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/index.js | 16 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/login.html | 46 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/public/addContact.html | 21 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/public/contacts.html | 25 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/public/stock.html | 9 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/public/welcome.html | 1 |
8 files changed, 125 insertions, 106 deletions
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;
diff --git a/csci4131/hw6/strap012_hw6/dbio.js b/csci4131/hw6/strap012_hw6/dbio.js index ab3ec42..302334b 100644 --- a/csci4131/hw6/strap012_hw6/dbio.js +++ b/csci4131/hw6/strap012_hw6/dbio.js @@ -16,56 +16,59 @@ connection.connect(function(err) { console.log("Connected to MYSQL database!"); }); -async function passcheck(user,pass) { - let ret = ''; - connection.query('SELECT * FROM tbl_accounts', function(err,rows,fields) { - if (err) throw err; - if (rows.length == 0) { - console.log("There are no entries in the accounts field!"); - } else { - for (var i = 0 ; i < rows.length; i++) { +function passcheck(user,pass) { + return new Promise(function(resolve, reject) { + connection.query('SELECT * FROM tbl_accounts', function(err, rows, fields) { + let ret = []; + if (err) { + return reject(err); + } + for (var i = 0; i < rows.length; i++) { if (rows[i].acc_login.localeCompare(user) === 0) { if (bcrypt.compareSync(pass, rows[i].acc_password)) { - ret += rows[i]; + ret += rows[i]; } } } - } - return ret; + resolve(ret); + }); }); - } function getContacts() { - let conTab = [] - connection.query('SELECT * FROM tbl_contacts', function(err,rows,fields) { - for (let i=0; i<rows.length; i++) { - conTab[i] = rows[i]; - } + return new Promise (function(resolve, reject) { + let conTab = [] + connection.query('SELECT * FROM tbl_contacts', function (err, rows, fields) { + if (err) { + return reject(err); + } + resolve(rows); + }); }); - return conTab; } -// Parameterized Insert -var rowToBeInserted = { - Title: 'A Book', // Dummy Book Name - Category: 'General', // Dummy Category Type - ISBN : '0000001234'// Dummy - }; +function addContacts(contact) { + let newCon = { + name: contact.name, + category: contact.category, + location: contact.location, + contact_info: contact.contact, + email: contact.email, + website_url: contact.website_name, + } + return new Promise(function(resolve, reject) { + connection.query('INSERT tbl_contacts SET ?', newCon, function (err, result) { //Parameterized insert + if (err) throw err; + console.log("Values inserted"); + resolve(); + }); + }); + +} -//connection.query('INSERT books SET ?', rowToBeInserted, function(err, result) { //Parameterized insert -// if(err) throw err; -// console.log("Values inserted"); -// }); - -var Title = 'Another Book'; -var Cat = 'Fiction'; -var ISBN = '0000002345'; - -//var sql = 'INSERT INTO books (Title,Category,ISBN) VALUES (' + '"' + Title + '"' + ',' + '"' + Cat + '"' + ',' + '"' + ISBN + '"' + ')'; -//connection.query(sql,function(err,result) { -// if (err) throw err; -// console.log ("Version 2 values inserted"); -// }); + + +exports.addContact = addContacts; exports.query = passcheck; +exports.getContacts = getContacts; diff --git a/csci4131/hw6/strap012_hw6/index.js b/csci4131/hw6/strap012_hw6/index.js index 474b165..851b096 100644 --- a/csci4131/hw6/strap012_hw6/index.js +++ b/csci4131/hw6/strap012_hw6/index.js @@ -49,15 +49,19 @@ app.get('/', function (req, res) { // GET method route for the contacts page. // It serves contact.html present in public folder app.get('/contacts', function(req, res) { - if(!req.session.value) { + if(!req.session.user) { res.redirect('/login'); } else { res.sendFile(path.join(__dirname, 'public/contacts.html')); } }); +app.get('/stocks', function(req, res) { + res.redirect('/stock'); +}) + app.get('/stock', function (req, res) { - if (!req.session.value) { + if (!req.session.user) { res.redirect('/login'); } else { res.sendFile(path.join(__dirname, 'public/stock.html')); @@ -65,7 +69,7 @@ app.get('/stock', function (req, res) { }); app.get('/addContact', function (req, res) { - if (!req.session.value) { + if (!req.session.user) { res.redirect('/login'); } else { res.sendFile(path.join(__dirname, 'public/addContact.html')); @@ -73,13 +77,17 @@ app.get('/addContact', function (req, res) { }); app.get('/login', function (req, res) { - if (req.session.value) { + if (req.session.user) { res.redirect('/contacts'); } else { res.sendFile(path.join(__dirname, 'login.html')); } }); +app.get('/logout', function(req, res) { + res.redirect('/api/logout') +}); + // Makes Express use a router called utilities app.use('/api', utilities); diff --git a/csci4131/hw6/strap012_hw6/login.html b/csci4131/hw6/strap012_hw6/login.html index 59e7dfa..9bb80cc 100644 --- a/csci4131/hw6/strap012_hw6/login.html +++ b/csci4131/hw6/strap012_hw6/login.html @@ -1,11 +1,17 @@ <html> <head> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-2.2.4.min.js"integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> - <form id="myForm" name="myForm"> + <div class="jumbotron" style="background: DarkSeaGreen !important"> + <h1>Login Page</h1> + <div id="error"></div> + <br><br> + <form id="myForm" name="myForm" style="list-style-type:none"> <div> <label for="login">login:</label> <input type="text" id="login" name="login" required> @@ -16,23 +22,27 @@ </div> <input type="submit"value="Submit!"> </form> + </div> <script> - $(document).ready(function () { - $('#myForm').submit(function (event) { - event.preventDefault();//collect the form data using Id Selector for whatever data you need to send to server - let login=$('#login').val(); - let password=$('#password').val(); - $.post('api/login', - {"login": login,"password": password}, - (data) => { - console.log(data); - if(data.status === 'success'){ - //pseudo code - //Make sure error message is not displayed - //Re-direct to contacts page, - window.location.href='contacts';} - else{ - //Display error message - }});});});</script> + //jQuery below. Tread with caution as sneezing near here may rupture the fabric of reality. + $(document).ready(function () { + $('#myForm').submit(function (event) { + event.preventDefault();//collect the form data using Id Selector for whatever data you need to send to server + let login=$('#login').val(); + let password=$('#password').val(); + $.post('api/login', + {"login": login,"password": password}, + (data) => { + if(data.status === 'success'){ + //pseudo code + //Make sure error message is not displayed + //Re-direct to contacts page, + window.location.href='contacts';} + else{ + //Display error message + $('#error').html("<b>Login failed. Please try again.</b>") + }}); + }); + });</script> </html> diff --git a/csci4131/hw6/strap012_hw6/public/addContact.html b/csci4131/hw6/strap012_hw6/public/addContact.html index 12bfaa2..629f9b2 100644 --- a/csci4131/hw6/strap012_hw6/public/addContact.html +++ b/csci4131/hw6/strap012_hw6/public/addContact.html @@ -13,10 +13,11 @@ <nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav navbar-nav">
- <li><a href="index.html"><b>Home</b></a></li>
- <li><a href="contacts.html"><b>Contacts</b></a></li>
- <li><a href="addContact.html"><b>Add Contact</b></a></li>
- <li><a href="stock.html"><b>Stock Page</b></a></li>
+ <li><a href="/"><b>Home</b></a></li>
+ <li><a href="contacts"><b>Contacts</b></a></li>
+ <li><a href="addContact"><b>Add Contact</b></a></li>
+ <li><a href="stock"><b>Stock Page</b></a></li>
+ <li><a href="logout"><b>Logout</b></a></li>
</ul>
</div>
</nav>
@@ -31,7 +32,7 @@ <div class="col-md-4"></div>
<div class="col-md-4">
<div class="panel panel-default">
- <form name="addContact" method="post" action="/postContactEntry">
+ <form name="addContact" method="post" action="/api/addContact">
<p></p>
<table class="table table-bordered table-hover">
<tbody>
@@ -83,15 +84,7 @@ <td class="col-md-6">Website Name</td>
<td class="col-md-6">
<div class="form-group">
- <input type="text" class="form-control" name="website_name" required maxlength="100">
- </div>
- </td>
- </tr>
- <tr>
- <td class="col-md-6">Website URL</td>
- <td class="col-md-6">
- <div class="form-group">
- <input type="url" class="form-control" name="website_url" required maxlength="200">
+ <input type="url" class="form-control" name="website_name" required maxlength="100">
</div>
</td>
</tr>
diff --git a/csci4131/hw6/strap012_hw6/public/contacts.html b/csci4131/hw6/strap012_hw6/public/contacts.html index 15e4032..5cd2907 100644 --- a/csci4131/hw6/strap012_hw6/public/contacts.html +++ b/csci4131/hw6/strap012_hw6/public/contacts.html @@ -8,7 +8,7 @@ <script type="text/javascript" defer>
//Get JSON
var xmlhttp = new XMLHttpRequest();
- var url = "contacts.json";
+ var url = "api/contacts";
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var parse = JSON.parse(xmlhttp.responseText);
@@ -20,17 +20,21 @@ function process(a) {
var table = document.getElementsByTagName("tbody")[0];
- var contacts = a.contacts;
+ var contacts = a;
for (let i = 0; i < contacts.length; i++) {
var contact = contacts[i];
var row = table.insertRow();
for (var j in contact) {
- if (j != "website_url") {
- var value = row.insertCell();
- value.innerHTML = contact[j];
- } else {
+ if (j === "contact_id") continue;
+ if (j === "website_url") {
var url = row.insertCell();
url.innerHTML = "<a href =" + contact[j] + ">" + contact[j] + "</a>";
+ } else if (j === "email") {
+ var email = row.insertCell();
+ email.innerHTML = "<a href =mailto://" + contact[j] + ">" + contact[j] + "</a>";
+ } else {
+ var value = row.insertCell();
+ value.innerHTML = contact[j];
}
}
}
@@ -42,10 +46,11 @@ <nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav navbar-nav">
- <li><a href="index.html"><b>Home</b></a></li>
- <li><a href="contacts.html"><b>Contacts</b></a></li>
- <li><a href="addContact.html"><b>Add Contact</b></a></li>
- <li><a href="stock.html"><b>Stock Page</b></a></li>
+ <li><a href="/"><b>Home</b></a></li>
+ <li><a href="contacts"><b>Contacts</b></a></li>
+ <li><a href="addContact"><b>Add Contact</b></a></li>
+ <li><a href="stock"><b>Stock Page</b></a></li>
+ <li><a href="logout"><b>Logout</b></a></li>
</ul>
</div>
</nav>
diff --git a/csci4131/hw6/strap012_hw6/public/stock.html b/csci4131/hw6/strap012_hw6/public/stock.html index b55ffcb..d63b233 100644 --- a/csci4131/hw6/strap012_hw6/public/stock.html +++ b/csci4131/hw6/strap012_hw6/public/stock.html @@ -26,10 +26,11 @@ <nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav navbar-nav">
- <li><a href="index.html"><b>Home</b></a></li>
- <li><a href="contacts.html"><b>Contacts</b></a></li>
- <li><a href="addContact.html"><b>Add Contact</b></a></li>
- <li><a href="stock.html"><b>Stock Page</b></a></li>
+ <li><a href="/"><b>Home</b></a></li>
+ <li><a href="contacts"><b>Contacts</b></a></li>
+ <li><a href="addContact"><b>Add Contact</b></a></li>
+ <li><a href="stock"><b>Stock Page</b></a></li>
+ <li><a href="logout"><b>Logout</b></a></li>
</ul>
</div>
</nav>
diff --git a/csci4131/hw6/strap012_hw6/public/welcome.html b/csci4131/hw6/strap012_hw6/public/welcome.html index 50ae883..095023e 100644 --- a/csci4131/hw6/strap012_hw6/public/welcome.html +++ b/csci4131/hw6/strap012_hw6/public/welcome.html @@ -6,7 +6,6 @@ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> - <link rel="stylesheet" type="text/css" href="/css/style.css"> <title>Welcome to Node.js</title> <style type="text/css"> .jumbotron { |