diff --git a/library-express/app.js b/library-express/app.js index cb08697..d046c9f 100644 --- a/library-express/app.js +++ b/library-express/app.js @@ -6,7 +6,6 @@ var express = require('express'); // We are using the express library for the web server var app = express(); // We need to instantiate an express object to interact with the server in our code PORT = 4402; // Set a port number at the top so it's easy to change in the future -const api_func = require('./helpers/api_helper'); var mysql = require('./helpers/db-connector.js'); var bodyParser = require('body-parser'); var handlebars = require('express-handlebars').create({ @@ -17,8 +16,6 @@ var db = require('./helpers/db-connector'); /* FUNCTIONS */ -// app.use(express.static(__dirname + "/public")); -// app.use('/', express.static('public')); app.use('/static', express.static('public')) app.use(express.static('public/css')) app.use(express.json()); @@ -53,32 +50,6 @@ app.use('/create_loan', require('./create_loan.js')); app.use('/loan_status', require('./loanstatus.js')); app.use('/delete_loan_status', require('./delete_loan_status')); -/* -Citation for the following function: -Date: 03NOV21 -Adapted from: codehandbook.org -Source URL: https://codehandbook.org/how-to-make-rest-api-calls-in-express-web-app/ -*/ -app.post('/query_book_api', (req, res) => { - let title, author, url; - title = (req.body.title) ? 'title=' + req.body.title : ''; - author = (req.body.author) ? 'author=' + req.body.author : ''; - url = 'http://openlibrary.org/search.json?'; - - if (title && author) { url = url + title + '&' + author } - else if (title) { url = url + title } - else if (author) { url = url + author } - url = url + '&language:eng&limit=10' - - console.log('url = ' + url) - api_func.make_API_call(url) - .then(response => { - res.json(response) - }) - .catch(error => { - res.send(error) - }) -}) /* LISTENER diff --git a/library-express/books.js b/library-express/books.js index 148579e..fddd709 100644 --- a/library-express/books.js +++ b/library-express/books.js @@ -45,7 +45,7 @@ module.exports = function(){ router.post('/', function(req, res){ console.log(req.body); var mysql = req.app.get('mysql'); - var sql = "INSERT INTO Books (ISBN, purchaseDate, isActive) VALUES (?, ?, ?)"; + var sql = "INSERT INTO Books (ISBN, purchaseDate, isActive) VALUES (?, ?, ?);"; var inserts = [req.body.ISBN, req.body.purchaseDate, 1]; sql = mysql.pool.query(sql, inserts, function(error, results, fields){ if(error){ @@ -64,7 +64,7 @@ module.exports = function(){ console.log(req.body); console.log(req.params); var mysql = req.app.get('mysql'); - var sql = "UPDATE Books SET isActive = ? WHERE bookID = ?"; + var sql = "UPDATE Books SET isActive = ? WHERE bookID = ?;"; var inserts = [req.body.activeStatus, req.params.id]; sql = mysql.pool.query(sql, inserts, function(error, results, fields){ if(error){ diff --git a/library-express/edit_loan.js b/library-express/edit_loan.js index 37748ec..aac80f0 100644 --- a/library-express/edit_loan.js +++ b/library-express/edit_loan.js @@ -4,7 +4,7 @@ module.exports = function () { /*Get loan statuses from database*/ function getStatuses(res, mysql, context, complete){ - mysql.pool.query("SELECT statusID, statusDescription FROM LoanStatus", function(error, results, fields){ + mysql.pool.query("SELECT statusID, statusDescription FROM LoanStatus;", function(error, results, fields){ if(error){ res.write(JSON.stringify(error)); res.end(); diff --git a/library-express/helpers/api_helper.js b/library-express/helpers/api_helper.js deleted file mode 100644 index 723bea7..0000000 --- a/library-express/helpers/api_helper.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - Citation for the following function: - Date: 03NOV21 - Copied from: codehandbook.org - Source URL: https://codehandbook.org/how-to-make-rest-api-calls-in-express-web-app/ -*/ - -const request = require('request') - -module.exports = { - make_API_call : function(url){ - return new Promise((resolve, reject) => { - request(url, { json: true }, (err, res, body) => { - if (err) reject(err) - resolve(body) - }); - }) - } -} \ No newline at end of file diff --git a/library-express/loanitems.js b/library-express/loanitems.js index 9d6ddf1..70966e2 100644 --- a/library-express/loanitems.js +++ b/library-express/loanitems.js @@ -2,7 +2,7 @@ module.exports = function () { var express = require('express'); var router = express.Router(); /*Get loan item data from database*/ - function getLoanItems(res, mysql, context, complete) { +function getLoanItems(res, mysql, context, complete) { var sql = "SELECT li.loanID, l.memberID, li.bookID, CONCAT(p.firstName, ' ', p.lastName) as 'patronName', t.bookTitle, DATE_FORMAT(li.dueDate, '%m/%d/%Y') as 'dueDate', ls.statusDescription, li.renewalCount FROM LoanItems li INNER JOIN LoanStatus ls ON li.loanStatus = ls.statusID INNER JOIN Loans l ON li.loanID = l.loanID INNER JOIN Patrons p ON l.memberID = p.memberID INNER JOIN Books b ON li.bookID = b.bookID INNER JOIN Titles t ON b.ISBN = t.ISBN ORDER BY li.loanID;"; mysql.pool.query(sql, function (error, results, fields) { if (error) { diff --git a/library-express/loanstatus.js b/library-express/loanstatus.js index 817a19a..2ef48b9 100644 --- a/library-express/loanstatus.js +++ b/library-express/loanstatus.js @@ -3,7 +3,7 @@ module.exports = function(){ var router = express.Router(); /*Get loan statuses from database*/ function getStatuses(res, mysql, context, complete){ - mysql.pool.query("SELECT statusID, statusDescription FROM LoanStatus", function(error, results, fields){ + mysql.pool.query("SELECT statusID, statusDescription FROM LoanStatus;", function(error, results, fields){ if(error){ res.write(JSON.stringify(error)); res.end(); @@ -30,7 +30,7 @@ module.exports = function(){ /* Adds a loan status, redirects to the title page after adding */ router.post('/', function(req, res){ var mysql = req.app.get('mysql'); - var sql = "INSERT INTO LoanStatus (statusDescription) VALUES (?)"; + var sql = "INSERT INTO LoanStatus (statusDescription) VALUES (?);"; var inserts = [req.body.statusDescription]; sql = mysql.pool.query(sql, inserts, function(error, results, fields){ if(error){ diff --git a/library-express/patrons.js b/library-express/patrons.js index 6725cb2..d86a17d 100644 --- a/library-express/patrons.js +++ b/library-express/patrons.js @@ -3,7 +3,7 @@ module.exports = function () { var router = express.Router(); /*Get patron data from database*/ function getPatrons(res, mysql, context, complete) { - mysql.pool.query("SELECT p.memberID AS id, p.firstName, p.lastName, DATE_FORMAT(p.registerDate, '%m/%d/%Y') as 'registerDate', p.contactEmail, p.contactPhone, t.bookTitle FROM Patrons p LEFT JOIN Titles t ON p.favoriteTitle = t.ISBN", function (error, results, fields) { + mysql.pool.query("SELECT p.memberID AS id, p.firstName, p.lastName, DATE_FORMAT(p.registerDate, '%m/%d/%Y') as 'registerDate', p.contactEmail, p.contactPhone, t.bookTitle FROM Patrons p LEFT JOIN Titles t ON p.favoriteTitle = t.ISBN;", function (error, results, fields) { if (error) { res.write(JSON.stringify(error)); res.end(); @@ -15,7 +15,7 @@ module.exports = function () { /*Get title data from database*/ function getTitles(res, mysql, context, complete) { - mysql.pool.query("SELECT ISBN, bookTitle FROM Titles", function (error, results, fields) { + mysql.pool.query("SELECT ISBN, bookTitle FROM Titles;", function (error, results, fields) { if (error) { res.write(JSON.stringify(error)); res.end(); @@ -74,7 +74,7 @@ module.exports = function () { router.post('/', function (req, res) { console.log(req.body); var mysql = req.app.get('mysql'); - var sql = "INSERT INTO Patrons (firstName, lastName, registerDate, contactEmail, contactPhone, favoriteTitle) VALUES (?, ?, ?, ?, ?, ?)"; + var sql = "INSERT INTO Patrons (firstName, lastName, registerDate, contactEmail, contactPhone, favoriteTitle) VALUES (?, ?, ?, ?, ?, ?);"; // checks if the favorite title was a null value var inserts = []; if (req.body.favoriteTitle == 'null') { @@ -133,7 +133,7 @@ module.exports = function () { router.post('/:id', function (req, res) { var mysql = req.app.get('mysql'); console.log(req.body) - var sql = "UPDATE Patrons SET firstName = ?, lastName = ?, registerDate = ?, contactEmail = ?, contactPhone = ?, favoriteTitle = ? WHERE memberID = ?"; + var sql = "UPDATE Patrons SET firstName = ?, lastName = ?, registerDate = ?, contactEmail = ?, contactPhone = ?, favoriteTitle = ? WHERE memberID = ?;"; // checks if the favorite title was a null value var inserts = []; if (req.body.favoriteTitle == 'null') { diff --git a/library-express/titles.js b/library-express/titles.js index d845052..a090227 100644 --- a/library-express/titles.js +++ b/library-express/titles.js @@ -3,7 +3,7 @@ module.exports = function(){ var router = express.Router(); /*Get title data from database*/ function getTitles(res, mysql, context, complete){ - mysql.pool.query("SELECT ISBN, bookTitle, author, publisher, DATE_FORMAT(datePublished, \"%m/%d/%Y\") as 'datePublished' FROM Titles", function(error, results, fields){ + mysql.pool.query("SELECT ISBN, bookTitle, author, publisher, DATE_FORMAT(datePublished, \"%m/%d/%Y\") as 'datePublished' FROM Titles;", function(error, results, fields){ if(error){ res.write(JSON.stringify(error)); res.end(); @@ -31,7 +31,7 @@ module.exports = function(){ router.post('/', function(req, res){ console.log(req.body); var mysql = req.app.get('mysql'); - var sql = "INSERT INTO Titles (ISBN, bookTitle, author, publisher, datePublished) VALUES (?, ?, ?, ?, ?)"; + var sql = "INSERT INTO Titles (ISBN, bookTitle, author, publisher, datePublished) VALUES (?, ?, ?, ?, ?);"; // check if the date published should be a null value var datePublished = req.body.datePublished; if (datePublished == '') { diff --git a/library-express/views/edit_loan.handlebars b/library-express/views/edit_loan.handlebars index 651ab14..d2b9785 100644 --- a/library-express/views/edit_loan.handlebars +++ b/library-express/views/edit_loan.handlebars @@ -12,6 +12,7 @@ Loan Status Due Date Renewal Count +