aboutsummaryrefslogtreecommitdiff
path: root/ufund-api/src/main/java/com/ufund/api/ufundapi/service
diff options
context:
space:
mode:
authorGunther6070 <haydenhartman10@yahoo.com>2025-03-07 15:43:17 -0500
committerGunther6070 <haydenhartman10@yahoo.com>2025-03-07 15:43:17 -0500
commit94869200a0d2f80f71fcd0efd4f82c0138b5440d (patch)
tree52eff6463b3cec7c3d6bda18e186f9272f528a2c /ufund-api/src/main/java/com/ufund/api/ufundapi/service
parentfa1f1140b1e13d495c8e06e80928efb333917d31 (diff)
parentcaaf278d1fa69fef69c210edb337fa54102d2737 (diff)
downloadJellySolutions-94869200a0d2f80f71fcd0efd4f82c0138b5440d.tar.gz
JellySolutions-94869200a0d2f80f71fcd0efd4f82c0138b5440d.tar.bz2
JellySolutions-94869200a0d2f80f71fcd0efd4f82c0138b5440d.zip
Merge branch 'api-auth' of https://github.com/RIT-SWEN-261-02/team-project-2245-swen-261-02-2b into api-auth
Diffstat (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service')
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java12
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java37
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java52
3 files changed, 73 insertions, 28 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java
index 7e54cfb..591d891 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/AuthService.java
@@ -1,11 +1,10 @@
package com.ufund.api.ufundapi.service;
-import java.io.IOException;
-
-import org.springframework.stereotype.Component;
-
import com.ufund.api.ufundapi.model.UserAuth;
import com.ufund.api.ufundapi.persistence.UserAuthDAO;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
@Component
public class AuthService {
@@ -24,9 +23,8 @@ public class AuthService {
* @param username The username of the user trying to be accessed.
* @param key The api key obtained by the client from logging in.
* @throws IllegalAccessException Thrown if access was denied to the user.
- * @throws IOException
- */
- public void authenticate(String username, String key) throws IllegalAccessException, IOException {
+ */
+ public void authenticate(String username, String key) throws IllegalAccessException, IOException {
var userAuth = userAuthDAO.getUserAuth(key);
if (userAuth == null || !userAuth.getUsername().equals(username)) {
throw new IllegalAccessException("Unauthorized");
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
index 6052e4f..c8609ab 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java
@@ -6,22 +6,27 @@ import java.util.Arrays;
import com.ufund.api.ufundapi.model.Need;
import com.ufund.api.ufundapi.persistence.CupboardDAO;
import org.springframework.stereotype.Component;
+import com.ufund.api.ufundapi.DuplicateKeyException;
@Component
public class CupboardService {
private final CupboardDAO cupboardDAO;
- public class DuplicateKeyException extends Exception {
- public DuplicateKeyException(String message) {
- super(message);
- }
- }
-
public CupboardService(CupboardDAO cupboardDAO) {
this.cupboardDAO = cupboardDAO;
}
+ /**
+ * Creates a new Need
+ *
+ * @param name The name of the need to create
+ * @param maxGoal The max goal of the new need
+ * @param goalType The goal type of the new need
+ * @return The need that was created
+ * @throws IOException Thrown if there was any issue saving the data
+ * @throws DuplicateKeyException If there already exists a need with the same name
+ */
public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException {
Need need = new Need(name, goalType, maxGoal);
@@ -39,6 +44,12 @@ public class CupboardService {
}
+ /**
+ * Get all the needs in the cupboard
+ *
+ * @return An array containing all needs
+ * @throws IOException Thrown if there was any issue saving the data
+ */
public Need[] getNeeds() throws IOException {
return cupboardDAO.getNeeds();
}
@@ -48,7 +59,7 @@ public class CupboardService {
*
* @param search The search substring
* @return The requested array
- * @throws IOException
+ * @throws IOException Thrown if there was any issue saving the data
*/
public Need[] searchNeeds(String search) throws IOException {
return Arrays.stream(cupboardDAO.getNeeds())
@@ -67,13 +78,17 @@ public class CupboardService {
}
/**
- * Modify a need
+ * Updates a need
*
- * @param need
- * @return
+ * @param id The ID of the need to update
+ * @param need The need object to set (note: the ID is ignored)
+ * @return The updated need object
* @throws IOException Thrown if there was an issue saving the changes
*/
- public Need updateNeed(Need need) throws IOException {
+ public Need updateNeed(Need need, int id) throws IOException {
+ if (need.getId() != id) {
+ throw new IllegalArgumentException("ID in URL and body must match");
+ }
return cupboardDAO.updateNeed(need);
}
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..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
@@ -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,60 @@ 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 {
- return userDAO.updateUser(user, name);
+ /**
+ * 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);
}
- 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);
}