full 3 lab

This commit is contained in:
Павел Сорокин 2023-04-03 16:06:40 +04:00
parent 32498b023a
commit b016ee75a3
3 changed files with 46 additions and 24 deletions

View File

@ -68,12 +68,6 @@ public class User {
post.setUser(this);
}
public void deletePost(Post post) {
posts.remove(post);
post.deleteUser();
}
public String getEmail()
{
return email;
@ -81,14 +75,6 @@ public class User {
public void setEmail(String email){
this.email=email;
}
public void removePost(Post post)
{
if(posts!=null)
{
posts.remove(post);
}
}
@Override
public boolean equals(Object o) {

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.User.service;
import jakarta.persistence.TypedQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -14,6 +15,7 @@ import jakarta.persistence.PersistenceContext;
import ru.ulstu.is.sbapp.Comment.service.CommentService;
import java.util.List;
import java.util.Objects;
@Service
public class UserService {
@ -25,10 +27,10 @@ public class UserService {
@Transactional
public User addUser(String firstName, String lastName, String email) {
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) ||!StringUtils.hasText(email)) {
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) || !StringUtils.hasText(email)) {
throw new IllegalArgumentException("Client info is null or empty");
}
final User user = new User(firstName, lastName,email);
final User user = new User(firstName, lastName, email);
em.persist(user);
return user;
}
@ -50,7 +52,7 @@ public class UserService {
@Transactional
public User updateUser(Long id, String firstName, String lastName, String email) {
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) ||!StringUtils.hasText(email)) {
if (!StringUtils.hasText(firstName) || !StringUtils.hasText(lastName) || !StringUtils.hasText(email)) {
throw new IllegalArgumentException("User info is null or empty");
}
final User currentUser = findUser(id);
@ -63,8 +65,8 @@ public class UserService {
@Transactional
public User deleteUser(Long id) {
final User currentUser = findUser(id);
em.createQuery("Delete from Post Where user.id = "+ id).executeUpdate();
em.createQuery("Delete from Comment Where user.id = "+id).executeUpdate();
em.createQuery("Delete from Post Where user.id = " + id).executeUpdate();
em.createQuery("Delete from Comment Where user.id = " + id).executeUpdate();
em.remove(currentUser);
return currentUser;
}
@ -75,19 +77,29 @@ public class UserService {
em.createQuery("Delete from Comment").executeUpdate();
em.createQuery("delete from User").executeUpdate();
}
@Transactional
public Post addNewPost(Long id, String Heading,String Content) {
User currentUser= findUser(id);
Post post=new Post(Heading,Content);
public Post addNewPost(Long id, String Heading, String Content) {
User currentUser = findUser(id);
Post post = new Post(Heading, Content);
post.setUser(currentUser);
return em.merge(post);
}
@Transactional
public void deletePost(Long id, Post post) {
em.createQuery("Delete Comment where post.Id = "+ post.getId()).executeUpdate();
em.createQuery("Delete from Post where Id = "+post.getId()).executeUpdate();
em.createQuery("Delete Comment where post.Id = " + post.getId()).executeUpdate();
em.createQuery("Delete from Post where Id = " + post.getId()).executeUpdate();
}
@Transactional
public List<Object[]> SelectCommentByText(String Text) {
return em.createQuery("SELECT u.email,p.Heading,c.Text FROM User u " +
"JOIN u.posts p JOIN p.comments c WHERE c.Text LIKE :texty",Object[].class)
.setParameter("texty",Text)
.getResultList();
}
}

View File

@ -7,11 +7,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ulstu.is.sbapp.Comment.model.Comment;
import ru.ulstu.is.sbapp.Post.model.Post;
import ru.ulstu.is.sbapp.Post.service.PostService;
import ru.ulstu.is.sbapp.User.model.User;
import ru.ulstu.is.sbapp.User.service.UserService;
import java.util.Arrays;
import java.util.List;
@SpringBootTest
@ -90,4 +92,26 @@ public class JpaClientTests {
userService.deleteAllUsers();
postService.deleteAllPosts();
}
//посты и коментарии содержащие определенный текст
@Test
void Selected()
{
postService.deleteAllPosts();
userService.deleteAllUsers();
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
final User user1 = userService.addUser("Pasha","Sorokin","zxcv@gmail.com");
final Post post =userService.addNewPost(user.getId(),"Text1","Да");
final Post post1=userService.addNewPost(user.getId(),"Привет","Да");
final Post post2 = userService.addNewPost(user1.getId(),"ага","конечно");
final Comment comment3 = postService.addCommentToPost(post2.getId(),user,"Привет");
final Comment comment = postService.addCommentToPost(post.getId(),user,"нект");
final Comment comment1 = postService.addCommentToPost(post1.getId(),user1,"Привет");
final User u = userService.findUser(user.getId());
final List<Post> userPosts = u.getPosts();
log.info("Сюда");
log.info(userPosts.toString());
List<Object[]> onk=(userService.SelectCommentByText("Привет"));
log.info(String.valueOf((userService.SelectCommentByText("Привет").size())));
}
}