package com.ufund.api.ufundapi.service; import java.io.IOException; import com.ufund.api.ufundapi.DuplicateKeyException; import com.ufund.api.ufundapi.model.User; import com.ufund.api.ufundapi.persistence.UserDAO; import org.springframework.stereotype.Component; @Component public class UserService { private final UserDAO userDAO; public UserService(UserDAO userDao) { this.userDAO = userDao; } /** * 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 { return userDAO.getUser(username); } /** * Updates a user * // TODO * @param user * @param name * @return * @throws IOException Thrown if there was any issue saving the data */ public User updateUser(User user, String name) throws IOException { return userDAO.updateUser(user, name); } /** * 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); } }