Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Kirill Mokevnin <[email protected]>
  • Loading branch information
mokevnin committed Oct 15, 2023
1 parent da518c7 commit 04c2e4f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,13 +26,16 @@ public class PostsCommentsController {
@Autowired
private PostCommentSpecification specBuilder;

@Autowired
private PostCommentMapper postCommentMapper;

@GetMapping("/posts_comments")
@ResponseStatus(HttpStatus.OK)
Page<PostComment> index(PostCommentParamsDTO params, @RequestParam(defaultValue = "1") int page) {
Page<PostCommentDTO> 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;
}
}

16 changes: 16 additions & 0 deletions src/main/java/io/hexlet/blog/dto/PostCommentDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions src/main/java/io/hexlet/blog/mapper/PostCommentMapper.java
Original file line number Diff line number Diff line change
@@ -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);
}

3 changes: 1 addition & 2 deletions src/main/java/io/hexlet/blog/model/PostComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public class PostComment implements BaseEntity {
@Column(columnDefinition = "TEXT")
private String body;

@NotNull
@ManyToOne
@ManyToOne(optional = false)
private Post post;

@LastModifiedDate
Expand Down

0 comments on commit 04c2e4f

Please sign in to comment.