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 0000000..822601a --- /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 7ae3d42..82b7dd4 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 { } + 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 4e9845c..b97265c 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();