Skip to content

Commit

Permalink
Can update a patrons favorite title to NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
allisonglittle committed Dec 4, 2021
1 parent c6b2579 commit 8fed22d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
18 changes: 15 additions & 3 deletions library-express/patrons.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ module.exports = function () {
console.log(req.body);
var mysql = req.app.get('mysql');
var sql = "INSERT INTO Patrons (firstName, lastName, registerDate, contactEmail, contactPhone, favoriteTitle) VALUES (?, ?, ?, ?, ?, ?)";
var inserts = [req.body.firstName, req.body.lastName, req.body.registerDate, req.body.contactEmail, req.body.contactPhone, req.body.favoriteTitle];
// checks if the favorite title was a null value
var inserts = [];
if (req.body.favoriteTitle == 'null') {
inserts = [req.body.firstName, req.body.lastName, req.body.registerDate, req.body.contactEmail, req.body.contactPhone, null];
} else {
inserts = [req.body.firstName, req.body.lastName, req.body.registerDate, req.body.contactEmail, req.body.contactPhone, req.body.favoriteTitle];
}
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(JSON.stringify(error));
Expand Down Expand Up @@ -142,8 +148,14 @@ 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 = ?, contactPhone = ?, contactEmail = ?, favoriteTitle = ? WHERE memberID = ?";
var inserts = [req.body.firstName, req.body.lastName, req.body.registerDate, req.body.contactPhone, req.body.contactEmail, req.body.favoriteTitle, req.params.id];
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') {
inserts = [req.body.firstName, req.body.lastName, req.body.registerDate, req.body.contactEmail, req.body.contactPhone, null, req.params.id];
} else {
inserts = [req.body.firstName, req.body.lastName, req.body.registerDate, req.body.contactEmail, req.body.contactPhone, req.body.favoriteTitle, req.params.id];
}
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(JSON.stringify(error));
Expand Down
1 change: 1 addition & 0 deletions library-express/views/patrons.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</p>
<p><label for="favoriteTitle">Favorite Book:</label><br>
<select name="favoriteTitle">
<option value=null selected>--</option>
{{#each titles}}
<option value="{{ISBN}}">{{ISBN}} - {{bookTitle}}</option>
{{/each}}
Expand Down
3 changes: 2 additions & 1 deletion library-express/views/update_patrons.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
<input type="tel" name="contactPhone" value="{{patron.contactPhone}}">
</p>
<p><label for="favoriteTitle">Favorite Book:</label><br>
<select name="favoriteTitle" id="title-selector" >
<select name="favoriteTitle" id="title-selector">
<option value=null>--</option>
{{#each titles}}
<option value="{{this.ISBN}}">{{this.ISBN}} - {{this.bookTitle}}</option>
{{/each}}
Expand Down

0 comments on commit 8fed22d

Please sign in to comment.