diff --git a/library-express/loanitems.js b/library-express/loanitems.js index 155abd1..9d6ddf1 100644 --- a/library-express/loanitems.js +++ b/library-express/loanitems.js @@ -1,10 +1,26 @@ -module.exports = function(){ +module.exports = function () { var express = require('express'); var router = express.Router(); /*Get loan item data from database*/ - function getLoanItems(res, mysql, context, complete){ - mysql.pool.query("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;", function(error, results, fields){ - if(error){ + 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) { + res.write(JSON.stringify(error)); + res.end(); + } + context.loanItems = results; + complete(); + }); + + } + + /*Get loan item data from a load id */ + function getItemsByLoanID(res, loanID, 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 WHERE li.loanID = ?;"; + var inserts = [loanID]; + mysql.pool.query(sql, inserts, function (error, results, fields) { + if (error) { res.write(JSON.stringify(error)); res.end(); } @@ -13,16 +29,22 @@ module.exports = function(){ }); } - /*Display all loan items.*/ - router.get('/', function(req, res){ + /*Display loan items.*/ + router.get('/', function (req, res) { var callbackCount = 0; var context = {}; context.jsscripts = ['renew_loan.js']; var mysql = req.app.get('mysql'); - getLoanItems(res, mysql, context, complete); - function complete(){ + // check if there are query parameters for the loan id + console.log(req.query); + if (req.query.loanID === undefined) { + getLoanItems(res, mysql, context, complete); + } else { + getItemsByLoanID(res, req.query.loanID, mysql, context, complete) + } + function complete() { callbackCount++; - if(callbackCount >= 1){ + if (callbackCount >= 1) { res.render('loan_items', context); } } diff --git a/library-express/views/create_loan.handlebars b/library-express/views/create_loan.handlebars index c010974..00fba4d 100644 --- a/library-express/views/create_loan.handlebars +++ b/library-express/views/create_loan.handlebars @@ -51,6 +51,7 @@