Repository navigation
Expand file tree
/
Copy pathUserServiceMapImpl.java
More file actions
68 lines (54 loc) · 1.64 KB
/
Copy pathUserServiceMapImpl.java
File metadata and controls
68 lines (54 loc) · 1.64 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
package com.baeldung.sparkjava;
import java.util.Collection;
import java.util.HashMap;
public class UserServiceMapImpl implements UserService {
private HashMap<String, User> userMap;
public UserServiceMapImpl() {
userMap = new HashMap<>();
}
@Override
public void addUser(User user) {
userMap.put(user.getId(), user);
}
@Override
public Collection<User> getUsers() {
return userMap.values();
}
@Override
public User getUser(String id) {
return userMap.get(id);
}
@Override
public User editUser(User forEdit) throws UserException {
try {
if (forEdit.getId() == null)
throw new UserException("ID cannot be blank");
User toEdit = userMap.get(forEdit.getId());
if (toEdit == null)
throw new UserException("User not found");
if (forEdit.getEmail() != null) {
toEdit.setEmail(forEdit.getEmail());
}
if (forEdit.getFirstName() != null) {
toEdit.setFirstName(forEdit.getFirstName());
}
if (forEdit.getLastName() != null) {
toEdit.setLastName(forEdit.getLastName());
}
if (forEdit.getId() != null) {
toEdit.setId(forEdit.getId());
}
return toEdit;
} catch (Exception ex) {
throw new UserException(ex.getMessage());
}
}
@Override
public void deleteUser(String id) {
userMap.remove(id);
}
@Override
public boolean userExist(String id) {
return userMap.containsKey(id);
}
}