This commit is contained in:
1yuee 2023-06-20 10:19:53 +04:00
parent db18f20ff6
commit 072a9514b6
5 changed files with 345 additions and 2 deletions

View File

@ -7,7 +7,6 @@ import java.util.List;
import java.util.Optional;
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLoginLike(String login);
User findOneByLoginIgnoreCase(String login);
Optional<User> findOneByLoginIgnoreCase(String login);
Optional<User> findById(Long Id);
}

View File

@ -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> comment = commentRepository.findById(id);
return comment.orElseThrow(EntityNotFoundException::new);
}
@Transactional
public List<Comment> findAllComments() {
return commentRepository.findAll();
}
@Transactional
public List<Post> findFilteredComments(String filter) {
List<Post> postList = postRepository.findByTextLike("%" + filter + "%");
List<Comment> commentList = commentRepository.findByTextLike("%" + filter + "%");
List<Post> 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;
}
}

View File

@ -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> post = postRepository.findById(id);
return post.orElseThrow(EntityNotFoundException::new);
}
@Transactional
public List<Post> findAllPosts() {
return postRepository.findAll();
}
@Transactional
public List<Post> findFilteredPosts(String filter) {
List<Post> postList = postRepository.findByTextLike("%" + filter + "%");
List<Comment> commentList = commentRepository.findByTextLike("%" + filter + "%");
List<Post> 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;
}
}

View File

@ -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<Topic> 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;
}
}

View File

@ -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> user = userRepository.findById(id);
return user.orElseThrow(EntityNotFoundException::new);
}
@Transactional
public User findUserByLogin(String login) {
final Optional<User> user = userRepository.findOneByLoginIgnoreCase(login);
return user.orElseThrow(EntityNotFoundException::new);
}
@Transactional
public List<User> 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()));
}
}