-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentRepositoryImpl.java
70 lines (53 loc) · 2.45 KB
/
CommentRepositoryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.welflex.aws.dynamodb.repository;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.welflex.aws.dynamodb.model.Comment;
import java.util.List;
public class CommentRepositoryImpl {
private final DynamoDBMapper mapper;
public CommentRepositoryImpl(AmazonDynamoDB dynamoDB) {
this.mapper = new DynamoDBMapper(dynamoDB);
}
public Comment save(Comment comment) {
mapper.save(comment);
return comment;
}
public Comment get(String itemId, String messageId) {
Comment comment = new Comment();
comment.setItemId(itemId);
comment.setMessageId(messageId);
return mapper.load(comment);
}
public List<Comment> getAll() {
return mapper.scan(Comment.class, new DynamoDBScanExpression());
}
public List<Comment> getAllForItem(String itemId) {
Comment comment = new Comment();
comment.setItemId(itemId);
DynamoDBQueryExpression<Comment> expression
= new DynamoDBQueryExpression<Comment>().withHashKeyValues(comment);
return mapper.query(Comment.class, expression);
}
public List<Comment> getAllForItemWithRatingGE(String itemId, int minRating) {
Comment comment = new Comment();
comment.setItemId(itemId);
DynamoDBQueryExpression<Comment> expression
= new DynamoDBQueryExpression<Comment>().withHashKeyValues(comment)
.withRangeKeyCondition("rating", new Condition().withComparisonOperator(ComparisonOperator.GE)
.withAttributeValueList(new AttributeValue().withN(Integer.toString(minRating))));
return mapper.query(Comment.class, expression);
}
public List<Comment> getForUser(String userId) {
Comment comment = new Comment();
comment.setUserId(userId);
DynamoDBQueryExpression<Comment> expression
= new DynamoDBQueryExpression<Comment>().withHashKeyValues(comment)
.withConsistentRead(false);
return mapper.query(Comment.class, expression);
}
}