-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathStateModel.java
More file actions
77 lines (67 loc) · 2.76 KB
/
StateModel.java
File metadata and controls
77 lines (67 loc) · 2.76 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
74
75
76
77
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;
/** Saves state objects to DynamoDB
Loads state objects from DynamoDB
Loads all state objects for a game
**/
public class StateModel {
/** 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 saveState(State state) throws SessionNotFoundException, GameNotFoundException {
// check session
String sessionId = state.getSession();
String gameId = state.getGame();
if (sessionModel.loadSession(sessionId) == null ) {
throw new SessionNotFoundException(sessionId);
}
if (gameModel.loadGame(gameId) == null ) {
throw new GameNotFoundException(gameId);
}
mapper.save(state);
}
public State loadState(String stateId) throws StateNotFoundException {
State state = mapper.load(State.class, stateId);
if ( state == null ) {
throw new StateNotFoundException(stateId);
}
return state;
}
public List<State> loadStates(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> attributeValues = new HashMap<String, AttributeValue>();
attributeValues.put(":val1", new AttributeValue().withS(gameId));
Map<String, String> attributeNames = new HashMap<String, String>();
attributeNames.put("#key1", "game");
DynamoDBQueryExpression<State> queryExpression = new DynamoDBQueryExpression<State>()
.withIndexName("game-index")
.withExpressionAttributeValues(attributeValues)
.withExpressionAttributeNames(attributeNames)
.withKeyConditionExpression("#key1 = :val1")
.withConsistentRead(false);
List<State> gameStates = mapper.query(State.class, queryExpression);
return gameStates;
}
public void deleteState(String stateId) throws StateNotFoundException {
State state = mapper.load(State.class, stateId);
if ( state == null ) {
throw new StateNotFoundException(stateId);
}
mapper.delete(state);
}
}