3 лаба вроде работает

This commit is contained in:
Павел Сорокин 2023-03-30 15:04:28 +04:00
parent 45a590f145
commit c1a8b9fe81
4 changed files with 51 additions and 9 deletions

Binary file not shown.

View File

@ -47,9 +47,14 @@ public class Comment {
return post;
}
public void setPost(Post post, User user){
this.post = post;
post.getComments().add(this);
this.user = user;
if(post!=null)
{
post.getComments().add(this);
this.post = post;
this.user = user;
}
}
@Override

View File

@ -3,6 +3,8 @@ package ru.ulstu.is.sbapp.Post.service;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -17,7 +19,7 @@ public class PostService {
@PersistenceContext
private EntityManager em;
private static final Logger log = LoggerFactory.getLogger(PostService.class);
@Transactional
public Post addPost(String Heading, String Content) {
if (!StringUtils.hasText(Heading) || !StringUtils.hasText(Content) ) {
@ -81,11 +83,10 @@ public class PostService {
@Transactional
public void removeCommentFromPost(Long id, Comment comment){
final Post post = findPost(id);
if(post == null){
throw new IllegalArgumentException("Post with id " + id + " not found");
}
log.info(post.toString());
post.getComments().remove(comment);
em.merge(post);
comment.setPost(null, null);
em.remove(comment);
em.createQuery("Delete from Comment where Id = "+comment.getId()).executeUpdate();
}
}

View File

@ -86,7 +86,11 @@ public class JpaPostTests {
userService.deleteAllUsers();
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
final Post post =userService.addNewPost(user.getId(),"Text1","Text2");
final Comment comment =postService.addCommentToPost(post.getId(),user,"СОООСИИ УЕБОК ЕБАННЫЙ");
final User user2=userService.findUser(user.getId());
log.info(user2.getPosts().toString());
final Post post2=postService.findPost(post.getId());
log.info(post2.toString());
final Comment comment =postService.addCommentToPost(post.getId(),user,"Крутой пост");
final List<Comment> comments = commentService.findAllComments();
log.info(comments.toString());
final User user1=userService.findUser(user.getId());
@ -94,4 +98,36 @@ public class JpaPostTests {
final Post post1=postService.findPost(post.getId());
log.info(post1.getComments().toString());
}
@Test
void TestDeleteCommentFromPost()
{
postService.deleteAllPosts();
userService.deleteAllUsers();
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
final Post post =userService.addNewPost(user.getId(),"Text1","Text2");
final User user2=userService.findUser(user.getId());
log.info("Посты юзера (добавили)"+user2.getPosts().toString());
final Post post2=postService.findPost(post.getId());
log.info("Пост который добавили"+post2.toString());
final Comment comment =postService.addCommentToPost(post.getId(),user,"Крутой пост");
final List<Comment> comments = commentService.findAllComments();
log.info("Добавили коммент"+comments.toString());
final User user1=userService.findUser(user.getId());
log.info("Комменты юзера"+user1.getComments().toString());
final Post post1=postService.findPost(post.getId());
log.info("Комменты к посту"+post1.getComments().toString());
Comment comment1 = commentService.findComment(comment.getId());
log.info("Удаляем коммент");
postService.removeCommentFromPost(post.getId(),comment1);
final User user3=userService.findUser(user.getId());
log.info(user3.getPosts().toString());
log.info("Комменты юзера после удаления"+user3.getComments().toString());
final Post post3 = postService.findPost(post.getId());
log.info(post3.toString());
log.info(post3.getComments().toString());
final List<Comment> comment4=commentService.findAllComments();
log.info(comment4.toString());
postService.deleteAllPosts();
userService.deleteAllUsers();
}
}