Skip to content

Commit 3be0c73

Browse files
added CRUD on user
1 parent d43daeb commit 3be0c73

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

index.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ app.get('/api/users/:id', (request, response) => {
2525
user
2626
? response.json(user)
2727
: response.status(404).end()
28+
}).catch(error => {
29+
response.status(400).send({error:'malformated id'}).end()
2830
})
2931
})
3032

3133
app.delete('/api/users/:id', (request, response) => {
32-
const id = Number(request.params.id)
33-
const user = users.filter(user => user.id !== id)
34-
response.json(user)
35-
response.status(204).end()
34+
const id = request.params.id
35+
User.findByIdAndRemove(id)
36+
.then(user => {
37+
user
38+
? response.json(user)
39+
: response.status(404).end()
40+
}).catch(error =>{
41+
console.log(error);
42+
response.status(400).send({error:'malformated id'}).end()
43+
})
3644
})
3745

3846
app.post('/api/users/', (request, response) => {
@@ -44,17 +52,13 @@ app.post('/api/users/', (request, response) => {
4452
"lname": user.lname,
4553
"password": user.password
4654
})
47-
48-
if(!email || !fname || !lname || !password) {
49-
response.json({error:"some Field(s) is/are missing"})
50-
}
55+
5156
newUser.save()
5257
.then(result => {
53-
response.status(200)
54-
response.json(newUser)
58+
response.json(newUser).status(200).end()
5559
}).catch(error => {
56-
console.log("error occured while saving the user", error);
57-
json.response({error: error})
60+
response.json({error: error})
61+
response.status(500).end()
5862
})
5963
})
6064

requests/addUser.rest

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
# Add a single user all fields are important
1+
# Add a single user all fields are
22
POST http://localhost:3001/api/users/
33
Content-Type: application/json
44

55
{
6-
"email": "[email protected]",
7-
"fname": "Bruce",
8-
"lname": "Wayne",
9-
"password": "1345BruceTest"
6+
"email": "delete",
7+
"fname": "de",
8+
"lname": "delete",
9+
"password": "delete"
1010
}

requests/deleteUser.rest

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DELETE http://localhost:3001/api/users/23452

requests/getUsers.rest

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ GET http://localhost:3001/api/users/
44
###
55

66
# Get single user
7-
GET http://localhost:3001/api/users/6072f0fa3c6c6a23c84764d5
7+
GET http://localhost:3001/api/users/234

0 commit comments

Comments
 (0)