-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
172 lines (153 loc) · 4.22 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
const express = require("express");
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
const path = require("path");
const databasePath = path.join(__dirname, "covid19India.db");
const app = express();
app.use(express.json());
let database = null;
const initializeDbAndServer = async () => {
try {
database = await open({
filename: databasePath,
driver: sqlite3.Database,
});
app.listen(3000, () =>
console.log("Server Running at http://localhost:3000/")
);
} catch (error) {
console.log(`DB Error: ${error.message}`);
process.exit(1);
}
};
initializeDbAndServer();
const convertStateDbObjectToResponseObject = (dbObject) => {
return {
stateId: dbObject.state_id,
stateName: dbObject.state_name,
population: dbObject.population,
};
};
const convertDistrictDbObjectToResponseObject = (dbObject) => {
return {
districtId: dbObject.district_id,
districtName: dbObject.district_name,
stateId: dbObject.state_id,
cases: dbObject.cases,
cured: dbObject.cured,
active: dbObject.active,
deaths: dbObject.deaths,
};
};
app.get("/states/", async (request, response) => {
const getStatesQuery = `
SELECT
*
FROM
state;`;
const statesArray = await database.all(getStatesQuery);
response.send(
statesArray.map((eachState) =>
convertStateDbObjectToResponseObject(eachState)
)
);
});
app.get("/states/:stateId/", async (request, response) => {
const { stateId } = request.params;
const getStateQuery = `
SELECT
*
FROM
state
WHERE
state_id = ${stateId};`;
const state = await database.get(getStateQuery);
response.send(convertStateDbObjectToResponseObject(state));
});
app.get("/districts/:districtId/", async (request, response) => {
const { districtId } = request.params;
const getDistrictsQuery = `
SELECT
*
FROM
district
WHERE
district_id = ${districtId};`;
const district = await database.get(getDistrictsQuery);
response.send(convertDistrictDbObjectToResponseObject(district));
});
app.post("/districts/", async (request, response) => {
const { stateId, districtName, cases, cured, active, deaths } = request.body;
const postDistrictQuery = `
INSERT INTO
district (state_id, district_name, cases, cured, active, deaths)
VALUES
(${stateId}, '${districtName}', ${cases}, ${cured}, ${active}, ${deaths});`;
await database.run(postDistrictQuery);
response.send("District Successfully Added");
});
app.delete("/districts/:districtId/", async (request, response) => {
const { districtId } = request.params;
const deleteDistrictQuery = `
DELETE FROM
district
WHERE
district_id = ${districtId}
`;
await database.run(deleteDistrictQuery);
response.send("District Removed");
});
app.put("/districts/:districtId/", async (request, response) => {
const { districtId } = request.params;
const { districtName, stateId, cases, cured, active, deaths } = request.body;
const updateDistrictQuery = `
UPDATE
district
SET
district_name = '${districtName}',
state_id = ${stateId},
cases = ${cases},
cured = ${cured},
active = ${active},
deaths = ${deaths}
WHERE
district_id = ${districtId};
`;
await database.run(updateDistrictQuery);
response.send("District Details Updated");
});
app.get("/states/:stateId/stats/", async (request, response) => {
const { stateId } = request.params;
const getStateStatsQuery = `
SELECT
SUM(cases),
SUM(cured),
SUM(active),
SUM(deaths)
FROM
district
WHERE
state_id=${stateId};`;
const stats = await database.get(getStateStatsQuery);
response.send({
totalCases: stats["SUM(cases)"],
totalCured: stats["SUM(cured)"],
totalActive: stats["SUM(active)"],
totalDeaths: stats["SUM(deaths)"],
});
});
app.get("/districts/:districtId/details/", async (request, response) => {
const { districtId } = request.params;
const getStateNameQuery = `
SELECT
state_name
FROM
district
NATURAL JOIN
state
WHERE
district_id=${districtId};`;
const state = await database.get(getStateNameQuery);
response.send({ stateName: state.state_name });
});
module.exports = app;