Skip to content

Instantly share code, notes, and snippets.

@RyPope
Created February 5, 2019 04:46
Show Gist options
  • Save RyPope/9ac6a66beedd46e5235f81ee41f61f6c to your computer and use it in GitHub Desktop.
Save RyPope/9ac6a66beedd46e5235f81ee41f61f6c to your computer and use it in GitHub Desktop.
public class GetFeedHandler implements RequestHandler<GetFeedRequest, GetFeedResponse> {
private final AmazonDynamoDB mClient = AmazonDynamoDBClientBuilder
.standard()
.build();
private final DynamoDB dynamoDB = new DynamoDB(mClient);
private Context mContext;
public GetFeedResponse handleRequest(final GetFeedRequest input, final Context context) {
mContext = context;
final List<String> activityIds = getActivitiesForUser(input.getUserId());
return new GetFeedResponse(activityIds);
}
private List<String> getActivitiesForUser(final String userId) {
final List<String> activityIds = new ArrayList<>();
mContext.getLogger().log(String.format("Querying activities for user %s", userId));
final Table table = dynamoDB.getTable(TableConstants.FEED_TABLE_NAME);
final Index index = table.getIndex(TableConstants.FEED_USERID_CREATED_AT_INDEX);
final QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("#U = :userId")
.withValueMap(new ValueMap()
.withString(":userId", userId))
.withNameMap(new NameMap()
.with("#U", TableConstants.FEED_USERID_COLUMN))
.withScanIndexForward(false);
final ItemCollection<QueryOutcome> items = index.query(spec);
for (final Item activity : items) {
final String activityId = (String) activity.get(TableConstants.FEED_ID_COLUMN);
mContext.getLogger().log(String.format("Found activity %s", activityId));
activityIds.add(activityId);
}
return activityIds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment