Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue volunteer can kick himself #106

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading