Merge pull request #47 from li7355608/master

fix(book): 发送评论增加小说存在性校验,用户评论查询增强代码健壮性
This commit is contained in:
xxy
2025-11-05 18:19:00 +08:00
committed by GitHub
2 changed files with 23 additions and 8 deletions

View File

@@ -99,6 +99,11 @@ public enum ErrorCodeEnum {
*/
USER_COMMENTED("A2001", "用户已发表评论"),
/**
* 小说不存在
*/
BOOK_NOT_FOUND("A2002", "小说ID不存在"),
/**
* 作家发布异常
*/

View File

@@ -230,6 +230,11 @@ public class BookServiceImpl implements BookService {
@Override
public RestResp<Void> saveComment(
@Key(expr = "#{userId + '::' + bookId}") UserCommentReqDto dto) {
// 校验书籍是否存在
BookInfo bookInfo = bookInfoMapper.selectById(dto.getBookId());
if (bookInfo == null) {
return RestResp.fail(ErrorCodeEnum.BOOK_NOT_FOUND);
}
// 校验用户是否已发表评论
QueryWrapper<BookComment> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(DatabaseConsts.BookCommentTable.COLUMN_USER_ID, dto.getUserId())
@@ -438,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)