Merge branch '14-DB-filter' into 'master'

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

See merge request romanov73/ng-tracker!19
This commit is contained in:
Anton Romanov 2018-11-13 11:46:25 +00:00
commit eaf03daba3
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.configuration.Constants;
import ru.ulstu.core.model.response.Response; import ru.ulstu.core.model.response.Response;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto;
import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.model.PaperStatusDto;
import ru.ulstu.paper.service.PaperService; import ru.ulstu.paper.service.PaperService;
@ -37,23 +38,28 @@ public class PaperController {
} }
@PostMapping @PostMapping
public Response createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException { public Response<Integer> createPaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response(paperService.create(paperDto)); return new Response<>(paperService.create(paperDto));
} }
@PutMapping @PutMapping
public Response updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException { public Response<Integer> updatePaper(@RequestBody @Valid PaperDto paperDto) throws IOException {
return new Response(paperService.update(paperDto)); return new Response<>(paperService.update(paperDto));
} }
@DeleteMapping("/{paper-id}") @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); paperService.delete(paperId);
return new Response(true); return new Response<>(true);
} }
@GetMapping("/statuses") @GetMapping("/statuses")
public Response<List<PaperStatusDto>> getPaperStatuses() { public Response<List<PaperStatusDto>> getPaperStatuses() {
return new Response<>(paperService.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; package ru.ulstu.paper.repository;
import org.springframework.data.jpa.repository.JpaRepository; 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.paper.model.Paper;
import ru.ulstu.user.model.User;
import java.util.List; import java.util.List;
public interface PaperRepository extends JpaRepository<Paper, Integer> { 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.file.service.FileService;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto;
import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.model.PaperStatusDto;
import ru.ulstu.paper.repository.PaperRepository; import ru.ulstu.paper.repository.PaperRepository;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
@ -40,7 +41,7 @@ public class PaperService {
return paperRepository.findAll().stream().sorted((paper1, paper2) -> { return paperRepository.findAll().stream().sorted((paper1, paper2) -> {
int statusCompareResult = int statusCompareResult =
Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper1.getStatus())) 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) { if (statusCompareResult != 0) {
return statusCompareResult; return statusCompareResult;
} }
@ -53,7 +54,7 @@ public class PaperService {
} }
@Transactional @Transactional
public int create(PaperDto paperDto) throws IOException { public Integer create(PaperDto paperDto) throws IOException {
Paper newPaper = copyFromDto(new Paper(), paperDto); Paper newPaper = copyFromDto(new Paper(), paperDto);
newPaper = paperRepository.save(newPaper); newPaper = paperRepository.save(newPaper);
paperNotificationService.sendCreateNotification(newPaper); paperNotificationService.sendCreateNotification(newPaper);
@ -72,9 +73,7 @@ public class PaperService {
paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
} }
if (paperDto.getAuthors() != null && !paperDto.getAuthors().isEmpty()) { if (paperDto.getAuthors() != null && !paperDto.getAuthors().isEmpty()) {
paperDto.getAuthors().forEach(authorIds -> { paperDto.getAuthors().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds.getId())));
paper.getAuthors().add(userService.findById(authorIds.getId()));
});
} }
return paper; return paper;
} }
@ -105,7 +104,7 @@ public class PaperService {
} }
public List<PaperStatusDto> getPaperStatuses() { 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 @Transactional
@ -123,4 +122,8 @@ public class PaperService {
return paper; return paper;
} }
public List<PaperDto> filter(PaperFilterDto filterDto) {
return convert(paperRepository.filter(userService.findById(filterDto.getAuthorId()), filterDto.getYear()), PaperDto::new);
}
} }