schedule-refactor #45

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

View File

@ -2,10 +2,7 @@ package core.api.controller;
import core.api.models.auth.SimpleStatusResponse;
import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.BasicScheduleInfo;
import core.api.models.timemanager.taskSchedule.ScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.ScheduleInfo;
import core.api.models.timemanager.taskSchedule.*;
import core.entities.timemanager.AbstractSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
@ -133,4 +130,14 @@ public class ScheduleController {
return ResponseEntity.ok(scheduleResult.getResult().toScheduleInfo());
}
}
@GetMapping("/schedules/active")
public ResponseEntity<?> getActiveSchedule() {
ServiceResult<AbstractSchedule> serviceResult = taskScheduleService.getActiveSchedule(SecurityContextHolder.getContext().getAuthentication().getName());
if(serviceResult.getExitCode() == ServiceExitCode.OK) {
return ResponseEntity.ok(serviceResult.getResult().toScheduleInfo());
} else {
return ResponseEntity.status(404).body(new SimpleStatusResponse("failed"));
}
}
}

View File

@ -5,6 +5,7 @@ import core.entities.timemanager.Task;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class BasicScheduleInfo extends ScheduleInfo{
@ -14,5 +15,15 @@ public class BasicScheduleInfo extends ScheduleInfo{
super(scheduleID, scheduleType, startTime, stopTime, activeMinutes, task, taskgroupPath);
}
public BasicScheduleInfo(long scheduleID) {
super(scheduleID, ScheduleType.BASIC, null, null, 0, null, new ArrayList<>());
}
public LocalDate getScheduleDate() {
return scheduleDate;
}
public void setScheduleDate(LocalDate scheduleDate) {
this.scheduleDate = scheduleDate;
}
}

View File

@ -6,10 +6,14 @@ import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Long> {
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1")
List<AbstractSchedule> findAllByUsername(String username);
@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

@ -94,4 +94,9 @@ public class TaskScheduleService {
return new ServiceResult<>(basicTaskSchedule);
}
}
public ServiceResult<AbstractSchedule> getActiveSchedule(String username) {
Optional<AbstractSchedule> activeSchedule = scheduleRepository.getActiveScheduleOfUser(username);
return activeSchedule.map(ServiceResult::new).orElseGet(() -> new ServiceResult<>(ServiceExitCode.MISSING_ENTITY));
}
}

View File

@ -1,7 +1,9 @@
package core.schedules;
import core.entities.User;
import core.entities.timemanager.AbstractSchedule;
import core.repositories.timemanager.ScheduleRepository;
import core.services.ServiceResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,7 +13,10 @@ import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
@DataJpaTest
@ -32,4 +37,21 @@ public class ScheduleRepositoryTest {
assertEquals(0, scheduleRepository.findAllByUsername(referenceUser_2.getUsername()).size());
assertEquals(5, scheduleRepository.findAllByUsername(referenceUser_1.getUsername()).size());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void getActiveScheduleOfUser() {
User referenceUser_1 = entityManager.find(User.class, 1L);
User referenceUser_2 = entityManager.find(User.class, 2L);
Optional<AbstractSchedule> result_1 = scheduleRepository.getActiveScheduleOfUser(referenceUser_2.getUsername());
assertTrue(result_1.isEmpty());
Optional<AbstractSchedule> result_2 = scheduleRepository.getActiveScheduleOfUser(referenceUser_1.getUsername());
assertTrue(result_2.isPresent());
}
}

View File

@ -1,6 +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', '2023-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);

View File

@ -1439,6 +1439,7 @@ paths:
- schedule
description: get all schedules of today
summary: get today's schedules
operationId: scheduleTaskNow
parameters:
- name: date
in: path