schedule-refactor #45

Merged
sebastian merged 18 commits from schedule-refactor into master 2023-11-11 18:56:16 +01:00
8 changed files with 49 additions and 9 deletions
Showing only changes of commit 311edbb6fc - Show all commits

View File

@ -189,4 +189,10 @@ public class ScheduleController {
return ResponseEntity.ok(new TaskScheduleStopResponse(scheduleResult.getResult().getActiveTime()));
}
}
@GetMapping("/schedules/missed")
public ResponseEntity<?> loadMissedSchedules() {
List<AbstractSchedule> missedSchedules = taskScheduleService.getAllMissedSchedulesOfUser(SecurityContextHolder.getContext().getAuthentication().getName());
return ResponseEntity.ok(missedSchedules.stream().map(AbstractSchedule::toScheduleInfo).toList());
}
}

View File

@ -4,6 +4,7 @@ import core.api.models.timemanager.taskSchedule.ScheduleInfo;
import javax.persistence.*;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;
@ -106,4 +107,6 @@ public abstract class AbstractSchedule {
public boolean isStartable() {
return startTime == null;
}
public abstract boolean isMissed(LocalDateTime timeReference);
}

View File

@ -16,4 +16,9 @@ public class AdvancedTaskSchedule extends AbstractSchedule {
public ScheduleInfo toScheduleInfo() {
return null;
}
@Override
public boolean isMissed(LocalDateTime timeReference) {
return false;
}
}

View File

@ -51,6 +51,11 @@ public class BasicTaskSchedule extends AbstractSchedule{
return new BasicScheduleInfo(scheduleID, ScheduleType.BASIC, startTime, stopTime, activeMinutes, task, taskgroupEntityInfos);
}
@Override
public boolean isMissed(LocalDateTime timeReference) {
return startTime == null && scheduleDate.isBefore(timeReference.toLocalDate());
}
public int calcActiveMinutes() {
if(startTime == null) {
return 0;

View File

@ -16,4 +16,5 @@ public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Lon
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1 AND s.startTime is NOT NULL and s.stopTime is NULL")
Optional<AbstractSchedule> getActiveScheduleOfUser(String username);
}

View File

@ -13,6 +13,7 @@ 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;
import java.util.Optional;
@ -134,4 +135,16 @@ public class TaskScheduleService {
return new ServiceResult<>(basicTaskSchedule);
}
public List<AbstractSchedule> getAllMissedSchedulesOfUser(String username) {
List<AbstractSchedule> missedSchedules = new ArrayList<>();
List<AbstractSchedule> schedules = getAllSchedulesOfUser(username);
LocalDateTime now = LocalDateTime.now();
for(AbstractSchedule schedule : schedules) {
if(schedule.isMissed(now)) {
missedSchedules.add(schedule);
}
}
return missedSchedules;
}
}

View File

@ -129,15 +129,13 @@ public class ScheduleServiceTest {
//user with tasks and schedules
List<AbstractSchedule> result_1 = taskScheduleService.getFilteredScheduledOfUser(LocalDate.of(2024,11,11), false, username);
assertEquals(5, result_1.size());
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 1L)));
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 2L)));
assertEquals(3, result_1.size());
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 3L)));
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 4L)));
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 5L)));
List<AbstractSchedule> result_2 = taskScheduleService.getFilteredScheduledOfUser(LocalDate.of(2024,11,11), true, username);
assertEquals(3, result_2.size());
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 1L)));
assertTrue(result_1.contains(entityManager.find(BasicTaskSchedule.class, 2L)));
assertEquals(1, result_2.size());
}
@Test
@SqlGroup({
@ -225,6 +223,15 @@ public class ScheduleServiceTest {
assertEquals(startTime, result_2.getResult().getStartTime());
assertEquals(finishTime, result_2.getResult().getStopTime());
assertThat(entityManager.find(BasicTaskSchedule.class, result_2.getResult().getScheduleID())).isNotNull();
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getAllMissedSchedulesOfUser() {
assertEquals(1, taskScheduleService.getAllMissedSchedulesOfUser(username).size());
}
}

View File

@ -1,7 +1,7 @@
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', '2023-10-10', '2024-11-11', 1, null, null),
VALUES (0, 1, null, null, '2010-11-11', 1, null, null),
(0, 2, null, null, '2048-11-11', 2, null, null),
(0, 3, '2023-10-10', '2022-10-10', '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),
(0, 6, '2023-10-10', null, '2024-11-11', 16, null, null),