From 072a9514b6851741d33bff871b72c784e3c92ceb Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 20 Jun 2023 10:19:53 +0400 Subject: [PATCH] services --- .../webproglabs/lab1/dao/UserRepository.java | 3 +- .../lab1/services/CommentService.java | 95 +++++++++++++++++++ .../lab1/services/PostService.java | 95 +++++++++++++++++++ .../lab1/services/TopicService.java | 62 ++++++++++++ .../lab1/services/UserService.java | 92 ++++++++++++++++++ 5 files changed, 345 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/webproglabs/lab1/services/CommentService.java create mode 100644 src/main/java/com/webproglabs/lab1/services/PostService.java create mode 100644 src/main/java/com/webproglabs/lab1/services/TopicService.java create mode 100644 src/main/java/com/webproglabs/lab1/services/UserService.java diff --git a/src/main/java/com/webproglabs/lab1/dao/UserRepository.java b/src/main/java/com/webproglabs/lab1/dao/UserRepository.java index 00ea705..9279fa9 100644 --- a/src/main/java/com/webproglabs/lab1/dao/UserRepository.java +++ b/src/main/java/com/webproglabs/lab1/dao/UserRepository.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Optional; public interface UserRepository extends JpaRepository { - List findByLoginLike(String login); - User findOneByLoginIgnoreCase(String login); + Optional findOneByLoginIgnoreCase(String login); Optional findById(Long Id); } diff --git a/src/main/java/com/webproglabs/lab1/services/CommentService.java b/src/main/java/com/webproglabs/lab1/services/CommentService.java new file mode 100644 index 0000000..f073a4c --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/services/CommentService.java @@ -0,0 +1,95 @@ +package com.webproglabs.lab1.services; + +import com.webproglabs.lab1.dao.CommentRepository; +import com.webproglabs.lab1.dao.PostRepository; +import com.webproglabs.lab1.dao.UserRepository; +import com.webproglabs.lab1.models.Comment; +import com.webproglabs.lab1.models.Post; +import com.webproglabs.lab1.models.User; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityNotFoundException; +import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; + +@Service +public class CommentService { + private final UserRepository userRepository; + private final CommentRepository commentRepository; + private final PostRepository postRepository; + + public CommentService(UserRepository profileRepository, CommentRepository commentRepository, PostRepository postRepository) { + this.userRepository = profileRepository; + this.commentRepository = commentRepository; + this.postRepository = postRepository; + } + + @Transactional + public Comment findCommentById(Long id) { + final Optional comment = commentRepository.findById(id); + return comment.orElseThrow(EntityNotFoundException::new); + } + + @Transactional + public List findAllComments() { + return commentRepository.findAll(); + } + + @Transactional + public List findFilteredComments(String filter) { + List postList = postRepository.findByTextLike("%" + filter + "%"); + List commentList = commentRepository.findByTextLike("%" + filter + "%"); + List allPosts = postRepository.findAll(); + for(Post post : allPosts) { + for (Comment comm : commentList) { + if (post.getComments().contains(comm) && !(postList.contains(post))) { + postList.add(post); + } + } + } + return postList; + } + + @Transactional + public Comment addComment(String text, Long userId, Long postId) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Comment data is null or empty"); + } + try{ + User user = userRepository.findById(userId).get(); + Post post = postRepository.findById(postId).get(); + Comment comment = new Comment(text, user, post); + user.addComment(comment); + post.addComment(comment); + userRepository.save(user); + postRepository.save(post); + return commentRepository.save(comment); + } + catch (Exception exception) { + return null; + } + } + + @Transactional + public Comment updateComment(Long id, String text) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Comment data is null or empty"); + } + Comment currentComment = findCommentById(id); + currentComment.setText(text); + final User owner = currentComment.getOwner(); + userRepository.save(owner); + final Post post = currentComment.getPost(); + postRepository.save(post); + return commentRepository.save(currentComment); + } + + @Transactional + public Comment deleteComment(Long id) { + final Comment currentComment = findCommentById(id); + commentRepository.delete(currentComment); + return currentComment; + } +} diff --git a/src/main/java/com/webproglabs/lab1/services/PostService.java b/src/main/java/com/webproglabs/lab1/services/PostService.java new file mode 100644 index 0000000..2cf1a08 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/services/PostService.java @@ -0,0 +1,95 @@ +package com.webproglabs.lab1.services; + +import com.webproglabs.lab1.dao.CommentRepository; +import com.webproglabs.lab1.dao.PostRepository; +import com.webproglabs.lab1.dao.TopicRepository; +import com.webproglabs.lab1.dao.UserRepository; +import com.webproglabs.lab1.models.Comment; +import com.webproglabs.lab1.models.Post; +import com.webproglabs.lab1.models.Topic; +import com.webproglabs.lab1.models.User; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityNotFoundException; +import javax.transaction.Transactional; +import java.util.List; +import java.util.Optional; + +@Service +public class PostService { + private final PostRepository postRepository; + private final CommentRepository commentRepository; + private final UserRepository userRepository; + + private final TopicRepository topicRepository; + + public PostService(PostRepository postRepository, CommentRepository commentRepository, UserRepository userRepository, TopicRepository topicRepository) { + this.postRepository = postRepository; + this.commentRepository = commentRepository; + this.userRepository = userRepository; + this.topicRepository = topicRepository; + } + + @Transactional + public Post findPostById(Long id) { + final Optional post = postRepository.findById(id); + return post.orElseThrow(EntityNotFoundException::new); + } + + @Transactional + public List findAllPosts() { + return postRepository.findAll(); + } + + @Transactional + public List findFilteredPosts(String filter) { + List postList = postRepository.findByTextLike("%" + filter + "%"); + List commentList = commentRepository.findByTextLike("%" + filter + "%"); + List allPosts = postRepository.findAll(); + for(Post post : allPosts) { + for (Comment comm : commentList) { + if (post.getComments().contains(comm) && !(postList.contains(post))) { + postList.add(post); + } + } + } + return postList; + } + + @Transactional + public Post addPost (String text, Long authorId, Long topicId) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Post data is null or empty"); + } + User author = userRepository.findById(authorId).get(); + Topic topic = topicRepository.findById(topicId).get(); + Post post = new Post(text, author, topic); + author.addPost(post); + topic.addPost(post); + userRepository.save(author); + topicRepository.save(topic); + return postRepository.save(post); + } + + @Transactional + public Post updatePost(Long id, String text) { + if (!StringUtils.hasText(text)) { + throw new IllegalArgumentException("Post data is null or empty"); + } + final Post currentPost = findPostById(id); + currentPost.setText(text); + final User author = currentPost.getAuthor(); + userRepository.save(author); + final Topic topic = currentPost.getTopic(); + topicRepository.save(topic); + return postRepository.save(currentPost); + } + + @Transactional + public Post deletePost(Long id) { + final Post currentPost = findPostById(id); + postRepository.delete(currentPost); + return currentPost; + } +} diff --git a/src/main/java/com/webproglabs/lab1/services/TopicService.java b/src/main/java/com/webproglabs/lab1/services/TopicService.java new file mode 100644 index 0000000..d2e2f0c --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/services/TopicService.java @@ -0,0 +1,62 @@ +package com.webproglabs.lab1.services; + +import com.webproglabs.lab1.dao.TopicRepository; +import com.webproglabs.lab1.models.Post; +import com.webproglabs.lab1.models.Topic; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityNotFoundException; +import javax.transaction.Transactional; +import java.util.List; + +@Service +public class TopicService { + private final TopicRepository topicRepository; + + public TopicService(TopicRepository topicRepository) { + this.topicRepository = topicRepository; + } + + @Transactional + public Topic addTopic(String name, String description) { + if (!StringUtils.hasText(name) || !StringUtils.hasText(description)) { + throw new IllegalArgumentException("Name is null or empty"); + } + final Topic topic = new Topic(name, description); + return topicRepository.save(topic); + } + + @Transactional + public Topic findTopicById(Long id) { + final Topic topic = topicRepository.findById(id).orElse(null); + if (topic == null) { + throw new EntityNotFoundException(String.format(" topic with id [%s] is not found", id)); + } + return topic; + } + + @Transactional + public List findAllTopics() { + return topicRepository.findAll(); + } + + @Transactional + public Topic updateTopic(Long id, String name, String description) { + if (!StringUtils.hasText(name) || !StringUtils.hasText(description)) { + throw new IllegalArgumentException("Name is null or empty"); + } + final Topic topic = findTopicById(id); + topic.setName(name); + topic.setDescription(description); + return topicRepository.save(topic); + } + + @Transactional + public Topic deleteTopic(Long id) { + final Topic topic = findTopicById(id); + topicRepository.delete(topic); + return topic; + } + +} diff --git a/src/main/java/com/webproglabs/lab1/services/UserService.java b/src/main/java/com/webproglabs/lab1/services/UserService.java new file mode 100644 index 0000000..564614a --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/services/UserService.java @@ -0,0 +1,92 @@ +package com.webproglabs.lab1.services; + +import com.webproglabs.lab1.dao.UserRepository; +import com.webproglabs.lab1.models.User; +import com.webproglabs.lab1.models.UserRole; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.core.userdetails.UsernameNotFoundException; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityNotFoundException; +import javax.transaction.Transactional; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +@Service +public class UserService implements UserDetailsService { + private final UserRepository userRepository; + private final PasswordEncoder passwordEncoder; + + public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) { + this.userRepository = userRepository; + this.passwordEncoder = passwordEncoder; + } + + @Transactional + public User findUserById(Long id) { + final Optional user = userRepository.findById(id); + return user.orElseThrow(EntityNotFoundException::new); + } + + @Transactional + public User findUserByLogin(String login) { + final Optional user = userRepository.findOneByLoginIgnoreCase(login); + return user.orElseThrow(EntityNotFoundException::new); + } + + @Transactional + public List findAllUsers() { + return userRepository.findAll(); + } + + @Transactional + public User createUser(String login, String password, String passwordConfirm, UserRole role) { + if (findUserByLogin(login) != null) { + throw new IllegalArgumentException("User " + login + " already exists"); + } + final User user = new User(login, passwordEncoder.encode(password), role); + if (!Objects.equals(password, passwordConfirm)) { + throw new IllegalArgumentException("Passwords not equals"); + } + return userRepository.save(user); + } + + @Transactional + public User createUser(String login, String password, String passwordConfirm) { + return createUser(login, password, passwordConfirm, UserRole.USER); + } + + @Transactional + public User updateUser(Long id, String login, String password) { + if (!StringUtils.hasText(login) || !StringUtils.hasText(password)) { + throw new IllegalArgumentException("User data is null or empty"); + } + final User currentUser = findUserById(id); + currentUser.setLogin(login); + currentUser.setPassword(password); + return userRepository.save(currentUser); + } + + @Transactional + public User deleteUser(Long id) { + final User currentUser = findUserById(id); + userRepository.delete(currentUser); + return currentUser; + } + + + @Override + public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { + final User userEntity = findUserByLogin(username); + if (userEntity == null) { + throw new UsernameNotFoundException(username); + } + return new org.springframework.security.core.userdetails.User( + userEntity.getLogin(), userEntity.getPassword(), Collections.singleton(userEntity.getRole())); + } +}