Skip to content

Commit

Permalink
Implement basic query from lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansimsmith committed Aug 12, 2024
1 parent a98806d commit f91113a
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import java.net.URI;
import javax.annotation.Nullable;
import javax.inject.Named;
import javax.inject.Singleton;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Module
public class DynamoDbModule {

@Provides
@Singleton
public DynamoDbClient dynamoDbClient(@Named("dynamoDbEndpoint") @Nullable URI dynamoDbEndpoint) {
var builder = DynamoDbClient.builder();
if (dynamoDbEndpoint != null) {
Expand All @@ -22,7 +26,16 @@ public DynamoDbClient dynamoDbClient(@Named("dynamoDbEndpoint") @Nullable URI dy
}

@Provides
@Singleton
public DynamoDbEnhancedClient dynamoDbEnhancedClient(DynamoDbClient dynamoDbClient) {
return DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDbClient).build();
}

@Provides
@Singleton
public DynamoDbTable<ImmersionTrackerItem> immersionTrackerTable(
DynamoDbEnhancedClient dynamoDbEnhancedClient) {
var schema = TableSchema.fromBean(ImmersionTrackerItem.class);
return dynamoDbEnhancedClient.table("immersion_tracker", schema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.google.common.annotations.VisibleForTesting;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import java.util.List;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional;
import software.amazon.awssdk.enhanced.dynamodb.model.QueryEnhancedRequest;

public class GetProgressHandler implements RequestHandler<Object, String> {
private final DynamoDbClient dynamoDbClient;
private final DynamoDbEnhancedClient dynamoDbEnhancedClient;
public class GetProgressHandler implements RequestHandler<Object, List<ImmersionTrackerItem>> {
private final DynamoDbTable<ImmersionTrackerItem> immersionTrackerTable;

public GetProgressHandler() {
this(ImmersionTrackerFactory.create());
}

@VisibleForTesting
GetProgressHandler(ImmersionTrackerFactory factory) {
this.dynamoDbEnhancedClient = factory.dynamoDbEnhancedClient();
this.dynamoDbClient = factory.dynamoDbClient();
this.immersionTrackerTable = factory.immersionTrackerTable();
}

@Override
public String handleRequest(Object s, Context context) {
public List<ImmersionTrackerItem> handleRequest(Object s, Context context) {

var schema = TableSchema.fromBean(ImmersionTrackerRecord.class);
var table = dynamoDbEnhancedClient.table("immersion_tracker", schema);
// TODO: auth
var user = "jordansimsmith";

return table.scan().items().stream().toList().toString();
var query =
immersionTrackerTable.query(
QueryEnhancedRequest.builder()
.queryConditional(
QueryConditional.keyEqualTo(
b -> b.partitionValue(ImmersionTrackerItem.formatPk(user))))
.build());

return query.items().stream().toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import java.net.URI;
import javax.annotation.Nullable;
import javax.inject.Named;
import javax.inject.Singleton;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

@Singleton
@Component(modules = {DynamoDbModule.class})
public interface ImmersionTrackerFactory {

DynamoDbClient dynamoDbClient();

DynamoDbEnhancedClient dynamoDbEnhancedClient();

DynamoDbTable<ImmersionTrackerItem> immersionTrackerTable();

@Component.Builder
interface Builder {
@BindsInstance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package com.jordansimsmith.immersiontracker;

import java.util.Objects;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbAttribute;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey;
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey;

@DynamoDbBean
public class ImmersionTrackerItem {
private String pk;
private String sk;
private String user;
private String folderName;
private String fileName;
private Integer timestamp;
private Integer tvdbId;
private String tvdbName;
private String tvdbImage;

@DynamoDbPartitionKey
@DynamoDbAttribute("pk")
public String getPk() {
return pk;
}

public void setPk(String pk) {
this.pk = pk;
}

@DynamoDbSortKey
@DynamoDbAttribute("sk")
public String getSk() {
return sk;
}

public void setSk(String sk) {
this.sk = sk;
}

@DynamoDbAttribute("user")
public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

@DynamoDbAttribute("folder_name")
public String getFolderName() {
return folderName;
}

public void setFolderName(String folderName) {
this.folderName = folderName;
}

@DynamoDbAttribute("file_name")
public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

@DynamoDbAttribute("timestamp")
public Integer getTimestamp() {
return timestamp;
}

public void setTimestamp(Integer timestamp) {
this.timestamp = timestamp;
}

@DynamoDbAttribute("tvdb_id")
public Integer getTvdbId() {
return tvdbId;
}

public void setTvdbId(Integer tvdbId) {
this.tvdbId = tvdbId;
}

@DynamoDbAttribute("tvdb_name")
public String getTvdbName() {
return tvdbName;
}

public void setTvdbName(String tvdbName) {
this.tvdbName = tvdbName;
}

@DynamoDbAttribute("tvdb_image")
public String getTvdbImage() {
return tvdbImage;
}

public void setTvdbImage(String tvdbImage) {
this.tvdbImage = tvdbImage;
}

@Override
public String toString() {
return "ImmersionTrackerItem{"
+ "pk='"
+ pk
+ '\''
+ ", sk='"
+ sk
+ '\''
+ ", user='"
+ user
+ '\''
+ ", folderName='"
+ folderName
+ '\''
+ ", fileName='"
+ fileName
+ '\''
+ ", timestamp="
+ timestamp
+ ", tvdbId="
+ tvdbId
+ ", tvdbName="
+ tvdbName
+ ", tvdbImage='"
+ tvdbImage
+ '\''
+ '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ImmersionTrackerItem that = (ImmersionTrackerItem) o;
return Objects.equals(pk, that.pk)
&& Objects.equals(sk, that.sk)
&& Objects.equals(user, that.user)
&& Objects.equals(folderName, that.folderName)
&& Objects.equals(fileName, that.fileName)
&& Objects.equals(timestamp, that.timestamp)
&& Objects.equals(tvdbId, that.tvdbId)
&& Objects.equals(tvdbName, that.tvdbName)
&& Objects.equals(tvdbImage, that.tvdbImage);
}

@Override
public int hashCode() {
return Objects.hash(pk, sk, user, folderName, fileName, timestamp, tvdbId, tvdbName, tvdbImage);
}

public static String formatPk(String user) {
return "USER#" + user;
}

public static String formatEpisodeSk(String folderName, String fileName) {
return "EPISODE#" + folderName + "#" + fileName;
}

public static String formatShowSk(String folderName) {
return "SHOW#" + folderName;
}

public static ImmersionTrackerItem createEpisode(
String user, String folderName, String fileName, int timestamp) {
var episode = new ImmersionTrackerItem();
episode.setPk(formatPk(user));
episode.setSk(formatEpisodeSk(folderName, fileName));
episode.setFolderName(folderName);
episode.setFileName(fileName);
episode.setTimestamp(timestamp);
episode.setUser(user);
return episode;
}

public static ImmersionTrackerItem createShow(String user, String folderName) {
var show = new ImmersionTrackerItem();
show.setPk(formatPk(user));
show.setSk(formatShowSk(folderName));
show.setUser(user);
return show;
}
}

This file was deleted.

Loading

0 comments on commit f91113a

Please sign in to comment.