-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathUserModel.java
More file actions
45 lines (38 loc) · 1.28 KB
/
UserModel.java
File metadata and controls
45 lines (38 loc) · 1.28 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
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.DynamoDBScanExpression;
import java.util.List;
public class UserModel {
/** AWS SDK credentials. */
private AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.build();
private DynamoDBMapper mapper = new DynamoDBMapper(client);
public void saveUser(User user) {
try {
mapper.save(user);
} catch (Exception e) {
throw e;
}
}
public User loadUser(String userId) throws UserNotFoundException {
User user = mapper.load(User.class, userId);
if ( user == null ) {
throw new UserNotFoundException(userId);
}
return user;
}
public List<User> loadUsers(){
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
List<User> scanResult = mapper.scan(User.class, scanExpression);
return scanResult;
}
public void deleteUser(String userId) throws UserNotFoundException {
User user = mapper.load(User.class, userId);
if ( user == null ) {
throw new UserNotFoundException(userId);
}
mapper.delete(user);
}
}