Commit 6af80433 by 胡学良

优化通过标签ID得到问答列表

parent c8e81e9f
...@@ -256,7 +256,7 @@ public class QuestionApi { ...@@ -256,7 +256,7 @@ public class QuestionApi {
} }
@Anonymous @Anonymous
@ApiOperation("标签下的问答列表") @ApiOperation("得到标签下的问答列表")
@GetMapping("/tag/{tagId}/questions") @GetMapping("/tag/{tagId}/questions")
public Response<PageVO<Question>> getQuestionsByTagId(@Valid PageForm pageForm, @PathVariable("tagId") String tagId) { public Response<PageVO<Question>> getQuestionsByTagId(@Valid PageForm pageForm, @PathVariable("tagId") String tagId) {
try { try {
......
...@@ -421,25 +421,31 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i ...@@ -421,25 +421,31 @@ public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> i
@Override @Override
public IPage<Question> getQuestionsByTagId(Integer pageNumber, Integer pageSize, String tagId) throws QuestionTagServiceException { public IPage<Question> getQuestionsByTagId(Integer pageNumber, Integer pageSize, String tagId) throws QuestionTagServiceException {
IPage<Question> page = new Page<>(pageNumber, pageSize);
//1.检查标签是否存在 //1.检查标签是否存在
Tag tag = tagService.getById(tagId); Tag tag = tagService.getById(tagId);
if (null == tag) { if (null == tag) {
throw new QuestionTagServiceException(QuestionTagErrorCode.TAG_NOT_FOUND); throw new QuestionTagServiceException(QuestionTagErrorCode.TAG_NOT_FOUND);
} }
IPage<Question> questionPage = new Page<>(pageNumber, pageSize);
IPage<QuestionTag> questionTagPage = new Page<>(pageNumber, pageSize);
//2.通过标签ID得到对应的问题ID //2.通过标签ID得到对应的问题ID
List<String> questionIds = questionTagService.list(QuestionUtils.getWrapper("tag_id", tag.getTagId())) QueryWrapper<QuestionTag> questionTagQueryWrapper = new QueryWrapper<>();
.stream().map(QuestionTag::getQuestionId).collect(Collectors.toList()); questionTagQueryWrapper.eq("tag_id", tag.getTagId());
questionTagService.page(questionTagPage,questionTagQueryWrapper);
List<String> questionIds = questionTagPage.getRecords().stream().map(QuestionTag::getQuestionId).collect(Collectors.toList());
questionPage.setTotal(questionTagPage.getTotal());
questionPage.setCurrent(questionTagPage.getCurrent());
if (!CollectionUtils.isEmpty(questionIds)) { if (!CollectionUtils.isEmpty(questionIds)) {
QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>(); questionPage.setRecords(questionMapper.listByIdsWithTags(questionIds));
questionQueryWrapper.in("q.question_id", questionIds)
.orderByDesc("q.create_time");
return questionMapper.selectPageWithTags(page, questionQueryWrapper);
}else { }else {
return page; questionPage.setRecords(List.of());
} }
return questionPage;
} }
} }
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