From 2a76acc7ad7de2a253ba45b234c4a165165cc661 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 13 Nov 2018 15:41:18 +0400 Subject: [PATCH] filter papers --- .../paper/controller/PaperController.java | 18 +++++++++----- .../ru/ulstu/paper/model/PaperFilterDto.java | 24 +++++++++++++++++++ .../paper/repository/PaperRepository.java | 6 +++++ .../ru/ulstu/paper/service/PaperService.java | 15 +++++++----- 4 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 src/main/java/ru/ulstu/paper/model/PaperFilterDto.java diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index e5acd23..c5cbf4f 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RestController; import ru.ulstu.configuration.Constants; import ru.ulstu.core.model.response.Response; import ru.ulstu.paper.model.PaperDto; +import ru.ulstu.paper.model.PaperFilterDto; import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.service.PaperService; @@ -37,23 +38,28 @@ public class PaperController { } @PostMapping - public Response createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException { - return new Response(paperService.create(paperDto)); + public Response createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException { + return new Response<>(paperService.create(paperDto)); } @PutMapping - public Response updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException { - return new Response(paperService.update(paperDto)); + public Response updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException { + return new Response<>(paperService.update(paperDto)); } @DeleteMapping("/{paper-id}") - public Response delete(@PathVariable("paper-id") Integer paperId) throws IOException { + public Response delete(@PathVariable("paper-id") Integer paperId) throws IOException { paperService.delete(paperId); - return new Response(true); + return new Response<>(true); } @GetMapping("/statuses") public Response> getPaperStatuses() { return new Response<>(paperService.getPaperStatuses()); } + + @PostMapping("/filter") + public Response> filter(@RequestBody @Valid PaperFilterDto paperFilterDto) throws IOException { + return new Response<>(paperService.filter(paperFilterDto)); + } } diff --git a/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java b/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java new file mode 100644 index 0000000..c832bed --- /dev/null +++ b/src/main/java/ru/ulstu/paper/model/PaperFilterDto.java @@ -0,0 +1,24 @@ +package ru.ulstu.paper.model; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class PaperFilterDto { + private final Integer authorId; + private final Integer year; + + @JsonCreator + public PaperFilterDto(@JsonProperty("authorId") Integer authorId, + @JsonProperty("year") Integer year) { + this.authorId = authorId; + this.year = year; + } + + public Integer getAuthorId() { + return authorId; + } + + public Integer getYear() { + return year; + } +} diff --git a/src/main/java/ru/ulstu/paper/repository/PaperRepository.java b/src/main/java/ru/ulstu/paper/repository/PaperRepository.java index 613bb6a..5d0cd9a 100644 --- a/src/main/java/ru/ulstu/paper/repository/PaperRepository.java +++ b/src/main/java/ru/ulstu/paper/repository/PaperRepository.java @@ -1,9 +1,15 @@ package ru.ulstu.paper.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.paper.model.Paper; +import ru.ulstu.user.model.User; import java.util.List; public interface PaperRepository extends JpaRepository { + + @Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND YEAR(p.createDate) = :year OR :year IS NULL") + List filter(@Param("author") User author, @Param("year") Integer year); } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index a5b5237..a75592b 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -5,6 +5,7 @@ import org.springframework.transaction.annotation.Transactional; import ru.ulstu.file.service.FileService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; +import ru.ulstu.paper.model.PaperFilterDto; import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.repository.PaperRepository; import ru.ulstu.user.model.User; @@ -40,7 +41,7 @@ public class PaperService { return paperRepository.findAll().stream().sorted((paper1, paper2) -> { int statusCompareResult = Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper1.getStatus())) - .compareTo(Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper2.getStatus()))); + .compareTo(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper2.getStatus())); if (statusCompareResult != 0) { return statusCompareResult; } @@ -53,7 +54,7 @@ public class PaperService { } @Transactional - public int create(PaperDto paperDto) throws IOException { + public Integer create(PaperDto paperDto) throws IOException { Paper newPaper = copyFromDto(new Paper(), paperDto); newPaper = paperRepository.save(newPaper); paperNotificationService.sendCreateNotification(newPaper); @@ -72,9 +73,7 @@ public class PaperService { paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); } if (paperDto.getAuthors() != null && !paperDto.getAuthors().isEmpty()) { - paperDto.getAuthors().forEach(authorIds -> { - paper.getAuthors().add(userService.findById(authorIds.getId())); - }); + paperDto.getAuthors().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds.getId()))); } return paper; } @@ -105,7 +104,7 @@ public class PaperService { } public List getPaperStatuses() { - return convert(Arrays.asList(Paper.PaperStatus.values()), status -> new PaperStatusDto(status)); + return convert(Arrays.asList(Paper.PaperStatus.values()), PaperStatusDto::new); } @Transactional @@ -123,4 +122,8 @@ public class PaperService { return paper; } + + public List filter(PaperFilterDto filterDto) { + return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new); + } }