Last active
December 24, 2018 15:09
-
-
Save bharat-tiwari/16c72b57a26a6c18939d1b1dbf546ff1 to your computer and use it in GitHub Desktop.
noteWordy API - Lambda function to update a word
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 { APP_CONSTANTS } from '../../config/app_constants'; | |
import getResponse from '../../utils/responseUtils'; | |
import * as AWS from "aws-sdk"; | |
const dynamoDb = new AWS.DynamoDB.DocumentClient({ | |
'region': 'us-east-1' | |
}); | |
export async function main(event , context, callback){ | |
const requestData = JSON.parse(event.body); | |
const params = { | |
TableName: APP_CONSTANTS.WORDS_TABLE, | |
Key: { | |
wordId: event.pathParameters.id, | |
userId: event.requestContext.identity.cognitoIdentityId | |
}, | |
UpdateExpression: 'set word = :word, meaning = :meaning, example = :example, comments = :comments', | |
ExpressionAttributeValues: { | |
':word': requestData.word || null, | |
':meaning': requestData.meaning || null, | |
':example': requestData.example || null, | |
':comments': requestData.comments || null | |
} | |
}; | |
try { | |
const result = await dynamoDb.update(params).promise(); | |
const response = getResponse(200, {result} ); | |
callback(null, response); | |
} catch (error) { | |
const errResponse = getResponse(500, {status: false}); | |
callback(errResponse, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment