schedule-refactor #45
@ -2,10 +2,7 @@ package core.api.controller;
|
|||||||
|
|
||||||
|
|
||||||
import core.api.models.auth.SimpleStatusResponse;
|
import core.api.models.auth.SimpleStatusResponse;
|
||||||
import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo;
|
import core.api.models.timemanager.taskSchedule.*;
|
||||||
import core.api.models.timemanager.taskSchedule.BasicScheduleInfo;
|
|
||||||
import core.api.models.timemanager.taskSchedule.ScheduleFieldInfo;
|
|
||||||
import core.api.models.timemanager.taskSchedule.ScheduleInfo;
|
|
||||||
import core.entities.timemanager.AbstractSchedule;
|
import core.entities.timemanager.AbstractSchedule;
|
||||||
import core.entities.timemanager.BasicTaskSchedule;
|
import core.entities.timemanager.BasicTaskSchedule;
|
||||||
import core.entities.timemanager.Task;
|
import core.entities.timemanager.Task;
|
||||||
@ -133,4 +130,14 @@ public class ScheduleController {
|
|||||||
return ResponseEntity.ok(scheduleResult.getResult().toScheduleInfo());
|
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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import core.entities.timemanager.Task;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class BasicScheduleInfo extends ScheduleInfo{
|
public class BasicScheduleInfo extends ScheduleInfo{
|
||||||
@ -14,5 +15,15 @@ public class BasicScheduleInfo extends ScheduleInfo{
|
|||||||
super(scheduleID, scheduleType, startTime, stopTime, activeMinutes, task, taskgroupPath);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,14 @@ import org.springframework.data.repository.CrudRepository;
|
|||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Long> {
|
public interface ScheduleRepository extends CrudRepository<AbstractSchedule, Long> {
|
||||||
|
|
||||||
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1")
|
@Query(value = "SELECT s FROM AbstractSchedule s WHERE s.task.taskgroup.user.username = ?1")
|
||||||
List<AbstractSchedule> findAllByUsername(String username);
|
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);
|
||||||
}
|
}
|
||||||
|
@ -94,4 +94,9 @@ public class TaskScheduleService {
|
|||||||
return new ServiceResult<>(basicTaskSchedule);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package core.schedules;
|
package core.schedules;
|
||||||
|
|
||||||
import core.entities.User;
|
import core.entities.User;
|
||||||
|
import core.entities.timemanager.AbstractSchedule;
|
||||||
import core.repositories.timemanager.ScheduleRepository;
|
import core.repositories.timemanager.ScheduleRepository;
|
||||||
|
import core.services.ServiceResult;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.jdbc.SqlGroup;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
@ExtendWith(SpringExtension.class)
|
@ExtendWith(SpringExtension.class)
|
||||||
@DataJpaTest
|
@DataJpaTest
|
||||||
@ -32,4 +37,21 @@ public class ScheduleRepositoryTest {
|
|||||||
assertEquals(0, scheduleRepository.findAllByUsername(referenceUser_2.getUsername()).size());
|
assertEquals(0, scheduleRepository.findAllByUsername(referenceUser_2.getUsername()).size());
|
||||||
assertEquals(5, scheduleRepository.findAllByUsername(referenceUser_1.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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
INSERT INTO abstract_schedule (schedule_type, scheduleid, start_time, stop_time, schedule_date, task, schedule_end, schedule_start)
|
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),
|
VALUES (0, 1, null, null, '2024-11-11', 1, null, null),
|
||||||
(0, 2, null, null, '2024-11-11', 2, 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, 4, '2023-10-10', null, '2024-11-11', 3, null, null),
|
||||||
(0, 5, null, null, '2024-11-11', 5, null, null);
|
(0, 5, null, null, '2024-11-11', 5, null, null);
|
@ -1439,6 +1439,7 @@ paths:
|
|||||||
- schedule
|
- schedule
|
||||||
description: get all schedules of today
|
description: get all schedules of today
|
||||||
summary: get today's schedules
|
summary: get today's schedules
|
||||||
|
operationId: scheduleTaskNow
|
||||||
parameters:
|
parameters:
|
||||||
- name: date
|
- name: date
|
||||||
in: path
|
in: path
|
||||||
|
Loading…
Reference in New Issue
Block a user