-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
117 lines (98 loc) · 4.39 KB
/
app.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
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var request = require('request');
var url = require('url');
var _ = require('lodash');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/dsd/code-enforcement', function(req, res) {
var dsdURL = "http://opendsd.sandiego.gov/web/Api/Maps/CECase?SearchType=CECase&SouthWestLatitude=32.71879985593221&SouthWestLongitude=-117.16525563507082&NorthEastLatitude=32.74399836325726&NorthEastLongitude=-117.12534436492922&Page=1&PageLimit=100";
request(dsdURL, function(error, response, body) {
var body = JSON.parse(body);
var features = new Array();
_.each(body.results, function(element, index) {
// Change fields based on permit type. Need to make flexible for other OpenDSD types, also, need
// to understand what the "Case Depiction" field actually does.
var titleString = "There's been a code enforcement case updated near you at: " +element.StreetAddress+".<br/> The violation is "+ element.Description +".<br/> You can find out more at: http://opendsd.sandiego.gov/web/CECases/Details/"+ element.CaseId
var feature = {
id: element.caseId,
type: "Feature",
geometry: {
type: "Point",
coordinates: [element.Longitude, element.Latitude],
},
properties: {
"id":element.CaseId,
"Description": element.Description,
"OpenDate":element.OpenDate,
"CloseDate":element.CloseDate,
"StreetAddress":element.StreetAddress,
"title": titleString,
}
};
features.push(feature);
});
var resp = {
"type": "FeatureCollection",
"features": features
}
return res.json(resp);
});
});
// Route for Discretionary Approvals. Access/GET http://localhost:8080/approvals)
router.get('/dsd/approvals', function(req, res) {
var dsdURL = "http://opendsd.sandiego.gov/web/Api/Maps/ApprovalsMap?SearchType=Discretionary&SouthWestLatitude=32.71879985593221&SouthWestLongitude=-117.16525563507082&NorthEastLatitude=32.74399836325726&NorthEastLongitude=-117.12534436492922&Page=1&PageLimit=100";
request(dsdURL, function(error, response, body) {
var body = JSON.parse(body);
var features = new Array();
_.each(body.results, function(element, index) {
// Change fields based on permit type. Need to make flexible for other OpenDSD types, also, need
// to understand what the "Case Depiction" field actually does.
var titleString = element.ApprovalType+" approval for project \"" +element.ProjectTitle+"\".<br/> You can find out more at: http://opendsd.sandiego.gov/web/Approvals/Details/"+element.ApprovalId
var feature = {
id: element.caseId,
type: "Feature",
geometry: {
type: "Point",
coordinates: [element.Longitude, element.Latitude],
},
properties: {
"ApprovalId":element.ApprovalId,
"ApprovalType": element.ApprovalType,
"ApprovalScope": element.ApprovalScope,
"CompleteCancelDate":element.CompleteCancelDate,
"IssuedDate":element.IssuedDate,
"ProjectId":element.ProjectId,
"ProjectTitle":element.ProjectTitle,
"title": titleString,
}
};
features.push(feature);
});
var resp = {
"type": "FeatureCollection",
"features": features
}
return res.json(resp);
});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);