-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathMoveModel.java
More file actions
73 lines (63 loc) · 2.52 KB
/
MoveModel.java
File metadata and controls
73 lines (63 loc) · 2.52 KB
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
71
72
73
package scorekeep;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MoveModel {
/** AWS SDK credentials. */
private AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.build();
private DynamoDBMapper mapper = new DynamoDBMapper(client);
private final SessionModel sessionModel = new SessionModel();
private final GameModel gameModel = new GameModel();
public void saveMove(Move move) throws SessionNotFoundException, GameNotFoundException {
// check session
String sessionId = move.getSession();
String gameId = move.getGame();
if (sessionModel.loadSession(sessionId) == null ) {
throw new SessionNotFoundException(sessionId);
}
if (gameModel.loadGame(gameId) == null ) {
throw new GameNotFoundException(gameId);
}
mapper.save(move);
}
public Move loadMove(String moveId) throws MoveNotFoundException {
Move move = mapper.load(Move.class, moveId);
if ( move == null ) {
throw new MoveNotFoundException(moveId);
}
return move;
}
public List<Move> loadMoves(String sessionId, String gameId) throws SessionNotFoundException, GameNotFoundException {
if ( sessionModel.loadSession(sessionId) == null ) {
throw new SessionNotFoundException(sessionId);
}
if ( gameModel.loadGame(gameId) == null ) {
throw new GameNotFoundException(gameId);
}
Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
eav.put(":val1", new AttributeValue().withS(gameId));
Map<String, String> ean = new HashMap<String, String>();
ean.put("#key1", "game");
DynamoDBQueryExpression<Move> queryExpression = new DynamoDBQueryExpression<Move>()
.withIndexName("game-index")
.withExpressionAttributeValues(eav)
.withExpressionAttributeNames(ean)
.withKeyConditionExpression("#key1 = :val1")
.withConsistentRead(false);
List<Move> gameMoves = mapper.query(Move.class, queryExpression);
return gameMoves;
}
public void deleteMove(String moveId) throws MoveNotFoundException {
Move move = mapper.load(Move.class, moveId);
if ( move == null ) {
throw new MoveNotFoundException(moveId);
}
mapper.delete(move);
}
}