diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java index 5487aa668eb92557a38f2333a9f13b634c3f08bd..9bb5d58d78aef857d41d92a7d0d3cf8d12afc0b4 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/user/UserController.java @@ -98,6 +98,12 @@ public class UserController { } + /** + * Adds the specified allergy to the user with the given username. + * + * @param allergyRequest the request object containing the username and allergy name + * @return a ResponseEntity with a boolean indicating whether the operation was successful + */ @PostMapping("/addAllergy") public ResponseEntity<Boolean> addAllergyToUser(@RequestBody AllergyRequest allergyRequest) { try { @@ -109,6 +115,24 @@ public class UserController { } } + /** + * Deletes the specified allergy from the user with the given username. + * + * @param allergyRequest the request object containing the username and allergy name + * @return a ResponseEntity with a boolean indicating whether the operation was successful + */ + @DeleteMapping("/deleteAllergy") + public ResponseEntity<Boolean> deleteAllergyFromUser(@RequestBody AllergyRequest allergyRequest) { + try { + return userService.deleteAllergy(allergyRequest.getUsername(), allergyRequest.getAllergyName()) + .map(user -> ResponseEntity.ok(true)) + .orElseGet(() -> ResponseEntity.notFound().build()); + }catch (Exception e){ + return ResponseEntity.status(409).body(false); + } + } + + /** * Update a user in the database. * @param username The username of the user to be updated. diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java index a3a5b43202ebc8d08784cfe51382cefedb533d5b..5c91b0fbdd356232a2e6e3375ab5643ec5abd4a3 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/User.java @@ -96,6 +96,24 @@ public class User implements UserDetails { this.allergies.add(allergy); } + + /** + * Deletes the specified allergy from this user's list of allergies. + * + * @param allergy the allergy to delete + * @return true if the allergy was successfully deleted, false otherwise + */ + public boolean deleteAllergy(Allergy allergy){ + if(this.allergies == null){ + this.allergies = new ArrayList<>(); + } + if(this.allergies.contains(allergy)){ + allergies.remove(allergy); + return true; + } + return false; + } + /** * adds a recipe to the user * @param recipe the recipe to add to the user diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java index 3343a58ca1ab85c8d6973b0e08259b38fec0eeed..5e900fcae7463b980ff957ba5552a7f6f2a3c5e1 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/UserService.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Service; import jakarta.persistence.EntityNotFoundException; +import java.nio.channels.FileChannel; import java.util.List; import java.util.Optional; @@ -170,4 +171,26 @@ public class UserService { } return Optional.empty(); } + + /** + * Deletes the specified allergy from the user with the given username. + * + * @param username the username of the user to delete the allergy from + * @param allergyName the name of the allergy to delete + * @return an Optional containing the updated User object if the operation was successful, or an empty Optional otherwise + * @throws EntityNotFoundException if the specified user or allergy cannot be found + */ + public Optional<User> deleteAllergy(String username, String allergyName) { + Optional<User> user = userRepository.findByUsername(username); + Optional<Allergy> allergy = allergyRepository.findByName(allergyName); + + if (user.isPresent() && allergy.isPresent()) { + if(user.get().deleteAllergy(allergy.get())) return Optional.of(userRepository.save(user.get())); + } else if (user.isEmpty()) { + throw new EntityNotFoundException("User not found"); + } else { + throw new EntityNotFoundException("Allergy not found"); + } + return Optional.empty(); + } } \ No newline at end of file