Last active
May 22, 2020 18:17
-
-
Save robertofrontado/7df8d1e4504661612b936818f94ca714 to your computer and use it in GitHub Desktop.
serverless-todo-createtodo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as AWS from 'aws-sdk' | |
import 'source-map-support/register' | |
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda' | |
import * as uuid from 'uuid' | |
import { TodoItem } from '../../models/TodoItem' | |
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => { | |
const { name } = JSON.parse(event.body); | |
const id = uuid.v4(); | |
const todo: TodoItem = { id, done: false, createdAt: new Date().toISOString(), name }; | |
const docClient = new AWS.DynamoDB.DocumentClient(); | |
await docClient.put({ | |
TableName: process.env.TODOS_TABLE, | |
Item: todo | |
}).promise(); | |
return { | |
statusCode: 201, | |
body: JSON.stringify({ | |
item: todo | |
}) | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment