-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
141 lines (120 loc) · 3 KB
/
api.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
const express = require('express')
const app = express()
const user = require('./schema');
app.get('/details', async (req, res) => {
try {
const users = await user.find({});
console.log(users)
res.send({
status: 1,
message: 'details retrieved',
users: users,
})
} catch (error) {
console.err()
res.send({
status: 0,
message: 'details not retrieve',
error: err.message,
})
}
})
app.get('/details/:id', async (req, res) => {
try {
const userId = req.params.id;
const users = await user.findById(userId);
console.log(users)
if (!users) {
res.send({
status: 0,
message: 'user not found',
})
} else {
res.send({
status: 1,
message: 'Successfully retrieve user',
users: users,
})
}
} catch (error) {
console.error(error)
res.send({
status: 0,
message: 'Error retrieving detail',
error: err.message,
})
}
});
app.post('/create', async (req, res) => {
try {
const userData = user(req.body);
const newUser = await userData.save();
console.log(newUser);
res.send({
status: 1,
message: 'User created successfully',
user: newUser,
});
} catch (error) {
console.error(error);
res.send({
status: 0,
message: 'Error creating user',
error: error.message,
});
}
});
app.post('/update/:id', async (req,res)=>{
try{
const userId = req.params.id;
const updatedUsers = req.body;
console.log(updatedUsers)
const users = await user.findByIdAndUpdate(userId,updatedUsers, {new:true})
if (!users){
res.send({
status: 0,
message:'User not found',
});
}else{
res.send({
status:1,
message:'Updated sucessfully',
users:users,
})
}
}catch(err){
console.error(err)
res.send({
status:0,
message:'Something went wrong',
error:err.message,
})
}
})
app.delete('/delete/:id',async(req,res)=>{
try{
const userId = req.params.id;
const users = await user.findByIdAndRemove(userId)
console.log(users)
if(!users){
res.send({
status: 0,
message:'User not found',
})
}else{
res.send({
status: 0,
message : 'User deleted sucessfully',
users:users,
})
}
}catch(error){
console.error(error)
res.send({
status: 0,
message:'error on deleting user',
error : err.message,
})
}
});
module.exports = app;