@@ -7,69 +7,67 @@ const tableParams = {
7
7
TableName : process . env . DYNAMODB_TABLE
8
8
} ;
9
9
10
- const listItem = ( req , res ) => {
11
- dynamoDb . scan ( tableParams , ( error , result ) => {
12
- if ( error ) {
13
- console . error ( error ) ;
14
- res
15
- . statusCode ( error . statusCode || 501 )
16
- . send ( { error : "Couldn't fetch the todos." } ) ;
17
- } else {
18
- res . send ( result . Items ) ;
19
- }
20
- } ) ;
10
+ const listItem = async ( req , res ) => {
11
+ try {
12
+ let result = await dynamoDb . scan ( tableParams ) . promise ( ) ;
13
+ res . send ( result . Items ) ;
14
+ } catch ( error ) {
15
+ console . error ( error ) ;
16
+ res
17
+ . statusCode ( error . statusCode || 501 )
18
+ . send ( { error : "Couldn't fetch the todos." } ) ;
19
+ }
21
20
} ;
22
21
23
- const createItem = ( req , res ) => {
22
+ const createItem = async ( req , res ) => {
24
23
const timestamp = new Date ( ) . getTime ( ) ;
25
24
const { text } = req . body ;
26
25
27
26
if ( typeof text !== "string" ) {
28
27
res . status ( 400 ) . send ( { error : "Couldn't create todo item." } ) ;
29
28
}
30
29
31
- const Item = {
30
+ const item = {
32
31
id : uuid . v1 ( ) ,
33
32
text : text ,
34
33
checked : false ,
35
34
createAt : timestamp ,
36
35
updateAt : timestamp
37
36
} ;
38
37
39
- const params = Object . assign ( { } , tableParams , { Item : Item } ) ;
38
+ const params = Object . assign ( { } , tableParams , { Item : item } ) ;
40
39
41
- dynamoDb . put ( params , error => {
42
- if ( error ) {
43
- console . error ( error ) ;
44
- res . status ( 400 ) . send ( "Couldn't create todo item." ) ;
45
- } else {
46
- res . send ( params ) ;
47
- }
48
- } ) ;
40
+ try {
41
+ let result = await dynamoDb . put ( params ) . promise ( ) ;
42
+ res . send ( item ) ;
43
+ } catch ( error ) {
44
+ console . error ( error ) ;
45
+ res . status ( 400 ) . send ( "Couldn't create todo item." ) ;
46
+ }
49
47
} ;
50
48
51
- const getItem = ( req , res ) => {
49
+ const getItem = async ( req , res ) => {
52
50
const params = Object . assign ( { } , tableParams , {
53
51
Key : {
54
52
id : req . params . id
55
53
}
56
54
} ) ;
57
55
58
- dynamoDb . get ( params , ( error , result ) => {
59
- if ( error ) {
60
- console . error ( error ) ;
61
- res . status ( 400 ) . send ( { error : "Could not get todo item" } ) ;
62
- }
56
+ try {
57
+ let result = await dynamoDb . get ( params ) . promise ( ) ;
63
58
64
59
if ( result . Item ) {
65
60
res . json ( result . Item ) ;
66
61
} else {
67
62
res . status ( 400 ) . send ( { error : "Item not found" } ) ;
68
63
}
69
- } ) ;
64
+ } catch ( error ) {
65
+ console . error ( error ) ;
66
+ res . status ( 400 ) . send ( { error : "Could not get todo item" } ) ;
67
+ }
70
68
} ;
71
69
72
- const editItem = ( req , res ) => {
70
+ const editItem = async ( req , res ) => {
73
71
const timestamp = new Date ( ) . getTime ( ) ;
74
72
const { text, checked } = req . body ;
75
73
@@ -96,33 +94,31 @@ const editItem = (req, res) => {
96
94
ReturnValues : "ALL_NEW"
97
95
} ) ;
98
96
99
- dynamoDb . update ( params , ( error , result ) => {
100
- if ( error ) {
101
- console . error ( error ) ;
102
- res
103
- . status ( error . statusCoee || 501 )
104
- . send ( { error : "Could not update todo item" } ) ;
105
- } else {
106
- res . send ( result . Attributes ) ;
107
- }
108
- } ) ;
97
+ try {
98
+ let result = await dynamoDb . update ( params ) . promise ( ) ;
99
+ res . send ( result . Attributes ) ;
100
+ } catch ( error ) {
101
+ console . error ( error ) ;
102
+ res
103
+ . status ( error . statusCoee || 501 )
104
+ . send ( { error : "Could not update todo item" } ) ;
105
+ }
109
106
} ;
110
107
111
- const deleteItem = ( req , res ) => {
108
+ const deleteItem = async ( req , res ) => {
112
109
const params = Object . assign ( { } , tableParams , {
113
110
Key : {
114
111
id : req . params . id
115
112
}
116
113
} ) ;
117
114
118
- dynamoDb . delete ( params , error => {
119
- if ( error ) {
120
- console . error ( error ) ;
121
- res . status ( 501 ) . send ( { error : "Could not delete toto item" } ) ;
122
- } else {
123
- res . status ( 204 ) . send ( ) ;
124
- }
125
- } ) ;
115
+ try {
116
+ await dynamoDb . delete ( params ) . promise ( ) ;
117
+ res . status ( 204 ) . send ( ) ;
118
+ } catch ( error ) {
119
+ console . error ( error ) ;
120
+ res . status ( 501 ) . send ( { error : "Could not delete toto item" } ) ;
121
+ }
126
122
} ;
127
123
128
124
module . exports = { listItem, createItem, getItem, editItem, deleteItem } ;
0 commit comments