blob: 3b5995343db81b6ece660ec3e803900d63a889b3 (
plain) (
blame)
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
78
79
80
81
|
package com.ufund.api.ufundapi.service;
import java.io.IOException;
import org.springframework.stereotype.Component;
import com.ufund.api.ufundapi.DuplicateKeyException;
import com.ufund.api.ufundapi.model.User;
import com.ufund.api.ufundapi.persistence.UserDAO;
@Component
public class UserService {
private final UserDAO userDAO;
private final CupboardService cupboardService;
public UserService(UserDAO userDao, CupboardService cupboardService) {
this.userDAO = userDao;
this.cupboardService = cupboardService;
}
/**
* Creates a new user
*
* @param username The username of the user
* @param password The password of the user
* @return The created user object
* @throws IOException Thrown on any problem saving the file
*/
public User createUser(String username, String password) throws IOException, DuplicateKeyException {
if (userDAO.getUser(username) != null) {
throw new DuplicateKeyException("A user with this name already exists");
}
User user = User.create(username, password);
return userDAO.addUser(user);
}
/**
* Gets a user with the given username
*
* @param username The username of the user
* @return The user object with that username
* @throws IOException If there was any problem saving the file
*/
public User getUser(String username) throws IOException {
User user = userDAO.getUser(username);
for (int needId : user.getBasketNeeds()) {
if (cupboardService.getNeed(needId) == null) {
user.removeBasketNeed(needId);
}
}
return userDAO.getUser(username);
}
/**
* Updates a user
*
* @param user The ID of the user to update
* @param username The user object to set (note: the ID is ignored)
* @return The updated user object
* @throws IOException Thrown if there was any issue saving the data
*/
public User updateUser(User user, String username) throws IOException {
if (!user.getUsername().equals(username)) {
throw new IllegalArgumentException("ID in URL and body must match");
}
return userDAO.updateUser(user);
}
/**
* Deletes a user
*
* @param username The username of the user to delete
* @return True if the user was deleted
* @throws IOException Thrown if there was any issue saving the data
*/
public boolean deleteUser(String username) throws IOException {
return userDAO.deleteUser(username);
}
}
|