Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
300 lines (283 loc) · 8.96 KB
/
Copy pathserver.js
File metadata and controls
300 lines (283 loc) · 8.96 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// importing modules
var express = require("express")
var sqlite3 = require("sqlite3")
var fs = require("fs")
var bodyParser = require("body-parser")
var path = require("path")
var js2xmlparser = require("js2xmlparser")
var cors = require("cors");
// set path for public directory and database
var public_dir = path.join(__dirname, "public");
var db_filename = path.join(__dirname, "public", "stpaul_crime.sqlite3");
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(cors());
var port = 8000;
app.use(express.static(public_dir));
// open stpaul_crime.sqlite3 database
var db = new sqlite3.Database(db_filename, sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.log("Error opening " + db_filename);
}
else {
console.log("Now connected to " + db_filename);
}
});
// returns JSON object with list of codes and their corresponding incident type
// query options: code, format
app.get("/codes", (req, res) => {
var codeObject = {};
var newKey = "";
var newValue = "";
var queryString = "SELECT * FROM Codes";
// if codes are specified, run a query for each code and send the object
if (req.query.code !== undefined) {
var requestedCodes = req.query.code.split(",");
queryString += " WHERE code=?";
for (let i=1; i<requestedCodes.length; i++) {
queryString += " OR code=?"
}
db.each(queryString, requestedCodes, (err, row) => {
newKey = "C" + row.code;
newValue = row.incident_type;
codeObject[newKey] = newValue;
}, () => {
if (req.query.format == "xml") {
// sends xml file if format=xml
var xmlCodes = js2xmlparser.parse("codes", codeObject);
res.type("xml").send(xmlCodes);
}
else {
// if xml is not specified, sends json object
res.type("json").send(codeObject);
}
});
} else {
// if no codes specified
db.each("SELECT * FROM Codes", (err, row) => {
newKey = "C" + row.code;
newValue = row.incident_type;
codeObject[newKey] = newValue;
}, () => {
// changes format of response
if (req.query.format == "xml") {
// sends xml file if format=xml
var xmlCodes = js2xmlparser.parse("codes", codeObject);
res.type("xml").send(xmlCodes);
}
else {
// if xml is not specified, sends json object
res.type("json").send(codeObject);
}
});
}
});
// returns JSON object wiht list of neighborhood ids and their corresponding neighborhood name
// query options: id, format
app.get("/neighborhoods", (req, res) => {
var neighborhoodObject = {};
var newKey = "";
var newValue = "";
var queryString = "SELECT * FROM Neighborhoods";
if (req.query.id !== undefined) {
var requestedNeighborhoods = req.query.id.split(",");
queryString += " WHERE neighborhood_number=?";
for (let i=1; i<requestedNeighborhoods.length; i++) {
queryString += " OR neighborhood_number=?"
}
console.log(queryString);
db.each(queryString, requestedNeighborhoods, (err, row) => {
newKey = "N" + row.neighborhood_number;
newValue = row.neighborhood_name;
neighborhoodObject[newKey] = newValue;
}, () => {
if (req.query.format == "xml") {
// sends xml file if format=xml
var xmlCodes = js2xmlparser.parse("neighborhoods", neighborhoodObject);
res.type("xml").send(xmlCodes);
}
else {
// if xml is not specified, sends json object
res.type("json").send(neighborhoodObject);
}
});
} else {
db.each("SELECT * FROM Neighborhoods", (err, row) => {
newKey = "N" + row.neighborhood_number;
newValue = row.neighborhood_name;
neighborhoodObject[newKey] = newValue;
}, () => {
if (req.query.format == "xml") {
// sends xml file if format=xml
var xmlCodes = js2xmlparser.parse("neighborhoods", neighborhoodObject);
res.type("xml").send(xmlCodes);
} else {
// if xml is not specified, sends json object
res.type("json").send(neighborhoodObject);
}
});
}
});
// returns JSON object with list of crime incidents
// seperate date and time fields
// query options: start_date, end_date, code, grid, neighborhood, limit, format
app.get("/incidents", (req, res) => {
var incidentObject ={};
var keys ={
date:{},
time:{},
incident:{},
neighborhood_number:{},
police_grid:{},
block:{}
};
var limit=10000;
var WhereString="";
if(req.query.limit!==undefined)
{
limit=parseInt(req.query.limit,10);
}
if(req.query.code!==undefined)
{
var codes=req.query.code.split(",");
WhereString=" WHERE (code="+codes[0];
for(let i =1;i<codes.length;i++)
{
WhereString=WhereString+ " OR code="+codes[i];
}
WhereString=WhereString+")";
}
if(req.query.id!==undefined)
{
var neighborhood= req.query.id.split(",");
if(WhereString==="")
{
WhereString= "WHERE (neighborhood_number="+neighborhood[0];
}
else
{
WhereString= WhereString+" AND (neighborhood_number="+neighborhood[0];
}
for(let i =1;i<neighborhood.length;i++)
{
WhereString= WhereString+ " OR neighborhood_number="+ neighborhood[i];
}
WhereString=WhereString+")";
}
if(req.query.grid!==undefined)
{
var grids= req.query.grid.split(",");
if(WhereString==="")
{
WhereString=" WHERE (police_grid="+grids[0];
}
else
{
WhereString=WhereString+" AND (police_grid="+grids[0];
}
for(let i=1;i<grids.length;i++)
{
WhereString= WhereString+ " OR police_grid="+grids[i];
}
WhereString=WhereString+")";
}
if(req.query.start_date!==undefined)
{
if(WhereString==="")
{
WhereString=" WHERE (date_time >= "+"'"+ req.query.start_date+"T00:00:00"+"')";
}
else
{
WhereString=WhereString+" AND (date_time >= "+"'"+req.query.start_date+"T"+"00:00:00')";
}
}
if(req.query.end_date!==undefined)
{
if(WhereString==="")
{
WhereString=" WHERE ( date_time <= "+"'"+ req.query.end_date+"T"+"23:59:59')";
}
else
{
WhereString=WhereString+" AND ( date_time <= "+ "'"+req.query.end_date+"T"+"23:59:59')";
}
}
console.log(WhereString);
db.each("SELECT * FROM incidents"+WhereString+" ORDER BY date_time LIMIT ?",[limit],(err,row) =>{// for limit sort by date time then do the first n objects
var checkTime=row.date_time.split("T")[1];
var position=checkTime.indexOf(".");
if(position!==-1)
{
var goodTime=checkTime.split(".")[0];
}
else
{
goodTime=checkTime;
}
incidentObject["I"+ row.case_number]={
date:row.date_time.split("T")[0],
time:goodTime,
code:row.code,
incident:row.incident,
police_grid:row.police_grid,
neighborhood_number:row.neighborhood_number,
block:row.block
};
}, () => {
if (req.query.format == "xml") {
// sends xml file if format=xml
var xmlCodes = js2xmlparser.parse("incident", incidentObject);
res.type("xml").send(xmlCodes);
}
else{
// sends json object
res.type("json").send(incidentObject);
}
});
});
// upload new incident to database
/*
fields:
case_number
date
time
code
incident
police_grid
neighborhood_number
block
*/
// reject with status 500 if case number already exists
app.put("/new-incident", (req, res) => {
var incident_check=false;
var incident_number= parseInt(req.body.case_number,10);
var date_time=req.body.date+"T"+req.body.time;
console.log(date_time);
var code=req.body.code;
var incident=req.body.incident;
var police_grid=req.body.police_grid;
var neighborhood_number=req.body.neighborhood_number;
var block=req.body.block;
db.get("SELECT * FROM incidents WHERE incidents=?",[incident_number] ,(err, row) => {
if(row === undefined)
{
db.run("INSERT INTO incidents (case_number,date_time,code,incident,police_grid,neighborhood_number,block) VALUES(?,?,?,?,?,?,?)",[incident_number,date_time,code,incident,police_grid,neighborhood_number,block],(err)=>{
if(err)
{
res.status(500).send('Error: something happened');
}
else
{
res.status(200).send('Success');
}
});
}
else
{
res.status(500).send('Error: case already exist');
}
}) // need to figure out where case_number is in the put request
});
var server = app.listen(port);
console.log("Now listening on port " + port);