@@ -25,14 +25,22 @@ app.get('/api/users/:id', (request, response) => {
25
25
user
26
26
? response . json ( user )
27
27
: response . status ( 404 ) . end ( )
28
+ } ) . catch ( error => {
29
+ response . status ( 400 ) . send ( { error :'malformated id' } ) . end ( )
28
30
} )
29
31
} )
30
32
31
33
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
+ } )
36
44
} )
37
45
38
46
app . post ( '/api/users/' , ( request , response ) => {
@@ -44,17 +52,13 @@ app.post('/api/users/', (request, response) => {
44
52
"lname" : user . lname ,
45
53
"password" : user . password
46
54
} )
47
-
48
- if ( ! email || ! fname || ! lname || ! password ) {
49
- response . json ( { error :"some Field(s) is/are missing" } )
50
- }
55
+
51
56
newUser . save ( )
52
57
. then ( result => {
53
- response . status ( 200 )
54
- response . json ( newUser )
58
+ response . json ( newUser ) . status ( 200 ) . end ( )
55
59
} ) . 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 ( )
58
62
} )
59
63
} )
60
64
0 commit comments