UNPKG

serverless-sam

Version:

Serverless framework plugin to export AWS SAM templates for a service

51 lines (45 loc) 1.18 kB
'use strict'; const uuid = require('uuid'); const dynamodb = require('./dynamodb'); module.exports.create = (event, context, callback) => { const timestamp = new Date().getTime(); const data = JSON.parse(event.body); if (typeof data.text !== 'string') { console.error('Validation Failed'); callback(null, { statusCode: 400, headers: { 'Content-Type': 'text/plain' }, body: 'Couldn\'t create the todo item.', }); return; } const params = { TableName: process.env.DYNAMODB_TABLE, Item: { id: uuid.v1(), text: data.text, checked: false, createdAt: timestamp, updatedAt: timestamp, }, }; // write the todo to the database dynamodb.put(params, (error) => { // handle potential errors if (error) { console.error(error); callback(null, { statusCode: error.statusCode || 501, headers: { 'Content-Type': 'text/plain' }, body: 'Couldn\'t create the todo item.', }); return; } // create a response const response = { statusCode: 200, body: JSON.stringify(params.Item), }; callback(null, response); }); };