schedule-refactor #45
@ -114,4 +114,23 @@ public class ScheduleController {
|
||||
|
||||
return ResponseEntity.ok(abstractSchedules.stream().map(AbstractSchedule::toScheduleInfo).toList());
|
||||
}
|
||||
|
||||
@PostMapping("/schedules/{taskID}/now")
|
||||
public ResponseEntity<?> scheduleNow(@PathVariable long taskID) {
|
||||
PermissionResult<Task> permissionResult = taskService.getTaskPermissions(taskID, 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> scheduleResult = taskScheduleService.scheduleNow(permissionResult.getResult());
|
||||
if(scheduleResult.getExitCode() == ServiceExitCode.ENTITY_ALREADY_EXIST) {
|
||||
return ResponseEntity.status(409).body(new SimpleStatusResponse("failed"));
|
||||
} else {
|
||||
return ResponseEntity.ok(scheduleResult.getResult().toScheduleInfo());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,11 @@ public abstract class AbstractSchedule {
|
||||
this.stopTime = stopTime;
|
||||
}
|
||||
|
||||
public AbstractSchedule(Task task, LocalDateTime startTime) {
|
||||
this.task = task;
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public AbstractSchedule(Task task) {
|
||||
this.task = task;
|
||||
}
|
||||
@ -82,4 +87,8 @@ public abstract class AbstractSchedule {
|
||||
}
|
||||
|
||||
public abstract ScheduleInfo toScheduleInfo();
|
||||
|
||||
public boolean isActive() {
|
||||
return startTime != null && stopTime == null;
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,11 @@ public class BasicTaskSchedule extends AbstractSchedule{
|
||||
this.scheduleDate = scheduleDate;
|
||||
}
|
||||
|
||||
public BasicTaskSchedule(Task task, LocalDateTime startTime) {
|
||||
super(task, startTime);
|
||||
this.scheduleDate = startTime.toLocalDate();
|
||||
}
|
||||
|
||||
public BasicTaskSchedule(Task task, LocalDate scheduleDate) {
|
||||
super(task);
|
||||
this.scheduleDate = scheduleDate;
|
||||
|
@ -151,4 +151,13 @@ public class Task {
|
||||
", workTime=" + workTime +
|
||||
'}';
|
||||
}
|
||||
|
||||
public boolean hasActiveSchedule() {
|
||||
for(AbstractSchedule schedule : getBasicTaskSchedules()) {
|
||||
if(schedule.isActive()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -79,4 +80,18 @@ public class TaskScheduleService {
|
||||
}
|
||||
return filteredSchedules;
|
||||
}
|
||||
|
||||
public ServiceResult<AbstractSchedule> scheduleNow(Task task) {
|
||||
if(task.isFinished()) {
|
||||
return new ServiceResult<>(ServiceExitCode.INVALID_OPERATION);
|
||||
}
|
||||
|
||||
if(task.hasActiveSchedule()) {
|
||||
return new ServiceResult<>(ServiceExitCode.ENTITY_ALREADY_EXIST);
|
||||
} else {
|
||||
BasicTaskSchedule basicTaskSchedule = new BasicTaskSchedule(task, LocalDateTime.now());
|
||||
scheduleRepository.save(basicTaskSchedule);
|
||||
return new ServiceResult<>(basicTaskSchedule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,6 @@ public class ScheduleRepositoryTest {
|
||||
User referenceUser_2 = entityManager.find(User.class, 2L);
|
||||
|
||||
assertEquals(0, scheduleRepository.findAllByUsername(referenceUser_2.getUsername()).size());
|
||||
assertEquals(3, scheduleRepository.findAllByUsername(referenceUser_1.getUsername()).size());
|
||||
assertEquals(5, scheduleRepository.findAllByUsername(referenceUser_1.getUsername()).size());
|
||||
}
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ public class ScheduleServiceTest {
|
||||
assertEquals(0, taskScheduleService.getAllSchedulesOfUser(username2).size());
|
||||
|
||||
List<AbstractSchedule> result_1 = taskScheduleService.getAllSchedulesOfUser(username);
|
||||
assertEquals(3, result_1.size());
|
||||
assertEquals(5, result_1.size());
|
||||
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 1L)));
|
||||
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 2L)));
|
||||
assertTrue(result_1.contains(entityManager.find(AbstractSchedule.class, 3L)));
|
||||
@ -127,14 +127,34 @@ public class ScheduleServiceTest {
|
||||
|
||||
//user with tasks and schedules
|
||||
List<AbstractSchedule> result_1 = taskScheduleService.getFilteredScheduledOfUser(LocalDate.of(2024,11,11), false, username);
|
||||
assertEquals(3, result_1.size());
|
||||
assertEquals(5, result_1.size());
|
||||
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 1L)));
|
||||
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 2L)));
|
||||
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 3L)));
|
||||
|
||||
List<AbstractSchedule> result_2 = taskScheduleService.getFilteredScheduledOfUser(LocalDate.of(2024,11,11), true, username);
|
||||
assertEquals(2, result_2.size());
|
||||
assertEquals(3, result_2.size());
|
||||
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 1L)));
|
||||
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 2L)));
|
||||
}
|
||||
@Test
|
||||
@SqlGroup({
|
||||
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
|
||||
@Sql("classpath:taskRepositoryEntries.sql"),
|
||||
@Sql("classpath:basicScheduleEntries.sql")
|
||||
})
|
||||
void scheduleNow() {
|
||||
//Situation 1: Task has already an active schedule
|
||||
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 3L));
|
||||
assertEquals(ServiceExitCode.ENTITY_ALREADY_EXIST, result_1.getExitCode());
|
||||
|
||||
//Situation 2: Task is already finished
|
||||
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 2L));
|
||||
assertEquals(ServiceExitCode.INVALID_OPERATION, result_2.getExitCode());
|
||||
|
||||
//Situation 3: Task can be scheduled and started
|
||||
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.scheduleNow(entityManager.find(Task.class, 5L));
|
||||
assertEquals(ServiceExitCode.OK, result_3.getExitCode());
|
||||
assertThat(entityManager.find(BasicTaskSchedule.class, result_3.getResult().getScheduleID())).isNotNull();
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
INSERT INTO abstract_schedule (schedule_type, scheduleid, start_time, stop_time, schedule_date, task, schedule_end, schedule_start)
|
||||
VALUES (0, 1, null, null, '2024-11-11', 1, null, null),
|
||||
(0, 2, null, null, '2024-11-11', 2, null, null),
|
||||
(0, 3, '2023-10-10', null, '2024-11-11', 1, null, null);
|
||||
(0, 3, '2023-10-10', null, '2024-11-11', 1, null, null),
|
||||
(0, 4, '2023-10-10', null, '2024-11-11', 3, null, null),
|
||||
(0, 5, null, null, '2024-11-11', 5, null, null);
|
Loading…
Reference in New Issue
Block a user