ReplyServiceImpl.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package nju.seec.helper.service.impl;
  2. import lombok.extern.slf4j.Slf4j;
  3. import nju.seec.helper.dao.CommentDAO;
  4. import nju.seec.helper.dao.ReplyDAO;
  5. import nju.seec.helper.dao.SlideDAO;
  6. import nju.seec.helper.dto.LoginUser;
  7. import nju.seec.helper.dto.ReplyDTO;
  8. import nju.seec.helper.entity.Comment;
  9. import nju.seec.helper.entity.Reply;
  10. import nju.seec.helper.entity.Slide;
  11. import nju.seec.helper.service.MessageService;
  12. import nju.seec.helper.service.ReplyService;
  13. import nju.seec.helper.service.util.AuthUtils;
  14. import nju.seec.helper.util.enums.ExceptionType;
  15. import nju.seec.helper.util.enums.MessageType;
  16. import nju.seec.helper.util.exception.HelperException;
  17. import nju.seec.helper.vo.ReplyVO;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import java.util.Collections;
  21. /**
  22. * @author cst
  23. */
  24. @Slf4j
  25. @Service
  26. public class ReplyServiceImpl implements ReplyService {
  27. private final SlideDAO slideDAO;
  28. private final CommentDAO commentDAO;
  29. private final ReplyDAO replyDAO;
  30. private final MessageService messageService;
  31. public ReplyServiceImpl(SlideDAO slideDAO, CommentDAO commentDAO, ReplyDAO replyDAO, MessageService messageService) {
  32. this.slideDAO = slideDAO;
  33. this.commentDAO = commentDAO;
  34. this.replyDAO = replyDAO;
  35. this.messageService = messageService;
  36. }
  37. @Transactional(rollbackFor = Exception.class)
  38. @Override
  39. public ReplyVO createReply(LoginUser user, ReplyDTO replyDTO) {
  40. Comment comment = commentDAO.findCommentById(replyDTO.getCommentId());
  41. AuthUtils.checkDataAuth(user.getId(), slideDAO.findSlideById(comment.getSlideId()).getTeacherId(), "您无权回复该评论");
  42. if (comment.getReply() != null) {
  43. throw HelperException.of(ExceptionType.CONFLICT, "您已回复该评论");
  44. }
  45. Reply reply = new Reply()
  46. .setComment(comment)
  47. .setTeacherId(user.getId())
  48. .setContent(replyDTO.getContent());
  49. reply.setComment(comment);
  50. reply = replyDAO.save(reply);
  51. // 增加通知
  52. if (!user.getId().equals(comment.getUserId())) {
  53. Slide slide = slideDAO.findSlideById(comment.getSlideId());
  54. messageService.createMessage(Collections.singleton(comment.getUserId()), MessageType.COMMENT_REPLY, String.format("您在课件「%s」发表的题为「%s」的讨论帖已收到回复", comment.getTitle(), slide.getName()), slide.getId());
  55. }
  56. return new ReplyVO(reply);
  57. }
  58. @Transactional(rollbackFor = Exception.class)
  59. @Override
  60. public void removeReply(LoginUser user, Integer replyId) {
  61. Reply reply = replyDAO.findReplyById(replyId);
  62. AuthUtils.checkDataAuth(user.getId(), reply.getTeacherId(), "您无权回复该评论");
  63. Comment comment = reply.getComment();
  64. comment.setReply(null);
  65. replyDAO.delete(reply);
  66. }
  67. }