diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 2928fe2..76f3ed9 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -12,6 +12,7 @@ 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; @@ -48,23 +49,30 @@ public class GrantController { } @GetMapping("/grant") - public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) { + public void getGrant(ModelMap modelMap, GrantUserFilterDto grantUserFilterDto, @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, Errors errors) throws IOException { + public String save(@Valid GrantDto grantDto, GrantUserFilterDto grantUserFilterDto, Errors errors, ModelMap modelMap /*@ModelAttribute List allAuthors*/) + 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; @@ -92,6 +100,12 @@ 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); @@ -108,6 +122,11 @@ public class GrantController { return grantService.getGrantAuthors(); } +// @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/GrantUserFilterDto.java b/src/main/java/ru/ulstu/grant/model/GrantUserFilterDto.java new file mode 100644 index 0000000..36f88f6 --- /dev/null +++ b/src/main/java/ru/ulstu/grant/model/GrantUserFilterDto.java @@ -0,0 +1,54 @@ +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 92dec43..1b2e64d 100644 --- a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -1,8 +1,29 @@ 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; public interface GrantRepository extends JpaRepository { +// @Query(value = "SELECT u.last_name " + +// "FROM users u, grants g " + +// "WHERE (g.leader_id = u.id) " + +// "AND (u.birth_date < 35) " + +// "AND (u.degree = 'CANDIDATE')" + +// "Group by u.last_name", +// nativeQuery = true) +// List filter(@Param("leader") Integer leader, @Param("birthYear") Date birthYear, @Param("degree") String degree); + + @Query("SELECT u.lastName FROM User u, Grant g WHERE (g.leader = u.id OR :leader IS FALSE) " + + "AND (extract(year from interval (age(birth_date::date))) < 35 OR :birthDate IS FALSE) " + + "AND (u.degree = 'CANDIDATE' OR :degree IS FALSE)" + + "GROUP BY u.lastName") + List filterUsers(@Param("leader") boolean leader, + @Param("birthDate") boolean birthDate, + @Param("degree") boolean degree); } diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index cbf37ce..197c4f2 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -8,11 +8,13 @@ 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,4 +143,9 @@ public class GrantService { public List getGrantAuthors() { return userService.findAll(); } + + public List filterUsers(GrantUserFilterDto filterDto) { + return convert(grantRepository.filterUsers( + filterDto.isLeader(), filterDto.isBirthDate(), filterDto.isDegree()), UserDto::new); + } } diff --git a/src/main/resources/templates/grants/grant.html b/src/main/resources/templates/grants/grant.html index aae3b02..ed9036d 100644 --- a/src/main/resources/templates/grants/grant.html +++ b/src/main/resources/templates/grants/grant.html @@ -9,7 +9,7 @@
-
+
@@ -94,17 +94,22 @@
- + +
- +
- +
@@ -125,27 +130,30 @@

+
-
+
-

Incorrect member

+