schedule-refactor #45
@ -140,4 +140,19 @@ public class ScheduleController {
|
|||||||
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -99,4 +99,12 @@ public class TaskScheduleService {
|
|||||||
Optional<AbstractSchedule> activeSchedule = scheduleRepository.getActiveScheduleOfUser(username);
|
Optional<AbstractSchedule> activeSchedule = scheduleRepository.getActiveScheduleOfUser(username);
|
||||||
return activeSchedule.map(ServiceResult::new).orElseGet(() -> new ServiceResult<>(ServiceExitCode.MISSING_ENTITY));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,4 +157,25 @@ public class ScheduleServiceTest {
|
|||||||
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
|
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
|
||||||
assertThat(entityManager.find(BasicTaskSchedule.class, result_3.getResult().getScheduleID())).isNotNull();
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user