Skip to content

Instantly share code, notes, and snippets.

@bharat-tiwari
Created December 24, 2018 05:10
Show Gist options
  • Save bharat-tiwari/d3b191b53ae2de33be8d419c4eebbbd0 to your computer and use it in GitHub Desktop.
Save bharat-tiwari/d3b191b53ae2de33be8d419c4eebbbd0 to your computer and use it in GitHub Desktop.
NoteWordy API : endpoint to get word by id
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