-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
286 lines (262 loc) · 10.3 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
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
//Main Server File
var express = require('express');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
var neo4j = require('neo4j-driver').v1;
//ExpressJS
var app = express();
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.set('public', path.join(__dirname, 'public'))
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(__dirname + "/public"));
//Initialize neo4j driver
var driver = neo4j.driver('bolt://hobby-nimcknmcieflgbkepkpnpidl.dbs.graphenedb.com:24787', neo4j.auth.basic('myapp', 'b.yntraNmvs9zf.OB5EVHINaBBWtc3M'))
var session = driver.session()
//app getters and posters
app.get("/", (req, res) => {
res.render("index.ejs");
})
app.get("/claim", (req, res) => {
res.render("claim.ejs");
});
app.get('/populate', function (req, res) {
session
.run("START t=node(*) RETURN t")
.then(function (result) {
var peopleArr = []
result.records.forEach(function (record) {
peopleArr.push({
id: record._fields[0].identity.low,
title: record._fields[0].properties.name,
role: record._fields[0].properties.role,
org: record._fields[0].properties.organization,
})
})
res.render('populate.ejs', {
people: peopleArr
})
})
.catch(function (err) {
console.log(err)
})
})
app.get('/search', function (req, res) {
session
.run("START n=node(*) RETURN n LIMIT 0")
.then(function (result) {
var peopleArr = []
result.records.forEach(function (record) {
peopleArr.push({
id: record._fields[0].identity.low,
title: record._fields[0].properties.name,
safe: record._fields[0].properties.safe,
})
})
res.render('search.ejs', {
people: peopleArr
})
})
.catch(function (err) {
console.log(err)
})
})
app.post('/search', function (req, res) {
var name = req.body.name;
var role = req.body.role;
var org = req.body.org;
var peopleArr = []
session
.run("MATCH (n {name:{nameParam}}) RETURN n", { nameParam: name })
.then(function (result) {
result.records.forEach(function (record) {
peopleArr.push({
id: record._fields[0].identity.low,
title: record._fields[0].properties.name,
safe: record._fields[0].properties.safe,
geo: record._fields[0].properties.geo,
cond: record._fields[0].properties.cond,
msg: record._fields[0].properties.msg,
})
})
session
.run("MATCH (n {role:{roleParam}}) RETURN n", { roleParam: role })
.then(function (result) {
result.records.forEach(function (record) {
peopleArr.push({
id: record._fields[0].identity.low,
title: record._fields[0].properties.name,
safe: record._fields[0].properties.safe,
geo: record._fields[0].properties.geo,
cond: record._fields[0].properties.cond,
msg: record._fields[0].properties.msg,
})
})
session
.run("MATCH (n {organization:{orgParam}}) RETURN n", { orgParam: org })
.then(function (result) {
result.records.forEach(function (record) {
peopleArr.push({
id: record._fields[0].identity.low,
title: record._fields[0].properties.name,
safe: record._fields[0].properties.safe,
geo: record._fields[0].properties.geo,
cond: record._fields[0].properties.cond,
msg: record._fields[0].properties.msg,
})
})
res.render('search.ejs', {
people: peopleArr
})
}).catch(function (err) {
console.log(err)
})
}).catch(function (err) {
console.log(err)
})
})
.catch(function (err) {
console.log(err)
})
})
app.post('/claim/person/person', function (req, res) {
var name = req.body.name;
var geo1 = req.body.geo;
var condition = req.body.condition;
var custommsg = req.body.custommsg;
session
.run("MATCH (n { name: {nameParam}}) SET n.safe = true SET n.geo={geoParam} SET n.cond={condParam} SET n.msg={msgParam}", { nameParam: name, geoParam: geo1, condParam: condition, msgParam: custommsg })
.then(function (result) {
res.redirect('/claim#about')
session.close()
})
.catch(function (err) {
console.log(err)
})
})
app.post('/person/add', function (req, res) {
var name1 = req.body.name;
var rol1 = req.body.role
var org1 = req.body.org;
var type1 = req.body.type;
var geo = req.body.geo;
var email = req.body.email;
if (type1 === "person") {
session
.run("CREATE(n:person {name:{nameParam},role:{rolParam},organization:{orgParam}, geo:{geoParam}, email:{emailParam}, safe:true}) RETURN n", { rolParam: rol1, nameParam: name1, orgParam: org1, geoParam: geo, emailParam: email })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
}
else if (type1 === "pet") {
session
.run("CREATE(n:pet {name:{nameParam},role:{rolParam},organization:{orgParam}, safe:true}) RETURN n", { rolParam: rol1, nameParam: name1, orgParam: org1 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
}
else {
session
.run("CREATE(n:other {name:{nameParam},role:{rolParam},organization:{orgParam},safe:true}) RETURN n", { rolParam: rol1, nameParam: name1, orgParam: org1 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
}
})
app.post('/person/del', function (req, res) {
var name1 = req.body.name1;
var name2 = req.body.name2;
session
.run("MATCH (n { name: {nameParam1} })-[r]->(k {name: {nameParam2}}) DELETE r", { nameParam1: name1, nameParam2: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
})
app.post('/person/link', function (req, res) {
var name1 = req.body.name1;
var connect = req.body.connect;
var name2 = req.body.name2;
if (connect === "friend")
session
.run("MATCH (a:person),(b:person) WHERE a.name = {name1Param} AND b.name = {name2Param} CREATE (a)-[r:friend]->(b)", { name1Param: name1, name2Param: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
else if (connect === "relative")
session
.run("MATCH (a:person),(b:person) WHERE a.name = {name1Param} AND b.name = {name2Param} CREATE (a)-[r:relative]->(b)", { name1Param: name1, name2Param: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
else if (connect === "neighbour")
session
.run("MATCH (a:person),(b:person) WHERE a.name = {name1Param} AND b.name = {name2Param} CREATE (a)-[r:neighbour]->(b)", { name1Param: name1, name2Param: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
else if (connect === "colleague")
session
.run("MATCH (a:person),(b:person) WHERE a.name = {name1Param} AND b.name = {name2Param} CREATE (a)-[r:colleague]->(b)", { name1Param: name1, name2Param: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
else if (connect === "owner")
session
.run("MATCH (a:person),(b:pet) WHERE a.name = {name1Param} AND b.name = {name2Param} CREATE (a)-[r:owner]->(b)", { name1Param: name1, name2Param: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
else
session
.run("MATCH (a:person),(b:person) WHERE a.name = {name1Param} AND b.name = {name2Param} CREATE (a)-[r:other]->(b)", { name1Param: name1, name2Param: name2 })
.then(function (result) {
res.redirect('/populate#results')
session.close()
})
.catch(function (err) {
console.log(err)
})
})
//process.env.PORT for listening to the correct port after deployment
app.listen(process.env.PORT || 3000)
console.log('Server started!')
module.exports = app