Skip to content

Commit

Permalink
Invalid slot switch attempt doesn't unslot
Browse files Browse the repository at this point in the history
  • Loading branch information
Alf-Melmac committed Feb 2, 2022
1 parent 9ae6952 commit 347fc55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/main/java/de/webalf/slotbot/model/Slot.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,31 @@ public void slot(User user) {
}

/**
* @see Slot#slot(User)
* Doesn't trigger the slotUpdate
*
* @throws BusinessRuntimeException if the user is already slotted on this slot or the slot is already occupied
* @throws BusinessRuntimeException if the given user can't be slotted to the slot
*/
void slotWithoutUpdate(@NonNull User user) {
public void assertSlotIsPossible(@NonNull User user) {
if (isSlotWithSlottedUser(user)) {
throw BusinessRuntimeException.builder().title("Die Person ist bereits auf diesem Slot").build();
} else if (!isEmpty()) {
throw BusinessRuntimeException.builder().title("Auf dem Slot befindet sich eine andere Person").build();
} else if (getEffectiveReservedFor() != null && user.getGuilds().stream().noneMatch(guild -> guild.equals(getEffectiveReservedFor()))) {
throw BusinessRuntimeException.builder().title("Dieser Slot ist für Mitglieder einer anderen Gruppe reserviert").build();
} else {
//Remove the user from any other slot in the Event
getEvent().findSlotOfUser(user).ifPresent(slot -> slot.unslotWithoutUpdate(user));
setUser(user);
}
}

/**
* @see Slot#slot(User)
* Doesn't trigger the slotUpdate
*
* @throws BusinessRuntimeException if the user is already slotted on this slot or the slot is already occupied
*/
void slotWithoutUpdate(@NonNull User user) {
assertSlotIsPossible(user);
//Remove the user from any other slot in the Event
getEvent().findSlotOfUser(user).ifPresent(slot -> slot.unslotWithoutUpdate(user));
setUser(user);
}

/**
* Removes the given user from the slot if no other user occupies the slot
*
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/webalf/slotbot/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ public void deleteEvent(Event event) {
public Event slot(@NonNull Event event, int slotNumber, UserDto userDto) {
Slot slot = event.findSlot(slotNumber).orElseThrow(ResourceNotFoundException::new);
User user = userService.find(userDto);

slot.assertSlotIsPossible(user);
event.unslotIfAlreadySlotted(user);
eventRepository.saveAndFlush(event);
slotService.slot(slot, user);
Expand Down

0 comments on commit 347fc55

Please sign in to comment.