-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
210 lines (186 loc) · 8.9 KB
/
search.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// 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 call a mysql query to select the id, name, birthdate, breed, sex, adoptedBy, foodType, and image from dogs, left joins on adopters to get first and last names of adopter (or null)
//if no adopter and inner joins on Food Supply to get the brand name and product name of the foodType
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; //define the results as "dogs"
complete(); //call the complete function to increase callbackcount
});
}
//function to call a mysql query to select the id, brand name, and product name of all food from the database to populate the dropdown in the search 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 call a mysql query to select the id, first name, and last name of all adopters from the database to populate the dropdown in the search form
function getAdopters(res, 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.adopter = results;
complete();
});
}
//function to select dogs that match the search parameters, modifies the query based on what is selected/entered
//source: https://love2dev.com/blog/javascript-remove-from-array/
function dogSearch(req, res, mysql, context, complete) {
//begining part of the query that selects everything that needs to be displayed
var 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 ";
var inserts = [req.params.name, req.params.birthdate, req.params.breed, req.params.food, req.params.sex, req.params.adopter]; //holds the variables to be used to select dogs based on searchDogs.js
//WHERE portion of query
//adjusts if name is a parameter, if the user does not enter a name, removes that parameter from inserts
if (req.params.name != "none") {
var name1 = "WHERE d.name = ? AND "
}
else {
name1 = "WHERE ";
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.name) {
inserts.splice(i, 1);
}
}
}
//adjusts if birthdate is a parameter, if the user does not enter a birthdate, removes that parameter from inserts
if (req.params.birthdate != "none") {
var bdate = "d.birthdate = ? AND "
}
else {
bdate = "";
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.birthdate) {
inserts.splice(i, 1);
}
}
}
//adjusts if breed is a parameter, if the user does not enter a breed, removes that parameter from inserts
if (req.params.breed != "none") {
var breed1 = "d.breed = ? AND "
}
else {
breed1 = "";
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.breed) {
inserts.splice(i, 1);
}
}
}
//adjusts the food parameter, if the user chooses any food, selects dogs with any food type (matching the other parameters)
if (req.params.food == "any") {
var food1 = "d.foodType = d.foodType "
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.food) {
inserts.splice(i, 1);
}
}
}
else {
food1 = "d.foodType = ? "
}
//adjusts the sex parameter, if the user chooses all sexes, selects dogs regardless of sex (matching the other parameters)
if (req.params.sex == "All") {
var sex1 = "AND d.sex = ANY (SELECT sex FROM Dogs) "
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.sex) {
inserts.splice(i, 1);
}
}
}
else {
sex1 = "AND d.sex = ? "
}
//adjusts the food parameter, if the user chooses either, selects dogs with or without an adopter, if the user chooses any, selects dogs with any adopters,
//if the user chooses "NULL" selects dogs without adopters (matching the other parameters)
if (req.params.adopter == "either") {
var adopter1 = "";
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.adopter) {
inserts.splice(i, 1);
}
}
}
else if (req.params.adopter == "any") {
adopter1 = "AND d.adoptedBy = ANY (SELECT adoptedBy FROM Dogs) ORDER BY d.id";
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.adopter) {
inserts.splice(i, 1);
}
}
}
else if (req.params.adopter == "NULL") {
adopter1 = "AND d.adoptedBy IS NULL ORDER BY d.id";
for (var i = 0; i < inserts.length - 1; i++) {
if (inserts[i] === req.params.adopter) {
inserts.splice(i, 1);
}
}
}
else {
adopter1 = "AND d.adoptedBy = ? ORDER BY d.id;";
}
query = query + name1 + bdate + breed1 + food1 + sex1 + adopter1; //put together the entire query into one string
console.log(req.params);
mysql.pool.query(query, inserts, function (error, results, fields) {
if (error) {
res.write(JSON.stringify(error));
res.end();
}
console.log(results)
context.dogs = results; //define the results as dogs
complete(); //call the complete function to increase callbackcount
});
}
//GET request to populate the "/dogsearch" page when initially loading, calls the appropriate functions to poplate the page
router.get('/', function (req, res) {
var callbackCount = 0; //used to keep track that all data from the database has been retrieved
var context = {}; //holds the results
var mysql = req.app.get('mysql');
getDogs(res, mysql, context, complete);
getFood(res, mysql, context, complete);
getAdopters(res, mysql, context, complete);
function complete() { //function to prevent the page from loading until all data has been retrieved
callbackCount++;
if (callbackCount >= 3) {
res.render('dogsearch', context);
}
}
});
//GET request to populate the page with the search results
router.get('/:name/:birthdate/:breed/:food/:sex/:adopter', function (req, res) {
var callbackCount = 0;
var context = {};
context.jsscripts = ["searchDogs.js"]; //retrieves information about search from searchDogs.js
var mysql = req.app.get('mysql');
dogSearch(req, res, mysql, context, complete);
getFood(res, mysql, context, complete);
getAdopters(res, mysql, context, complete);
function complete() {
callbackCount++;
if (callbackCount >= 3) {
res.render('dogsearch', context);
}
}
});
return router;
}();