fix(book):增强评论获取代码健壮性

- 添加对书籍信息为空时的空值检查
- 优化书评列表查询逻辑,避免直接返回空列表导致的数据处理问题
- 统一返回结果的构建方式,提升代码可读性
This commit is contained in:
Neko
2025-11-05 11:16:15 +08:00
parent ab97d9b485
commit 81a28d1774

View File

@@ -443,23 +443,28 @@ public class BookServiceImpl implements BookService {
.orderByDesc(DatabaseConsts.CommonColumnEnum.UPDATE_TIME.getName());
IPage<BookComment> bookCommentPage = bookCommentMapper.selectPage(page, queryWrapper);
List<BookComment> comments = bookCommentPage.getRecords();
List<UserCommentRespDto> commentRespDtoList = Collections.emptyList();
if (!CollectionUtils.isEmpty(comments)) {
List<Long> bookIds = comments.stream().map(BookComment::getBookId).toList();
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper.in(DatabaseConsts.CommonColumnEnum.ID.getName(), bookIds);
Map<Long, BookInfo> bookInfoMap = bookInfoMapper.selectList(bookInfoQueryWrapper).stream()
.collect(Collectors.toMap(BookInfo::getId, Function.identity()));
return RestResp.ok(PageRespDto.of(pageReqDto.getPageNum(), pageReqDto.getPageSize(), page.getTotal(),
comments.stream().map(v -> UserCommentRespDto.builder()
commentRespDtoList = comments.stream().map(v -> {
BookInfo bookInfo = bookInfoMap.get(v.getBookId());
return UserCommentRespDto.builder()
.commentContent(v.getCommentContent())
.commentBook(bookInfoMap.get(v.getBookId()).getBookName())
.commentBookPic(bookInfoMap.get(v.getBookId()).getPicUrl())
.commentBook(bookInfo != null ? bookInfo.getBookName() : null)
.commentBookPic(bookInfo != null ? bookInfo.getPicUrl() : null)
.commentTime(v.getCreateTime())
.build()).toList()));
.build();
}).toList();
}
return RestResp.ok(PageRespDto.of(pageReqDto.getPageNum(), pageReqDto.getPageSize(), page.getTotal(),
Collections.emptyList()));
return RestResp.ok(PageRespDto.of(pageReqDto.getPageNum(), pageReqDto.getPageSize(),
page.getTotal(), commentRespDtoList));
}
@Transactional(rollbackFor = Exception.class)