4 lab PostController
This commit is contained in:
parent
08dad1b6a4
commit
419eb0b15a
@ -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<PostDto> 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));
|
||||
}
|
||||
}
|
||||
|
@ -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<Comment> 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;
|
||||
|
@ -47,24 +47,25 @@ public class PostRepositoryImpl implements PostRepositoryExtension{
|
||||
@Override
|
||||
public void addComment(Long id, Long userId, String text) {
|
||||
Optional<Post> 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<Post> optionalPost = postRepository.findById(id);
|
||||
Optional<Comment> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -49,9 +49,13 @@ public class UserRepositoryImpl implements UserRepositoryExtension {
|
||||
@Override
|
||||
public void addPost(Long id, String Heading, String Content) {
|
||||
Optional<User> 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
|
||||
|
Loading…
Reference in New Issue
Block a user