-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify.ts
61 lines (49 loc) · 1.39 KB
/
modify.ts
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
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda'
import ServiceError from './lib/ServiceError'
import { handleErrorResponse } from './lib/error'
import Task from './model/Task'
const tableName = process.env.DYNAMODB_TABLE
export const handler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
try {
if (!tableName) {
throw new ServiceError(
'DYNAMODB_TABLE environment variable not defined',
500
)
}
if (!event.pathParameters || !event.pathParameters.id) {
throw new ServiceError(
'Bad Request: No ID provided in path parameters',
400
)
}
const id = event.pathParameters.id
if (!event.body) {
throw new ServiceError('Bad Request: No body provided', 400)
}
const task = new Task(tableName)
const result = await task.fetch(id)
if (!result) {
throw new ServiceError('Task not found', 404)
}
const { text, done } = JSON.parse(
Buffer.from(event.body, 'base64').toString()
)
await task.modify(id, text, done)
const updatedTask = await task.fetch(id)
return {
statusCode: 200,
body: JSON.stringify({
message: 'Task updated successfully',
updatedTask,
}),
headers: {
'Content-Type': 'application/json',
},
}
} catch (error) {
return handleErrorResponse(error)
}
}