Resolve "Метод чтения из БД для фильтра статей" #146

Merged
alefly merged 1 commits from 14-DB-filter into master 2018-11-13 15:46:26 +04:00
4 changed files with 51 additions and 12 deletions

View File

@ -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<Integer> 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<Integer> 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<Boolean> delete(@PathVariable("paper-id") Integer paperId) throws IOException {
paperService.delete(paperId);
return new Response(true);
return new Response<>(true);
}
@GetMapping("/statuses")
public Response<List<PaperStatusDto>> getPaperStatuses() {
return new Response<>(paperService.getPaperStatuses());
}
@PostMapping("/filter")
public Response<List<PaperDto>> filter(@RequestBody @Valid PaperFilterDto paperFilterDto) throws IOException {
return new Response<>(paperService.filter(paperFilterDto));
}
}

View File

@ -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;
}
}

View File

@ -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<Paper, Integer> {
@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<Paper> filter(@Param("author") User author, @Param("year") Integer year);
}

View File

@ -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<PaperStatusDto> 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<PaperDto> filter(PaperFilterDto filterDto) {
return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new);
}
}