Skip to content

Commit

Permalink
Merge pull request #106 from UdL-EPS-SoftArch/Issue-volunteer-can-kic…
Browse files Browse the repository at this point in the history
…k-himself

Issue volunteer can kick himself
  • Loading branch information
rogargon authored Jun 6, 2024
2 parents 9565c5b + 04c0a4a commit d185971
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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 { }
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
public class VolunteerFromDifferentShelter extends RuntimeException { }

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit d185971

Please sign in to comment.