schedule-refactor #45

Merged
sebastian merged 18 commits from schedule-refactor into master 2023-11-11 18:56:16 +01:00
4 changed files with 65 additions and 5 deletions
Showing only changes of commit e36bbb0d65 - Show all commits

View File

@ -6,6 +6,7 @@ import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.BasicScheduleInfo;
import core.api.models.timemanager.taskSchedule.ScheduleFieldInfo;
import core.entities.timemanager.AbstractSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.services.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -66,4 +67,27 @@ public class ScheduleController {
return ResponseEntity.status(400).body(new SimpleStatusResponse("failed"));
}
}
@PostMapping("/schedules/{scheduleID}")
public ResponseEntity<?> editSchedule(@PathVariable long scheduleID, @RequestBody @Valid ScheduleFieldInfo scheduleFieldInfo) {
PermissionResult<AbstractSchedule> permissionResult = taskScheduleService.getSchedulePermissions(scheduleID, SecurityContextHolder.getContext().getAuthentication().getName());
if(permissionResult.isHasPermissions()) {
return ResponseEntity.status(403).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getExitCode() == ServiceExitCode.MISSING_ENTITY) {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
if(permissionResult.getResult() instanceof BasicTaskSchedule && scheduleFieldInfo instanceof BasicScheduleFieldInfo) {
ServiceResult<AbstractSchedule> updatedSchedule = taskScheduleService.editBasicSchedule((BasicTaskSchedule) permissionResult.getResult(), (BasicScheduleFieldInfo) scheduleFieldInfo);
if(updatedSchedule.getExitCode() == ServiceExitCode.INVALID_OPERATION) {
return ResponseEntity.status(400).body(new SimpleStatusResponse("failed"));
} else {
return ResponseEntity.ok(updatedSchedule.getResult().toScheduleInfo());
}
}
return ResponseEntity.status(400).body(new SimpleStatusResponse("failed"));
}
}

View File

@ -1,6 +1,7 @@
package core.services;
import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.BasicScheduleInfo;
import core.api.models.timemanager.taskSchedule.ScheduleFieldInfo;
import core.entities.User;
import core.entities.timemanager.AbstractSchedule;
@ -35,4 +36,19 @@ public class TaskScheduleService {
return new ServiceResult<>(basicTaskSchedule);
}
public PermissionResult<AbstractSchedule> getSchedulePermissions(long scheduleID, String username) {
Optional<AbstractSchedule> abstractSchedule = scheduleRepository.findById(scheduleID);
return abstractSchedule.map(schedule -> new PermissionResult<>(schedule, schedule.getTask().getTaskgroup().getUser().getUsername().equals(username))).orElseGet(() -> new PermissionResult<>(ServiceExitCode.MISSING_ENTITY));
}
public ServiceResult<AbstractSchedule> editBasicSchedule(BasicTaskSchedule schedule, BasicScheduleFieldInfo scheduleFieldInfo) {
if(schedule.getTask().isFinished() || scheduleFieldInfo.getScheduleDate().isBefore(LocalDate.now())) {
return new ServiceResult<>(ServiceExitCode.INVALID_OPERATION);
}
schedule.setScheduleDate(scheduleFieldInfo.getScheduleDate());
scheduleRepository.save(schedule);
return new ServiceResult<>(schedule);
}
}

View File

@ -23,9 +23,7 @@ import java.time.LocalDate;
import java.util.List;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.hibernate.validator.internal.util.Contracts.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@Transactional
@ -75,4 +73,26 @@ public class ScheduleServiceTest {
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertThat(entityManager.find(BasicTaskSchedule.class, result_3.getResult().getScheduleID())).isNotNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void editBasicSchedule() {
//Situation 1: Reschedule finished task
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 2L), new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_1.getExitCode());
//Situation 2: Reschedule unfinished task with invalid reschedule date
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 1L), new BasicScheduleFieldInfo(LocalDate.of(2011, 3, 4)));
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
//Situation 3: Reschedule unfinished task with valid reschedule date
LocalDate oldDate = entityManager.find(BasicTaskSchedule.class, 1L).getScheduleDate();
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.editBasicSchedule(entityManager.find(BasicTaskSchedule.class, 1L), new BasicScheduleFieldInfo(LocalDate.now()));
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
assertNotEquals(((BasicTaskSchedule) result_3.getResult()).getScheduleDate(), oldDate);
}
}

View File

@ -1,3 +1,3 @@
INSERT INTO abstract_schedule (schedule_type, scheduleid, start_time, stop_time, schedule_date, task, schedule_end, schedule_start)
VALUES (0, 1, null, null, '2023-11-11', 1, null, null),
(0, 2, null, null, '2023-11-11', 2, null, null);
VALUES (0, 1, null, null, '2024-11-11', 1, null, null),
(0, 2, null, null, '2024-11-11', 2, null, null);