Commit c8e81e9f by 胡学良

得到标签下的问答列表

parent 0d81a6b5
......@@ -255,5 +255,18 @@ public class QuestionApi {
return Response.success(questionPageVO);
}
@Anonymous
@ApiOperation("标签下的问答列表")
@GetMapping("/tag/{tagId}/questions")
public Response<PageVO<Question>> getQuestionsByTagId(@Valid PageForm pageForm, @PathVariable("tagId") String tagId) {
try {
IPage<Question> page = questionService.getQuestionsByTagId(pageForm.getPage(), pageForm.getSize(), tagId);
PageVO<Question> pageVO = new PageVO<>(page);
return Response.success(pageVO);
} catch (QuestionTagServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
}
......@@ -7,7 +7,10 @@ import cn.meteor.beyondclouds.core.IErrorCode;
* @since 2020/2/1
*/
public enum QuestionTagErrorCode implements IErrorCode {
;
/**
* 标签不存在
*/
TAG_NOT_FOUND(6001, "标签不存在");
private long code;
private String msg;
......
......@@ -93,4 +93,14 @@ public interface IQuestionService extends IService<Question> {
*/
IPage<Question> getHotsQuestionsPage(Integer pageNumber, Integer pageSize);
/**
* 得到标签下的问答列表
* @param pageNumber 页数
* @param pageSize 页面大小
* @param tagId 标签ID
* @return 分页对象
* @throws QuestionTagServiceException 问题标签业务异常
*/
IPage<Question> getQuestionsByTagId(Integer pageNumber, Integer pageSize, String tagId) throws QuestionTagServiceException;
}
......@@ -3,6 +3,7 @@ package cn.meteor.beyondclouds.modules.question.service.impl;
import cn.meteor.beyondclouds.modules.question.bean.QuestionDetails;
import cn.meteor.beyondclouds.modules.question.entity.*;
import cn.meteor.beyondclouds.modules.question.enums.QuestionErrorCode;
import cn.meteor.beyondclouds.modules.question.enums.QuestionTagErrorCode;
import cn.meteor.beyondclouds.modules.question.exception.QuestionServiceException;
import cn.meteor.beyondclouds.modules.question.exception.QuestionTagServiceException;
import cn.meteor.beyondclouds.modules.question.mapper.QuestionMapper;
......@@ -418,4 +419,27 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
questionTagService.remove(questionTagQueryWrapper);
}
@Override
public IPage<Question> getQuestionsByTagId(Integer pageNumber, Integer pageSize, String tagId) throws QuestionTagServiceException {
IPage<Question> page = new Page<>(pageNumber, pageSize);
//1.检查标签是否存在
Tag tag = tagService.getById(tagId);
if (null == tag) {
throw new QuestionTagServiceException(QuestionTagErrorCode.TAG_NOT_FOUND);
}
//2.通过标签ID得到对应的问题ID
List<String> questionIds = questionTagService.list(QuestionUtils.getWrapper("tag_id", tag.getTagId()))
.stream().map(QuestionTag::getQuestionId).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(questionIds)) {
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
questionQueryWrapper.in("q.question_id", questionIds)
.orderByDesc("q.create_time");
return questionMapper.selectPageWithTags(page, questionQueryWrapper);
}else {
return page;
}
}
}
......@@ -3,7 +3,7 @@ spring:
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
username: root
password: 100Centa30821%mysql
password: 197442
mail:
host: smtp.163.com
username: 13546386889@163.com
......
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