From 408d281cb4804d9bc487c808eb5f3c82af9cfc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Wed, 5 Jun 2024 20:23:48 +0200 Subject: [PATCH 1/2] New condition added --- .../demo/handler/ShelterVolunteerEventHandler.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java index 4e9845c6..b97265c2 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ShelterVolunteerEventHandler.java @@ -2,6 +2,7 @@ import cat.udl.eps.softarch.demo.domain.Shelter; import cat.udl.eps.softarch.demo.domain.ShelterVolunteer; import cat.udl.eps.softarch.demo.domain.User; +import cat.udl.eps.softarch.demo.exceptions.VolunteerCannotKickHimself; import cat.udl.eps.softarch.demo.exceptions.VolunteerFromDifferentShelter; import cat.udl.eps.softarch.demo.repository.ShelterVolunteerRepository; import cat.udl.eps.softarch.demo.repository.UserRepository; @@ -29,12 +30,14 @@ public ShelterVolunteerEventHandler(ShelterVolunteerRepository shelterVolunteerR } @HandleBeforeDelete - public void handleShelterVolunteerBeforeDelete(ShelterVolunteer volunteer) { + public void handleShelterVolunteerBeforeDelete(ShelterVolunteer volunteer) { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String username = authentication.getName(); ShelterVolunteer requestVolunteer = shelterVolunteerRepository.findById(username).orElse(null); - if(requestVolunteer!=null && Objects.equals(requestVolunteer.getUserShelter().getId(), volunteer.getUserShelter().getId())) { - logger.info("Volunteer {} is deleting volunteer {}.", username, volunteer.getUsername()); + if(requestVolunteer!=null && Objects.equals(requestVolunteer.getUserShelter().getId(), volunteer.getUserShelter().getId()) ) { + if(!username.equals(volunteer.getUsername())) + logger.info("Volunteer {} is deleting volunteer {}.", username, volunteer.getUsername()); + else throw new VolunteerCannotKickHimself(); } else throw new VolunteerFromDifferentShelter(); From 04c0a4ab4b9dcf07136957a1bc344f23bd7a308c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Escol=C3=A0=20Curc=C3=B3?= Date: Wed, 5 Jun 2024 20:23:58 +0200 Subject: [PATCH 2/2] New exception added --- .../demo/exceptions/VolunteerCannotKickHimself.java | 7 +++++++ .../demo/exceptions/VolunteerFromDifferentShelter.java | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerCannotKickHimself.java diff --git a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerCannotKickHimself.java b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerCannotKickHimself.java new file mode 100644 index 00000000..822601af --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerCannotKickHimself.java @@ -0,0 +1,7 @@ +package cat.udl.eps.softarch.demo.exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.PRECONDITION_FAILED, reason = "No te puedes expulsar a ti mismo") +public class VolunteerCannotKickHimself extends RuntimeException { } \ No newline at end of file diff --git a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java index 7ae3d426..82b7dd48 100644 --- a/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java +++ b/src/main/java/cat/udl/eps/softarch/demo/exceptions/VolunteerFromDifferentShelter.java @@ -2,4 +2,5 @@ import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(code = HttpStatus.PRECONDITION_FAILED, reason = "El usuario debe ser de tipo shelterVolunteer") -public class VolunteerFromDifferentShelter extends RuntimeException { } \ No newline at end of file +public class VolunteerFromDifferentShelter extends RuntimeException { } +