forked from knightsamar/cs340_sample_nodejs_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
people.js
197 lines (175 loc) · 7.39 KB
/
people.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
module.exports = function(){
var express = require('express');
var router = express.Router();
function getPlanets(res, mysql, context, complete){
mysql.pool.query("SELECT planet_id as id, name FROM bsg_planets", function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.planets = results;
complete();
});
}
function getPeople(res, mysql, context, complete){
mysql.pool.query("SELECT bsg_people.character_id as id, fname, lname, bsg_planets.name AS homeworld, age FROM bsg_people INNER JOIN bsg_planets ON homeworld = bsg_planets.planet_id", function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.people = results;
complete();
});
}
function getPeoplebyHomeworld(req, res, mysql, context, complete){
var query = "SELECT bsg_people.character_id as id, fname, lname, bsg_planets.name AS homeworld, age FROM bsg_people INNER JOIN bsg_planets ON homeworld = bsg_planets.planet_id WHERE bsg_people.homeworld = ?";
console.log(req.params)
var inserts = [req.params.homeworld]
mysql.pool.query(query, inserts, function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.people = results;
complete();
});
}
/* Find people whose fname starts with a given string in the req */
function getPeopleWithNameLike(req, res, mysql, context, complete) {
//sanitize the input as well as include the % character
var query = "SELECT bsg_people.character_id as id, fname, lname, bsg_planets.name AS homeworld, age FROM bsg_people INNER JOIN bsg_planets ON homeworld = bsg_planets.planet_id WHERE bsg_people.fname LIKE " + mysql.pool.escape(req.params.s + '%');
console.log(query)
mysql.pool.query(query, function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.people = results;
complete();
});
}
function getPerson(res, mysql, context, id, complete){
var sql = "SELECT character_id as id, fname, lname, homeworld, age FROM bsg_people WHERE character_id = ?";
var inserts = [id];
mysql.pool.query(sql, inserts, function(error, results, fields){
if(error){
res.write(JSON.stringify(error));
res.end();
}
context.person = results[0];
complete();
});
}
/*Display all people. Requires web based javascript to delete users with AJAX*/
router.get('/', function(req, res){
var callbackCount = 0;
var context = {};
context.jsscripts = ["deleteperson.js","filterpeople.js","searchpeople.js"];
var mysql = req.app.get('mysql');
getPeople(res, mysql, context, complete);
getPlanets(res, mysql, context, complete);
function complete(){
callbackCount++;
if(callbackCount >= 2){
res.render('people', context);
}
}
});
/*Display all people from a given homeworld. Requires web based javascript to delete users with AJAX*/
router.get('/filter/:homeworld', function(req, res){
var callbackCount = 0;
var context = {};
context.jsscripts = ["deleteperson.js","filterpeople.js","searchpeople.js"];
var mysql = req.app.get('mysql');
getPeoplebyHomeworld(req,res, mysql, context, complete);
getPlanets(res, mysql, context, complete);
function complete(){
callbackCount++;
if(callbackCount >= 2){
res.render('people', context);
}
}
});
/*Display all people whose name starts with a given string. Requires web based javascript to delete users with AJAX */
router.get('/search/:s', function(req, res){
var callbackCount = 0;
var context = {};
context.jsscripts = ["deleteperson.js","filterpeople.js","searchpeople.js"];
var mysql = req.app.get('mysql');
getPeopleWithNameLike(req, res, mysql, context, complete);
getPlanets(res, mysql, context, complete);
function complete(){
callbackCount++;
if(callbackCount >= 2){
res.render('people', context);
}
}
});
/* Display one person for the specific purpose of updating people */
router.get('/:id', function(req, res){
callbackCount = 0;
var context = {};
context.jsscripts = ["selectedplanet.js", "updateperson.js"];
var mysql = req.app.get('mysql');
getPerson(res, mysql, context, req.params.id, complete);
getPlanets(res, mysql, context, complete);
function complete(){
callbackCount++;
if(callbackCount >= 2){
res.render('update-person', context);
}
}
});
/* Adds a person, redirects to the people page after adding */
router.post('/', function(req, res){
console.log(req.body.homeworld)
console.log(req.body)
var mysql = req.app.get('mysql');
var sql = "INSERT INTO bsg_people (fname, lname, homeworld, age) VALUES (?,?,?,?)";
var inserts = [req.body.fname, req.body.lname, req.body.homeworld, req.body.age];
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('/people');
}
});
});
/* The URI that update data is sent to in order to update a person */
router.put('/:id', function(req, res){
var mysql = req.app.get('mysql');
console.log(req.body)
console.log(req.params.id)
var sql = "UPDATE bsg_people SET fname=?, lname=?, homeworld=?, age=? WHERE character_id=?";
var inserts = [req.body.fname, req.body.lname, req.body.homeworld, req.body.age, 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();
}
});
});
/* Route to delete a person, simply returns a 202 upon success. Ajax will handle this. */
router.delete('/:id', function(req, res){
var mysql = req.app.get('mysql');
var sql = "DELETE FROM bsg_people WHERE character_id = ?";
var inserts = [req.params.id];
sql = mysql.pool.query(sql, inserts, function(error, results, fields){
if(error){
console.log(error)
res.write(JSON.stringify(error));
res.status(400);
res.end();
}else{
res.status(202).end();
}
})
})
return router;
}();