diff --git a/src/main/java/ru/ulstu/students/controller/TaskController.java b/src/main/java/ru/ulstu/students/controller/TaskController.java index ef602e2..e231d39 100644 --- a/src/main/java/ru/ulstu/students/controller/TaskController.java +++ b/src/main/java/ru/ulstu/students/controller/TaskController.java @@ -7,7 +7,9 @@ import org.springframework.web.bind.annotation.*; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.students.model.Task; import ru.ulstu.students.model.TaskDto; +import ru.ulstu.students.model.TaskFilterDto; import ru.ulstu.students.service.TaskService; +import ru.ulstu.tags.model.Tag; import springfox.documentation.annotations.ApiIgnore; import javax.validation.Valid; @@ -29,23 +31,21 @@ public class TaskController { this.taskService = taskService; } - @GetMapping("/tasks") - public void getTasks(ModelMap modelMap) { - modelMap.put("tasks", taskService.findAllDto()); - } - @GetMapping("/dashboard") public void getDashboard(ModelMap modelMap) { modelMap.put("tasks", taskService.findAllDto()); } - @GetMapping("/task") - public void getTask(ModelMap modelMap, @RequestParam(value = "id") Integer id) { - if (id != null && id > 0) { - modelMap.put("taskDto", taskService.findOneDto(id)); - } else { - modelMap.put("taskDto", new TaskDto()); - } + @GetMapping("/tasks") + public void getTask(ModelMap modelMap) { + modelMap.put("filteredTasks", new TaskFilterDto(taskService.findAllDto(), null, null)); + } + + @PostMapping("/tasks") + public void filterTasks(@Valid TaskFilterDto taskFilterDto, ModelMap modelMap) { + modelMap.put("filteredTasks", new TaskFilterDto(taskService.filter(taskFilterDto), + taskFilterDto.getStatus(), + taskFilterDto.getTag())); } @PostMapping(value = "/task", params = "save") @@ -83,6 +83,11 @@ public class TaskController { return taskService.getTaskStatuses(); } + @ModelAttribute("allTags") + public List getTags() { + return taskService.getTags(); + } + private void filterEmptyDeadlines(TaskDto taskDto) { taskDto.setDeadlines(taskDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/students/model/Task.java b/src/main/java/ru/ulstu/students/model/Task.java index be1b1c6..17a6236 100644 --- a/src/main/java/ru/ulstu/students/model/Task.java +++ b/src/main/java/ru/ulstu/students/model/Task.java @@ -38,7 +38,7 @@ public class Task extends BaseEntity { private String description; @Enumerated(value = EnumType.STRING) - private ru.ulstu.students.model.Task.TaskStatus status = TaskStatus.IN_WORK; + private TaskStatus status = TaskStatus.IN_WORK; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "task_id", unique = true) diff --git a/src/main/java/ru/ulstu/students/model/TaskDto.java b/src/main/java/ru/ulstu/students/model/TaskDto.java index 941a6df..8d52e01 100644 --- a/src/main/java/ru/ulstu/students/model/TaskDto.java +++ b/src/main/java/ru/ulstu/students/model/TaskDto.java @@ -26,7 +26,7 @@ public class TaskDto { private Date createDate; private Date updateDate; private Set tagIds; - private List tags; + private List tags = new ArrayList<>(); public TaskDto() { deadlines.add(new Deadline()); diff --git a/src/main/java/ru/ulstu/students/model/TaskFilterDto.java b/src/main/java/ru/ulstu/students/model/TaskFilterDto.java new file mode 100644 index 0000000..d323911 --- /dev/null +++ b/src/main/java/ru/ulstu/students/model/TaskFilterDto.java @@ -0,0 +1,44 @@ +package ru.ulstu.students.model; + +import java.util.List; + +public class TaskFilterDto { + + private List tasks; + private Task.TaskStatus status; + private Integer tagId; + + public TaskFilterDto(List tasks, Task.TaskStatus status, Integer tagId) { + this.tasks = tasks; + this.status = status; + this.tagId = tagId; + } + + public TaskFilterDto() { + + } + + public List getTasks() { + return tasks; + } + + public void setTasks(List tasks) { + this.tasks = tasks; + } + + public Task.TaskStatus getStatus() { + return status; + } + + public void setStatus(Task.TaskStatus status) { + this.status = status; + } + + public Integer getTag() { + return tagId; + } + + public void setTag(Integer tagId) { + this.tagId = tagId; + } +} diff --git a/src/main/java/ru/ulstu/students/repository/TaskRepository.java b/src/main/java/ru/ulstu/students/repository/TaskRepository.java index 6f48d1f..83121ee 100644 --- a/src/main/java/ru/ulstu/students/repository/TaskRepository.java +++ b/src/main/java/ru/ulstu/students/repository/TaskRepository.java @@ -1,7 +1,15 @@ package ru.ulstu.students.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import ru.ulstu.students.model.Task; +import ru.ulstu.tags.model.Tag; + +import java.util.List; public interface TaskRepository extends JpaRepository { + + @Query("SELECT t FROM Task t WHERE (t.status = :status OR :status IS NULL) AND (:tag IS NULL OR :tag MEMBER OF t.tags)") + List filter(@Param("status") Task.TaskStatus status, @Param("tag") Tag tag); } diff --git a/src/main/java/ru/ulstu/students/service/TaskService.java b/src/main/java/ru/ulstu/students/service/TaskService.java index d400fa6..9b3b9c4 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -6,7 +6,9 @@ import org.springframework.transaction.annotation.Transactional; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.students.model.Task; import ru.ulstu.students.model.TaskDto; +import ru.ulstu.students.model.TaskFilterDto; import ru.ulstu.students.repository.TaskRepository; +import ru.ulstu.tags.model.Tag; import ru.ulstu.tags.service.TagService; import java.io.IOException; @@ -48,6 +50,12 @@ public class TaskService { return new TaskDto(taskRepository.findOne(id)); } + public List filter(TaskFilterDto filterDto) { + return convert(taskRepository.filter( + filterDto.getStatus(), + filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new); + } + @Transactional public Integer create(TaskDto taskDto) throws IOException { Task newTask = copyFromDto(new Task(), taskDto); @@ -94,4 +102,8 @@ public class TaskService { return Arrays.asList(Task.TaskStatus.values()); } + public List getTags() { + return tagService.getTags(); + } + } diff --git a/src/main/java/ru/ulstu/tags/service/TagService.java b/src/main/java/ru/ulstu/tags/service/TagService.java index 4e88304..ba942f1 100644 --- a/src/main/java/ru/ulstu/tags/service/TagService.java +++ b/src/main/java/ru/ulstu/tags/service/TagService.java @@ -50,4 +50,12 @@ public class TagService { return newTag; } + public List getTags() { + return tagRepository.findAll(); + } + + public Tag findById(Integer tagId) { + return tagRepository.findOne(tagId); + } + } diff --git a/src/main/resources/templates/students/tasks.html b/src/main/resources/templates/students/tasks.html index d3401cd..daa689d 100644 --- a/src/main/resources/templates/students/tasks.html +++ b/src/main/resources/templates/students/tasks.html @@ -20,27 +20,28 @@
- +
Фильтр:
- + -