diff --git a/src/main/java/ru/ulstu/students/controller/TaskController.java b/src/main/java/ru/ulstu/students/controller/TaskController.java index 2dd23b5..5d0f4e8 100644 --- a/src/main/java/ru/ulstu/students/controller/TaskController.java +++ b/src/main/java/ru/ulstu/students/controller/TaskController.java @@ -45,7 +45,7 @@ public class TaskController { @GetMapping("/tasks") public void getTask(ModelMap modelMap) { - modelMap.put("filteredTasks", new TaskFilterDto(taskService.findAllDto(), null, null)); + modelMap.put("filteredTasks", new TaskFilterDto(taskService.findAllDto(), null, null, null)); } @GetMapping("/task") @@ -61,7 +61,8 @@ public class TaskController { public void filterTasks(@Valid TaskFilterDto taskFilterDto, ModelMap modelMap) { modelMap.put("filteredTasks", new TaskFilterDto(taskService.filter(taskFilterDto), taskFilterDto.getStatus(), - taskFilterDto.getTag())); + taskFilterDto.getTag(), + taskFilterDto.getOrder())); } @PostMapping(value = "/task", params = "save") diff --git a/src/main/java/ru/ulstu/students/model/TaskFilterDto.java b/src/main/java/ru/ulstu/students/model/TaskFilterDto.java index d323911..21bd5ac 100644 --- a/src/main/java/ru/ulstu/students/model/TaskFilterDto.java +++ b/src/main/java/ru/ulstu/students/model/TaskFilterDto.java @@ -7,11 +7,13 @@ public class TaskFilterDto { private List tasks; private Task.TaskStatus status; private Integer tagId; + private String order; - public TaskFilterDto(List tasks, Task.TaskStatus status, Integer tagId) { + public TaskFilterDto(List tasks, Task.TaskStatus status, Integer tagId, String order) { this.tasks = tasks; this.status = status; this.tagId = tagId; + this.order = order; } public TaskFilterDto() { @@ -41,4 +43,12 @@ public class TaskFilterDto { public void setTag(Integer tagId) { this.tagId = tagId; } + + public String getOrder() { + return order; + } + + public void setOrder(String order) { + this.order = order; + } } diff --git a/src/main/java/ru/ulstu/students/repository/TaskRepository.java b/src/main/java/ru/ulstu/students/repository/TaskRepository.java index 83121ee..ee49ab8 100644 --- a/src/main/java/ru/ulstu/students/repository/TaskRepository.java +++ b/src/main/java/ru/ulstu/students/repository/TaskRepository.java @@ -10,6 +10,9 @@ 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); + @Query("SELECT t FROM Task t WHERE (t.status = :status OR :status IS NULL) AND (:tag IS NULL OR :tag MEMBER OF t.tags) ORDER BY create_date DESC") + List filterNew(@Param("status") Task.TaskStatus status, @Param("tag") Tag tag); + + @Query("SELECT t FROM Task t WHERE (t.status = :status OR :status IS NULL) AND (:tag IS NULL OR :tag MEMBER OF t.tags) ORDER BY create_date ASC") + List filterOld(@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 9b3b9c4..a8747c7 100644 --- a/src/main/java/ru/ulstu/students/service/TaskService.java +++ b/src/main/java/ru/ulstu/students/service/TaskService.java @@ -1,6 +1,7 @@ package ru.ulstu.students.service; import org.apache.commons.lang3.StringUtils; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.deadline.service.DeadlineService; @@ -29,15 +30,15 @@ public class TaskService { private final DeadlineService deadlineService; private final TagService tagService; - public TaskService(TaskRepository grantRepository, + public TaskService(TaskRepository taskRepository, DeadlineService deadlineService, TagService tagService) { - this.taskRepository = grantRepository; + this.taskRepository = taskRepository; this.deadlineService = deadlineService; this.tagService = tagService; } public List findAll() { - return taskRepository.findAll(); + return taskRepository.findAll(new Sort(Sort.Direction.DESC, "createDate")); } public List findAllDto() { @@ -51,9 +52,15 @@ public class TaskService { } public List filter(TaskFilterDto filterDto) { - return convert(taskRepository.filter( - filterDto.getStatus(), - filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new); + if (filterDto.getOrder().compareTo("new") == 0) { + return convert(taskRepository.filterNew( + filterDto.getStatus(), + filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new); + } else { + return convert(taskRepository.filterOld( + filterDto.getStatus(), + filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new); + } } @Transactional diff --git a/src/main/resources/public/css/tasks.css b/src/main/resources/public/css/tasks.css index d61d7da..7e350ed 100644 --- a/src/main/resources/public/css/tasks.css +++ b/src/main/resources/public/css/tasks.css @@ -24,6 +24,10 @@ line-height: 25px; } +.sorting .bootstrap-select{ + margin-bottom: 10px; +} + .input-tag-name { border: none; box-shadow: none; diff --git a/src/main/resources/templates/students/tasks.html b/src/main/resources/templates/students/tasks.html index 2fec161..72c4194 100644 --- a/src/main/resources/templates/students/tasks.html +++ b/src/main/resources/templates/students/tasks.html @@ -25,6 +25,19 @@
+
+
Сортировать:
+ +
Фильтр: