Created
December 24, 2018 05:10
-
-
Save bharat-tiwari/d3b191b53ae2de33be8d419c4eebbbd0 to your computer and use it in GitHub Desktop.
NoteWordy API : endpoint to get word by id
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 params = { | |
TableName: APP_CONSTANTS.WORDS_TABLE, | |
Key: { | |
wordId: event.pathParameters.id, | |
userId: event.requestContext.identity.cognitoIdentityId | |
} | |
}; | |
try { | |
const data = await dynamoDb.get(params).promise(); | |
if (data.Item) { | |
const response = getResponse(200, data.Item); | |
callback(null, response); | |
} else { | |
throw new Error('Item not found'); | |
} | |
} catch (error) { | |
const errResponse = getResponse(500, error); | |
callback(errResponse, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment