-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbs.js
More file actions
101 lines (87 loc) · 3.06 KB
/
Copy pathdbs.js
File metadata and controls
101 lines (87 loc) · 3.06 KB
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
const JSONdb = require('simple-json-db')
const db = new JSONdb('db.json')
const express = require('express')
const fs = require('fs')
const app = express()
app.use(express.json())
app.get('/', (req, res) => {
const output = { value: 'hello world!' }
res.send(output)
})
/* Create - POST method */
app.post('/user/add', (req, res) => {
//get the existing user data
const existUsers = getUserData()
//get the new user data from post request
const userData = req.body
//check if the userData fields are missing
if (userData.fullname == null || userData.age == null || userData.username == null || userData.password == null) {
return res.status(401).send({error: true, msg: 'User data missing'})
}
//check if the username exist already
const findExist = existUsers.find( user => user.username === userData.username )
if (findExist) {
return res.status(409).send({error: true, msg: 'username already exist'})
}
//append the user data
existUsers.push(userData)
//save the new user data
saveUserData(existUsers);
res.send({success: true, msg: 'User data added successfully'})
})
/* Read - GET method */
app.get('/user/list', (req, res) => {
const users = getUserData()
res.send(users)
})
/* Update - Patch method */
app.patch('/user/update/:username', (req, res) => {
//get the username from url
const username = req.params.username
//get the update data
const userData = req.body
//get the existing user data
const existUsers = getUserData()
//check if the username exist or not
const findExist = existUsers.find( user => user.username === username )
if (!findExist) {
return res.status(409).send({error: true, msg: 'username not exist'})
}
//filter the userdata
const updateUser = existUsers.filter( user => user.username !== username )
//push the updated data
updateUser.push(userData)
//finally save it
saveUserData(updateUser)
res.send({success: true, msg: 'User data updated successfully'})
})
/* Delete - Delete method */
app.delete('/user/delete/:username', (req, res) => {
const username = req.params.username
//get the existing userdata
const existUsers = getUserData()
//filter the userdata to remove it
const filterUser = existUsers.filter( user => user.username !== username )
if ( existUsers.length === filterUser.length ) {
return res.status(409).send({error: true, msg: 'username does not exist'})
}
//save the filtered data
saveUserData(filterUser)
res.send({success: true, msg: 'User removed successfully'})
})
/* util functions */
//read the user data from json file
const saveUserData = (data) => {
const stringifyData = JSON.stringify(data)
fs.writeFileSync('db.json', stringifyData)
}
//get the user data from json file
const getUserData = () => {
const jsonData = fs.readFileSync('db.json')
return JSON.parse(jsonData)
}
/* util functions ends */
//configure the server port
app.listen(80, () => {
console.log('Server runs on port 80')
})