Resolve "Фильтр задач по типу (тегам)" #188

Merged
ashette merged 1 commits from 76-students-tags-filter into dev 2019-04-24 13:36:06 +04:00
6 changed files with 49 additions and 11 deletions
Showing only changes of commit 7248a84c30 - Show all commits

View File

@ -45,7 +45,7 @@ public class TaskController {
@GetMapping("/tasks") @GetMapping("/tasks")
public void getTask(ModelMap modelMap) { 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") @GetMapping("/task")
@ -61,7 +61,8 @@ public class TaskController {
public void filterTasks(@Valid TaskFilterDto taskFilterDto, ModelMap modelMap) { public void filterTasks(@Valid TaskFilterDto taskFilterDto, ModelMap modelMap) {
modelMap.put("filteredTasks", new TaskFilterDto(taskService.filter(taskFilterDto), modelMap.put("filteredTasks", new TaskFilterDto(taskService.filter(taskFilterDto),
taskFilterDto.getStatus(), taskFilterDto.getStatus(),
taskFilterDto.getTag())); taskFilterDto.getTag(),
taskFilterDto.getOrder()));
} }
@PostMapping(value = "/task", params = "save") @PostMapping(value = "/task", params = "save")

View File

@ -7,11 +7,13 @@ public class TaskFilterDto {
private List<TaskDto> tasks; private List<TaskDto> tasks;
private Task.TaskStatus status; private Task.TaskStatus status;
private Integer tagId; private Integer tagId;
private String order;
public TaskFilterDto(List<TaskDto> tasks, Task.TaskStatus status, Integer tagId) { public TaskFilterDto(List<TaskDto> tasks, Task.TaskStatus status, Integer tagId, String order) {
this.tasks = tasks; this.tasks = tasks;
this.status = status; this.status = status;
this.tagId = tagId; this.tagId = tagId;
this.order = order;
} }
public TaskFilterDto() { public TaskFilterDto() {
@ -41,4 +43,12 @@ public class TaskFilterDto {
public void setTag(Integer tagId) { public void setTag(Integer tagId) {
this.tagId = tagId; this.tagId = tagId;
} }
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
} }

View File

@ -10,6 +10,9 @@ import java.util.List;
public interface TaskRepository extends JpaRepository<Task, Integer> { public interface TaskRepository extends JpaRepository<Task, Integer> {
@Query("SELECT t FROM Task t WHERE (t.status = :status OR :status IS NULL) AND (:tag IS NULL OR :tag MEMBER OF t.tags)") @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<Task> filter(@Param("status") Task.TaskStatus status, @Param("tag") Tag tag); List<Task> 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<Task> filterOld(@Param("status") Task.TaskStatus status, @Param("tag") Tag tag);
} }

View File

@ -1,6 +1,7 @@
package ru.ulstu.students.service; package ru.ulstu.students.service;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
@ -29,15 +30,15 @@ public class TaskService {
private final DeadlineService deadlineService; private final DeadlineService deadlineService;
private final TagService tagService; private final TagService tagService;
public TaskService(TaskRepository grantRepository, public TaskService(TaskRepository taskRepository,
DeadlineService deadlineService, TagService tagService) { DeadlineService deadlineService, TagService tagService) {
this.taskRepository = grantRepository; this.taskRepository = taskRepository;
this.deadlineService = deadlineService; this.deadlineService = deadlineService;
this.tagService = tagService; this.tagService = tagService;
} }
public List<Task> findAll() { public List<Task> findAll() {
return taskRepository.findAll(); return taskRepository.findAll(new Sort(Sort.Direction.DESC, "createDate"));
} }
public List<TaskDto> findAllDto() { public List<TaskDto> findAllDto() {
@ -51,9 +52,15 @@ public class TaskService {
} }
public List<TaskDto> filter(TaskFilterDto filterDto) { public List<TaskDto> filter(TaskFilterDto filterDto) {
return convert(taskRepository.filter( if (filterDto.getOrder().compareTo("new") == 0) {
filterDto.getStatus(), return convert(taskRepository.filterNew(
filterDto.getTag() == null ? null : tagService.findById(filterDto.getTag())), TaskDto::new); 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 @Transactional

View File

@ -24,6 +24,10 @@
line-height: 25px; line-height: 25px;
} }
.sorting .bootstrap-select{
margin-bottom: 10px;
}
.input-tag-name { .input-tag-name {
border: none; border: none;
box-shadow: none; box-shadow: none;

View File

@ -25,6 +25,19 @@
</th:block> </th:block>
</div> </div>
<div class="col-md-3 col-sm-12"> <div class="col-md-3 col-sm-12">
<div class="sorting">
<h5>Сортировать:</h5>
<select class="form-control selectpicker" size="auto" th:field="${filteredTasks.order}"
id="order"
onchange="this.form.submit();">
<option th:value="new">
Сначала новые
</option>
<option th:value="old">
Сначала старые
</option>
</select>
</div>
<div class="filter"> <div class="filter">
<h5>Фильтр:</h5> <h5>Фильтр:</h5>
<select class="form-control selectpicker" size="auto" th:field="${filteredTasks.status}" <select class="form-control selectpicker" size="auto" th:field="${filteredTasks.status}"