src/main/java/com/forum/service/CommentsService.java
package com.forum.service;
import com.forum.dto.CommentsDto;
import com.forum.exceptions.PostNotFoundException;
import com.forum.mapper.CommentMapper;
import com.forum.model.Comment;
import com.forum.model.NotificationEmail;
import com.forum.model.Post;
import com.forum.model.User;
import com.forum.repository.CommentRepository;
import com.forum.repository.PostRepository;
import com.forum.repository.UserRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;
import static java.util.stream.Collectors.toList;
@Service
@AllArgsConstructor
@Slf4j
public class CommentsService {
private final PostRepository postRepository;
private final UserRepository userRepository;
private final CommentRepository commentRepository;
private final AuthService authService;
private final CommentMapper commentMapper;
private static final String POST_URL = "";
private final MailContentBuilder mailContentBuilder;
private final MailService mailService;
public void create(CommentsDto commentsDto) {
Post post = postRepository.findById(commentsDto.getPostId()).orElseThrow(() -> new PostNotFoundException(commentsDto.getPostId().toString()));
User currentUser = authService.getCurrentUser();
Comment comment = commentMapper.map(commentsDto, post, currentUser);
commentRepository.save(comment);
String message = mailContentBuilder.build(post.getUser().getUsername() + " posted a comment on your post." + POST_URL);
sendCommentNotification(message, post.getUser());
}
private void sendCommentNotification(String message, User user) {
mailService.sendMail(new NotificationEmail(user.getUsername() + " Commented on your post", user.getEmail(), message));
}
public List<CommentsDto> getAllCommentsForPost(Long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new PostNotFoundException(postId.toString()));
return commentRepository.findByPost(post)
.stream()
.map(commentMapper::mapToDto)
.collect(toList());
}
public List<CommentsDto> getAllCommentsForUser(String userName) {
User user = userRepository.findByUsername(userName).orElseThrow(() -> new UsernameNotFoundException(userName));
return commentRepository.findAllByUser(user)
.stream()
.map(commentMapper::mapToDto)
.collect(toList());
}
}