File tree Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Expand file tree Collapse file tree 1 file changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -77,7 +77,42 @@ router
77
77
} ) ;
78
78
} )
79
79
. put ( function update ( req , res ) {
80
- res . send ( "update" ) ;
80
+ const timestamp = new Date ( ) . getTime ( ) ;
81
+ const { text, checked } = req . body ;
82
+
83
+ // validation
84
+ if ( typeof text !== "string" || typeof checked !== "boolean" ) {
85
+ console . error ( "Validation failed" ) ;
86
+ res . status ( 400 ) . send ( { error : "Validation failed" } ) ;
87
+ }
88
+
89
+ const params = Object . assign ( { } , tableParams , {
90
+ Key : {
91
+ id : req . params . id
92
+ } ,
93
+ ExpressionAttributeNames : {
94
+ "#todo_text" : "text"
95
+ } ,
96
+ ExpressionAttributeValues : {
97
+ ":text" : text ,
98
+ ":checked" : checked ,
99
+ ":updatedAt" : timestamp
100
+ } ,
101
+ UpdateExpression :
102
+ "SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt" ,
103
+ ReturnValues : "ALL_NEW"
104
+ } ) ;
105
+
106
+ dynamoDb . update ( params , ( error , result ) => {
107
+ if ( error ) {
108
+ console . error ( error ) ;
109
+ res
110
+ . status ( error . statusCoee || 501 )
111
+ . send ( { error : "Could not update todo item" } ) ;
112
+ } else {
113
+ res . send ( result . Attributes ) ;
114
+ }
115
+ } ) ;
81
116
} )
82
117
. delete ( ( req , res ) => {
83
118
res . end ( "delete" ) ;
You can’t perform that action at this time.
0 commit comments