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 66 additions and 0 deletions
Showing only changes of commit 5d550d69ac - Show all commits

View File

@ -140,4 +140,19 @@ public class ScheduleController {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
}
@PostMapping("schedules/{scheduleID}/activate")
public ResponseEntity<?> activateSchedule(@PathVariable long scheduleID) {
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"));
}
ServiceResult<AbstractSchedule> serviceResult = taskScheduleService.activateSchedule(permissionResult.getResult());
return ResponseEntity.ok(new ScheduleActivateResponse(serviceResult.getResult().getStartTime()));
}
}

View File

@ -0,0 +1,22 @@
package core.api.models.timemanager.taskSchedule;
import java.time.LocalDateTime;
public class ScheduleActivateResponse {
private LocalDateTime startTime;
public ScheduleActivateResponse(LocalDateTime startTime) {
this.startTime = startTime;
}
public ScheduleActivateResponse() {
}
public LocalDateTime getStartTime() {
return startTime;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}
}

View File

@ -99,4 +99,12 @@ public class TaskScheduleService {
Optional<AbstractSchedule> activeSchedule = scheduleRepository.getActiveScheduleOfUser(username);
return activeSchedule.map(ServiceResult::new).orElseGet(() -> new ServiceResult<>(ServiceExitCode.MISSING_ENTITY));
}
public ServiceResult<AbstractSchedule> activateSchedule(AbstractSchedule schedule) {
if(schedule.getStartTime() == null) {
schedule.setStartTime(LocalDateTime.now());
scheduleRepository.save(schedule);
}
return new ServiceResult<>(schedule);
}
}

View File

@ -157,4 +157,25 @@ 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 activateSchedule() {
//Activate already running schedule
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.activateSchedule(entityManager.find(BasicTaskSchedule.class, 4L));
assertNotNull(result_1.getResult().getStartTime());
assertNull(result_1.getResult().getStopTime());
entityManager.remove(entityManager.find(BasicTaskSchedule.class, 4L));
assertThat(entityManager.find(BasicTaskSchedule.class, 4L)).isNull();
//Activate not running schedule
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.activateSchedule(entityManager.find(BasicTaskSchedule.class, 5L));
assertNotNull(result_2.getResult().getStartTime());
assertNull(result_2.getResult().getStopTime());
}
}