mirror of
https://gitee.com/novel_dev_team/novel
synced 2026-07-20 17:52:45 +08:00
Merge pull request #47 from li7355608/master
fix(book): 发送评论增加小说存在性校验,用户评论查询增强代码健壮性
This commit is contained in:
@@ -99,6 +99,11 @@ public enum ErrorCodeEnum {
|
||||
*/
|
||||
USER_COMMENTED("A2001", "用户已发表评论"),
|
||||
|
||||
/**
|
||||
* 小说不存在
|
||||
*/
|
||||
BOOK_NOT_FOUND("A2002", "小说ID不存在"),
|
||||
|
||||
/**
|
||||
* 作家发布异常
|
||||
*/
|
||||
|
||||
@@ -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()
|
||||
.commentContent(v.getCommentContent())
|
||||
.commentBook(bookInfoMap.get(v.getBookId()).getBookName())
|
||||
.commentBookPic(bookInfoMap.get(v.getBookId()).getPicUrl())
|
||||
.commentTime(v.getCreateTime())
|
||||
.build()).toList()));
|
||||
|
||||
commentRespDtoList = comments.stream().map(v -> {
|
||||
BookInfo bookInfo = bookInfoMap.get(v.getBookId());
|
||||
return UserCommentRespDto.builder()
|
||||
.commentContent(v.getCommentContent())
|
||||
.commentBook(bookInfo != null ? bookInfo.getBookName() : null)
|
||||
.commentBookPic(bookInfo != null ? bookInfo.getPicUrl() : null)
|
||||
.commentTime(v.getCreateTime())
|
||||
.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)
|
||||
|
||||
Reference in New Issue
Block a user