Skip to content

Commit

Permalink
Added link to loan items from loans
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonglittle committed Dec 6, 2021
1 parent ed11c02 commit 5fd2db6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
40 changes: 31 additions & 9 deletions library-express/loanitems.js
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions library-express/views/create_loan.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<td>{{loanDate}}</td>
<td>{{bookCount}}</td>
<input type="hidden" id="loanID" name="loanID" value={{loanID}}>
<td><button type="submit" formaction="/loan_items" formmethod="get">View Details</button>
<td><button type="submit" formaction="/delete_loan" formmethod="post">Delete</button></td>
</form>
</tr>
Expand Down

0 comments on commit 5fd2db6

Please sign in to comment.