diff --git a/src/main/java/mops/gruppen2/repository/EventRepository.java b/src/main/java/mops/gruppen2/repository/EventRepository.java index 7b84ce0..f328fdf 100644 --- a/src/main/java/mops/gruppen2/repository/EventRepository.java +++ b/src/main/java/mops/gruppen2/repository/EventRepository.java @@ -30,4 +30,7 @@ public interface EventRepository extends CrudRepository { @Query("SELECT MAX(event_id) FROM event") public Long getHighesEvent_ID(); + + @Query("SELECT MAX(group_id) FROM event") + public Long getMaxGroupID(); } diff --git a/src/main/java/mops/gruppen2/service/EventService.java b/src/main/java/mops/gruppen2/service/EventService.java index f545456..7fd67e8 100644 --- a/src/main/java/mops/gruppen2/service/EventService.java +++ b/src/main/java/mops/gruppen2/service/EventService.java @@ -59,22 +59,17 @@ public class EventService { } /** - * Sorgt dafür die Group_id immer um 1 zu erhöhen + * Gibt die nächst höhere groupID zurück die belegt werden kann. + * Gibt 1 zurück, falls keine Gruppe vorhanden ist. * * @return Gibt Long zurück */ public Long checkGroup() { - Long tmpId = 1L; - Iterable eventDTOS = eventStore.findAll(); - for (EventDTO event : eventDTOS) { - if (event.getGroup_id() == null) { - return tmpId; - } - if (tmpId <= event.getGroup_id()) { - tmpId++; - } + Long maxGroupID = eventStore.getMaxGroupID(); + if (maxGroupID == null) { + return 1L; } - return tmpId; + return maxGroupID + 1; } /**