diff options
author | Hayden Hartman <haydenhartman10@gmail.com> | 2025-03-15 23:59:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-15 23:59:47 -0400 |
commit | 9baaa0590fbc38c06d530786a1de804ee9edd7db (patch) | |
tree | 7c94dc98f9b1978f8ccf3c38bb3777237bf0788a /ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java | |
parent | e4e6ae9a3d142fc78f31ee19464ec5e54bfb516f (diff) | |
parent | a3150b8a8e17c8a71f617745bb8588b397a75f47 (diff) | |
download | JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.tar.gz JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.tar.bz2 JellySolutions-9baaa0590fbc38c06d530786a1de804ee9edd7db.zip |
Merge pull request #8 from RIT-SWEN-261-02/api-auth
First attempt at an authentication system.
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java')
-rw-r--r-- | ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java new file mode 100644 index 0000000..935ee72 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -0,0 +1,72 @@ +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 + * + * @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); + } + +} |