1
+ 'use strict' ;
2
+
3
+ const mongoose = require ( 'mongoose' ) ;
4
+ mongoose . connect ( 'mongodb://MONGODB_URI:27017/db' , { useMongoClient : true } ) ;
5
+ mongoose . Promise = global . Promise ;
6
+ const Note = mongoose . model ( 'Note' , { content : String } ) ;
7
+
8
+ module . exports . create = ( event , context , callback ) => {
9
+ const data = JSON . parse ( event . body ) ;
10
+ const params = {
11
+ content : data . content
12
+ } ;
13
+
14
+ Note . create ( params )
15
+ . then ( note => callback ( null , {
16
+ statusCode : 200 ,
17
+ body : JSON . stringify ( note )
18
+ } ) )
19
+ . catch ( err => callback ( null , {
20
+ statusCode : err . statusCode || 500 ,
21
+ headers : { 'Content-Type' : 'text/plain' } ,
22
+ body : 'Could not create the note.'
23
+ } ) ) ;
24
+ }
25
+
26
+ module . exports . getOne = ( event , context , callback ) => {
27
+ const id = event . pathParameters . id ;
28
+
29
+ Note . findById ( id )
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
+ } ) ) ;
39
+ } ;
40
+
41
+ module . exports . getAll = ( event , context , callback ) => {
42
+ Note . find ( )
43
+ . then ( notes => callback ( null , {
44
+ statusCode : 200 ,
45
+ body : JSON . stringify ( notes )
46
+ } ) )
47
+ . catch ( err => callback ( null , {
48
+ statusCode : err . statusCode || 500 ,
49
+ headers : { 'Content-Type' : 'text/plain' } ,
50
+ body : 'Could not fetch the notes.'
51
+ } ) ) ;
52
+ } ;
53
+
54
+ module . exports . update = ( event , context , callback ) => {
55
+ const data = JSON . parse ( event . body ) ;
56
+ Note . findByIdAndUpdate ( event . pathParameters . id , data , { new : true } )
57
+ . then ( note => callback ( null , {
58
+ statusCode : 200 ,
59
+ body : JSON . stringify ( note )
60
+ } ) )
61
+ . catch ( err => callback ( null , {
62
+ statusCode : err . statusCode || 500 ,
63
+ headers : { 'Content-Type' : 'text/plain' } ,
64
+ body : 'Could not fetch the notes.'
65
+ } ) ) ;
66
+ } ;
67
+
68
+ module . exports . delete = ( event , context , callback ) => {
69
+ Note . findByIdAndRemove ( event . pathParameters . id )
70
+ . then ( note => callback ( null , {
71
+ statusCode : 200 ,
72
+ body : JSON . stringify ( { message : 'Removed note with id: ' + note . _id , note : note } )
73
+ } ) )
74
+ . catch ( err => callback ( null , {
75
+ statusCode : err . statusCode || 500 ,
76
+ headers : { 'Content-Type' : 'text/plain' } ,
77
+ body : 'Could not fetch the notes.'
78
+ } ) ) ;
79
+ } ;
0 commit comments