Commit 17cacd87 by 段启岩

Merge remote-tracking branch 'origin/huxueliang02-02'

parents c32f8f71 cf7653bd
...@@ -21,11 +21,13 @@ import io.swagger.annotations.Api; ...@@ -21,11 +21,13 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
...@@ -72,9 +74,12 @@ public class QuestionApi { ...@@ -72,9 +74,12 @@ public class QuestionApi {
//祛除重复标签 //祛除重复标签
HashSet<String> tags = QuestionUtils.toHashSet(questionForm.getTags()); HashSet<String> tags = QuestionUtils.toHashSet(questionForm.getTags());
//祛除重复话题
HashSet<String> topicIds = QuestionUtils.toHashSet(questionForm.getTopicIds());
//发布问题 //发布问题
try { try {
questionService.postQuestion(question,questionForm.getQuestionDetail(),tags); questionService.postQuestion(question,questionForm.getQuestionDetail(),tags, topicIds);
return Response.success(); return Response.success();
} catch (QuestionServiceException e) { } catch (QuestionServiceException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -216,8 +221,8 @@ public class QuestionApi { ...@@ -216,8 +221,8 @@ public class QuestionApi {
@GetMapping("/my/question/participated") @GetMapping("/my/question/participated")
public Response<PageVO<QuestionReply>> getMyParticipateQuestions(@Valid PageForm pageForm, @CurrentSubject Subject subject) { public Response<PageVO<QuestionReply>> getMyParticipateQuestions(@Valid PageForm pageForm, @CurrentSubject Subject subject) {
//获取我的列表并返回 //获取我的列表并返回
IPage<QuestionReply> userParticipateQuestionReplyPage = questionService.getUserParticipateQuestionReplyPage(pageForm.getPage(), pageForm.getSize(), (String) subject.getId()); IPage<QuestionReply> myParticipateQuestionReplyPage = questionService.getUserParticipateQuestionReplyPage(pageForm.getPage(), pageForm.getSize(), (String) subject.getId());
PageVO<QuestionReply> questionReplyPageVO = new PageVO<>(userParticipateQuestionReplyPage); PageVO<QuestionReply> questionReplyPageVO = new PageVO<>(myParticipateQuestionReplyPage);
return Response.success(questionReplyPageVO); return Response.success(questionReplyPageVO);
} }
...@@ -237,5 +242,21 @@ public class QuestionApi { ...@@ -237,5 +242,21 @@ public class QuestionApi {
return Response.success(questionPageVO); return Response.success(questionPageVO);
} }
/**
* 用户参与的问答列表
* @param pageForm 分页表单
* @param userId 用户ID
* @return 用户参与的问答列表
*/
@Anonymous
@ApiOperation("用户参与的问答列表")
@GetMapping("/user/{userId}/question/participated")
public Response<PageVO<QuestionReply>> getUserParticipateQuestions(@Valid PageForm pageForm, @PathVariable("userId") String userId) {
//获取用户参与的问答列表并返回
IPage<QuestionReply> userParticipateQuestionReplyPage = questionService.getUserParticipateQuestionReplyPage(pageForm.getPage(), pageForm.getSize(), userId);
PageVO<QuestionReply> questionReplyPageVO = new PageVO<>(userParticipateQuestionReplyPage);
return Response.success(questionReplyPageVO);
}
} }
package cn.meteor.beyondclouds.modules.question.api; package cn.meteor.beyondclouds.modules.question.api;
import cn.meteor.beyondclouds.common.form.PageForm;
import cn.meteor.beyondclouds.common.vo.PageVO;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
import cn.meteor.beyondclouds.core.annotation.CurrentSubject; import cn.meteor.beyondclouds.core.annotation.CurrentSubject;
import cn.meteor.beyondclouds.core.api.Response; import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.bean.Subject; import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReply;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyServiceException; import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyServiceException;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyService; import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import javax.validation.Valid;
import org.springframework.web.bind.annotation.RestController;
/** /**
* @author 胡学良 * @author 胡学良
...@@ -20,12 +24,12 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -20,12 +24,12 @@ import org.springframework.web.bind.annotation.RestController;
@Api(tags = "问题回复Api") @Api(tags = "问题回复Api")
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
public class QuestionReply { public class QuestionReplyApi {
private IQuestionReplyService questionReplyService; private IQuestionReplyService questionReplyService;
@Autowired @Autowired
public QuestionReply(IQuestionReplyService questionReplyService) { public QuestionReplyApi(IQuestionReplyService questionReplyService) {
this.questionReplyService = questionReplyService; this.questionReplyService = questionReplyService;
} }
...@@ -47,4 +51,62 @@ public class QuestionReply { ...@@ -47,4 +51,62 @@ public class QuestionReply {
return Response.error(e); return Response.error(e);
} }
} }
/**
* 采纳回复
* @param questionId 问题ID
* @param replyId 回复ID
* @param userId 发布问题用户ID
* @return default
*/
@ApiOperation("采纳回复")
@PutMapping("/question/reply/{replyId}/adoption")
public Response adoptReply(String questionId, @PathVariable("replyId") String replyId, String userId) {
try {
questionReplyService.adoptReply(questionId, replyId, userId);
return Response.success();
} catch (QuestionReplyServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
/**
* 问题的回复列表
* @param pageForm 分页表单
* @param questionId 问题ID
* @return 问题的回复列表
*/
@Anonymous
@ApiOperation("问题的回复列表")
@GetMapping("/question/{questionId}/replies")
public Response<PageVO<QuestionReply>> getReplies(@Valid PageForm pageForm, String questionId) {
//获取问题的回复列表
try {
IPage<QuestionReply> questionReplyPage = questionReplyService.getReplies(pageForm.getPage(), pageForm.getSize(), questionId);
PageVO<QuestionReply> questionReplyPageVO = new PageVO<>(questionReplyPage);
return Response.success(questionReplyPageVO);
} catch (QuestionReplyServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
/**
* 删除回复
* @param replyId 回复ID
* @param subject 访问者信息
* @return default
*/
@ApiOperation("删除回复")
@DeleteMapping("/question/reply/{replyId}")
public Response deleteReply(@PathVariable("replyId") String replyId, @CurrentSubject Subject subject) {
try {
questionReplyService.deleteReply(replyId, (String) subject.getId());
return Response.success();
} catch (QuestionReplyServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
} }
package cn.meteor.beyondclouds.modules.question.api;
import cn.meteor.beyondclouds.common.form.PageForm;
import cn.meteor.beyondclouds.common.vo.PageVO;
import cn.meteor.beyondclouds.core.annotation.Anonymous;
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.entity.QuestionReplyComment;
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 com.baomidou.mybatisplus.core.metadata.IPage;
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;
}
/**
* 发表评论
* @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,
@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);
}
}
/**
* 删除评论
* @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);
}
}
/**
* 评论列表
* @param replyId 回复ID
* @param pageForm 分页表单
* @param parentId 父评论ID
* @return 评论列表
*/
@Anonymous
@ApiOperation("评论列表")
@GetMapping("/question/reply/{replyId}/comments")
public Response<PageVO<QuestionReplyComment>> getReplyComments(@PathVariable("replyId") String replyId,
@Valid PageForm pageForm,
@RequestParam(value = "parentId", required = false) String parentId) {
try {
IPage<QuestionReplyComment> replyCommentPage = questionReplyCommentService.getReplyCommentPage(pageForm.getPage(), pageForm.getSize(), replyId, parentId);
PageVO<QuestionReplyComment> questionReplyCommentPageVO = new PageVO<>(replyCommentPage);
return Response.success(questionReplyCommentPageVO);
} 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(5002, "该评论不存在"),
/**
* 父评论不存在
*/
PARENT_COMMENT_NOT_FOUND(5001, "父评论不存在"),
/**
* 无权删除评论
*/
NO_DELETE_PRIVILEGES(5003,"无权删除该评论 ");
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;
}
}
...@@ -7,7 +7,18 @@ import cn.meteor.beyondclouds.core.IErrorCode; ...@@ -7,7 +7,18 @@ import cn.meteor.beyondclouds.core.IErrorCode;
* @since 2020/2/1 * @since 2020/2/1
*/ */
public enum QuestionReplyErrorCode implements IErrorCode { public enum QuestionReplyErrorCode implements IErrorCode {
; /**
* 回复不存在
*/
REPLY_NOT_FOUND(4001, "回复不存在"),
/**
* 回复已被采纳
*/
REPLY_ADOPTED(4002, "回复已被采纳"),
/**
* 无权删除评论
*/
NO_DELETE_PRIVILEGES(4003,"无权删除该回复");
private long code; private long code;
private String msg; private String msg;
...@@ -19,11 +30,11 @@ public enum QuestionReplyErrorCode implements IErrorCode { ...@@ -19,11 +30,11 @@ public enum QuestionReplyErrorCode implements IErrorCode {
@Override @Override
public long code() { public long code() {
return 0; return code;
} }
@Override @Override
public String msg() { public String msg() {
return null; 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);
}
}
...@@ -2,11 +2,13 @@ package cn.meteor.beyondclouds.modules.question.form; ...@@ -2,11 +2,13 @@ package cn.meteor.beyondclouds.modules.question.form;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import java.util.List;
/** /**
* @author 胡学良 * @author 胡学良
...@@ -37,4 +39,7 @@ public class QuestionForm { ...@@ -37,4 +39,7 @@ public class QuestionForm {
@ApiModelProperty("详情") @ApiModelProperty("详情")
private String questionDetail; private String questionDetail;
@ApiModelProperty("话题ID")
private String topicIds;
} }
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; package cn.meteor.beyondclouds.modules.question.service;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReplyComment; import cn.meteor.beyondclouds.modules.question.entity.QuestionReplyComment;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyCommentServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.models.auth.In;
/** /**
* <p> * <p>
...@@ -13,4 +16,32 @@ import com.baomidou.mybatisplus.extension.service.IService; ...@@ -13,4 +16,32 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/ */
public interface IQuestionReplyCommentService extends IService<QuestionReplyComment> { 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;
/**
* 删除评论
* @param userId 用户ID
* @param commentId 评论ID
* @throws QuestionReplyCommentServiceException 问题回复评论业务异常
*/
void deleteReplyComment(String userId, String commentId) throws QuestionReplyCommentServiceException;
/**
* 评论列表
* @param pageNumber 页数
* @param pageSize 页面大小
* @param replyId 回复ID
* @param parentId 父评论ID
* @return 分页对象
* @throws QuestionReplyCommentServiceException
*/
IPage<QuestionReplyComment> getReplyCommentPage(Integer pageNumber, Integer pageSize, String replyId, String parentId) throws QuestionReplyCommentServiceException;
} }
...@@ -2,7 +2,9 @@ package cn.meteor.beyondclouds.modules.question.service; ...@@ -2,7 +2,9 @@ package cn.meteor.beyondclouds.modules.question.service;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReply; import cn.meteor.beyondclouds.modules.question.entity.QuestionReply;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyServiceException; import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyServiceException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.models.auth.In;
/** /**
* <p> * <p>
...@@ -19,6 +21,34 @@ public interface IQuestionReplyService extends IService<QuestionReply> { ...@@ -19,6 +21,34 @@ public interface IQuestionReplyService extends IService<QuestionReply> {
* @param questionId 问题ID * @param questionId 问题ID
* @param reply 回复内容 * @param reply 回复内容
* @param userId 用户ID * @param userId 用户ID
* @throws QuestionReplyServiceException 问题回复业务异常
*/ */
void replyQuestion(String questionId, String reply, String userId) throws QuestionReplyServiceException; void replyQuestion(String questionId, String reply, String userId) throws QuestionReplyServiceException;
/**
* 采纳回复
* @param questionId 问题ID
* @param replyId 回复ID
* @param userId 用户ID
* @throws QuestionReplyServiceException 问题回复业务异常
*/
void adoptReply(String questionId, String replyId, String userId) throws QuestionReplyServiceException;
/**
* 问题的回复列表
* @param pageNumber 页数
* @param pageSize 页面大小
* @param questionId 问题ID
* @return 页面对象
* @throws QuestionReplyServiceException 问题回复业务异常
*/
IPage<QuestionReply> getReplies(Integer pageNumber, Integer pageSize, String questionId) throws QuestionReplyServiceException;
/**
* 删除问题
* @param replyId 回复ID
* @param userId 用户ID
* @throws QuestionReplyServiceException 问题回复业务异常
*/
void deleteReply(String replyId, String userId) throws QuestionReplyServiceException;
} }
...@@ -27,9 +27,10 @@ public interface IQuestionService extends IService<Question> { ...@@ -27,9 +27,10 @@ public interface IQuestionService extends IService<Question> {
* @param question 问题基本信息 * @param question 问题基本信息
* @param questionDetail 问题详细信息 * @param questionDetail 问题详细信息
* @param tags 问题标签 * @param tags 问题标签
* @param topicIds 话题ID
* @throws QuestionServiceException 问题业务异常 * @throws QuestionServiceException 问题业务异常
*/ */
void postQuestion(Question question, String questionDetail, HashSet<String> tags) throws QuestionServiceException; void postQuestion(Question question, String questionDetail, HashSet<String> tags, HashSet<String> topicIds) throws QuestionServiceException;
/** /**
* 删除问题 * 删除问题
......
package cn.meteor.beyondclouds.modules.question.service.impl; 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.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.mapper.QuestionReplyCommentMapper;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyCommentService; 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 cn.meteor.beyondclouds.modules.question.util.QuestionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.nio.file.Watchable;
/** /**
* <p> * <p>
...@@ -17,4 +33,124 @@ import org.springframework.stereotype.Service; ...@@ -17,4 +33,124 @@ import org.springframework.stereotype.Service;
@Service @Service
public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCommentMapper, QuestionReplyComment> implements IQuestionReplyCommentService { public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCommentMapper, QuestionReplyComment> implements IQuestionReplyCommentService {
private IQuestionService questionService;
private IQuestionReplyService questionReplyService;
@Autowired
public void setQuestionReplyService(IQuestionReplyService questionReplyService) {
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.notNull(replyId, "replyId must not be null");
Assert.hasText(comment, "comment 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.PARENT_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);
}
@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);
}
@Override
public IPage<QuestionReplyComment> getReplyCommentPage(Integer pageNumber, Integer pageSize, String replyId, String parentId) throws QuestionReplyCommentServiceException {
Assert.notNull(replyId, "replyId must not be null");
IPage<QuestionReplyComment> page = new Page<>(pageNumber, pageSize);
//如果parentId为空,则只获取一级评论
if (null == parentId) {
QueryWrapper<QuestionReplyComment> questionReplyCommentQueryWrapper = new QueryWrapper<>();
questionReplyCommentQueryWrapper.eq("reply_id",replyId)
.eq("depth",0)
.orderByDesc("create_time");
return page(page, questionReplyCommentQueryWrapper);
}
//如果parentId不为null,则获取其子评论
//判断父评论是否存在
QuestionReplyComment questionReplyComment = getById(parentId);
if (null == questionReplyComment) {
throw new QuestionReplyCommentServiceException(QuestionReplyCommentErrorCode.PARENT_COMMENT_NOT_FOUND);
}
return page(page, QuestionUtils.getWrapper("parent_id",parentId));
}
} }
...@@ -3,10 +3,17 @@ package cn.meteor.beyondclouds.modules.question.service.impl; ...@@ -3,10 +3,17 @@ package cn.meteor.beyondclouds.modules.question.service.impl;
import cn.meteor.beyondclouds.modules.question.entity.Question; import cn.meteor.beyondclouds.modules.question.entity.Question;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReply; import cn.meteor.beyondclouds.modules.question.entity.QuestionReply;
import cn.meteor.beyondclouds.modules.question.enums.QuestionErrorCode; import cn.meteor.beyondclouds.modules.question.enums.QuestionErrorCode;
import cn.meteor.beyondclouds.modules.question.enums.QuestionReplyErrorCode;
import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyServiceException; import cn.meteor.beyondclouds.modules.question.exception.QuestionReplyServiceException;
import cn.meteor.beyondclouds.modules.question.mapper.QuestionReplyMapper; import cn.meteor.beyondclouds.modules.question.mapper.QuestionReplyMapper;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyCommentService;
import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyService; import cn.meteor.beyondclouds.modules.question.service.IQuestionReplyService;
import cn.meteor.beyondclouds.modules.question.service.IQuestionService; import cn.meteor.beyondclouds.modules.question.service.IQuestionService;
import cn.meteor.beyondclouds.modules.question.util.QuestionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -22,13 +29,22 @@ import org.springframework.stereotype.Service; ...@@ -22,13 +29,22 @@ import org.springframework.stereotype.Service;
@Service @Service
public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, QuestionReply> implements IQuestionReplyService { public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, QuestionReply> implements IQuestionReplyService {
private int ADOPTED_REPLY_STATUS = 1;
private IQuestionService questionService; private IQuestionService questionService;
private IQuestionReplyCommentService questionReplyCommentService;
@Autowired @Autowired
public void setQuestionService(IQuestionService questionService) { public void setQuestionService(IQuestionService questionService) {
this.questionService = questionService; this.questionService = questionService;
} }
@Autowired
public void setQuestionReplyCommentService(IQuestionReplyCommentService questionReplyCommentService) {
this.questionReplyCommentService = questionReplyCommentService;
}
@Override @Override
public void replyQuestion(String questionId, String reply, String userId) throws QuestionReplyServiceException { public void replyQuestion(String questionId, String reply, String userId) throws QuestionReplyServiceException {
//1.判断问题是否存在 //1.判断问题是否存在
...@@ -48,4 +64,74 @@ public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, Q ...@@ -48,4 +64,74 @@ public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, Q
questionReply.setReplyStatus(0); questionReply.setReplyStatus(0);
save(questionReply); save(questionReply);
} }
@Override
public void adoptReply(String questionId, String replyId, String userId) throws QuestionReplyServiceException {
//1.判断用户是否发布过该问题
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.eq("user_id", userId)
.eq("question_id", questionId);
Question question = questionService.getOne(questionQueryWrapper);
//若问题不存在,则抛出问题不存在异常
if (null == question) {
throw new QuestionReplyServiceException(QuestionErrorCode.QUESTION_NOT_FOUND);
}
//2.判断该问题的回复是否存在
QueryWrapper<QuestionReply> questionReplyQueryWrapper = new QueryWrapper<>();
questionReplyQueryWrapper.eq("question_id", questionId)
.eq("reply_id", replyId);
QuestionReply questionReply = getOne(questionReplyQueryWrapper);
//若该回复不存在,则抛出回复不存在异常
if (null == questionReply) {
throw new QuestionReplyServiceException(QuestionReplyErrorCode.REPLY_NOT_FOUND);
}
//3.判断该回复之前是否已被采纳,若已被采纳,则抛出回复已被采纳异常
if (1 == questionReply.getReplyStatus()) {
throw new QuestionReplyServiceException(QuestionReplyErrorCode.REPLY_ADOPTED);
}
//4.采纳回复
UpdateWrapper<QuestionReply> updateWrapper = new UpdateWrapper<>();
updateWrapper.set("reply_status",ADOPTED_REPLY_STATUS).eq("reply_id",replyId);
update(updateWrapper);
}
@Override
public IPage<QuestionReply> getReplies(Integer pageNumber, Integer pageSize, String questionId) throws QuestionReplyServiceException {
//1.判断该问题是否存在
Question question = questionService.getOne(QuestionUtils.getWrapper("question_id", questionId));
//若问题不存在,则抛出问题不存在异常
if (null == question) {
throw new QuestionReplyServiceException(QuestionErrorCode.QUESTION_NOT_FOUND);
}
//2.得到页面信息
IPage<QuestionReply> questionReplyPage = new Page<>(pageNumber, pageSize);
return page(questionReplyPage, QuestionUtils.getWrapper("question_id",questionId));
}
@Override
public void deleteReply(String replyId, String userId) throws QuestionReplyServiceException {
//1.判断该回复是否存在
QuestionReply questionReply = getById(replyId);
if (null == questionReply) {
throw new QuestionReplyServiceException(QuestionReplyErrorCode.REPLY_NOT_FOUND);
}
//2.判断用户是否有权限删除该回复
if (!questionReply.getUserId().equals(userId)) {
Question question = questionService.getById(questionReply.getQuestionId());
if (!question.getUserId().equals(userId)) {
throw new QuestionReplyServiceException(QuestionReplyErrorCode.NO_DELETE_PRIVILEGES);
}
}
//3.删除该回复及评论
questionReplyCommentService.remove(QuestionUtils.getWrapper("reply_id",replyId));
removeById(replyId);
}
} }
package cn.meteor.beyondclouds.modules.question.service.impl; package cn.meteor.beyondclouds.modules.question.service.impl;
import cn.meteor.beyondclouds.modules.question.bean.QuestionDetails; import cn.meteor.beyondclouds.modules.question.bean.QuestionDetails;
import cn.meteor.beyondclouds.modules.question.entity.Question; import cn.meteor.beyondclouds.modules.question.entity.*;
import cn.meteor.beyondclouds.modules.question.entity.QuestionCategory;
import cn.meteor.beyondclouds.modules.question.entity.QuestionExt;
import cn.meteor.beyondclouds.modules.question.entity.QuestionReply;
import cn.meteor.beyondclouds.modules.question.enums.QuestionErrorCode; import cn.meteor.beyondclouds.modules.question.enums.QuestionErrorCode;
import cn.meteor.beyondclouds.modules.question.exception.QuestionServiceException; import cn.meteor.beyondclouds.modules.question.exception.QuestionServiceException;
import cn.meteor.beyondclouds.modules.question.exception.QuestionTagServiceException; import cn.meteor.beyondclouds.modules.question.exception.QuestionTagServiceException;
...@@ -12,6 +9,10 @@ import cn.meteor.beyondclouds.modules.question.mapper.QuestionMapper; ...@@ -12,6 +9,10 @@ import cn.meteor.beyondclouds.modules.question.mapper.QuestionMapper;
import cn.meteor.beyondclouds.modules.question.service.*; import cn.meteor.beyondclouds.modules.question.service.*;
import cn.meteor.beyondclouds.modules.question.util.QuestionUtils; import cn.meteor.beyondclouds.modules.question.util.QuestionUtils;
import cn.meteor.beyondclouds.modules.tag.entity.Tag; import cn.meteor.beyondclouds.modules.tag.entity.Tag;
import cn.meteor.beyondclouds.modules.topic.entity.Topic;
import cn.meteor.beyondclouds.modules.topic.entity.TopicReference;
import cn.meteor.beyondclouds.modules.topic.service.ITopicReferenceService;
import cn.meteor.beyondclouds.modules.topic.service.ITopicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
...@@ -24,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional; ...@@ -24,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
...@@ -51,11 +53,16 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i ...@@ -51,11 +53,16 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
private IQuestionCategoryService questionCategoryService; private IQuestionCategoryService questionCategoryService;
private ITopicService topicService;
private ITopicReferenceService topicReferenceService;
@Autowired @Autowired
public QuestionServiceImpl(IQuestionExtService questionExtService, IQuestionReplyCommentService questionReplyCommentService, IQuestionCategoryService questionCategoryService) { public QuestionServiceImpl(IQuestionExtService questionExtService, IQuestionCategoryService questionCategoryService, ITopicService topicService, ITopicReferenceService topicReferenceService) {
this.questionExtService = questionExtService; this.questionExtService = questionExtService;
this.questionReplyCommentService = questionReplyCommentService;
this.questionCategoryService = questionCategoryService; this.questionCategoryService = questionCategoryService;
this.topicService = topicService;
this.topicReferenceService = topicReferenceService;
} }
@Autowired @Autowired
...@@ -68,9 +75,14 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i ...@@ -68,9 +75,14 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
this.questionTagService = questionTagService; this.questionTagService = questionTagService;
} }
@Autowired
public void setQuestionReplyCommentService(IQuestionReplyCommentService questionReplyCommentService) {
this.questionReplyCommentService = questionReplyCommentService;
}
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@Override @Override
public void postQuestion(Question question, String questionDetail, HashSet<String> tags) throws QuestionServiceException { public void postQuestion(Question question, String questionDetail, HashSet<String> tags, HashSet<String> topicIds) throws QuestionServiceException {
//1.检查问题标题是否以?结尾 //1.检查问题标题是否以?结尾
if (!question.getQuestionTitle().endsWith(QUESTION_END_EN) && !question.getQuestionTitle().endsWith(QUESTION_END_CN)){ if (!question.getQuestionTitle().endsWith(QUESTION_END_EN) && !question.getQuestionTitle().endsWith(QUESTION_END_CN)){
throw new QuestionServiceException(QuestionErrorCode.QUESTION_END_ERROR); throw new QuestionServiceException(QuestionErrorCode.QUESTION_END_ERROR);
...@@ -93,6 +105,32 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i ...@@ -93,6 +105,32 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
//5.保存问题标签信息 //5.保存问题标签信息
questionTagService.saveQuestionTag(tags,question.getQuestionId()); questionTagService.saveQuestionTag(tags,question.getQuestionId());
//6.保存话题引用信息并更新话题引用次数
Iterator<String> iterator = topicIds.iterator();
TopicReference topicReference = null;
while (iterator.hasNext()) {
System.out.println("============");
Topic topic = topicService.getById(iterator.next());
if (topic == null) {
System.out.println("111111111111");
continue;
}
//保存话题引用信息
topicReference = new TopicReference();
topicReference.setTopicId(topic.getTopicId());
topicReference.setReferencerId(question.getQuestionId());
topicReference.setReferencerType(2);
topicReferenceService.save(topicReference);
//更新话题引用次数
UpdateWrapper<Topic> updateWrapper = new UpdateWrapper<>();
updateWrapper.set("reference_count",topic.getReferenceCount()+1)
.eq("topic_id",topic.getTopicId());
topicService.update(updateWrapper);
}
} }
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
......
...@@ -3,7 +3,7 @@ spring: ...@@ -3,7 +3,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root username: root
password: 100Centa30821%mysql password: 197442
swagger: swagger:
enable: true enable: true
......
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