Skip to content

Commit

Permalink
Added functionality to set book status
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonglittle committed Dec 5, 2021
1 parent 6fc394a commit b8a8f9b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
46 changes: 21 additions & 25 deletions library-express/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function(){

/*Get book data from database*/
function getBooks(res, mysql, context, complete){
mysql.pool.query("SELECT b.bookID, b.ISBN, t.bookTitle, DATE_FORMAT(b.purchaseDate, '%m/%d/%Y') as 'purchaseDate', CASE WHEN b.isActive = 1 THEN 'True' ELSE 'False' END AS 'isActive' FROM Books b INNER JOIN Titles t ON b.ISBN = t.ISBN;", function(error, results, fields){
mysql.pool.query("SELECT b.bookID, b.ISBN, t.bookTitle, DATE_FORMAT(b.purchaseDate, '%m/%d/%Y') as 'purchaseDate', b.isActive FROM Books b INNER JOIN Titles t ON b.ISBN = t.ISBN;", function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
Expand All @@ -25,30 +25,6 @@ module.exports = function(){
});
}

/*Get loan statuses from database*/
function getLoanStatuses(res, mysql, context, complete){
mysql.pool.query("SELECT statusID as 'statusID', statusDescription as 'statusDescription' FROM LoanStatus;", function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.statuses = results;
complete();
});
}

/*Get patrons from database*/
function getPatronDetails(res, mysql, context, complete){
mysql.pool.query("SELECT memberID, favoriteTitle, firstName, lastName, DATE_FORMAT(registerDate, '%m/%d/%Y') as 'registerDate', contactPhone, contactEmail from Patrons;", function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.patrons = results;
complete();
});
}

/*Display all books.*/
router.get('/', function(req, res){
var callbackCount = 0;
Expand All @@ -59,6 +35,7 @@ module.exports = function(){
function complete(){
callbackCount++;
if(callbackCount >= 2){
console.log(context);
res.render('books', context);
}
}
Expand All @@ -82,5 +59,24 @@ module.exports = function(){

});

/* Updates the active status of a book */
router.post('/:id', function(req, res){
console.log(req.body);
console.log(req.params);
var mysql = req.app.get('mysql');
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){
console.log(JSON.stringify(error));
res.write(JSON.stringify(error));
res.end();
} else{
res.status(200);
res.redirect('/books');
}
});
});

return router;
}();
17 changes: 1 addition & 16 deletions library-express/patrons.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@ module.exports = function () {
}
});


/* Display patrons filtered by first name */
function getPatronsByFirstName(req, res, mysql, context, complete) {
var query = "SELECT p.memberID as id, p.firstName, p.lastName, DATE_FORMAT(p.registerDate, '%d/%m/%Y') as 'registerDate', p.contactEmail, p.contactPhone, t.bookTitle FROM Patrons p LEFT JOIN Titles t ON p.favoriteTitle = t.ISBN WHERE p.firstName LIKE '%?%'";
console.log(req.params)
var inserts = [req.params.filterFName]
mysql.pool.query(query, inserts, function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.patrons = results;
complete();
});
}

/* Find people whose fname starts with a given string in the req */
function getPatronsWithNameLike(req, res, mysql, context, complete) {
//sanitize the input as well as include the % character
Expand Down Expand Up @@ -104,6 +88,7 @@ module.exports = function () {
res.write(JSON.stringify(error));
res.end();
} else {
res.status(200);
res.redirect('/patrons');
}
});
Expand Down
25 changes: 18 additions & 7 deletions library-express/views/books.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
</form>
<br>
<div>
<form>
<label>Search by Title:
<input type="text" />
</label>
</form>
<table>
<caption>Titles</caption>
<thead>
<th>Book ID</th>
<th>ISBN</th>
<th>Title</th>
<th>Date Purchased</th>
<th>Is Active?</th>
<th>Active Item</th>
</thead>
<tbody>
{{#each books}}
Expand All @@ -34,7 +29,23 @@
<td>{{ISBN}}</td>
<td>{{bookTitle}}</td>
<td>{{purchaseDate}}</td>
<td>{{isActive}}</td>
<td>{{#if isActive}}
Yes
{{else}}
No
{{/if}}
</td>
<td>
<form action="/books/{{bookID}}" method="post">
{{#if isActive}}
<input type="hidden" name="activeStatus" value=0>
<button type="submit">Set to Inactive</button>
{{else}}
<input type="hidden" name="activeStatus" value=1>
<button type="submit">Set to Active</button>
{{/if}}
</form>
</td>
</tr>
{{/each}}
</tbody>
Expand Down

0 comments on commit b8a8f9b

Please sign in to comment.