Skip to content
Snippets Groups Projects
Commit eca16b8a authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch 'featur/addEndpoitToDeleteAllergy' into 'main'

An endpoint to delete an allergy from a user was added

See merge request idatt2106-v23-03/backend!152
parents 698a434b 254c0d5c
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
......@@ -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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment