issue-77 #105

Merged
sebastian merged 17 commits from issue-77 into master 2024-03-16 10:12:35 +01:00
4 changed files with 81 additions and 13 deletions
Showing only changes of commit 7d24ed1229 - Show all commits

View File

@ -21,15 +21,10 @@ public class TaskSeriesController {
@Autowired private TaskService taskService; @Autowired private TaskService taskService;
@Autowired private TaskSeriesService taskSeriesService; @Autowired private TaskSeriesService taskSeriesService;
@PostMapping("/tasks/{taskID}/taskseries/weekly") @PostMapping("/tasks/taskseries/weekly")
public ResponseEntity<?> onCreateTaskSeries(@PathVariable long taskID, @Valid @RequestBody TaskRepeatWeekInfo taskRepeatWeekInfo) { public ResponseEntity<?> onCreateTaskSeries(@Valid @RequestBody TaskRepeatWeekInfo taskRepeatWeekInfo) {
var taskPermission = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName()); ServiceExitCode serviceExitCode = taskSeriesService.createTaskSeries(taskRepeatWeekInfo);
if(taskPermission.hasIssue()) { return serviceExitCode.mapToResponseEntity();
return taskPermission.mapToResponseEntity();
} else {
ServiceExitCode serviceExitCode = taskSeriesService.createTaskSeries(taskPermission.getResult(), taskRepeatWeekInfo);
return serviceExitCode.mapToResponseEntity();
}
} }
@PostMapping("/tasks/{taskID}/taskseries/daily") @PostMapping("/tasks/{taskID}/taskseries/daily")

View File

@ -1,9 +1,37 @@
package core.api.models.timemanager.tasks.repeatinginfo; package core.api.models.timemanager.tasks.repeatinginfo;
import core.api.models.timemanager.tasks.TaskEntityInfo;
import core.entities.timemanager.Task;
import java.time.DayOfWeek;
public class TaskRepeatWeekDayInfo { public class TaskRepeatWeekDayInfo {
private int weekday; private DayOfWeek dayOfWeek;
private int offset; private int offset;
private long taskID;
public DayOfWeek getDayOfWeek() {
return dayOfWeek;
}
public void setDayOfWeek(DayOfWeek dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public long getTaskID() {
return taskID;
}
public void setTaskID(long taskID) {
this.taskID = taskID;
}
} }

View File

@ -1,5 +1,6 @@
package core.api.models.timemanager.tasks.repeatinginfo; package core.api.models.timemanager.tasks.repeatinginfo;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
import java.time.LocalDate; import java.time.LocalDate;
@ -9,10 +10,11 @@ public class TaskRepeatWeekInfo {
@Length(min = 1, max = 7) @Length(min = 1, max = 7)
private List<TaskRepeatWeekDayInfo> weekDayInfos; private List<TaskRepeatWeekDayInfo> weekDayInfos;
private DeadlineStrategy deadlineStrategy; private DeadlineStrategy deadlineStrategy;
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private LocalDate endDate; private LocalDate endDate;
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private LocalDate startDate;
public List<TaskRepeatWeekDayInfo> getWeekDayInfos() { public List<TaskRepeatWeekDayInfo> getWeekDayInfos() {
return weekDayInfos; return weekDayInfos;

View File

@ -2,6 +2,7 @@ package core.services;
import core.api.models.timemanager.tasks.repeatinginfo.DeadlineStrategy; import core.api.models.timemanager.tasks.repeatinginfo.DeadlineStrategy;
import core.api.models.timemanager.tasks.repeatinginfo.TaskRepeatDayInfo; import core.api.models.timemanager.tasks.repeatinginfo.TaskRepeatDayInfo;
import core.api.models.timemanager.tasks.repeatinginfo.TaskRepeatWeekDayInfo;
import core.api.models.timemanager.tasks.repeatinginfo.TaskRepeatWeekInfo; import core.api.models.timemanager.tasks.repeatinginfo.TaskRepeatWeekInfo;
import core.entities.timemanager.Task; import core.entities.timemanager.Task;
import core.entities.timemanager.TaskSerie; import core.entities.timemanager.TaskSerie;
@ -13,10 +14,12 @@ import core.repositories.timemanager.TaskSeriesRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class TaskSeriesService { public class TaskSeriesService {
@ -26,8 +29,48 @@ public class TaskSeriesService {
@Autowired private TaskSerieItemRepository taskSerieItemRepository; @Autowired private TaskSerieItemRepository taskSerieItemRepository;
public ServiceExitCode createTaskSeries(TaskRepeatWeekInfo taskRepeatInfo) {
List<Task> createdTasks = new ArrayList<>();
TaskSerie taskSerie = new TaskSerie();
for(TaskRepeatWeekDayInfo taskRepeatDayInfo : taskRepeatInfo.getWeekDayInfos()) {
Optional<Task> task = taskRepository.findById(taskRepeatDayInfo.getTaskID());
if(task.isEmpty()) return ServiceExitCode.MISSING_ENTITY;
LocalDate currentTaskDate = task.get().getStartDate().plusDays(taskRepeatDayInfo.getOffset());
while(currentTaskDate.isBefore(taskRepeatInfo.getEndDate())) {
Task clonedTask = Task.cloneTask(task.get());
clonedTask.setStartDate(currentTaskDate);
TaskSerieItem taskSerieItem = taskSerie.addTask(clonedTask);
clonedTask.setTaskSerieItem(taskSerieItem);
createdTasks.add(clonedTask);
}
}
taskSerie.getTasks().sort(Comparator.comparing(o -> o.getTask().getStartDate()));
for(int i=0; i<taskSerie.getTasks().size(); i++) {
taskSerie.getTasks().get(i).setSeriesIndex(i);
if(taskRepeatInfo.getDeadlineStrategy() == DeadlineStrategy.DEADLINE_EQUAL_START) {
taskSerie.getTasks().get(i).getTask().setDeadline(taskSerie.getTasks().get(i).getTask().getStartDate());
} else {
if(i + 1 == taskSerie.getTasks().size()) {
int firstWeekDayIndex = i % taskRepeatInfo.getWeekDayInfos().size();
LocalDate reference_start = taskSerie.getTasks().get(firstWeekDayIndex).getTask().getStartDate();
LocalDate reference_deadline = taskSerie.getTasks().get(firstWeekDayIndex).getTask().getDeadline();
Duration duration = Duration.between(reference_start, reference_deadline);
long days = duration.toDays();
taskSerie.getTasks().get(i).getTask().setDeadline(taskSerie.getTasks().get(i).getTask().getStartDate().plusDays(days));
} else {
taskSerie.getTasks().get(i).getTask().setDeadline(taskSerie.getTasks().get(i+1).getTask().getStartDate().minusDays(1));
}
}
}
taskSeriesRepository.save(taskSerie);
taskRepository.saveAll(createdTasks);
taskSerieItemRepository.saveAll(taskSerie.getTasks());
public ServiceExitCode createTaskSeries(Task rootTask, TaskRepeatWeekInfo taskRepeatInfo) {
return ServiceExitCode.OK; return ServiceExitCode.OK;
} }