schedule-refactor #45

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

View File

@ -155,4 +155,19 @@ public class ScheduleController {
ServiceResult<AbstractSchedule> serviceResult = taskScheduleService.activateSchedule(permissionResult.getResult());
return ResponseEntity.ok(new ScheduleActivateResponse(serviceResult.getResult().getStartTime()));
}
@PostMapping("schedules/{scheduleID}/stop/{finish}")
public ResponseEntity<?> stopSchedule(@PathVariable long scheduleID, @PathVariable boolean finish) {
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.stopSchedule(permissionResult.getResult(), finish);
return ResponseEntity.ok(new TaskScheduleStopResponse(serviceResult.getResult().getActiveTime()));
}
}

View File

@ -0,0 +1,18 @@
package core.api.models.timemanager.taskSchedule;
public class TaskScheduleStopResponse {
private int activeTime;
public TaskScheduleStopResponse(int activeTime) {
this.activeTime = activeTime;
}
public int getActiveTime() {
return activeTime;
}
public void setActiveTime(int activeTime) {
this.activeTime = activeTime;
}
}

View File

@ -3,6 +3,7 @@ package core.entities.timemanager;
import core.api.models.timemanager.taskSchedule.ScheduleInfo;
import javax.persistence.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Objects;
@ -91,4 +92,18 @@ public abstract class AbstractSchedule {
public boolean isActive() {
return startTime != null && stopTime == null;
}
public int getActiveTime() {
if(startTime == null) {
return 0;
} else if(stopTime == null) {
return (int) Duration.between(startTime, LocalDateTime.now()).toMinutes();
} else {
return (int) Duration.between(startTime, stopTime).toMinutes();
}
}
public boolean isStartable() {
return startTime == null;
}
}

View File

@ -3,6 +3,7 @@ package core.entities.timemanager;
import javax.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@ -153,11 +154,21 @@ public class Task {
}
public boolean hasActiveSchedule() {
for(AbstractSchedule schedule : getBasicTaskSchedules()) {
if(schedule.isActive()) {
for (AbstractSchedule schedule : getBasicTaskSchedules()) {
if (schedule.isActive()) {
return true;
}
}
return false;
}
public List<AbstractSchedule> getStartableSchedules() {
List<AbstractSchedule> abstractSchedules = new ArrayList<>();
for(AbstractSchedule schedule : getBasicTaskSchedules()) {
if(schedule.isStartable()) {
abstractSchedules.add(schedule);
}
}
return abstractSchedules;
}
}

View File

@ -1,20 +1,17 @@
package core.services;
import core.api.models.timemanager.taskSchedule.BasicScheduleFieldInfo;
import core.api.models.timemanager.taskSchedule.BasicScheduleInfo;
import core.api.models.timemanager.taskSchedule.ScheduleFieldInfo;
import core.entities.User;
import core.entities.timemanager.AbstractSchedule;
import core.entities.timemanager.BasicTaskSchedule;
import core.entities.timemanager.Task;
import core.repositories.UserRepository;
import core.repositories.timemanager.ScheduleRepository;
import core.repositories.timemanager.TaskRepository;
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;
import java.util.Optional;
@ -24,6 +21,8 @@ public class TaskScheduleService {
@Autowired private ScheduleRepository scheduleRepository;
@Autowired private UserRepository userRepository;
@Autowired
private TaskRepository taskRepository;
public List<AbstractSchedule> getAllSchedulesOfUser(String username) {
return scheduleRepository.findAllByUsername(username);
@ -107,4 +106,20 @@ public class TaskScheduleService {
}
return new ServiceResult<>(schedule);
}
public ServiceResult<AbstractSchedule> stopSchedule(AbstractSchedule schedule, boolean finish) {
if(schedule.getStartTime() != null && schedule.getStopTime() == null) {
schedule.setStopTime(LocalDateTime.now());
scheduleRepository.save(schedule);
}
if(finish) {
schedule.getTask().setFinished(true);
taskRepository.save(schedule.getTask());
List<AbstractSchedule> startableSchedules = schedule.getTask().getStartableSchedules();
scheduleRepository.deleteAll(startableSchedules);
}
return new ServiceResult<>(schedule);
}
}

View File

@ -178,4 +178,29 @@ public class ScheduleServiceTest {
assertNotNull(result_2.getResult().getStartTime());
assertNull(result_2.getResult().getStopTime());
}
@Test
@SqlGroup({
@Sql("classpath:taskgroupRepositoryTestEntries.sql"),
@Sql("classpath:taskRepositoryEntries.sql"),
@Sql("classpath:basicScheduleEntries.sql")
})
void stopSchedule() {
//Stop schedule that is not running 4
ServiceResult<AbstractSchedule> result_1 = taskScheduleService.stopSchedule(entityManager.find(BasicTaskSchedule.class, 5L), false);
assertNull(result_1.getResult().getStartTime());
assertNull(result_1.getResult().getStopTime());
//Stop schedule (without finishing)
ServiceResult<AbstractSchedule> result_2 = taskScheduleService.stopSchedule(entityManager.find(BasicTaskSchedule.class, 4L), false);
assertNotNull(result_2.getResult().getStartTime());
assertNotNull(result_2.getResult().getStopTime());
//Stop schedule with finishing
ServiceResult<AbstractSchedule> result_3 = taskScheduleService.stopSchedule(entityManager.find(BasicTaskSchedule.class, 7L), true);
assertNotNull(result_3.getResult().getStartTime());
assertNotNull(result_3.getResult().getStopTime());
assertTrue(result_3.getResult().getTask().isFinished());
assertFalse(result_3.getResult().isStartable());
}
}

