4 lab юзер вроде функционирует (тесты проходят)
This commit is contained in:
parent
ee923e986d
commit
0c1764ab47
src
main/java/ru/ulstu/is/sbapp/User
repository
service
test/java/ru/ulstu/is/sbapp
@ -3,6 +3,6 @@ package ru.ulstu.is.sbapp.User.repository;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
public interface UserRepository extends JpaRepository<User, Long>,UserRepositoryExtension {
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
package ru.ulstu.is.sbapp.User.repository;
|
||||
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepositoryExtension {
|
||||
Optional<User> safeRemove(Long id);
|
||||
void safeRemoveAll();
|
||||
void addPost(Long id, String Heading, String Content);
|
||||
void removePost(Long id, Long postId);
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package ru.ulstu.is.sbapp.User.repository;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import ru.ulstu.is.sbapp.Comment.repository.CommentRepository;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.repository.PostRepository;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class UserRepositoryImpl implements UserRepositoryExtension {
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private PostRepository postRepository;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
@Override
|
||||
public Optional<User> safeRemove(Long id) {
|
||||
Optional<User> optionalUser = userRepository.findById(id);
|
||||
if(optionalUser.isPresent())
|
||||
{
|
||||
em.createQuery("Delete from Comment Where user.id = :id")
|
||||
.setParameter("id",id)
|
||||
.executeUpdate();
|
||||
em.createQuery("Delete from Post Where user.id = :id")
|
||||
.setParameter("id",id)
|
||||
.executeUpdate();
|
||||
em.remove(optionalUser.get());
|
||||
}
|
||||
return optionalUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void safeRemoveAll() {
|
||||
em.createQuery("Delete from Comment").executeUpdate();
|
||||
em.createQuery("Delete from Post").executeUpdate();
|
||||
em.createQuery("delete from User").executeUpdate();
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removePost(Long id, Long postId) {
|
||||
em.createQuery("Delete Comment where post.Id = :postId")
|
||||
.setParameter("postId",postId)
|
||||
.executeUpdate();
|
||||
em.createQuery("Delete from Post where Id = :postId")
|
||||
.setParameter("postId",postId)
|
||||
.executeUpdate();
|
||||
}
|
||||
}
|
@ -1,102 +1,74 @@
|
||||
package ru.ulstu.is.sbapp.User.service;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostNotFoundException;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import ru.ulstu.is.sbapp.Comment.service.CommentService;
|
||||
import ru.ulstu.is.sbapp.User.repository.UserRepository;
|
||||
import ru.ulstu.is.sbapp.Util.validation.ValidatorUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Autowired
|
||||
CommentService commentService;
|
||||
private final UserRepository userRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public UserService(UserRepository userRepository, ValidatorUtil validatorUtil)
|
||||
{
|
||||
this.userRepository=userRepository;
|
||||
this.validatorUtil=validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public User addUser(String firstName, String lastName, String 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);
|
||||
em.persist(user);
|
||||
return user;
|
||||
validatorUtil.validate(user);
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public User findUser(Long id) {
|
||||
final User user = em.find(User.class, id);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
||||
}
|
||||
return user;
|
||||
final Optional<User> user = userRepository.findById(id);
|
||||
return user.orElseThrow(() -> new UserNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<User> findAllUsers() {
|
||||
return em.createQuery("select u from User u", User.class)
|
||||
.getResultList();
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public User updateUser(Long id, String firstName, String lastName, String 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);
|
||||
currentUser.setFirstName(firstName);
|
||||
currentUser.setLastName(lastName);
|
||||
currentUser.setEmail(email);
|
||||
return em.merge(currentUser);
|
||||
validatorUtil.validate(currentUser);
|
||||
return userRepository.save(currentUser);
|
||||
}
|
||||
|
||||
@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.remove(currentUser);
|
||||
return currentUser;
|
||||
final Optional<User> user = userRepository.safeRemove(id);
|
||||
return user.orElseThrow(() -> new PostNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllUsers() {
|
||||
em.createQuery("Delete from Post").executeUpdate();
|
||||
em.createQuery("Delete from Comment").executeUpdate();
|
||||
em.createQuery("delete from User").executeUpdate();
|
||||
userRepository.safeRemoveAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
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);
|
||||
public void addNewPost(Long id, String Heading, String Content) {
|
||||
userRepository.addPost(id,Heading,Content);
|
||||
}
|
||||
|
||||
@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();
|
||||
public void deletePost(Long id, Long postId) {
|
||||
userRepository.removePost(id,postId);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,10 @@ 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.PostNotFoundException;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostService;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
import ru.ulstu.is.sbapp.User.service.UserNotFoundException;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -47,7 +49,7 @@ public class JpaClientTests {
|
||||
@Test
|
||||
void testClientReadNotFound() {
|
||||
userService.deleteAllUsers();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> userService.findUser(-1L));
|
||||
Assertions.assertThrows(UserNotFoundException.class, () -> userService.findUser(-1L));
|
||||
}
|
||||
@Test
|
||||
void testClientReadAll() {
|
||||
@ -74,8 +76,8 @@ public class JpaClientTests {
|
||||
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 Post post1=userService.addNewPost(user.getId(),"Привет","Да");
|
||||
userService.addNewPost(user.getId(),"Text1","Text2"); //post
|
||||
userService.addNewPost(user.getId(),"Привет","Да"); //post1
|
||||
final List<Post> posts =postService.findAllPosts();
|
||||
final User user1 = userService.findUser(user.getId());
|
||||
final List<Post> posts1 = user1.getPosts();
|
||||
@ -83,9 +85,9 @@ public class JpaClientTests {
|
||||
Assertions.assertEquals(posts.get(0).getUser(), user);
|
||||
Assertions.assertEquals(posts.toString(), posts1.toString());
|
||||
log.info("testAddAndDeletePost :: Delete ");
|
||||
userService.deletePost(user.getId(),post);
|
||||
userService.deletePost(user.getId(),posts.get(0).getId());
|
||||
final User us = userService.findUser(user.getId());
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> postService.findPost(post.getId()));
|
||||
Assertions.assertThrows(PostNotFoundException.class, () -> postService.findPost(posts.get(0).getId()));
|
||||
final List<Post> postss =postService.findAllPosts();
|
||||
log.info(us.getPosts().toString());
|
||||
log.info(postss.toString());
|
||||
@ -93,25 +95,5 @@ public class JpaClientTests {
|
||||
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())));
|
||||
|
||||
}*/
|
||||
}
|
||||
|
@ -100,17 +100,18 @@ public class JpaPostTests {
|
||||
postService.deleteAllPosts();
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final Post post =userService.addNewPost(user.getId(),"Text1","Text2");
|
||||
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());
|
||||
final List<Post> posts = postService.findAllPosts();
|
||||
final Post post2=postService.findPost(posts.get(0).getId());
|
||||
log.info(post2.toString());
|
||||
postService.addCommentToPost(post.getId(),user.getId(),"Крутой пост");
|
||||
postService.addCommentToPost(posts.get(0).getId(),user.getId(),"Крутой пост");
|
||||
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());
|
||||
final Post post1=postService.findPost(posts.get(0).getId());
|
||||
log.info(post1.getComments().toString());
|
||||
}
|
||||
@Test
|
||||
@ -120,26 +121,27 @@ public class JpaPostTests {
|
||||
postService.deleteAllPosts();
|
||||
userService.deleteAllUsers();
|
||||
final User user = userService.addUser("Pasha","Sorokin","sorokin.zxcv@gmail.com");
|
||||
final Post post =userService.addNewPost(user.getId(),"Text1","Text2");
|
||||
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());
|
||||
final List<Post> posts = postService.findAllPosts();
|
||||
final Post post2=postService.findPost(posts.get(0).getId());
|
||||
log.info("Пост который добавили"+post2.toString());
|
||||
postService.addCommentToPost(post.getId(),user.getId(),"Крутой пост");
|
||||
postService.addCommentToPost(post.getId(),user.getId(),"Пост плохой");
|
||||
postService.addCommentToPost(posts.get(0).getId(),user.getId(),"Крутой пост");
|
||||
postService.addCommentToPost(posts.get(0).getId(),user.getId(),"Пост плохой");
|
||||
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());
|
||||
final Post post1=postService.findPost(posts.get(0).getId());
|
||||
log.info("Комменты к посту"+post1.getComments().toString());
|
||||
Comment comment1 = commentService.findComment(comments.get(0).getId());
|
||||
log.info("Удаляем коммент");
|
||||
postService.removeCommentFromPost(post.getId(),comment1.getId());
|
||||
postService.removeCommentFromPost(posts.get(0).getId(),comment1.getId());
|
||||
final User user3=userService.findUser(user.getId());
|
||||
log.info(user3.getPosts().toString());
|
||||
log.info("Комменты юзера после удаления"+user3.getComments().toString());
|
||||
final Post post3 = postService.findPost(post.getId());
|
||||
final Post post3 = postService.findPost(posts.get(0).getId());
|
||||
log.info(post3.toString());
|
||||
log.info(post3.getComments().toString());
|
||||
final List<Comment> comment4=commentService.findAllComments();
|
||||
|
Loading…
x
Reference in New Issue
Block a user