Commit f6eeb5d6 by 胡学良

发表评论

parent b7efbf09
package cn.meteor.beyondclouds.modules.question.api;
import cn.meteor.beyondclouds.core.annotation.CurrentSubject;
import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyCommentServiceException;
import cn.meteor.beyondclouds.modules.question.form.QuestionReplyCommentForm;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
/**
* @author 胡学良
* @since 2020/2/2
*/
@Api(tags = "问题回复的评论Api")
@RestController
@RequestMapping("/api")
public class QuestionReplyCommentApi {
private IQuestionReplyCommentService questionReplyCommentService;
@Autowired
public QuestionReplyCommentApi(IQuestionReplyCommentService questionReplyCommentService) {
this.questionReplyCommentService = questionReplyCommentService;
}
@ApiOperation("发表评论")
@PostMapping("/question/reply/{replyId}/comment")
public Response publishReplyComment(@RequestBody @Valid QuestionReplyCommentForm questionReplyCommentForm, BindingResult bindingResult,
@PathVariable("replyId") String replyId, @CurrentSubject Subject subject){
if (bindingResult.hasErrors()) {
return Response.fieldError(bindingResult.getFieldError());
}
try {
questionReplyCommentService.publishReplyComment((String) subject.getId(), replyId, questionReplyCommentForm.getParentId(), questionReplyCommentForm.getComment());
return Response.success();
} catch (QuestionReplyCommentServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
}
package cn.meteor.beyondclouds.modules.question.enums;
import cn.meteor.beyondclouds.core.IErrorCode;
/**
* @author 胡学良
* @since 2020/2/2
*/
public enum QuestionReplyCommentErrorCode implements IErrorCode {
COMMENT_NOT_FOUND(5001, "父评论不存在");
private long code;
private String msg;
QuestionReplyCommentErrorCode(long code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public long code() {
return code;
}
@Override
public String msg() {
return msg;
}
}
package cn.meteor.beyondclouds.modules.question.exception;
import cn.meteor.beyondclouds.core.IErrorCode;
import cn.meteor.beyondclouds.core.exception.ServiceException;
/**
* @author 胡学良
* @since 2020/2/2
*/
public class QuestionReplyCommentServiceException extends ServiceException {
public QuestionReplyCommentServiceException(long errorCode, String errorMsg) {
super(errorCode, errorMsg);
}
public QuestionReplyCommentServiceException(IErrorCode errorCode) {
super(errorCode);
}
}
package cn.meteor.beyondclouds.modules.question.form;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
/**
* 回复评论表单
* @author 胡学良
* @since 2020/2/2
*/
@Data
public class QuestionReplyCommentForm {
@ApiModelProperty("父评论ID")
private String parentId;
@ApiModelProperty("评论内容")
@NotEmpty(message = "评论内容不能为空")
private String comment;
}
package cn.meteor.beyondclouds.modules.question.service;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReplyComment;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyCommentServiceException;
import com.baomidou.mybatisplus.extension.service.IService;
/**
......@@ -13,4 +14,13 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface IQuestionReplyCommentService extends IService<QuestionReplyComment> {
/**
* 发表评论
* @param userId 用户ID
* @param replyId 回复ID
* @param parentId 父评论ID
* @param comment 评论内容
* @throws QuestionReplyCommentServiceException 问题回复评论业务异常
*/
void publishReplyComment(String userId, String replyId, String parentId, String comment) throws QuestionReplyCommentServiceException;
}
package cn.meteor.beyondclouds.modules.question.service.impl;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReply;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReplyComment;
import cn.meteor.beyondclouds.modules.question.enums.QuestionReplyCommentErrorCode;
import cn.meteor.beyondclouds.modules.question.enums.QuestionReplyErrorCode;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyCommentServiceException;
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 com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* <p>
......@@ -17,4 +26,56 @@ import org.springframework.stereotype.Service;
@Service
public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCommentMapper, QuestionReplyComment> implements IQuestionReplyCommentService {
private IQuestionReplyService questionReplyService;
@Autowired
public void setQuestionReplyService(IQuestionReplyService questionReplyService) {
this.questionReplyService = questionReplyService;
}
@Override
public void publishReplyComment(String userId, String replyId, String parentId, String comment) throws QuestionReplyCommentServiceException {
Assert.hasText(replyId, "replyId must not be empty");
//1.判断是否存在该回复
QuestionReply questionReply = questionReplyService.getById(replyId);
//若回复为空,则抛出回复不存在异常
if (questionReply == null) {
throw new QuestionReplyCommentServiceException(QuestionReplyErrorCode.REPLY_NOT_FOUND);
}
QuestionReplyComment parentQuestionReplyComment = null;
//2.判断夫评论是否存在
if (!StringUtils.isEmpty(parentId)) {
QueryWrapper<QuestionReplyComment> questionReplyCommentQueryWrapper = new QueryWrapper<>();
questionReplyCommentQueryWrapper.eq("reply_id",replyId)
.eq("comment_id",parentId);
parentQuestionReplyComment = getOne(questionReplyCommentQueryWrapper);
//若父评论为空,则抛出父评论不存在异常
if (null == parentQuestionReplyComment) {
throw new QuestionReplyCommentServiceException(QuestionReplyCommentErrorCode.COMMENT_NOT_FOUND);
}
}
//3.保存评论信息
QuestionReplyComment questionReplyComment = new QuestionReplyComment();
questionReplyComment.setUserId(userId);
questionReplyComment.setReplyId(replyId);
questionReplyComment.setParentId(parentId);
questionReplyComment.setComment(comment);
save(questionReplyComment);
//4.更新评论层级和评论路径
if (null != parentQuestionReplyComment) {
//子级评论
questionReplyComment.setDepth(parentQuestionReplyComment.getDepth()+1);
questionReplyComment.setThread(parentQuestionReplyComment.getThread()+"/"+questionReplyComment.getCommentId());
}else {
//1级评论
questionReplyComment.setDepth(0);
questionReplyComment.setThread("/"+questionReplyComment.getCommentId());
}
updateById(questionReplyComment);
}
}
......@@ -52,9 +52,8 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
private IQuestionCategoryService questionCategoryService;
@Autowired
public QuestionServiceImpl(IQuestionExtService questionExtService, IQuestionReplyCommentService questionReplyCommentService, IQuestionCategoryService questionCategoryService) {
public QuestionServiceImpl(IQuestionExtService questionExtService, IQuestionCategoryService questionCategoryService) {
this.questionExtService = questionExtService;
this.questionReplyCommentService = questionReplyCommentService;
this.questionCategoryService = questionCategoryService;
}
......@@ -68,6 +67,11 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
this.questionTagService = questionTagService;
}
@Autowired
public void setQuestionReplyCommentService(IQuestionReplyCommentService questionReplyCommentService) {
this.questionReplyCommentService = questionReplyCommentService;
}
@Transactional(rollbackFor = Exception.class)
@Override
public void postQuestion(Question question, String questionDetail, HashSet<String> tags) throws QuestionServiceException {
......
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