Commit d297b635 by 胡学良

删除评论

parent f6eeb5d6
......@@ -30,6 +30,14 @@ public class QuestionReplyCommentApi {
this.questionReplyCommentService = questionReplyCommentService;
}
/**
* 发表评论
* @param questionReplyCommentForm 评论表单
* @param bindingResult 校验信息
* @param replyId 回复ID
* @param subject 访问者信息
* @return default
*/
@ApiOperation("发表评论")
@PostMapping("/question/reply/{replyId}/comment")
public Response publishReplyComment(@RequestBody @Valid QuestionReplyCommentForm questionReplyCommentForm, BindingResult bindingResult,
......@@ -47,4 +55,22 @@ public class QuestionReplyCommentApi {
return Response.error(e);
}
}
/**
* 删除评论
* @param commentId 评论ID
* @param subject 访问者信息
* @return default
*/
@ApiOperation("删除评论")
@DeleteMapping("/question/reply/comment/{commentId}")
public Response deleteReplyComment(@PathVariable("commentId") String commentId, @CurrentSubject Subject subject) {
try {
questionReplyCommentService.deleteReplyComment((String) subject.getId(), commentId);
return Response.success();
} catch (QuestionReplyCommentServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
}
......@@ -7,7 +7,18 @@ import cn.meteor.beyondclouds.core.IErrorCode;
* @since 2020/2/2
*/
public enum QuestionReplyCommentErrorCode implements IErrorCode {
COMMENT_NOT_FOUND(5001, "父评论不存在");
/**
* 评论不存在
*/
COMMENT_NOT_FOUND(5002, "该评论不存在"),
/**
* 父评论不存在
*/
PARENT_COMMENT_NOT_FOUND(5001, "父评论不存在"),
/**
* 无权删除评论
*/
NO_DELETE_PRIVILEGES(5003,"无权删除该评论 ");
private long code;
private String msg;
......
......@@ -23,4 +23,12 @@ public interface IQuestionReplyCommentService extends IService<QuestionReplyComm
* @throws QuestionReplyCommentServiceException 问题回复评论业务异常
*/
void publishReplyComment(String userId, String replyId, String parentId, String comment) throws QuestionReplyCommentServiceException;
/**
* 删除评论
* @param userId 用户ID
* @param commentId 评论ID
* @throws QuestionReplyCommentServiceException 问题回复评论业务异常
*/
void deleteReplyComment(String userId, String commentId) throws QuestionReplyCommentServiceException;
}
package cn.meteor.beyondclouds.modules.question.service.impl;
import cn.meteor.beyondclouds.modules.question.entity.Question;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReply;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReplyComment;
import cn.meteor.beyondclouds.modules.question.enums.QuestionReplyCommentErrorCode;
......@@ -8,6 +9,7 @@ import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyCommentSer
import cn.meteor.beyondclouds.modules.question.mapper.QuestionReplyCommentMapper;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyCommentService;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyService;
import cn.meteor.beyondclouds.modules.question.service.IQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -26,6 +28,8 @@ import org.springframework.util.StringUtils;
@Service
public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCommentMapper, QuestionReplyComment> implements IQuestionReplyCommentService {
private IQuestionService questionService;
private IQuestionReplyService questionReplyService;
@Autowired
......@@ -33,9 +37,15 @@ public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCo
this.questionReplyService = questionReplyService;
}
@Autowired
public void setQuestionService(IQuestionService questionService) {
this.questionService = questionService;
}
@Override
public void publishReplyComment(String userId, String replyId, String parentId, String comment) throws QuestionReplyCommentServiceException {
Assert.hasText(replyId, "replyId must not be empty");
Assert.notNull(replyId, "replyId must not be null");
Assert.hasText(comment, "comment must not be empty");
//1.判断是否存在该回复
QuestionReply questionReply = questionReplyService.getById(replyId);
......@@ -54,7 +64,7 @@ public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCo
parentQuestionReplyComment = getOne(questionReplyCommentQueryWrapper);
//若父评论为空,则抛出父评论不存在异常
if (null == parentQuestionReplyComment) {
throw new QuestionReplyCommentServiceException(QuestionReplyCommentErrorCode.COMMENT_NOT_FOUND);
throw new QuestionReplyCommentServiceException(QuestionReplyCommentErrorCode.PARENT_COMMENT_NOT_FOUND);
}
}
......@@ -78,4 +88,37 @@ public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCo
}
updateById(questionReplyComment);
}
@Override
public void deleteReplyComment(String userId, String commentId) throws QuestionReplyCommentServiceException {
Assert.notNull(commentId, "commentId must not be null");
Assert.notNull(userId, "userId must not be null");
//1.判断是否存在该评论
QuestionReplyComment questionReplyComment = getById(commentId);
if (null == questionReplyComment) {
throw new QuestionReplyCommentServiceException(QuestionReplyCommentErrorCode.COMMENT_NOT_FOUND);
}
//2.判断用户是否有权限删除该评论
if (!questionReplyComment.getUserId().equals(userId)) {
//用户未发表过该评论,判断用户是否为该回复或者该问题的发布者
//得到评论对应的回复对象
QuestionReply questionReply = questionReplyService.getById(questionReplyComment.getReplyId());
//得到回复对应的问题对象
Question question = questionService.getById(questionReply.getQuestionId());
//判断用户是否为该回复或该问题的发布者
if (!question.getUserId().equals(userId) && !questionReply.getUserId().equals(userId)) {
throw new QuestionReplyCommentServiceException(QuestionReplyCommentErrorCode.NO_DELETE_PRIVILEGES);
}
}
//3.删除评论及其子评论
QueryWrapper<QuestionReplyComment> questionReplyCommentQueryWrapper = new QueryWrapper<>();
questionReplyCommentQueryWrapper.like("thread",questionReplyComment.getThread());
remove(questionReplyCommentQueryWrapper);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment