From a3fbcd713ae9a6b3f38dcc42a5c4c2f369a5d6f5 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 6 Mar 2025 22:53:36 -0500 Subject: more javadocs and cleanup --- .../ufund/api/ufundapi/service/UserService.java | 47 +++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java') 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 index 6af3897..776d09a 100644 --- 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 @@ -2,6 +2,7 @@ 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; @@ -11,29 +12,57 @@ public class UserService { private final UserDAO userDAO; - /** - * Create a user controller to receive REST signals - * - * @param userDao The Data Access Object - */ public UserService(UserDAO userDao) { this.userDAO = userDao; } - public User createUser(String username, String password) throws IOException { + /** + * 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); } - public User getUser(String username) throws IOException, IllegalAccessException { + /** + * 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); } - public User updateUser(User user, String name) throws IllegalAccessException, IOException { + /** + * 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); } - public Boolean deleteUser(String username) throws IllegalAccessException, IOException { + /** + * 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); } -- cgit v1.2.3 From 34903015992ac0cd7719b662af3ceb54a801351c Mon Sep 17 00:00:00 2001 From: sowgro Date: Fri, 7 Mar 2025 00:02:56 -0500 Subject: Finish update methods --- .../java/com/ufund/api/ufundapi/service/UserService.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java') 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 index 776d09a..935ee72 100644 --- 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 @@ -45,14 +45,17 @@ public class UserService { /** * Updates a user - * // TODO - * @param user - * @param name - * @return + * + * @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 name) throws IOException { - return userDAO.updateUser(user, name); + 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); } /** -- cgit v1.2.3