-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
168 lines (145 loc) · 3.27 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const uuid = require('uuid');
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Hello from Dhruv!',
},
null,
2
),
};
};
module.exports.createTodo = async (event) => {
const { text = "" } = JSON.parse(event.body);
const id = uuid.v1();
const timestamp = new Date().getTime();
const params = {
TableName: process.env.TODO_TABLE,
Item: {
id: id,
text: text,
checked: false,
createdAt: timestamp,
updatedAt: timestamp,
},
};
const data = await dynamoDb.put(params).promise();
return {
statusCode: 200,
body: JSON.stringify(
params.Item,
null,
2
),
};
};
module.exports.getAllTodos = async (event) => {
const params = {
TableName: process.env.TODO_TABLE,
};
const data = await dynamoDb.scan(params).promise();
return {
statusCode: 200,
body: JSON.stringify(
{
todos: data.Items,
},
null,
2
),
};
};
module.exports.deleteTodo = async (event) => {
const params = {
TableName: process.env.TODO_TABLE,
Key: {
id: event.pathParameters.id,
}
};
const data = await dynamoDb.delete(params).promise();
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Todo deleted successfully',
},
null,
2
),
};
};
module.exports.getTodoById = async (event) => {
const params = {
TableName: process.env.TODO_TABLE,
Key: {
id: event.pathParameters.id,
}
};
const todo = await dynamoDb.get(params).promise();
return {
statusCode: 200,
body: JSON.stringify(
todo.Item,
null,
2
),
};
};
module.exports.updateTodo = (event, context, callback) => {
const timestamp = new Date().getTime();
const data = JSON.parse(event.body);
// validation
if (typeof data.checked !== 'boolean') {
console.error('Validation Failed');
callback(null, {
statusCode: 400,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
'message': 'Couldn\'t update the todo item.',
}, null, 2),
});
return;
}
const params = {
TableName: process.env.TODO_TABLE,
Key: {
id: event.pathParameters.id,
},
ExpressionAttributeNames: {
'#todo_text': 'text',
},
ExpressionAttributeValues: {
':text': data.text,
':checked': data.checked,
':updatedAt': timestamp,
},
UpdateExpression: 'SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt',
ReturnValues: 'ALL_NEW',
};
// update the todo in the database
dynamoDb.update(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
'message': 'Couldn\'t fetch the todo item.',
}, null, 2),
});
return;
}
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(result.Attributes, null, 2),
};
callback(null, response);
});
};