Commit 35986ecc by 胡学良

修改删除回复

parent 66cb99ac
......@@ -15,20 +15,17 @@ import cn.meteor.beyondclouds.modules.question.exception.QuestionTagServiceExcep
import cn.meteor.beyondclouds.modules.question.form.QuestionForm;
import cn.meteor.beyondclouds.modules.question.service.IQuestionService;
import cn.meteor.beyondclouds.modules.question.service.IQuestionTagService;
import cn.meteor.beyondclouds.modules.question.util.QuestionUtils;
import cn.meteor.beyondclouds.modules.tag.entity.Tag;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashSet;
import java.util.List;
/**
......@@ -59,14 +56,14 @@ public class QuestionApi {
*/
@ApiOperation("发布问题")
@PostMapping("/question")
public Response postQuestion(@RequestBody @Validated(InsertGroup.class) QuestionForm questionForm, BindingResult result, @CurrentSubject Subject subject){
public Response postQuestion(@RequestBody @Validated(InsertGroup.class) QuestionForm questionForm, BindingResult result, @CurrentSubject Subject subject) {
if (result.hasErrors()) {
return Response.fieldError(result.getFieldError());
}
//将问题表单转换为问题对象
Question question = new Question();
BeanUtils.copyProperties(questionForm,question);
BeanUtils.copyProperties(questionForm, question);
question.setCategoryId(questionForm.getCategoryId());
//设置用户ID(问题发布者ID)
......@@ -90,7 +87,7 @@ public class QuestionApi {
*/
@ApiOperation("删除问题")
@DeleteMapping("/question/{questionId}")
public Response deleteQuestion(@PathVariable("questionId") String questionId, @CurrentSubject Subject subject){
public Response deleteQuestion(@PathVariable("questionId") String questionId, @CurrentSubject Subject subject) {
try {
questionService.deleteQuestion(questionId, (String) subject.getId());
return Response.success();
......@@ -110,7 +107,7 @@ public class QuestionApi {
*/
@ApiOperation("修改问题")
@PutMapping("/question/{questionId}")
public Response modifyQuestion(@PathVariable("questionId") String questionId, @RequestBody @Validated(UpdateGroup.class) QuestionForm questionForm, BindingResult result, @CurrentSubject Subject subject){
public Response modifyQuestion(@PathVariable("questionId") String questionId, @RequestBody @Validated(UpdateGroup.class) QuestionForm questionForm, BindingResult result, @CurrentSubject Subject subject) {
if (result.hasErrors()) {
return Response.fieldError(result.getFieldError());
}
......@@ -143,7 +140,7 @@ public class QuestionApi {
@Anonymous
@ApiOperation("问题详情")
@GetMapping("/question/{questionId}")
public Response<QuestionDetails> questionDetails(@PathVariable("questionId") String questionId){
public Response<QuestionDetails> questionDetails(@PathVariable("questionId") String questionId) {
QuestionDetails questionDetails = null;
try {
questionDetails = questionService.questionDetails(questionId);
......
......@@ -42,7 +42,7 @@ public class QuestionReplyApi {
*/
@ApiOperation("发布回复")
@PostMapping("/question/{questionId}/reply")
public Response replyQuestion(@PathVariable("questionId") String questionId, String reply, @CurrentSubject Subject subject){
public Response replyQuestion(@PathVariable("questionId") String questionId, String reply, @CurrentSubject Subject subject) {
try {
questionReplyService.replyQuestion(questionId, reply, (String) subject.getId());
return Response.success();
......
......@@ -46,7 +46,7 @@ public class QuestionReplyCommentApi {
@ApiOperation("发表评论")
@PostMapping("/question/reply/{replyId}/comment")
public Response publishReplyComment(@RequestBody @Valid QuestionReplyCommentForm questionReplyCommentForm, BindingResult bindingResult,
@PathVariable("replyId") String replyId, @CurrentSubject Subject subject){
@PathVariable("replyId") String replyId, @CurrentSubject Subject subject) {
if (bindingResult.hasErrors()) {
return Response.fieldError(bindingResult.getFieldError());
......
......@@ -11,7 +11,13 @@ public enum QuestionErrorCode implements IErrorCode {
* 问题没有以?结尾
*/
QUESTION_END_ERROR(3001,"问题没有以?结尾"),
/**
* 问题的类别ID错误
*/
INCORRECT_CATEGORY(3002,"问题类别错误"),
/**
* 找不到该问题
*/
QUESTION_NOT_FOUND(3003, "找不到该问题");
private long code;
......
......@@ -19,11 +19,11 @@ public enum QuestionTagErrorCode implements IErrorCode {
@Override
public long code() {
return 0;
return code;
}
@Override
public String msg() {
return null;
return msg;
}
}
package cn.meteor.beyondclouds.modules.question.form;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
/**
* @author 胡学良
* @since 2020/1/31
*/
@ApiModel(value = "QuestionForm", description = "问题表单")
@Data
public class QuestionUpdateForm {
private static final long serialVersionUID=1L;
@ApiModelProperty("类别ID")
@NotNull(message = "问题类别ID不能为空")
private Integer categoryId;
@ApiModelProperty("标题")
@NotEmpty(message = "问题标题不能为空")
@Size(max = 256, message = "问题标题太长")
private String questionTitle;
@ApiModelProperty("摘要")
@Size(max = 256, message = "问题摘要太长")
private String questionAbstract;
@ApiModelProperty("标签")
@Size(max = 12, message = "问题标签个数不能超过5个")
private List<String> tagIds;
@ApiModelProperty("详情")
private String questionDetail;
@ApiModelProperty("话题ID")
private List<String> topicIds;
}
......@@ -18,13 +18,6 @@ import java.util.List;
*/
public interface IQuestionTagService extends IService<QuestionTag> {
/**
* 保存问题标签
* @param tags 问题对应标签
* @param questionId 问题ID
*/
void saveQuestionTag(HashSet<String> tags, String questionId);
/**
* 得到问题引用标签
......
......@@ -71,8 +71,8 @@ public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCo
//2.判断父评论是否存在
if (!StringUtils.isEmpty(parentId)) {
QueryWrapper<QuestionReplyComment> questionReplyCommentQueryWrapper = new QueryWrapper<>();
questionReplyCommentQueryWrapper.eq("reply_id",replyId)
.eq("comment_id",parentId);
questionReplyCommentQueryWrapper.eq("reply_id", replyId)
.eq("comment_id", parentId);
parentQuestionReplyComment = getOne(questionReplyCommentQueryWrapper);
//若父评论为空,则抛出父评论不存在异常
if (null == parentQuestionReplyComment) {
......@@ -95,11 +95,11 @@ public class QuestionReplyCommentServiceImpl extends ServiceImpl<QuestionReplyCo
if (null != parentQuestionReplyComment) {
//子级评论
questionReplyComment.setDepth(parentQuestionReplyComment.getDepth()+1);
questionReplyComment.setThread(parentQuestionReplyComment.getThread()+"/"+questionReplyComment.getCommentId());
questionReplyComment.setThread(parentQuestionReplyComment.getThread() + "/" + questionReplyComment.getCommentId());
}else {
//1级评论
questionReplyComment.setDepth(0);
questionReplyComment.setThread("/"+questionReplyComment.getCommentId());
questionReplyComment.setThread("/" + questionReplyComment.getCommentId());
}
updateById(questionReplyComment);
}
......
......@@ -20,6 +20,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* <p>
* 问题回复表 服务实现类
......@@ -86,7 +89,7 @@ public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, Q
//更新问题的回复数量
UpdateWrapper<Question> questionUpdateWrapper = new UpdateWrapper<>();
questionUpdateWrapper.set("reply_number",question.getReplyNumber()+1)
questionUpdateWrapper.set("reply_number",question.getReplyNumber() + 1)
.eq("question_id",questionId);
questionService.update(questionUpdateWrapper);
}
......@@ -121,14 +124,14 @@ public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, Q
//4.采纳回复
UpdateWrapper<QuestionReply> updateWrapper = new UpdateWrapper<>();
updateWrapper.set("reply_status",ADOPTED_REPLY_STATUS).eq("reply_id",replyId);
updateWrapper.set("reply_status", ADOPTED_REPLY_STATUS).eq("reply_id", replyId);
update(updateWrapper);
//5.更新问题状态
if (!question.getSolved()) {
UpdateWrapper<Question> questionUpdateWrapper = new UpdateWrapper<>();
questionUpdateWrapper.set("solved", true)
.eq("question_id",questionId);
.eq("question_id", questionId);
questionService.update(questionUpdateWrapper);
}
}
......@@ -145,7 +148,7 @@ public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, Q
//2.得到页面信息
IPage<QuestionReply> questionReplyPage = new Page<>(pageNumber, pageSize);
return page(questionReplyPage, QuestionUtils.getWrapper("question_id",questionId));
return page(questionReplyPage, QuestionUtils.getWrapper("question_id", questionId));
}
@Override
......@@ -165,14 +168,29 @@ public class QuestionReplyServiceImpl extends ServiceImpl<QuestionReplyMapper, Q
}
//3.删除该回复及评论
questionReplyCommentService.remove(QuestionUtils.getWrapper("reply_id",replyId));
questionReplyCommentService.remove(QuestionUtils.getWrapper("reply_id", replyId));
removeById(replyId);
//4.更新问题的回复数量
UpdateWrapper<Question> questionUpdateWrapper = new UpdateWrapper<>();
questionUpdateWrapper.set("reply_number",question.getReplyNumber()-1)
.eq("question_id",question.getQuestionId());
questionUpdateWrapper.set("reply_number", question.getReplyNumber() - 1)
.eq("question_id", question.getQuestionId());
questionService.update(questionUpdateWrapper);
//5.更新问题的状态,如果删除回复后问题的采纳回复数为0且问题之前为已解决状态,则需要将其变为未解决
if (1 == questionReply.getReplyStatus()) {
//查询数据库中该问题已采纳回复的数目
QueryWrapper<QuestionReply> questionReplyQueryWrapper = new QueryWrapper<>();
questionReplyQueryWrapper.eq("question_id", question.getQuestionId())
.eq("reply_status", 1);
List<QuestionReply> questionReplies = list(questionReplyQueryWrapper);
if (questionReplies.size() == 0 && question.getSolved()) {
UpdateWrapper<Question> solvedUpdateWrapper = new UpdateWrapper<>();
solvedUpdateWrapper.set("solved", false)
.eq("question_id", question.getQuestionId());
questionService.update(solvedUpdateWrapper);
}
}
}
@Override
......
......@@ -29,7 +29,6 @@ import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
......@@ -114,7 +113,7 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
//3.检查问题摘要是否为空,若为空则自动填充
if (StringUtils.isEmpty(question.getQuestionAbstract())) {
question.setQuestionAbstract(AbstractUtils.extractWithoutHtml(questionDetail,20));
question.setQuestionAbstract(AbstractUtils.extractWithoutHtml(questionDetail, 20));
}
//3.保存问题信息
......@@ -171,38 +170,37 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
@Transactional(rollbackFor = Exception.class)
@Override
public void deleteQuestion(String questionId, String userId) throws QuestionServiceException{
public void deleteQuestion(String questionId, String userId) throws QuestionServiceException {
//1.判断自己是否发布过此问题
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.eq("question_id",questionId)
.eq("user_id",userId);
questionQueryWrapper.eq("question_id", questionId)
.eq("user_id", userId);
Question question = getOne(questionQueryWrapper);
if (null == question) {
throw new QuestionServiceException(QuestionErrorCode.QUESTION_NOT_FOUND);
}
//2.删除question-tag表中关于该问题的标签
questionTagService.remove(QuestionUtils.getWrapper("question_id",questionId));
questionTagService.remove(QuestionUtils.getWrapper("question_id", questionId));
//3.获取该问题的所有回复
List<QuestionReply> questionReplies = questionReplyService.list(QuestionUtils.getWrapper("question_id",questionId));
List<QuestionReply> questionReplies = questionReplyService.list(QuestionUtils.getWrapper("question_id", questionId));
//4.删除question-reply-comment表中关于该问题的所有回复
for (QuestionReply questionReply : questionReplies){
questionReplyCommentService.remove(QuestionUtils.getWrapper("reply_id",questionReply.getReplyId()));
}
List<String> replyIds = questionReplies.stream().map(QuestionReply::getReplyId).collect(Collectors.toList());
questionReplyCommentService.removeByIds(replyIds);
//5.删除question-reply表中关于该问题的所有回复
questionReplyService.remove(QuestionUtils.getWrapper("question_id",questionId));
questionReplyService.remove(QuestionUtils.getWrapper("question_id", questionId));
//6.删除question-ext表中的问题信息
questionExtService.remove(QuestionUtils.getWrapper("question_id",questionId));
questionExtService.remove(QuestionUtils.getWrapper("question_id", questionId));
//7.删除question表中的问题信息
removeById(questionId);
//8.删除话题引用表中的问题信息
topicReferenceService.remove(QuestionUtils.getWrapper("referencer_id",question.getQuestionId()));
topicReferenceService.remove(QuestionUtils.getWrapper("referencer_id", question.getQuestionId()));
}
......@@ -211,8 +209,8 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
public void updateQuestion(Question question, String questionDetail, List<String> tagIds, List<String> topicIds) throws QuestionServiceException {
//1.判断自己是否发布过此问题
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.eq("question_id",question.getQuestionId())
.eq("user_id",question.getUserId());
questionQueryWrapper.eq("question_id", question.getQuestionId())
.eq("user_id", question.getUserId());
Question questionInDb = getOne(questionQueryWrapper);
if (null == questionInDb) {
throw new QuestionServiceException(QuestionErrorCode.QUESTION_NOT_FOUND);
......@@ -220,7 +218,7 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
//2.检查问题标题是否以?结尾
if (!StringUtils.isEmpty(question.getQuestionTitle())) {
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);
}
}
......@@ -239,12 +237,9 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
//3.更新问题扩展信息
if (!StringUtils.isEmpty(questionDetail)) {
QuestionExt questionExt = new QuestionExt();
questionExt.setQuestionId(question.getQuestionId());
questionExt.setQuestionDetail(questionDetail);
UpdateWrapper<QuestionExt> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("question_id",question.getQuestionId());
questionExtService.update(questionExt,updateWrapper);
updateWrapper.set("question_detail", questionDetail).eq("question_id", question.getQuestionId());
questionExtService.update(updateWrapper);
}
//5.判断是否引用话题
......@@ -253,9 +248,7 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
List<String> existsTopicIds = topicService.listByIds(topicIds).stream().map(Topic::getTopicId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTopicIds)) {
// 删除旧话题引用
QueryWrapper<TopicReference> topicReferenceQueryWrapper = new QueryWrapper<>();
topicReferenceQueryWrapper.eq("referencer_id", question.getQuestionId());
topicReferenceService.remove(topicReferenceQueryWrapper);
topicReferenceService.remove(QuestionUtils.getWrapper("referencer_id", question.getQuestionId()));
List<TopicReference> topicReferenceList = new ArrayList<>();
......@@ -281,9 +274,7 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
List<String> existsTagIds = tagService.listByIds(tagIds).stream().filter(tag -> 2 == tag.getTagType()).map(Tag::getTagId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(existsTagIds)) {
// 删除旧标签
QueryWrapper<QuestionTag> questionTagQueryWrapper = new QueryWrapper<>();
questionTagQueryWrapper.eq("question_id", question.getQuestionId());
questionTagService.remove(questionTagQueryWrapper);
questionTagService.remove(QuestionUtils.getWrapper("question_id", question.getQuestionId()));
List<QuestionTag> questionTagList = new ArrayList<>();
for (String tagId : existsTagIds) {
......@@ -320,10 +311,8 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
//5.获取话题详情
List<TopicReference> references = topicReferenceService.list(QuestionUtils.getWrapper("referencer_id", question.getQuestionId()));
List<Topic> topics = new ArrayList<>();
for (TopicReference topicReference : references ) {
topics.add(topicService.getById(topicReference.getTopicId()));
}
List<String> topicIds = references.stream().map(TopicReference::getTopicId).collect(Collectors.toList());
List<Topic> topics = topicService.listByIds(topicIds);
if (CollectionUtils.isEmpty(topics)) {
question.setTopics(List.of());
......@@ -341,7 +330,7 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
@Override
public IPage<Question> getQuestionPage(Integer pageNumber, Integer pageSize) {
IPage<Question> page = new Page<>(pageNumber,pageSize);
IPage<Question> page = new Page<>(pageNumber, pageSize);
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.orderByDesc("q.create_time");
return questionMapper.selectPageWithTags(page, questionQueryWrapper);
......
......@@ -45,21 +45,6 @@ public class QuestionTagServiceImpl extends ServiceImpl<QuestionTagMapper, Quest
this.questionService = questionService;
}
@Override
public void saveQuestionTag(HashSet<String> tags, String questionId) {
Iterator<String> iterator = tags.iterator();
QuestionTag questionTag;
while(iterator.hasNext()){
Tag tag = tagService.getById(iterator.next());
if (tag == null || tag.getTagType() != 2) {
continue;
}
questionTag = new QuestionTag();
questionTag.setQuestionId(questionId);
questionTag.setTagId(tag.getTagId());
save(questionTag);
}
}
@Override
public List<Tag> getQuestionTags(String questionId) throws QuestionTagServiceException {
......
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