@@ -6,90 +6,85 @@ const Note = require('./models/Note');
6
6
7
7
module . exports . create = ( event , context , callback ) => {
8
8
context . callbackWaitsForEmptyEventLoop = false ;
9
-
10
- connectToDatabase ( )
11
- . then ( ( ) => {
9
+ return connectToDatabase ( )
10
+ . then ( ( ) =>
12
11
Note . create ( JSON . parse ( event . body ) )
13
- . then ( note => callback ( null , {
14
- statusCode : 200 ,
15
- body : JSON . stringify ( note )
16
- } ) )
17
- . catch ( err => callback ( null , {
18
- statusCode : err . statusCode || 500 ,
19
- headers : { 'Content-Type' : 'text/plain' } ,
20
- body : 'Could not create the note.'
21
- } ) ) ;
22
- } ) ;
23
- } ;
12
+ )
13
+ . then ( note => callback ( null , {
14
+ statusCode : 200 ,
15
+ body : JSON . stringify ( note )
16
+ } ) )
17
+ . catch ( err => callback ( null , {
18
+ statusCode : err . statusCode || 500 ,
19
+ headers : { 'Content-Type' : 'text/plain' } ,
20
+ body : 'Could not create the note.'
21
+ } ) ) ;
22
+ }
24
23
25
24
module . exports . getOne = ( event , context , callback ) => {
26
25
context . callbackWaitsForEmptyEventLoop = false ;
27
-
28
- connectToDatabase ( )
29
- . then ( ( ) => {
26
+ return connectToDatabase ( )
27
+ . then ( ( ) =>
30
28
Note . findById ( event . pathParameters . id )
31
- . then ( note => callback ( null , {
32
- statusCode : 200 ,
33
- body : JSON . stringify ( note )
34
- } ) )
35
- . catch ( err => callback ( null , {
36
- statusCode : err . statusCode || 500 ,
37
- headers : { 'Content-Type' : 'text/plain' } ,
38
- body : 'Could not fetch the note.'
39
- } ) ) ;
40
- } ) ;
29
+ )
30
+ . then ( note => callback ( null , {
31
+ statusCode : 200 ,
32
+ body : JSON . stringify ( note )
33
+ } ) )
34
+ . catch ( err => callback ( null , {
35
+ statusCode : err . statusCode || 500 ,
36
+ headers : { 'Content-Type' : 'text/plain' } ,
37
+ body : 'Could not fetch the note.'
38
+ } ) ) ;
41
39
} ;
42
40
43
41
module . exports . getAll = ( event , context , callback ) => {
44
42
context . callbackWaitsForEmptyEventLoop = false ;
45
-
46
- connectToDatabase ( )
47
- . then ( ( ) => {
43
+ return connectToDatabase ( )
44
+ . then ( ( ) =>
48
45
Note . find ( )
49
- . then ( notes => callback ( null , {
50
- statusCode : 200 ,
51
- body : JSON . stringify ( notes )
52
- } ) )
53
- . catch ( err => callback ( null , {
54
- statusCode : err . statusCode || 500 ,
55
- headers : { 'Content-Type' : 'text/plain' } ,
56
- body : 'Could not fetch the notes.'
57
- } ) )
58
- } ) ;
46
+ )
47
+ . then ( notes => callback ( null , {
48
+ statusCode : 200 ,
49
+ body : JSON . stringify ( notes )
50
+ } ) )
51
+ . catch ( err => callback ( null , {
52
+ statusCode : err . statusCode || 500 ,
53
+ headers : { 'Content-Type' : 'text/plain' } ,
54
+ body : 'Could not fetch the notes.'
55
+ } ) )
59
56
} ;
60
57
61
58
module . exports . update = ( event , context , callback ) => {
62
59
context . callbackWaitsForEmptyEventLoop = false ;
63
-
64
- connectToDatabase ( )
65
- . then ( ( ) => {
60
+ return connectToDatabase ( )
61
+ . then ( ( ) =>
66
62
Note . findByIdAndUpdate ( event . pathParameters . id , JSON . parse ( event . body ) , { new : true } )
67
- . then ( note => callback ( null , {
68
- statusCode : 200 ,
69
- body : JSON . stringify ( note )
70
- } ) )
71
- . catch ( err => callback ( null , {
72
- statusCode : err . statusCode || 500 ,
73
- headers : { 'Content-Type' : 'text/plain' } ,
74
- body : 'Could not fetch the notes.'
75
- } ) ) ;
76
- } ) ;
63
+ )
64
+ . then ( note => callback ( null , {
65
+ statusCode : 200 ,
66
+ body : JSON . stringify ( note )
67
+ } ) )
68
+ . catch ( err => callback ( null , {
69
+ statusCode : err . statusCode || 500 ,
70
+ headers : { 'Content-Type' : 'text/plain' } ,
71
+ body : 'Could not fetch the notes.'
72
+ } ) ) ;
77
73
} ;
78
74
79
75
module . exports . delete = ( event , context , callback ) => {
80
76
context . callbackWaitsForEmptyEventLoop = false ;
81
-
82
- connectToDatabase ( )
83
- . then ( ( ) => {
77
+ return connectToDatabase ( )
78
+ . then ( ( ) =>
84
79
Note . findByIdAndRemove ( event . pathParameters . id )
85
- . then ( note => callback ( null , {
86
- statusCode : 200 ,
87
- body : JSON . stringify ( { message : 'Removed note with id: ' + note . _id , note : note } )
88
- } ) )
89
- . catch ( err => callback ( null , {
90
- statusCode : err . statusCode || 500 ,
91
- headers : { 'Content-Type' : 'text/plain' } ,
92
- body : 'Could not fetch the notes.'
93
- } ) ) ;
94
- } ) ;
80
+ )
81
+ . then ( note => callback ( null , {
82
+ statusCode : 200 ,
83
+ body : JSON . stringify ( { message : 'Removed note with id: ' + note . _id , note : note } )
84
+ } ) )
85
+ . catch ( err => callback ( null , {
86
+ statusCode : err . statusCode || 500 ,
87
+ headers : { 'Content-Type' : 'text/plain' } ,
88
+ body : 'Could not fetch the notes.'
89
+ } ) ) ;
95
90
} ;
0 commit comments