-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (121 loc) · 3.59 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
const express = require('express')
const app = express()
app.use(express.json())
const sqlite3 = require('sqlite3').verbose()
const {open} = require('sqlite')
const path = require('path')
const jwt = require('jsonwebtoken')
const bcrypt = require('bcrypt')
let db = null
const dbpath = path.join(__dirname, 'covid19IndiaPortal.db')
const func = async () => {
try {
db = await open({
filename: dbpath,
driver: sqlite3.Database,
})
app.listen(3002, () => {
console.log('running')
})
} catch (e) {
console.log(e)
}
}
func()
app.post('/login/', async (req, res) => {
const {username, password} = req.body
const query3 = `select * from user where username="${username}"`
const a3 = await db.get(query3)
console.log(a3)
if (a3) {
console.log(a3.password)
const hashpassword = a3.password
const comp = await bcrypt.compare(password, hashpassword)
if (comp) {
const payload = {username: a3.name}
const token = jwt.sign(payload, 'tejaram')
res.send({jwtToken: token})
} else {
res.status(400)
res.send('Invalid password')
}
} else {
res.status(400)
res.send('Invalid user')
}
})
const logger = (req, res, next) => {
const authtoken = req.headers['authorization']
console.log(authtoken)
let Token
if (authtoken !== undefined) {
Token = authtoken.split(' ')[1]
}
if (Token === undefined) {
res.status(401)
res.send('Invalid JWT Token')
} else {
jwt.verify(Token, 'tejaram', async (error, payload) => {
if (error) {
res.status(401)
res.send('Invalid JWT Token')
} else {
next()
}
})
}
}
app.get(`/states/`, logger, async (req, res) => {
const query = `select * from state`
const states = await db.all(query)
res.send(states)
})
app.get(`/states/:stateId/`, logger, async (req, res) => {
const {stateId} = req.params
const query2 = `select * from state where state_id=${stateId} `
const state = await db.get(query2)
res.send(state)
})
app.post(`/districts/`, logger, async (req, res) => {
const {districtName, stateId, cases, cured, active, deaths} = req.body
const query3 = `insert into district (district_name,state_id,cases,cured,active,deaths) values ("${districtName}",${stateId},${cases},${cured},${active},${deaths})`
await db.run(query3)
res.send('District Successfully Added')
})
app.get(`/districts/:districtId/`, logger, async (req, res) => {
const {districtId} = req.params
const query4 = `select * from district where district_id=${districtId}`
const district = await db.get(query4)
res.send(district)
})
app.put(`/districts/:districtId/`, logger, async (req, res) => {
const {districtId} = req.params
const {districtName, stateId, cases, cured, active, deaths} = req.body
const query4 = `UPDATE district SET
district_name="${districtName}",
state_id=${stateId},
cases=${cases},
cured=${cured},
active=${active},
deaths=${deaths}
WHERE district_id=${districtId}`
await db.run(query4)
res.send('District Details Updated')
})
app.delete(`/districts/:districtId/`, logger, async (req, res) => {
const {districtId} = req.params
const query5 = `delete from district where district_id=${districtId}`
await db.run(query5)
res.send('District Removed')
})
app.get(`/states/:stateId/stats/`, logger, async (req, res) => {
const {stateId} = req.params
const query6 = `select sum(cases) as totalCases,
sum(cured) as totalCured,
sum(active) as totalActive,
sum(deaths) as totalDeaths
from district where state_id=${stateId}`
const results = await db.get(query6)
res.send(results)
})
module.exports = app