Merge remote-tracking branch 'origin/feature_taskList_m2' into feature_gardenplan-model_M2
This commit is contained in:
		
						commit
						52b4b1c01d
					
				| 
						 | 
					@ -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<Task> 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<Task> getTaskList() throws IOException {
 | 
				
			||||||
 | 
					        return getFilteredTaskList(LocalDate.MIN, LocalDate.MAX);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<Task> getFutureTasks() throws IOException {
 | 
				
			||||||
 | 
					        return getFilteredTaskList(LocalDate.now(), LocalDate.MAX);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<Task> getPastTasks() throws IOException {
 | 
				
			||||||
 | 
					        return getFilteredTaskList(LocalDate.MIN, LocalDate.now());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public List<Task>[] getTasksUpcomingWeek() throws IOException {
 | 
				
			||||||
 | 
					        List<Task>[] 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<Task> getFilteredTaskList(LocalDate start, LocalDate end) throws IOException {
 | 
				
			||||||
 | 
					        return getSortedTaskList(taskDatabase.getTaskList(start, end), sortByStartDate);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private List<Task> getSortedTaskList(List<Task> taskList, Comparator<Task> comparator) {
 | 
				
			||||||
 | 
					        return taskList.stream().sorted(comparator).collect(Collectors.toList());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue