-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.js
172 lines (146 loc) · 4.49 KB
/
handler.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
'use strict';
const { mongoose } = require('mongoose');
const { WeatherSchema } = require('./model/Weather')
const mongoDbUrl = "mongodb+srv://admin:[email protected]/weather-task?retryWrites=true&w=majority";
function connect() {
return new Promise(resolve => {
resolve(mongoose.connect(mongoDbUrl, {
useNewUrlParser: true
}));
});
}
module.exports.InsertWeathers = async (event, context) => {
console.log("event: ", event);
console.log("context: ", context);
try {
console.log("MomgoDB Connecting...");
await connect();
mongoose.connection.on("open", () => {
console.log("MomgoDB Connected...");
});
mongoose.connection.on("error", () => {
console.error("Couldn't Connected to MomgoDB...");
const response = {
statusCode: 500,
body: JSON.stringify({
message: "Unable to Connect to MongoDB",
error: error
},
null,
4
),
};
return response;
});
const weathers = [];
JSON.parse(event.body).forEach(element => {
weathers.push(new WeatherSchema(element));
});
return WeatherSchema.insertMany(weathers
).then((docs) => {
console.log(docs.length, "Inserted");
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Weathers Data Inserted',
docs: docs
},
null,
4
),
};
return response;
}).catch((error) => {
console.error("Unable to Insert, Error: ", error);
const response = {
statusCode: 500,
body: JSON.stringify({
message: "Unable to Insert Weather Data",
error: error
},
null,
4
),
};
return response;
});
} catch(error) {
console.error("Error: ", error);
const response = {
statusCode: 500,
body: JSON.stringify({
message: "Error Occured",
error: error
},
null,
4
),
};
return response;
}
};
module.exports.GetWeathers = async (event, context) => {
console.log("event: ", event);
console.log("context: ", context);
try {
console.log("MomgoDB Connecting...");
await connect();
mongoose.connection.on("open", () => {
console.log("MomgoDB Connected...");
});
mongoose.connection.on("error", () => {
console.error("Couldn't Connected to MomgoDB...");
const response = {
statusCode: 500,
body: JSON.stringify({
message: "Unable to Connect to MongoDB",
error: error
},
null,
4
),
};
return response;
});
return WeatherSchema.find(
).then((docs) => {
console.log(docs.length, "Read");
const response = {
statusCode: 200,
body: JSON.stringify({
weathers: docs
},
null,
4
),
};
return response;
}).catch((error) => {
console.error("Unable to Read, Error: ", error);
const response = {
statusCode: 500,
body: JSON.stringify({
message: "Unable to Read Weather Data",
error: error
},
null,
4
),
};
return response;
});
} catch(error) {
console.error("Error: ", error);
const response = {
statusCode: 500,
body: JSON.stringify({
message: "Error Occured",
error: error
},
null,
4
),
};
return response;
}
};