diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2025-03-17 17:17:06 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2025-03-17 17:17:06 -0400 |
commit | baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754 (patch) | |
tree | e9213224b8f1b35b860f016a6a3d1318def8aae2 /ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java | |
parent | bf33fa3ca9f29b1e75cc077ae2eaaf4f5725e4b3 (diff) | |
parent | d737551fba5617843f3014be6994490dd4328183 (diff) | |
download | JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.gz JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.tar.bz2 JellySolutions-baf4f2c0189d5c5f8ade40f0ceaed3ab7a7d4754.zip |
Merge remote-tracking branch 'origin/main' into cupboard-component
# Conflicts:
# ufund-api/data/cupboard.json
# ufund-ui/src/app/app.module.ts
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); + } + +} |