| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package nju.seec.helper.service.impl;
- import lombok.extern.slf4j.Slf4j;
- import nju.seec.helper.dao.CommentDAO;
- import nju.seec.helper.dao.ReplyDAO;
- import nju.seec.helper.dao.SlideDAO;
- import nju.seec.helper.dao.UserDAO;
- import nju.seec.helper.dto.reply.ReplyDTO;
- import nju.seec.helper.aspect.auth.LoginUser;
- import nju.seec.helper.entity.Comment;
- import nju.seec.helper.entity.Reply;
- import nju.seec.helper.entity.Slide;
- import nju.seec.helper.service.MessageService;
- import nju.seec.helper.service.ReplyService;
- import nju.seec.helper.service.util.AuthUtils;
- import nju.seec.helper.enums.ExceptionType;
- import nju.seec.helper.enums.MessageType;
- import nju.seec.helper.exception.HelperException;
- import nju.seec.helper.vo.ReplyVO;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Collections;
- /**
- * @author cst
- */
- @Slf4j
- @Service
- public class ReplyServiceImpl implements ReplyService {
- private final SlideDAO slideDAO;
- private final CommentDAO commentDAO;
- private final ReplyDAO replyDAO;
- private final UserDAO userDAO;
- private final MessageService messageService;
- public ReplyServiceImpl(SlideDAO slideDAO, CommentDAO commentDAO, ReplyDAO replyDAO, UserDAO userDAO, MessageService messageService) {
- this.slideDAO = slideDAO;
- this.commentDAO = commentDAO;
- this.replyDAO = replyDAO;
- this.userDAO = userDAO;
- this.messageService = messageService;
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public ReplyVO createReply(LoginUser user, ReplyDTO replyDTO) {
- Comment comment = commentDAO.findCommentById(replyDTO.getCommentId());
- AuthUtils.checkDataAuth(user.getId(), slideDAO.findSlideById(comment.getSlideId()).getTeacher().getId(), "您无权回复该评论");
- if (comment.getReply() != null) {
- throw HelperException.of(ExceptionType.CONFLICT, "该评论已回复");
- }
- Reply reply = new Reply()
- .setComment(comment)
- .setTeacher(userDAO.findUserById(user.getId()))
- .setContent(replyDTO.getContent());
- comment.setReply(reply);
- reply.setComment(comment);
- reply = replyDAO.save(reply);
- // 增加通知
- if (!user.getId().equals(comment.getUser().getId())) {
- Slide slide = slideDAO.findSlideById(comment.getSlideId());
- messageService.createMessage(Collections.singleton(comment.getUser().getId()), MessageType.COMMENT_REPLY, String.format("您在 [%s] - [%s] 第%d页发表的讨论帖已收到回复", slide.getCourse().getName(), slide.getName(), comment.getPageNumber()), slide.getId());
- }
- return new ReplyVO(reply);
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void deleteReply(LoginUser user, Long replyId) {
- Reply reply = replyDAO.findReplyById(replyId);
- AuthUtils.checkDataAuth(user.getId(), reply.getTeacher().getId(), "您无权删除该评论");
- Comment comment = reply.getComment();
- comment.setReply(null);
- replyDAO.delete(reply);
- }
- }
|