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 0b2c555..1fdcbdc 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 @@ -1,4 +1,57 @@ package ru.ulstu.is.sbapp.Post.controller; +import org.springframework.web.bind.annotation.*; +import ru.ulstu.is.sbapp.Post.service.PostService; +import ru.ulstu.is.sbapp.User.controller.UserDto; +import ru.ulstu.is.sbapp.User.service.UserService; + +import java.util.List; + +@RestController +@RequestMapping("/post") public class PostController { + private final PostService postService; + public PostController(PostService postService) { + this.postService = postService; + } + @GetMapping("/{id}") + public PostDto getPost(@PathVariable Long id) { + return new PostDto(postService.findPost(id)); + } + @GetMapping + public List getPosts() { + return postService.findAllPosts().stream() + .map(PostDto::new) + .toList(); + } + @PostMapping + public PostDto createPost(@RequestParam("Heading") String Heading, + @RequestParam("Content") String Content){ + return new PostDto(postService.addPost(Heading, Content)); + } + + @PutMapping("/{id}") + public PostDto updatePost(@PathVariable Long id, + @RequestParam("Heading") String Heading, + @RequestParam("Content") String Content){ + return new PostDto(postService.updatePost(id,Heading,Content)); + } + @PostMapping("/{id}/Comment/{userId}") + public void addComment(@PathVariable Long id, + @PathVariable Long userId, + @RequestParam("Text") String Text) { + postService.addCommentToPost(id, userId,Text); + } + @DeleteMapping("/{id}/Comment/{postId}") + public void removeComment(@PathVariable Long id, + @PathVariable Long commentId) + { + postService.removeCommentFromPost(id,commentId); + } + + + @DeleteMapping("/{id}") + public PostDto deletePost(@PathVariable Long id) { + return new PostDto(postService.deletePost(id)); + } } 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 fa58bb7..e8e485e 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 @@ -1,6 +1,7 @@ package ru.ulstu.is.sbapp.Post.controller; import ru.ulstu.is.sbapp.Comment.model.Comment; +import ru.ulstu.is.sbapp.Post.model.Post; import ru.ulstu.is.sbapp.User.model.User; import java.util.ArrayList; @@ -17,12 +18,13 @@ public class PostDto { private List comments = new ArrayList<>(); - public PostDto(String Heading, String Content) + public PostDto(Post post) { - this.Heading = Heading; - this.Content = Content; + this.Heading = post.getHeading(); + this.Content = post.getContent(); } + public Long getId() { return 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 52725ac..fd480a6 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 @@ -47,24 +47,25 @@ public class PostRepositoryImpl implements PostRepositoryExtension{ @Override public void addComment(Long id, Long userId, String text) { Optional optionalPost = postRepository.findById(id); - if(optionalPost.get()==null){ - throw new IllegalArgumentException("Post with id " + id + " not found"); + if(optionalPost.isPresent()) { + Comment comment = new Comment(text); + comment.setPost(optionalPost.get(), em.find(User.class, userId)); + commentRepository.save(comment); } - Comment comment=new Comment(text); - comment.setPost(optionalPost.get(), em.find(User.class, userId)); - commentRepository.save(comment); } @Override public void removeComment(Long id, Long commentId) { Optional optionalPost = postRepository.findById(id); Optional optionalComment = commentRepository.findById(commentId); - optionalPost.get().getComments().remove(optionalComment.get()); - em.merge(optionalPost.get()); - optionalComment.get().setPost(null, null); - em.createQuery("Delete from Comment where Id = :commentId") - .setParameter("commentId",commentId) - .executeUpdate(); + if(optionalPost.isPresent() & optionalComment.isPresent()) { + optionalPost.get().getComments().remove(optionalComment.get()); + em.merge(optionalPost.get()); + optionalComment.get().setPost(null, null); + em.createQuery("Delete from Comment where Id = :commentId") + .setParameter("commentId", commentId) + .executeUpdate(); + } } } diff --git a/src/main/java/ru/ulstu/is/sbapp/User/repository/UserRepositoryImpl.java b/src/main/java/ru/ulstu/is/sbapp/User/repository/UserRepositoryImpl.java index 9419954..10194ba 100644 --- a/src/main/java/ru/ulstu/is/sbapp/User/repository/UserRepositoryImpl.java +++ b/src/main/java/ru/ulstu/is/sbapp/User/repository/UserRepositoryImpl.java @@ -49,9 +49,13 @@ public class UserRepositoryImpl implements UserRepositoryExtension { @Override public void addPost(Long id, String Heading, String Content) { Optional currentUser = userRepository.findById(id); - Post post = new Post(Heading, Content); - post.setUser(currentUser.get()); - em.merge(post); + if(currentUser.isPresent()) + { + Post post = new Post(Heading, Content); + post.setUser(currentUser.get()); + em.merge(post); + } + } @Override