-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
347 lines (320 loc) · 7.62 KB
/
server.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
const express = require("express");
const db = require("./db");
const morgan = require("morgan");
const cors = require("cors");
const app = express();
const path = require("path");
require("dotenv").config();
PORT = process.env.PORT || 5000;
HOST = "0.0.0.0";
//middleware
app.use(express.json());
app.use(cors());
app.use(morgan("dev"));
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
// app.get("/*", function (req, res) {
// res.sendFile(path.join(__dirname, "client", "build", "index.html"));
// });
}
//CREATE PUB SERVANT
app.post("/api/publicservant", async (req, res) => {
try {
const result = await db.query(
"INSERT INTO publicservant (email, department) values ($1, $2) returning *",
[req.body.email, req.body.department]
);
res.json({
status: "success",
data: {
user: result.rows[0],
},
});
} catch (e) {
console.log(e);
}
});
//CREATE USER
app.post("/api/createuser", async (req, res) => {
console.log("RESPONSE", req.body.email);
console.log("RESPONSE", req.body.name);
console.log("RESPONSE", req.body.surname);
try {
const result = await db.query(
"INSERT INTO users (email, name, surname, salary, phone, cname) values ($1, $2, $3, $4, $5, $6) returning *",
[
req.body.email,
req.body.name,
req.body.surname,
req.body.salary,
req.body.phone,
req.body.cname,
]
);
res.json({
status: "success",
data: {
user: result.rows[0],
},
});
} catch (e) {
console.log(e);
}
});
//get top countries
app.get("/api/topcountries/", async (req, res) => {
try {
const result = await db.query(
"SELECT cname, sum(total_patients) FROM record group by cname order by sum(total_patients) desc"
);
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET ALL RECORDS
app.get("/api/records/", async (req, res) => {
try {
const result = await db.query("select * from record");
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET covid-19 patients
app.post("/api/patients", async (req, res) => {
try {
const body = req.body.disease_code;
const result = await db.query(
"select sum(record.total_patients) from record where record.disease_code = $1 ",
[body]
);
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (err) {
console.log(err);
}
});
//GET Specializations for salary
app.get("/api/salaryspecs/", async (req, res) => {
try {
const result = await db.query(
"select distinct dt.description from specialize left join diseasetype as dt on dt.id = specialize.id"
);
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET AVG Salary per specialization
app.post("/api/avgsalary/", async (req, res) => {
try {
const body = req.body.description;
const result = await db.query(
"SELECT ROUND(avg(users.salary), 0) as avg_salary FROM specialize INNER JOIN diseasetype as dt ON specialize.id = dt.id INNER JOIN doctor as doc ON specialize.email = doc.email INNER JOIN users ON doc.email = users.email INNER JOIN country ON users.cname = country.cname where dt.description = $1 ",
[body]
);
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET SPECS
app.get("/api/specializations/", async (req, res) => {
try {
const result = await db.query(
"select spec.index, spec.email,dt.description from specialize as spec inner join diseasetype as dt on dt.id = spec.id order by spec.email"
);
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//DELETE SPEC
app.delete("/api/spec/:id", async (req, res) => {
try {
const id = req.params.id;
const result = await db.query("DELETE FROM specialize where index = $1", [
id,
]);
res.status(204).json({
status: "success",
});
} catch (e) {
console.log(e);
}
});
//GET PUBLIC SERVANTS
app.get("/api/pubservants/", async (req, res) => {
try {
const result = await db.query("select email from publicservant");
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET USERS
app.get("/api/users/", async (req, res) => {
try {
const result = await db.query("select email from users");
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET COUNTRIES
app.get("/api/countries/", async (req, res) => {
try {
const result = await db.query("select cname from country");
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET DISEASES
app.get("/api/diseases/", async (req, res) => {
try {
const result = await db.query("select disease_code from disease");
res.json({
status: "success",
result: result.rows.length,
data: {
users: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//GET PARTICULAR RECORD
app.get("/api/user/", async (req, res) => {
try {
const email = req.body.email;
const result = await db.query("select * from users where email = $1", [
email,
]);
res.json({
status: "success",
data: {
user: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//UPDATE RECORD
app.put("/api/record/:id", async (req, res) => {
try {
const id = req.params.id;
const result = await db.query(
"UPDATE record SET total_deaths = $2, total_patients = $3 where id = $1 returning *",
[id, req.body.total_deaths, req.body.total_patients]
);
res.json({
status: "success",
data: {
user: result.rows,
},
});
} catch (e) {
console.log(e);
}
});
//CREATE RECORD
app.post("/api/record", async (req, res) => {
try {
const result = await db.query(
"INSERT INTO record (email, cname, disease_code, total_deaths, total_patients) values ($1, $2, $3, $4, $5) returning *",
[
req.body.email,
req.body.cname,
req.body.disease_code,
req.body.total_deaths,
req.body.total_patients,
]
);
res.json({
status: "success",
data: {
user: result.rows[0],
},
});
} catch (e) {
console.log(e);
}
});
//DELETE RECORD
app.delete("/api/record/:id", async (req, res) => {
try {
const id = req.params.id;
const result = await db.query("DELETE FROM record where id = $1", [id]);
res.status(204).json({
status: "success",
});
} catch (e) {
console.log(e);
}
});
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "client", "build", "index.html"));
});
}
app.listen(PORT, () => {
console.log(`Server has started on PORT ${PORT}`);
});