From 34033fac5fc7f2d06e022673cd8ed9a06e02fa9b Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Fri, 19 Apr 2019 17:36:46 +0400 Subject: [PATCH] #59 added filtration --- .../controller/ConferenceController.java | 24 +++++++++++++++++++ .../repository/ConferenceRepository.java | 7 ++++++ .../conference/service/ConferenceService.java | 13 ++++++++++ src/main/resources/public/css/conference.css | 4 +++- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index 6867be5..370e996 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -15,10 +15,13 @@ import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.user.model.User; import springfox.documentation.annotations.ApiIgnore; import javax.validation.Valid; import java.io.IOException; +import java.util.ArrayList; +import java.util.Calendar; import java.util.List; import java.util.stream.Collectors; @@ -44,6 +47,13 @@ public class ConferenceController { modelMap.put("filteredConferences", new ConferenceFilterDto(conferenceService.findAllDto())); } + @PostMapping("/conferences") + public void filterConferences(@Valid ConferenceFilterDto conferenceFilterDto, ModelMap modelMap) { + modelMap.put("filteredConferences", new ConferenceFilterDto(conferenceService.filter(conferenceFilterDto), + conferenceFilterDto.getFilterUserId(), + conferenceFilterDto.getYear())); + } + @GetMapping("/conference") public void getConference(ModelMap modelMap, @RequestParam(value = "id") Integer id) { if (id != null && id > 0) { @@ -119,6 +129,20 @@ public class ConferenceController { return conferenceService.getAllDeposit(); } + @ModelAttribute("allUsers") + public List getAllUsers() { + return conferenceService.getAllUsers(); + } + + @ModelAttribute("allYears") + public List getAllYears() { + List years = new ArrayList<>(); + for (int i = Calendar.getInstance().get(Calendar.YEAR); i > 2010; i--) { + years.add(i); + } + return years; + } + private void filterEmptyDeadlines(ConferenceDto conferenceDto) { conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) diff --git a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java index 4325633..99f311e 100644 --- a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java +++ b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java @@ -1,7 +1,14 @@ package ru.ulstu.conference.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.conference.model.Conference; +import ru.ulstu.user.model.User; + +import java.util.List; public interface ConferenceRepository extends JpaRepository { + @Query("SELECT c FROM Conference c LEFT JOIN c.users u WHERE (:user IS NULL OR u.user = :user) AND (YEAR(c.beginDate) = :year OR :year IS NULL)") + List filter(@Param("user") User user, @Param("year") Integer year); } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 6295d5c..b03e9d3 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -5,11 +5,13 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.conference.model.Conference; import ru.ulstu.conference.model.ConferenceDto; +import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.repository.ConferenceRepository; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; +import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; import java.io.IOException; @@ -121,6 +123,10 @@ public class ConferenceService { return paperService.findAllNotSelect(paperIds); } + public List getAllUsers() { + return userService.findAll(); + } + public List getAllParticipations() { return Arrays.asList(ConferenceUser.Participation.values()); } @@ -150,4 +156,11 @@ public class ConferenceService { public boolean isCurrentUserParticipant(List conferenceUsers) { return conferenceUsers.stream().anyMatch(participant -> participant.getUser().equals(userService.getCurrentUser())); } + + public List filter(ConferenceFilterDto conferenceFilterDto) { + return convert(conferenceRepository.filter( + conferenceFilterDto.getFilterUserId() == null ? null : userService.findById(conferenceFilterDto.getFilterUserId()), + conferenceFilterDto.getYear()), ConferenceDto::new); + + } } diff --git a/src/main/resources/public/css/conference.css b/src/main/resources/public/css/conference.css index fe19fd0..bc70ca4 100644 --- a/src/main/resources/public/css/conference.css +++ b/src/main/resources/public/css/conference.css @@ -11,7 +11,9 @@ body { color: white; } - +.filter .dropdown { + margin-bottom: 10px; +} .form-group textarea {