From 04c2e4f7b761554a8ea43edf43157c95429a2f1d Mon Sep 17 00:00:00 2001 From: Kirill Mokevnin Date: Sun, 15 Oct 2023 17:18:48 +0600 Subject: [PATCH] update Signed-off-by: Kirill Mokevnin --- .../api/PostsCommentsController.java | 12 ++++++---- .../io/hexlet/blog/dto/PostCommentDTO.java | 16 +++++++++++++ .../hexlet/blog/mapper/PostCommentMapper.java | 23 +++++++++++++++++++ .../io/hexlet/blog/model/PostComment.java | 3 +-- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/hexlet/blog/dto/PostCommentDTO.java create mode 100644 src/main/java/io/hexlet/blog/mapper/PostCommentMapper.java diff --git a/src/main/java/io/hexlet/blog/controller/api/PostsCommentsController.java b/src/main/java/io/hexlet/blog/controller/api/PostsCommentsController.java index fc18bab..3c5b2f5 100644 --- a/src/main/java/io/hexlet/blog/controller/api/PostsCommentsController.java +++ b/src/main/java/io/hexlet/blog/controller/api/PostsCommentsController.java @@ -5,13 +5,14 @@ import org.springframework.data.domain.PageRequest; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import io.hexlet.blog.dto.PostCommentDTO; import io.hexlet.blog.dto.PostCommentParamsDTO; +import io.hexlet.blog.mapper.PostCommentMapper; import io.hexlet.blog.model.PostComment; import io.hexlet.blog.repository.PostCommentRepository; import io.hexlet.blog.specification.PostCommentSpecification; @@ -25,13 +26,16 @@ public class PostsCommentsController { @Autowired private PostCommentSpecification specBuilder; + @Autowired + private PostCommentMapper postCommentMapper; + @GetMapping("/posts_comments") @ResponseStatus(HttpStatus.OK) - Page index(PostCommentParamsDTO params, @RequestParam(defaultValue = "1") int page) { + Page index(PostCommentParamsDTO params, @RequestParam(defaultValue = "1") int page) { var spec = specBuilder.build(params); var comments = repository.findAll(spec, PageRequest.of(page - 1, 10)); + var result = comments.map(postCommentMapper::map); - return comments; + return result; } } - diff --git a/src/main/java/io/hexlet/blog/dto/PostCommentDTO.java b/src/main/java/io/hexlet/blog/dto/PostCommentDTO.java new file mode 100644 index 0000000..6abd345 --- /dev/null +++ b/src/main/java/io/hexlet/blog/dto/PostCommentDTO.java @@ -0,0 +1,16 @@ +package io.hexlet.blog.dto; + +import java.util.Date; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class PostCommentDTO { + private Long id; + private Long authorId; + private Long postId; + private String body; + private Date createdAt; +} diff --git a/src/main/java/io/hexlet/blog/mapper/PostCommentMapper.java b/src/main/java/io/hexlet/blog/mapper/PostCommentMapper.java new file mode 100644 index 0000000..d0d27bb --- /dev/null +++ b/src/main/java/io/hexlet/blog/mapper/PostCommentMapper.java @@ -0,0 +1,23 @@ +package io.hexlet.blog.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingConstants; +import org.mapstruct.NullValuePropertyMappingStrategy; +import org.mapstruct.ReportingPolicy; + +import io.hexlet.blog.dto.PostCommentDTO; +import io.hexlet.blog.model.PostComment; + +@Mapper( + // uses = { JsonNullableMapper.class, ReferenceMapper.class }, + nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE, + componentModel = MappingConstants.ComponentModel.SPRING, + unmappedTargetPolicy = ReportingPolicy.IGNORE +) +public abstract class PostCommentMapper { + @Mapping(source = "author.id", target = "authorId") + @Mapping(source = "post.id", target = "postId") + public abstract PostCommentDTO map(PostComment model); +} + diff --git a/src/main/java/io/hexlet/blog/model/PostComment.java b/src/main/java/io/hexlet/blog/model/PostComment.java index 20ca9c2..c280d3d 100644 --- a/src/main/java/io/hexlet/blog/model/PostComment.java +++ b/src/main/java/io/hexlet/blog/model/PostComment.java @@ -42,8 +42,7 @@ public class PostComment implements BaseEntity { @Column(columnDefinition = "TEXT") private String body; - @NotNull - @ManyToOne + @ManyToOne(optional = false) private Post post; @LastModifiedDate