issue-77 #105
@ -21,16 +21,11 @@ public class TaskSeriesController {
|
||||
@Autowired private TaskService taskService;
|
||||
@Autowired private TaskSeriesService taskSeriesService;
|
||||
|
||||
@PostMapping("/tasks/{taskID}/taskseries/weekly")
|
||||
public ResponseEntity<?> onCreateTaskSeries(@PathVariable long taskID, @Valid @RequestBody TaskRepeatWeekInfo taskRepeatWeekInfo) {
|
||||
var taskPermission = taskService.getTaskPermissions(taskID, SecurityContextHolder.getContext().getAuthentication().getName());
|
||||
if(taskPermission.hasIssue()) {
|
||||
return taskPermission.mapToResponseEntity();
|
||||
} else {
|
||||
ServiceExitCode serviceExitCode = taskSeriesService.createTaskSeries(taskPermission.getResult(), taskRepeatWeekInfo);
|
||||
@PostMapping("/tasks/taskseries/weekly")
|
||||
public ResponseEntity<?> onCreateTaskSeries(@Valid @RequestBody TaskRepeatWeekInfo taskRepeatWeekInfo) {
|
||||
ServiceExitCode serviceExitCode = taskSeriesService.createTaskSeries(taskRepeatWeekInfo);
|
||||
return serviceExitCode.mapToResponseEntity();
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/tasks/{taskID}/taskseries/daily")
|
||||
public ResponseEntity<?> onCreateTaskSeries(@PathVariable long taskID, @Valid @RequestBody TaskRepeatDayInfo taskRepeatDayInfo) {
|
||||
|
@ -1,9 +1,37 @@
|
||||
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 {
|
||||
|
||||
private int weekday;
|
||||
private DayOfWeek dayOfWeek;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package core.api.models.timemanager.tasks.repeatinginfo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@ -9,10 +10,11 @@ public class TaskRepeatWeekInfo {
|
||||
|
||||
@Length(min = 1, max = 7)
|
||||
private List<TaskRepeatWeekDayInfo> weekDayInfos;
|
||||
|
||||
private DeadlineStrategy deadlineStrategy;
|
||||
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
|
||||
private LocalDate endDate;
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
|
||||
private LocalDate startDate;
|
||||
|
||||
public List<TaskRepeatWeekDayInfo> getWeekDayInfos() {
|
||||
return weekDayInfos;
|
||||
|
@ -2,6 +2,7 @@ package core.services;
|
||||
|
||||
import core.api.models.timemanager.tasks.repeatinginfo.DeadlineStrategy;
|
||||
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.entities.timemanager.Task;
|
||||
import core.entities.timemanager.TaskSerie;
|
||||
@ -13,10 +14,12 @@ import core.repositories.timemanager.TaskSeriesRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class TaskSeriesService {
|
||||
@ -26,8 +29,48 @@ public class TaskSeriesService {
|
||||
@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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user