| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package nju.seec.helper.service.impl;
- import nju.seec.helper.dao.CommentDAO;
- import nju.seec.helper.dao.SlideDAO;
- import nju.seec.helper.dto.CommentDTO;
- import nju.seec.helper.dto.LoginUser;
- import nju.seec.helper.entity.Comment;
- import nju.seec.helper.entity.Slide;
- import nju.seec.helper.service.CommentService;
- import nju.seec.helper.service.MessageService;
- import nju.seec.helper.service.util.AuthUtils;
- import nju.seec.helper.util.enums.MessageType;
- import nju.seec.helper.vo.CommentVO;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.Collections;
- /**
- * @author cst
- */
- @Service
- public class CommentServiceImpl implements CommentService {
- private final CommentDAO commentDAO;
- private final SlideDAO slideDAO;
- private final MessageService messageService;
- public CommentServiceImpl(CommentDAO commentDAO, SlideDAO slideDAO, MessageService messageService) {
- this.commentDAO = commentDAO;
- this.slideDAO = slideDAO;
- this.messageService = messageService;
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public CommentVO createComment(LoginUser user, CommentDTO commentDTO) {
- Comment comment = new Comment()
- .setSlideId(commentDTO.getSlideId())
- .setPageNumber(commentDTO.getPageNumber())
- .setUserId(user.getId())
- .setUserName(user.getName())
- .setUserType(user.getType())
- .setTitle(commentDTO.getTitle())
- .setContent(commentDTO.getContent());
- comment = commentDAO.save(comment);
- // 增加通知
- Slide slide = slideDAO.findSlideById(comment.getSlideId());
- if (!user.getId().equals(slide.getTeacherId())) {
- messageService.createMessage(Collections.singleton(slide.getTeacherId()), MessageType.COMMENT_NEW, String.format("课程 [%s] 的课件 [%s] 在第%d页有新评论", slide.getCourseName(), slide.getName(), comment.getPageNumber()), slide.getId());
- }
- return new CommentVO(comment);
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void removeComment(LoginUser user, Long commentId) {
- Comment comment = commentDAO.findCommentById(commentId);
- AuthUtils.checkDataAuth(user.getId(), comment.getUserId(), "您无权删除该评论");
- unTopComment(comment);
- commentDAO.delete(comment);
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void topComment(LoginUser user, Long commentId) {
- Comment comment = commentDAO.findCommentById(commentId);
- AuthUtils.checkDataAuth(user.getId(), slideDAO.findSlideById(comment.getSlideId()).getTeacherId(), "您无权在该评论区置顶评论");
- unTopComment(comment);
- comment.setTopNumber(commentDAO.countTopComments(comment.getSlideId(), comment.getPageNumber()) + 1);
- commentDAO.save(comment);
- }
- @Transactional(rollbackFor = Exception.class)
- @Override
- public void unTopComment(LoginUser user, Long commentId) {
- Comment comment = commentDAO.findCommentById(commentId);
- AuthUtils.checkDataAuth(user.getId(), slideDAO.findSlideById(comment.getSlideId()).getTeacherId(), "您无权在该评论区取消置顶评论");
- unTopComment(comment);
- commentDAO.save(comment);
- }
- private void unTopComment(Comment comment) {
- Integer topNumber = comment.getTopNumber();
- if (topNumber != null) {
- comment.setTopNumber(null);
- commentDAO.decTopNumber(comment.getSlideId(), comment.getPageNumber(), topNumber);
- }
- }
- @Transactional(readOnly = true)
- @Override
- public Page<CommentVO> getCommentsBySlideAndPageNumber(Long slideId, Integer pageNumber, Pageable pageable) {
- return commentDAO.findBySlideIdAndPageNumber(slideId, pageNumber, pageable).map(CommentVO::new);
- }
- @Transactional(readOnly = true)
- @Override
- public CommentVO getOneComment(Long commentId) {
- return new CommentVO(commentDAO.findCommentById(commentId));
- }
- }
|