-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
182 lines (170 loc) · 4.58 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
const express = require("express");
const app = express();
const path = require("path");
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
app.use(express.json());
let db = null;
let dbPath = path.join(__dirname, "covid19India.db");
const initializeDbAndServer = async () => {
try {
db = await open({
filename: dbPath,
driver: sqlite3.Database,
});
app.listen(3000, () => {
console.log("The Server is running at http://localhost:3000/");
});
} catch (e) {
console.log(`DB Error : ${e.message}`);
process.exit(1);
}
};
initializeDbAndServer();
//get all states
app.get("/states/", async (request, response) => {
const allStatesQuery = `
SELECT
*
FROM
state;`;
const allStatesArray = await db.all(allStatesQuery);
const getAllDetails = (eachArray) => {
return {
stateId: eachArray.state_id,
stateName: eachArray.state_name,
population: eachArray.population,
};
};
response.send(allStatesArray.map((eachArray) => getAllDetails(eachArray)));
});
//get a state
app.get("/states/:stateId/", async (request, response) => {
const { stateId } = request.params;
const getStateQuery = `
SELECT
*
FROM
state
WHERE
state_id = ${stateId};`;
const getState = await db.get(getStateQuery);
const getStateDetails = (State) => {
return {
stateId: State.state_id,
stateName: State.state_name,
population: State.population,
};
};
response.send(getStateDetails(getState));
});
//create a District
app.post("/districts/", async (request, response) => {
const districtDetails = request.body;
const {
districtName,
stateId,
cases,
cured,
active,
deaths,
} = districtDetails;
const addDistrictQuery = `
INSERT INTO
district (district_name, state_id, cases, cured, active, deaths)
VALUES
('${districtName}', ${stateId}, ${cases}, ${cured}, ${active}, ${deaths});`;
await db.run(addDistrictQuery);
response.send("District Successfully Added");
});
//get a district based on district id
app.get("/districts/:districtId/", async (request, response) => {
const { districtId } = request.params;
const getDistrictQuery = `
SELECT
*
FROM
district
WHERE
district_id = ${districtId};`;
const dbDistrict = await db.get(getDistrictQuery);
const getDistrictCamel = (district) => {
return {
districtId: district.district_id,
districtName: district.district_name,
stateId: district.state_id,
cases: district.cases,
cured: district.cured,
active: district.active,
deaths: district.deaths,
};
};
response.send(getDistrictCamel(dbDistrict));
});
//delete a district based on district id
app.delete("/districts/:districtId/", async (request, response) => {
const { districtId } = request.params;
const deleteQuery = `
DELETE FROM
district
WHERE
district_id = ${districtId}`;
const dbDelete = await db.run(deleteQuery);
response.send("District Removed");
});
//update a district based on district id
app.put("/districts/:districtId/", async (request, response) => {
const { districtId } = request.params;
const districtBody = request.body;
const { districtName, stateId, cases, cured, active, deaths } = districtBody;
const updateDistrictQuery = `
UPDATE
district
SET
district_name='${districtName}',
state_id= ${stateId},
cases=${cases},
cured=${cured},
active=${active},
deaths=${deaths}`;
const updateDistrict = await db.run(updateDistrictQuery);
response.send("District Details Updated");
});
//statistics of total cases, cured, active, deaths of a specific state based on state ID
app.get("/states/:stateId/stats/", async (request, response) => {
const { stateId } = request.params;
const getStatisticsQuery = `
SELECT
SUM(cases) AS totalCases,
SUM(cured) AS totalCured,
SUM(active) AS totalActive,
SUM(deaths) AS totalDeaths
FROM
district
WHERE
state_id = ${stateId};`;
const statistics = await db.get(getStatisticsQuery);
response.send(statistics);
});
//Returns an object containing the state name of a district based on the district ID
app.get("/districts/:districtId/details/", async (request, response) => {
const { districtId } = request.params;
const getStateIdQuery = `
SELECT
state_id
FROM
district
WHERE
district_id = ${districtId};`;
const gotStateId = await db.get(getStateIdQuery);
const getStateNameQuery = `
SELECT
state_name AS stateName
FROM
state
WHERE
state_id =${gotStateId.state_id};`;
const stateName = await db.get(getStateNameQuery);
response.send(stateName);
});
module.exports = app;