From 4cfacd63b1552bf6ea33e28f3f66e11b75e5756a Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Thu, 6 Mar 2025 12:45:35 -0500 Subject: Created Cupboard Service and refactored the controller and DAO to add the service as an inbetween with logic --- .../api/ufundapi/service/CupboardService.java | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java') 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 new file mode 100644 index 0000000..860a2a8 --- /dev/null +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -0,0 +1,83 @@ +package com.ufund.api.ufundapi.service; + +import java.io.IOException; +import java.util.Arrays; + +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardDAO; + +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; + } + + public Need createNeed(String name, int maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { + + Need need = new Need(name, goalType, maxGoal); + + if (need.getMaxGoal() <= 0) { + throw new IllegalArgumentException("Max Goal must be greater than zero"); + } else { + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (need.getName().equalsIgnoreCase(searchNeed.getName())) { + throw new DuplicateKeyException("Duplicate names are not allowed"); + } + } + return cupboardDAO.addNeed(need); + } + + } + + public Need[] getNeeds() throws IOException { + return cupboardDAO.getNeeds(); + } + + /** + * Returns an array of needs filtered by a search + * + * @param search The search substring + * @return The requested array + * @throws IOException + */ + public Need[] searchNeeds(String search) throws IOException { + return Arrays.stream(cupboardDAO.getNeeds()) + .filter(i -> i.getName().toLowerCase().contains(search.toLowerCase())) + .toArray(Need[]::new); + } + + /** + * @param id + * @return + * @throws IOException + */ + public Need getNeed(int id) throws IOException { + return cupboardDAO.getNeed(id); + } + + /** + * + * @param need + * @return + * @throws IOException + */ + public Need updateNeed(Need need) throws IOException { + return cupboardDAO.updateNeed(need); + } + + public boolean deleteNeed(int id) throws IOException { + return cupboardDAO.deleteNeed(id); + } + + +} -- cgit v1.2.3 From bb9ce55cb5b55a6aaed2399e39a01d68f2491ce3 Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 6 Mar 2025 21:41:39 -0500 Subject: Push current changes (working on documentation and tests) --- .../api/ufundapi/service/CupboardService.java | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java') 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 860a2a8..6052e4f 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 @@ -5,17 +5,17 @@ import java.util.Arrays; import com.ufund.api.ufundapi.model.Need; import com.ufund.api.ufundapi.persistence.CupboardDAO; +import org.springframework.stereotype.Component; +@Component public class CupboardService { private final CupboardDAO cupboardDAO; public class DuplicateKeyException extends Exception { - public DuplicateKeyException(String message) { super(message); } - } public CupboardService(CupboardDAO cupboardDAO) { @@ -57,27 +57,34 @@ public class CupboardService { } /** - * @param id - * @return - * @throws IOException + * Gets a need with the specified ID + * + * @param id the ID of the need + * @return The resulting Need or null if the need was not found */ public Need getNeed(int id) throws IOException { return cupboardDAO.getNeed(id); } /** - * + * Modify a need + * * @param need * @return - * @throws IOException + * @throws IOException Thrown if there was an issue saving the changes */ public Need updateNeed(Need need) throws IOException { return cupboardDAO.updateNeed(need); } + /** + * Delete a need from the cupboard + * + * @param id the ID of the need + * @return True if the need was deleted + * @throws IOException Thrown on any problem removing the need + */ public boolean deleteNeed(int id) throws IOException { return cupboardDAO.deleteNeed(id); } - - } -- cgit v1.2.3 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 --- .../api/ufundapi/service/CupboardService.java | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java') 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..15f8442 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()) @@ -68,7 +79,7 @@ public class CupboardService { /** * Modify a need - * + * // TODO * @param need * @return * @throws IOException Thrown if there was an issue saving the changes -- 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 --- .../com/ufund/api/ufundapi/service/CupboardService.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java') 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 15f8442..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 @@ -78,13 +78,17 @@ public class CupboardService { } /** - * Modify a need - * // TODO - * @param need - * @return + * Updates a need + * + * @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); } -- cgit v1.2.3 From 4ac7711c4d9dd3275ae4037f843347e4fbcb1f2a Mon Sep 17 00:00:00 2001 From: Gunther6070 Date: Sat, 15 Mar 2025 23:18:54 -0400 Subject: Added additional check to createNeed method --- .../com/ufund/api/ufundapi/service/CupboardService.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java') 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 c8609ab..6dd120c 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 @@ -3,10 +3,11 @@ package com.ufund.api.ufundapi.service; import java.io.IOException; 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; +import com.ufund.api.ufundapi.model.Need; +import com.ufund.api.ufundapi.persistence.CupboardDAO; @Component public class CupboardService { @@ -27,16 +28,18 @@ public class CupboardService { * @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 { + public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { Need need = new Need(name, goalType, maxGoal); if (need.getMaxGoal() <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); } else { - for (Need searchNeed : cupboardDAO.getNeeds()) { - if (need.getName().equalsIgnoreCase(searchNeed.getName())) { - throw new DuplicateKeyException("Duplicate names are not allowed"); + if (cupboardDAO.getNeeds().length > 0) { + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (need.getName().equalsIgnoreCase(searchNeed.getName())) { + throw new DuplicateKeyException("Duplicate names are not allowed"); + } } } return cupboardDAO.addNeed(need); -- cgit v1.2.3 From a3150b8a8e17c8a71f617745bb8588b397a75f47 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sat, 15 Mar 2025 23:52:58 -0400 Subject: fix testCreateNeed() --- .../api/ufundapi/service/CupboardService.java | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java') 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 6dd120c..78f8f85 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 @@ -29,22 +29,20 @@ public class CupboardService { * @throws DuplicateKeyException If there already exists a need with the same name */ public Need createNeed(String name, double maxGoal, Need.GoalType goalType) throws IOException, DuplicateKeyException { - - Need need = new Need(name, goalType, maxGoal); - if (need.getMaxGoal() <= 0) { + if (maxGoal <= 0) { throw new IllegalArgumentException("Max Goal must be greater than zero"); - } else { - if (cupboardDAO.getNeeds().length > 0) { - for (Need searchNeed : cupboardDAO.getNeeds()) { - if (need.getName().equalsIgnoreCase(searchNeed.getName())) { - throw new DuplicateKeyException("Duplicate names are not allowed"); - } - } + } + + for (Need searchNeed : cupboardDAO.getNeeds()) { + if (searchNeed.getName().equalsIgnoreCase(name)) { + throw new DuplicateKeyException("Duplicate names are not allowed"); } - return cupboardDAO.addNeed(need); } - + + Need need = new Need(name, goalType, maxGoal); + return cupboardDAO.addNeed(need); + } /** -- cgit v1.2.3