diff --git a/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java new file mode 100644 index 0000000..7e158c4 --- /dev/null +++ b/src/main/java/ch/zhaw/gartenverwaltung/taskList/TaskListModel.java @@ -0,0 +1,64 @@ +package ch.zhaw.gartenverwaltung.taskList; + +import ch.zhaw.gartenverwaltung.io.JsonTaskDatabase; +import ch.zhaw.gartenverwaltung.io.TaskDatabase; +import ch.zhaw.gartenverwaltung.types.Task; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class TaskListModel { + private TaskDatabase taskDatabase; + + static final Comparator sortByStartDate = Comparator.comparing(Task::getStartDate); + + + + public TaskListModel(){ + taskDatabase = new JsonTaskDatabase(); + } + + public TaskListModel(TaskDatabase taskDatabase) { + this.taskDatabase = taskDatabase; + } + + public void addTask(Task task) throws IOException { + taskDatabase.saveTask(task); + } + + public void removeTask(Task task) throws IOException { + taskDatabase.removeTask(task); + } + + public List getTaskList() throws IOException { + return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX); + } + + public List getFutureTasks() throws IOException { + return getFilteredTaskList(LocalDate.now(), LocalDate.MAX); + } + + public List getPastTasks() throws IOException { + return getFilteredTaskList(LocalDate.MIN, LocalDate.now()); + } + + public List[] getTasksUpcomingWeek() throws IOException { + List[] listArray = new List[7]; + for(int i = 0; i < 7; i++) { + LocalDate date = LocalDate.now().plusDays(i); + listArray[i] = taskDatabase.getTaskList(date, date); + } + return listArray; + } + + public List getFilteredTaskList(LocalDate start, LocalDate end) throws IOException { + return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate); + } + + private List getSortedTaskList(List taskList, Comparator comparator) { + return taskList.stream().sorted(comparator).collect(Collectors.toList()); + } +}