diff --git a/src/main/java/cat/udl/eps/softarch/demo/handler/ScheduleEventHandler.java b/src/main/java/cat/udl/eps/softarch/demo/handler/ScheduleEventHandler.java new file mode 100644 index 0000000..e7ff697 --- /dev/null +++ b/src/main/java/cat/udl/eps/softarch/demo/handler/ScheduleEventHandler.java @@ -0,0 +1,37 @@ +package cat.udl.eps.softarch.demo.handler; + +import cat.udl.eps.softarch.demo.domain.Schedule; +import cat.udl.eps.softarch.demo.repository.ScheduleRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.rest.core.annotation.HandleBeforeCreate; +import org.springframework.data.rest.core.annotation.HandleBeforeSave; +import org.springframework.data.rest.core.annotation.RepositoryEventHandler; +import org.springframework.stereotype.Component; +import org.springframework.validation.annotation.Validated; + + + +@Component +@RepositoryEventHandler +@Validated +public class ScheduleEventHandler { + + private final Logger logger = LoggerFactory.getLogger(Schedule.class); + + private final ScheduleRepository scheduleRepository; + + public ScheduleEventHandler(ScheduleRepository scheduleRepository) { + this.scheduleRepository = scheduleRepository; + } + + @HandleBeforeCreate + @HandleBeforeSave + public void handleScheduleValidation(Schedule schedule) { + if (schedule.getStartTime().isAfter(schedule.getEndTime())) { + throw new IllegalArgumentException("Start time must be before end time"); + } + logger.info("Schedule is valid: {}", schedule); + scheduleRepository.save(schedule); + } +} diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java index 9faea4d..ac3630e 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/ScheduleStepDefs.java @@ -22,8 +22,6 @@ public class ScheduleStepDefs { @Autowired private ScheduleRepository scheduleRepository; - /*@Autowired - private ShelterRepository shelterRepository; falta aprovar PR Shelter Test*/ public static String newResourceUri; @When("I create a new schedule with startTime \"([^\"]*)\" and endTime \"([^\"]*)\" for shelter \"([^\"]*)\"$") @@ -34,12 +32,8 @@ public void iCreateANewScheduledWithStartTimeAndEndTime(String startTime, String ZonedDateTime start = ZonedDateTime.parse(startTime); ZonedDateTime end = ZonedDateTime.parse(endTime); - if(start.compareTo(end) >= 0) schedule.setStartTime(null); - else { - schedule.setStartTime(start); - schedule.setEndTime(end); - } - //Shelter shelter1 = shelterRepository.getbyname... falta aprovar PR Shelter Test + schedule.setStartTime(start); + schedule.setEndTime(end); stepDefs.result = stepDefs.mockMvc.perform( post("/schedules") diff --git a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java index 172c7dd..f5c9a9e 100644 --- a/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java +++ b/src/test/java/cat/udl/eps/softarch/demo/steps/UpdateScheduleStepDefs.java @@ -60,11 +60,8 @@ public void iUpdateAScheduleWithStartTimeAndEndTimeWithNewValuesStartTimeAndEndT Schedule schedule = scheduleRepository.findScheduleByStartTimeAndEndTime(existingStart, existingEnd); if (schedule != null) { - if (newStart != null) { - if (newStart.compareTo(newEnd) >= 0) schedule.setStartTime(null); - else schedule.setStartTime(newStart); - } - if (newEnd != null) schedule.setEndTime(newEnd); + schedule.setStartTime(newStart); + schedule.setEndTime(newEnd); } diff --git a/src/test/resources/features/CreateSchedule.feature b/src/test/resources/features/CreateSchedule.feature index 221cfac..2d06767 100644 --- a/src/test/resources/features/CreateSchedule.feature +++ b/src/test/resources/features/CreateSchedule.feature @@ -23,5 +23,5 @@ Feature: Create Schedule Scenario: Create a new schedule with startTime higher than endTime Given I can login with username "username" and password "password" When I create a new schedule with startTime "2024-03-17T10:00:00+01:00" and endTime "2024-03-17T08:00:00+01:00" for shelter "shelter" - Then The response code is 400 + Then The response code is 500 And No schedule is created diff --git a/src/test/resources/features/UpdateSchedule.feature b/src/test/resources/features/UpdateSchedule.feature index f2f43b1..cfb87c5 100644 --- a/src/test/resources/features/UpdateSchedule.feature +++ b/src/test/resources/features/UpdateSchedule.feature @@ -27,4 +27,4 @@ Feature: Update Schedule Given I can login with username "username" and password "password" And There is a registered schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" When I update a schedule with startTime "2024-03-17T08:00:00+01:00" and endTime "2024-03-17T21:00:00+01:00" with new values startTime "2024-03-17T10:00:00+01:00" and endTime "2024-03-17T08:00:00+01:00" - Then The response code is 400 + Then The response code is 500