Skip to content

Commit

Permalink
additional housekeeping updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ivyadam committed Dec 6, 2021
1 parent 27a8407 commit 45cca6b
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 60 deletions.
29 changes: 0 additions & 29 deletions library-express/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions library-express/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion library-express/edit_loan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 0 additions & 19 deletions library-express/helpers/api_helper.js

This file was deleted.

2 changes: 1 addition & 1 deletion library-express/loanitems.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = function(){
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){
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){
res.write(JSON.stringify(error));
res.end();
Expand Down
4 changes: 2 additions & 2 deletions library-express/loanstatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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){
Expand Down
8 changes: 4 additions & 4 deletions library-express/patrons.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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') {
Expand Down
4 changes: 2 additions & 2 deletions library-express/titles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 == '') {
Expand Down

0 comments on commit 45cca6b

Please sign in to comment.