From 1756f29505810ea8c65bd9ff41d7ae705f1f78da Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Sat, 13 Apr 2019 12:39:53 +0400 Subject: [PATCH] #42 filter authors by leader and degree --- .../grant/controller/GrantController.java | 33 ++++-------- .../java/ru/ulstu/grant/model/GrantDto.java | 38 ++++++++++++- .../ulstu/grant/model/GrantUserFilterDto.java | 54 ------------------- .../grant/repository/GrantRepository.java | 3 -- .../ru/ulstu/grant/service/GrantService.java | 14 ++--- .../resources/templates/grants/grant.html | 25 ++++----- 6 files changed, 61 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/ru/ulstu/grant/model/GrantUserFilterDto.java diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 638e013..82fc80e 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -12,7 +12,6 @@ import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; -import ru.ulstu.grant.model.GrantUserFilterDto; import ru.ulstu.grant.service.GrantService; import ru.ulstu.user.model.User; import springfox.documentation.annotations.ApiIgnore; @@ -26,7 +25,6 @@ import static org.springframework.util.StringUtils.isEmpty; import static ru.ulstu.core.controller.Navigation.GRANTS_PAGE; import static ru.ulstu.core.controller.Navigation.GRANT_PAGE; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; -import static ru.ulstu.core.controller.Navigation.hasErrors; @Controller() @@ -50,30 +48,23 @@ public class GrantController { } @GetMapping("/grant") - public void getGrant(ModelMap modelMap, GrantUserFilterDto grantUserFilterDto, @RequestParam(value = "id") Integer id) { + public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) { if (id != null && id > 0) { modelMap.put("grantDto", grantService.findOneDto(id)); } else { modelMap.put("grantDto", new GrantDto()); } - modelMap.put("filteredAuthors", new GrantUserFilterDto(grantService.filterUsers(grantUserFilterDto), false, false, false)); } @PostMapping(value = "/grant", params = "save") - public String save(@Valid GrantDto grantDto, GrantUserFilterDto grantUserFilterDto, Errors errors, ModelMap modelMap /*@ModelAttribute List allAuthors*/) + public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { filterEmptyDeadlines(grantDto); - modelMap.put("filteredAuthors", new GrantUserFilterDto(grantService.filterUsers(grantUserFilterDto), - grantUserFilterDto.isLeader(), - grantUserFilterDto.isBirthDate(), - grantUserFilterDto.isDegree())); if (grantDto.getDeadlines().isEmpty()) { errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } if (grantDto.getLeaderId().equals(null)) { errors.rejectValue("leader", "errorCode", "Укажите руководителя"); -// } else { -// allAuthors.remove(grantDto.getLeaderId()); } if (errors.hasErrors()) { return GRANT_PAGE; @@ -82,6 +73,11 @@ public class GrantController { return String.format(REDIRECT_TO, GRANTS_PAGE); } + @PostMapping(value = "/grant", params = "filterUsers") + public String filterUsers() { + return GRANT_PAGE; + } + @PostMapping(value = "/grant", params = "addDeadline") public String addDeadline(@Valid GrantDto grantDto, Errors errors) { filterEmptyDeadlines(grantDto); @@ -101,12 +97,6 @@ public class GrantController { return GRANT_PAGE; } -// @PostMapping(value = "/grant", params = "updateAuthors") -// public String updateAuthors(@ModelAttribute List allAuthors, @Valid GrantDto grantDto) { -// allAuthors.remove(grantDto.getLeaderId()); -// return GRANT_PAGE; -// } - @GetMapping("/delete/{grant-id}") public String delete(@PathVariable("grant-id") Integer grantId) throws IOException { grantService.delete(grantId); @@ -119,15 +109,10 @@ public class GrantController { } @ModelAttribute("allAuthors") - public List getAllAuthors() { - return grantService.getGrantAuthors(); + public List getAllAuthors(GrantDto grantDto) { + return grantService.getGrantAuthors(grantDto); } -// @ModelAttribute("allLeaders") -// public List getAllLeaders() { -// return grantService.getGrantAuthors(); -// } - private void filterEmptyDeadlines(GrantDto grantDto) { grantDto.setDeadlines(grantDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index cc0f2da..1a3a2e0 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -29,6 +29,9 @@ public class GrantDto { private Set authorIds; private Set authors; private Integer leaderId; + private boolean leader; + private boolean birthDate; + private boolean degree; public GrantDto() { deadlines.add(new Deadline()); @@ -43,7 +46,10 @@ public class GrantDto { @JsonProperty("project") ProjectDto project, @JsonProperty("authorIds") Set authorIds, @JsonProperty("authors") Set authors, - @JsonProperty("leader") Integer leaderId) { + @JsonProperty("leader") Integer leaderId, + @JsonProperty("leader") boolean leader, + @JsonProperty("birthDate") boolean birthDate, + @JsonProperty("degree") boolean degree) { this.id = id; this.title = title; this.status = status; @@ -53,6 +59,9 @@ public class GrantDto { this.project = project; this.authors = authors; this.leaderId = leaderId; + this.leader = leader; + this.birthDate = birthDate; + this.degree = degree; } public GrantDto(Grant grant) { @@ -66,6 +75,9 @@ public class GrantDto { this.authorIds = convert(grant.getAuthors(), user -> user.getId()); this.authors = convert(grant.getAuthors(), UserDto::new); this.leaderId = grant.getLeader().getId(); + this.leader = false; + this.birthDate = false; + this.degree = false; } public Integer getId() { @@ -154,4 +166,28 @@ public class GrantDto { public void setLeaderId(Integer leaderId) { this.leaderId = leaderId; } + + public boolean isLeader() { + return leader; + } + + public void setLeader(boolean leader) { + this.leader = leader; + } + + public boolean isBirthDate() { + return birthDate; + } + + public void setBirthDate(boolean birthDate) { + this.birthDate = birthDate; + } + + public boolean isDegree() { + return degree; + } + + public void setDegree(boolean degree) { + this.degree = degree; + } } diff --git a/src/main/java/ru/ulstu/grant/model/GrantUserFilterDto.java b/src/main/java/ru/ulstu/grant/model/GrantUserFilterDto.java deleted file mode 100644 index 36f88f6..0000000 --- a/src/main/java/ru/ulstu/grant/model/GrantUserFilterDto.java +++ /dev/null @@ -1,54 +0,0 @@ -package ru.ulstu.grant.model; - -import ru.ulstu.user.model.UserDto; - -import java.util.List; - -public class GrantUserFilterDto { - private List users; - private boolean leader; - private boolean birthDate; - private boolean degree; - - public GrantUserFilterDto() { - } - - public GrantUserFilterDto(List users, boolean leader, boolean birthDate, boolean degree) { - this.users = users; - this.leader = leader; - this.birthDate = birthDate; - this.degree = degree; - } - - public List getUsers() { - return users; - } - - public void setUsers(List users) { - this.users = users; - } - - public boolean isLeader() { - return leader; - } - - public void setLeader(boolean leader) { - this.leader = leader; - } - - public boolean isBirthDate() { - return birthDate; - } - - public void setBirthDate(boolean birthDate) { - this.birthDate = birthDate; - } - - public boolean isDegree() { - return degree; - } - - public void setDegree(boolean degree) { - this.degree = degree; - } -} diff --git a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java index 68b31a6..44c2cc0 100644 --- a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -1,10 +1,7 @@ package ru.ulstu.grant.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.grant.model.Grant; -import ru.ulstu.user.model.User; import java.util.List; diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 5474089..aa8ffac 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -8,13 +8,11 @@ import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.file.service.FileService; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; -import ru.ulstu.grant.model.GrantUserFilterDto; import ru.ulstu.grant.repository.GrantRepository; import ru.ulstu.project.model.Project; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.service.ProjectService; import ru.ulstu.user.model.User; -import ru.ulstu.user.model.UserDto; import ru.ulstu.user.service.UserService; import java.io.IOException; @@ -141,19 +139,15 @@ public class GrantService { } } - public List getGrantAuthors() { - return userService.findAll(); - } - - public List filterUsers(GrantUserFilterDto filterDto) { - List filteredUsers = userService.filterByAgeAndDegree(filterDto.isBirthDate(), filterDto.isDegree()); - if (filterDto.isLeader()) { + public List getGrantAuthors(GrantDto grantDto) { + List filteredUsers = userService.filterByAgeAndDegree(grantDto.isBirthDate(), grantDto.isDegree()); + if (grantDto.isLeader()) { filteredUsers = filteredUsers .stream() .filter(getCompletedGrantLeaders()::contains) .collect(Collectors.toList()); } - return convert(filteredUsers, UserDto::new); + return filteredUsers; } private List getCompletedGrantLeaders() { diff --git a/src/main/resources/templates/grants/grant.html b/src/main/resources/templates/grants/grant.html index ed9036d..05b2a03 100644 --- a/src/main/resources/templates/grants/grant.html +++ b/src/main/resources/templates/grants/grant.html @@ -8,7 +8,6 @@
-
@@ -95,22 +94,20 @@
- + th:field="*{leader}" th:onclick="|$('#filter').click();|"/>
+ th:field="*{birthDate}" th:onclick="|$('#filter').click();|"/>
-
+ th:field="*{degree}" th:onclick="|$('#filter').click();|"/> +
@@ -128,6 +125,8 @@
+
@@ -135,7 +134,7 @@ @@ -146,7 +145,7 @@ @@ -209,15 +208,13 @@