View File

@ -3,4 +3,6 @@ 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),
(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),
(0, 6, '2023-10-10', null, '2024-11-11', 16, null, null),
(0, 7, '2023-10-10', null, '2024-11-11', 16, null, null);

View File

@ -17,3 +17,5 @@ INSERT INTO tasks (taskid, deadline, eta, start_date, task_name, taskgroup_id, f
INSERT INTO tasks (taskid, deadline, eta, start_date, task_name, taskgroup_id, finished, work_time) VALUES (13, NULL, 0, '2022-03-20', 'Task 6', 2, true, 0);
INSERT INTO tasks (taskid, deadline, eta, start_date, task_name, taskgroup_id, finished, work_time) VALUES (14, '2044-03-20', 0, '2022-03-20', 'Task 6', 2, true, 0);
INSERT INTO tasks (taskid, deadline, eta, start_date, task_name, taskgroup_id, finished, work_time) VALUES (15, NULL, 0, NULL, 'Task 15', 2, false, 0);
INSERT INTO tasks (taskid, deadline, eta, start_date, task_name, taskgroup_id, finished, work_time) VALUES (16, NULL, 0, NULL, 'Task 15', 9, false, 0);

View File

@ -1,5 +1,6 @@
INSERT INTO users (id, username, email, password) VALUES (1, 'Testuser1', 'test1@test.com', '123456');
INSERT INTO users (id, username, email, password) VALUES (2, 'Testuser2', 'test2@test.com', '123456');
INSERT INTO users (id, username, email, password) VALUES (3, 'Testuser3', 'test3@test.com', '123456');
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (1, 'No children', null, 1);
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (2, 'Taskgroup 1', null, 1);
@ -9,4 +10,6 @@ INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (4,
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (5, 'Taskgroup 2', null, 1);
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (6, 'Taskgroup 2.1', 5, 1);
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (7, 'Taskgroup 2.2', 5, 1);
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (8, 'Taskgroup 2.1.2', 6, 1);
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (8, 'Taskgroup 2.1.2', 6, 1);
INSERT INTO taskgroups (taskgroupid, name, parent_id, taskgroupuser) VALUES (9, 'Taskgroup 1', null, 3);