-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (129 loc) · 3.39 KB
/
index.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
DATABASE_URL= "postgresql://Ashok:czMOGn1z2dS02HhyzZmMdw@free-tier4.aws-us-west-2.cockroachlabs.cloud:26257/defaultdb?sslmode=verify-full&options=--cluster%3Dgrim-cicada-3974"
//using cockroach DB
const Sequelize = require("sequelize-cockroachdb")
const sequelize = new Sequelize(DATABASE_URL)
//creating a location object to store
const Transfer = sequelize.define("transfer_v1", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
address: {
type: Sequelize.TEXT,
}
})
//get call to fetch a list of all the crime locations
const tr_list = async (event) => {
try {
const sync = await Transfer.sync({force: false})
console.log("sync")
console.log(sync)
const transfer = await Transfer.findAll()
console.log("location")
console.log(transfer)
return {
statusCode: 200,
body: JSON.stringify(transfer)
}
}
catch(error) {
console.log("error" + error)
return {
statusCode: 200,
body: JSON.stringify("generic")
}
}
}
//post method to add crime data to the database
const tr_add = async (event) => {
console.log("event " + event)
if (!event["address"]){
event = JSON.parse(event.body)
}
console.log(event)
console.log(event["address"])
return Transfer.sync({
force: false,
}).then(function(){
return {
statusCode: 200,
body: JSON.stringify(Transfer.bulkCreate([
{
address: event["address"]
}]))
}
})
}
const Location = sequelize.define("locations_v1", {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
address: {
type: Sequelize.TEXT,
},
longitude: {
type: Sequelize.FLOAT,
},
latitude: {
type: Sequelize.FLOAT,
},
crime_score: {
type: Sequelize.INTEGER,
}
})
//get call to fetch a list of all the crime locations
const list = async (event) => {
try {
const sync = await Location.sync({force: false})
console.log("sync")
console.log(sync)
const location = await Location.findAll()
console.log("location")
console.log(location)
return {
statusCode: 200,
body: JSON.stringify(location)
}
}
catch(error) {
console.log("error" + error)
return {
statusCode: 200,
body: JSON.stringify("generic")
}
}
}
//post method to add crime data to the database
const add = async (event) => {
console.log(event)
body = JSON.parse(event.body)
console.log(body)
return Location.sync({
force: false,
}).then(function(){
return {
statusCode: 200,
body: JSON.stringify(Location.bulkCreate([
{
address: body["address"],
longitude: body["longitude"],
latitude: body["latitude"],
crime_score: body["crime_score"]
}]))
}
})
}
//removes all locations from the database
const remove = async (event) => {
console.log(Location)
Location.drop();
return {
statusCode: 200,
body: JSON.stringify(location)
}
}
//exporting api calls
module.exports = {list, add, remove, tr_list, tr_add};