-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdogs.js
176 lines (162 loc) · 7.25 KB
/
dogs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// JavaScript source code
/***************************************************************************************
* --SOURCE--
* Title: people.js
* Authors: Justin Wolford,Samarendra Hedao
* Initial Publish Date: 10/25/2017
* Availability: https://github.com/knightsamar/CS340-Sample-Web-App
***************************************************************************************/
module.exports = function () {
var express = require('express');
var router = express.Router();
//function to select all information about dogs to display on the manage dogs page, left joins on adopters to get their first and last name if there is one (blank if not)
//and inner joins on food supply to get the brand name and product name
function getDogs(res, mysql, context, complete) {
mysql.pool.query("SELECT d.id, name, DATE_FORMAT(birthdate, '%m-%d-%Y') as bdate, breed, sex, adoptedBy, foodType, image, a.fname, a.lname, f.bname, f.pname FROM Dogs d LEFT JOIN Adopters a ON d.adoptedBy = a.id INNER JOIN FoodSupply f ON d.foodType = f.id ORDER BY d.id", function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.dogs = results;
complete();
});
}
//function to select one dog to prepopulate the edit page
function getDog(res, mysql, context, id, complete) {
var sql = "SELECT d.id, name, DATE_FORMAT(birthdate, '%Y-%m-%d') as bdate, breed, sex, adoptedBy, foodType, image, a.fname, a.lname, f.bname, f.pname FROM Dogs d LEFT JOIN Adopters a ON d.adoptedBy = a.id INNER JOIN FoodSupply f ON d.foodType = f.id WHERE d.id = ?;";
var inserts = [id];
mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.dog = results[0];
complete();
});
}
//function to select all food to autopopulate the dropdown in the add form
function getFood(res, mysql, context, complete) {
mysql.pool.query("SELECT id, bname, pname FROM FoodSupply", function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.food = results;
complete();
});
}
//function to select all adopters to autopopulate the dropdown in the add form
function getAdopters(req, mysql, context, complete) {
mysql.pool.query("SELECT id, fname, lname FROM Adopters", function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
context.adopters = results;
complete();
});
}
//GET request to render the /dogs page when it loads, calls appropriate functions to populate page
router.get('/', function (req, res) {
var callbackCount = 0;
var context = {};
var mysql = req.app.get('mysql');
getDogs(res, mysql, context, complete);
getFood(res, mysql, context, complete);
function complete() {
callbackCount++;
if (callbackCount >= 2) {
res.render('dogs', context);
}
}
});
//POST request to insert data into the dogs table
router.post('/', function (req, res) {
if (req.body.name != "" & req.body.birthdate != "" & req.body.breed != "" & req.body.sex != "") {
console.log(req.body);
var mysql = req.app.get('mysql');
var sql = "INSERT INTO Dogs(name, birthdate, breed, sex, foodType, image) VALUES(?,?,?,?,?,?)";
var inserts = [req.body.name, req.body.birthdate, req.body.breed, req.body.sex, req.body.food, req.body.image];
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.redirect('/dogs');
}
});
}
else {
console.log("All information must be filled out")
}
});
//DELETE request to delete data from the Dogs table, receives the id of the dog to be deleted from from addDog.js
router.delete('/:id', function (req, res) {
console.log(req.body);
var mysql = req.app.get('mysql');
var sql = "DELETE FROM Dogs WHERE id = ?";
var inserts = [req.body.id];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.status(400);
res.end();
} else {
res.status(202).end();
}
})
})
//GET request gets the id of the dog being edited, calls the appropriate function, then renders the edit page with prepopulated data
router.get('/:id', function (req, res) {
callbackCount = 0;
var context = {};
context.jsscripts = ["updateDog.js"];
var mysql = req.app.get('mysql');
getDog(res, mysql, context, req.params.id, complete);
getFood(res, mysql, context, complete);
getAdopters(res, mysql, context, complete)
function complete() {
callbackCount++;
if (callbackCount >= 3) {
res.render('dogedit', context);
}
}
});
//PUT request updates a dog with the information from the edit page and returns to the main dog page
router.put('/:id', function (req, res) {
var mysql = req.app.get('mysql');
console.log(req.body)
console.log(req.params)
//if there is an adopter
if (req.body.adoptedBy != '') {
var sql = "UPDATE Dogs SET name = ?, birthdate = ?, breed = ?, sex = ?, adoptedBy = ?, foodType = ?, image = ? WHERE id = ?;"
var inserts = [req.body.name, req.body.birthdate, req.body.breed, req.body.sex, req.body.adoptedBy, req.body.foodType, req.body.image, req.params.id];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(error)
res.write(JSON.stringify(error));
res.end();
} else {
res.status(200);
res.end();
}
});
}//else set adopter to NULL
else {
var sql = "UPDATE Dogs SET name = ?, birthdate = ?, breed = ?, sex = ?, adoptedBy = NULL, foodType = ?, image = ? WHERE id = ?;"
var inserts = [req.body.name, req.body.birthdate, req.body.breed, req.body.sex, req.body.foodType, req.body.image, req.params.id];
sql = mysql.pool.query(sql, inserts, function (error, results, fields) {
if (error) {
console.log(error)
res.write(JSON.stringify(error));
res.end();
} else {
res.status(200);
res.end();
}
});
}
});
return router;
}();