diff --git a/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentController.java b/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentController.java index 62da59a..dafa0e3 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentController.java +++ b/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentController.java @@ -2,13 +2,11 @@ package ru.ulstu.is.sbapp.Comment.controller; import org.springframework.web.bind.annotation.*; import ru.ulstu.is.sbapp.Comment.service.CommentService; -import ru.ulstu.is.sbapp.Post.controller.PostDto; -import ru.ulstu.is.sbapp.Post.service.PostService; import java.util.List; @RestController -@RequestMapping("/controller") +@RequestMapping("/comment") public class CommentController { private final CommentService commentService; public CommentController(CommentService commentService) { @@ -31,7 +29,7 @@ public class CommentController { @PutMapping("/{id}") public CommentDto updateComment(@PathVariable Long id, - @RequestParam("Text") String Text){ + @RequestParam("Text") String Text){ return new CommentDto(commentService.updateComment(id,Text)); } @DeleteMapping("/{id}") diff --git a/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentDto.java b/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentDto.java index 978c38e..0efd77d 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentDto.java +++ b/src/main/java/ru/ulstu/is/sbapp/Comment/controller/CommentDto.java @@ -7,12 +7,13 @@ import ru.ulstu.is.sbapp.User.model.User; public class CommentDto { private Long id; private String Text; - private Post post; - private User user; + private String userName; public CommentDto(){} public CommentDto(Comment comment) { + this.id= comment.getId(); this.Text=comment.getText(); + this.userName=comment.getUser().getFirstName() + " " + comment.getUser().getLastName(); } public Long getId() { @@ -20,14 +21,10 @@ public class CommentDto { } public String getText() {return Text;} - public User getUser() + public String getUser() { - return user; + return userName; } - public Post getPost() - { - return post; - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/Comment/model/Comment.java b/src/main/java/ru/ulstu/is/sbapp/Comment/model/Comment.java index 42104df..a117561 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Comment/model/Comment.java +++ b/src/main/java/ru/ulstu/is/sbapp/Comment/model/Comment.java @@ -1,5 +1,6 @@ package ru.ulstu.is.sbapp.Comment.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; import ru.ulstu.is.sbapp.Post.model.Post; @@ -16,9 +17,11 @@ public class Comment { private String Text; @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE) + @JsonIgnore private Post post; @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE) + @JsonIgnore private User user; public Comment() diff --git a/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostController.java b/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostController.java index aeba247..122b469 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostController.java +++ b/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostController.java @@ -2,6 +2,8 @@ package ru.ulstu.is.sbapp.Post.controller; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; +import ru.ulstu.is.sbapp.Comment.controller.CommentDto; +import ru.ulstu.is.sbapp.Post.model.Post; import ru.ulstu.is.sbapp.Post.service.PostService; import ru.ulstu.is.sbapp.User.controller.UserDto; import ru.ulstu.is.sbapp.User.service.UserService; @@ -25,6 +27,12 @@ public class PostController { .map(PostDto::new) .toList(); } + @GetMapping("/{id}/comments") + public List getComments(@PathVariable Long id) { + return postService.GetPostComments(id).stream() + .map(CommentDto::new) + .toList(); + } @PostMapping public PostDto createPost(@RequestBody @Valid PostDto postDto){ return new PostDto(postService.addPost(postDto)); @@ -32,19 +40,18 @@ public class PostController { @PutMapping("/{id}") public PostDto updatePost(@PathVariable Long id, - @RequestParam("Heading") String Heading, - @RequestParam("Content") String Content){ - return new PostDto(postService.updatePost(id,Heading,Content)); + @RequestBody @Valid PostDto postDto){ + return new PostDto(postService.updatePost(id,postDto)); } @PostMapping("/{id}/Comment/{userId}") public void addComment(@PathVariable Long id, @PathVariable Long userId, - @RequestParam("Text") String Text) { + @RequestParam("Text") String Text) { postService.addCommentToPost(id, userId,Text); } - @DeleteMapping("/{id}/Comment/{postId}") + @DeleteMapping("/{id}/Comment/{commentId}") public void removeComment(@PathVariable Long id, - @PathVariable Long commentId) + @PathVariable Long commentId) { postService.removeCommentFromPost(id,commentId); } diff --git a/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostDto.java b/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostDto.java index e8ccf02..6ed7350 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostDto.java +++ b/src/main/java/ru/ulstu/is/sbapp/Post/controller/PostDto.java @@ -15,15 +15,14 @@ public class PostDto { private String content; - private User user; private String image; - private List comments = new ArrayList<>(); public PostDto(){} public PostDto(Post post) { + this.id= post.getId(); this.heading = post.getHeading(); this.content = post.getContent(); this.image = new String(post.getImage(), StandardCharsets.UTF_8); @@ -42,15 +41,7 @@ public class PostDto { { return content; } - public User getUser() - { - return user; - } public String getImage() { return image; } - public List getComments() - { - return comments; - } } diff --git a/src/main/java/ru/ulstu/is/sbapp/Post/model/Post.java b/src/main/java/ru/ulstu/is/sbapp/Post/model/Post.java index a96803c..dcde6a8 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Post/model/Post.java +++ b/src/main/java/ru/ulstu/is/sbapp/Post/model/Post.java @@ -1,5 +1,6 @@ package ru.ulstu.is.sbapp.Post.model; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import jakarta.validation.constraints.NotBlank; import ru.ulstu.is.sbapp.Comment.model.Comment; @@ -27,6 +28,7 @@ public class Post { private byte[] image; @ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE) + @JsonIgnore private User user; @OneToMany(mappedBy = "post",fetch = FetchType.EAGER,cascade = CascadeType.ALL,orphanRemoval = true) diff --git a/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryExtension.java b/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryExtension.java index a0e14a3..d454113 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryExtension.java +++ b/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryExtension.java @@ -11,4 +11,5 @@ public interface PostRepositoryExtension { void safeRemoveAll(); void addComment(Long id,Long userId,String text); void removeComment(Long id, Long commentId); + List getPostComments(Long id); } diff --git a/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryImpl.java b/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryImpl.java index fd480a6..cab2baa 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryImpl.java +++ b/src/main/java/ru/ulstu/is/sbapp/Post/repository/PostRepositoryImpl.java @@ -68,4 +68,12 @@ public class PostRepositoryImpl implements PostRepositoryExtension{ } } + @Override + public List getPostComments(Long id) { + TypedQuery query = + em.createQuery("Select c from Comment c where post.id = :id",Comment.class) + .setParameter("id",id); + return query.getResultList(); + } + } diff --git a/src/main/java/ru/ulstu/is/sbapp/Post/service/PostService.java b/src/main/java/ru/ulstu/is/sbapp/Post/service/PostService.java index fc61ce4..bb5f9fe 100644 --- a/src/main/java/ru/ulstu/is/sbapp/Post/service/PostService.java +++ b/src/main/java/ru/ulstu/is/sbapp/Post/service/PostService.java @@ -12,6 +12,7 @@ import ru.ulstu.is.sbapp.Post.repository.PostRepository; import ru.ulstu.is.sbapp.User.model.User; import ru.ulstu.is.sbapp.Util.validation.ValidatorUtil; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Optional; @@ -46,14 +47,20 @@ public class PostService { } @Transactional - public Post updatePost(Long id, String Heading, String Content) { + public Post updatePost(Long id, PostDto postDto) { final Post currentPost = findPost(id); - currentPost.setHeading(Heading); - currentPost.setContent(Content); + currentPost.setHeading(postDto.getHeading()); + currentPost.setContent(postDto.getContent()); + currentPost.setImage(postDto.getImage().getBytes(StandardCharsets.UTF_8)); validatorUtil.validate(currentPost); return postRepository.save(currentPost); } + @Transactional + public List GetPostComments(Long id) + { + return postRepository.getPostComments(id); + } @Transactional public Post deletePost(Long id) { final Optional post = postRepository.safeRemove(id);