mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
feat: 增加小说最新评论查询接口
This commit is contained in:
parent
0ff6c56f27
commit
747d2c9fc7
@ -101,8 +101,8 @@ public class BookController {
|
|||||||
* 获取下一章节ID接口
|
* 获取下一章节ID接口
|
||||||
*/
|
*/
|
||||||
@GetMapping("next_chapter_id/{chapterId}")
|
@GetMapping("next_chapter_id/{chapterId}")
|
||||||
public RestResp<Long> nextChapterId(@PathVariable("chapterId") Long chapterId) {
|
public RestResp<Long> getNextChapterId(@PathVariable("chapterId") Long chapterId) {
|
||||||
return bookService.nextChapterId(chapterId);
|
return bookService.getNextChapterId(chapterId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,4 +129,13 @@ public class BookController {
|
|||||||
return bookService.listUpdateRankBooks();
|
return bookService.listUpdateRankBooks();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说最新评论查询接口
|
||||||
|
*/
|
||||||
|
@GetMapping("comment/newest_list")
|
||||||
|
public RestResp<BookCommentRespDto> listNewestComments(Long bookId) {
|
||||||
|
return bookService.listNewestComments(bookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -125,6 +125,19 @@ public class DatabaseConsts {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说评论表
|
||||||
|
*/
|
||||||
|
public static class BookCommentTable {
|
||||||
|
|
||||||
|
private BookCommentTable() {
|
||||||
|
throw new IllegalStateException(SystemConfigConsts.CONST_INSTANCE_EXCEPTION_MSG);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final String COLUMN_BOOK_ID = "book_id";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新闻内容表
|
* 新闻内容表
|
||||||
*/
|
*/
|
||||||
@ -166,6 +179,7 @@ public class DatabaseConsts {
|
|||||||
|
|
||||||
LIMIT_1("limit 1"),
|
LIMIT_1("limit 1"),
|
||||||
LIMIT_2("limit 2"),
|
LIMIT_2("limit 2"),
|
||||||
|
LIMIT_5("limit 5"),
|
||||||
LIMIT_30("limit 30"),
|
LIMIT_30("limit 30"),
|
||||||
LIMIT_500("limit 500");
|
LIMIT_500("limit 500");
|
||||||
|
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package io.github.xxyopen.novel.core.json.serializer;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名序列化类(敏感信息,不应该在页面上完全显示)
|
||||||
|
*
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @date 2022/5/20
|
||||||
|
*/
|
||||||
|
public class UsernameSerializer extends JsonSerializer<String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
|
||||||
|
jsonGenerator.writeString(s.substring(0,4) + "****" + s.substring(8));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package io.github.xxyopen.novel.dto.resp;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import io.github.xxyopen.novel.core.json.serializer.UsernameSerializer;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说评论 响应DTO
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @date 2022/5/17
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class BookCommentRespDto {
|
||||||
|
|
||||||
|
|
||||||
|
private Long commentTotal;
|
||||||
|
|
||||||
|
private List<CommentInfo> comments;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public static class CommentInfo {
|
||||||
|
|
||||||
|
private String commentContent;
|
||||||
|
|
||||||
|
@JsonSerialize(using = UsernameSerializer.class)
|
||||||
|
private String commentUser;
|
||||||
|
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
private LocalDateTime commentTime;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package io.github.xxyopen.novel.manager;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
|
||||||
|
import io.github.xxyopen.novel.dao.entity.UserInfo;
|
||||||
|
import io.github.xxyopen.novel.dao.mapper.UserInfoMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户模块 DAO管理类
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @date 2022/5/20
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class UserDaoManager {
|
||||||
|
|
||||||
|
private final UserInfoMapper userInfoMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户ID集合批量查询用户信息列表
|
||||||
|
* @param userIds 需要查询的用户ID集合
|
||||||
|
* @return 满足条件的用户信息列表
|
||||||
|
* */
|
||||||
|
public List<UserInfo> listUsers(List<Long> userIds){
|
||||||
|
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
|
||||||
|
queryWrapper.in(DatabaseConsts.CommonColumnEnum.ID.getName(),userIds);
|
||||||
|
return userInfoMapper.selectList(queryWrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -100,7 +100,7 @@ public interface BookService {
|
|||||||
* @param chapterId 章节ID
|
* @param chapterId 章节ID
|
||||||
* @return 下一章节ID
|
* @return 下一章节ID
|
||||||
*/
|
*/
|
||||||
RestResp<Long> nextChapterId(Long chapterId);
|
RestResp<Long> getNextChapterId(Long chapterId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小说章节列表查询
|
* 小说章节列表查询
|
||||||
@ -126,4 +126,11 @@ public interface BookService {
|
|||||||
*/
|
*/
|
||||||
RestResp<Void> saveComment(UserCommentReqDto dto);
|
RestResp<Void> saveComment(UserCommentReqDto dto);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说最新评论查询
|
||||||
|
*
|
||||||
|
* @param bookId 小说ID
|
||||||
|
* @return 小说最新评论数据
|
||||||
|
*/
|
||||||
|
RestResp<BookCommentRespDto> listNewestComments(Long bookId);
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import io.github.xxyopen.novel.core.constant.DatabaseConsts;
|
|||||||
import io.github.xxyopen.novel.dao.entity.BookChapter;
|
import io.github.xxyopen.novel.dao.entity.BookChapter;
|
||||||
import io.github.xxyopen.novel.dao.entity.BookComment;
|
import io.github.xxyopen.novel.dao.entity.BookComment;
|
||||||
import io.github.xxyopen.novel.dao.entity.BookInfo;
|
import io.github.xxyopen.novel.dao.entity.BookInfo;
|
||||||
|
import io.github.xxyopen.novel.dao.entity.UserInfo;
|
||||||
import io.github.xxyopen.novel.dao.mapper.BookChapterMapper;
|
import io.github.xxyopen.novel.dao.mapper.BookChapterMapper;
|
||||||
import io.github.xxyopen.novel.dao.mapper.BookCommentMapper;
|
import io.github.xxyopen.novel.dao.mapper.BookCommentMapper;
|
||||||
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
|
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
|
||||||
@ -23,10 +24,8 @@ import org.springframework.stereotype.Service;
|
|||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
import java.util.stream.Collectors;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 小说模块 服务实现类
|
* 小说模块 服务实现类
|
||||||
@ -55,6 +54,8 @@ public class BookServiceImpl implements BookService {
|
|||||||
|
|
||||||
private final BookCommentMapper bookCommentMapper;
|
private final BookCommentMapper bookCommentMapper;
|
||||||
|
|
||||||
|
private final UserDaoManager userDaoManager;
|
||||||
|
|
||||||
private static final Integer REC_BOOK_COUNT = 4;
|
private static final Integer REC_BOOK_COUNT = 4;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -63,8 +64,8 @@ public class BookServiceImpl implements BookService {
|
|||||||
page.setCurrent(condition.getPageNum());
|
page.setCurrent(condition.getPageNum());
|
||||||
page.setSize(condition.getPageSize());
|
page.setSize(condition.getPageSize());
|
||||||
List<BookInfo> bookInfos = bookInfoMapper.searchBooks(page, condition);
|
List<BookInfo> bookInfos = bookInfoMapper.searchBooks(page, condition);
|
||||||
return RestResp.ok(PageRespDto.of(condition.getPageNum(),condition.getPageSize(),page.getTotal()
|
return RestResp.ok(PageRespDto.of(condition.getPageNum(), condition.getPageSize(), page.getTotal()
|
||||||
,bookInfos.stream().map(v -> BookInfoRespDto.builder()
|
, bookInfos.stream().map(v -> BookInfoRespDto.builder()
|
||||||
.id(v.getId())
|
.id(v.getId())
|
||||||
.bookName(v.getBookName())
|
.bookName(v.getBookName())
|
||||||
.categoryId(v.getCategoryId())
|
.categoryId(v.getCategoryId())
|
||||||
@ -168,7 +169,7 @@ public class BookServiceImpl implements BookService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestResp<Long> nextChapterId(Long chapterId) {
|
public RestResp<Long> getNextChapterId(Long chapterId) {
|
||||||
// 查询小说ID 和 章节号
|
// 查询小说ID 和 章节号
|
||||||
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
|
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
|
||||||
Long bookId = chapter.getBookId();
|
Long bookId = chapter.getBookId();
|
||||||
@ -215,6 +216,38 @@ public class BookServiceImpl implements BookService {
|
|||||||
return RestResp.ok();
|
return RestResp.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RestResp<BookCommentRespDto> listNewestComments(Long bookId) {
|
||||||
|
// 查询评论总数
|
||||||
|
QueryWrapper<BookComment> commentCountQueryWrapper = new QueryWrapper<>();
|
||||||
|
commentCountQueryWrapper.eq(DatabaseConsts.BookCommentTable.COLUMN_BOOK_ID, bookId);
|
||||||
|
Long commentTotal = bookCommentMapper.selectCount(commentCountQueryWrapper);
|
||||||
|
BookCommentRespDto bookCommentRespDto = BookCommentRespDto.builder().commentTotal(commentTotal).build();
|
||||||
|
if (commentTotal > 0) {
|
||||||
|
|
||||||
|
// 查询最新的评论列表
|
||||||
|
QueryWrapper<BookComment> commentQueryWrapper = new QueryWrapper<>();
|
||||||
|
commentQueryWrapper.eq(DatabaseConsts.BookCommentTable.COLUMN_BOOK_ID, bookId)
|
||||||
|
.orderByDesc(DatabaseConsts.CommonColumnEnum.CREATE_TIME.getName())
|
||||||
|
.last(DatabaseConsts.SqlEnum.LIMIT_5.getSql());
|
||||||
|
List<BookComment> bookComments = bookCommentMapper.selectList(commentQueryWrapper);
|
||||||
|
|
||||||
|
// 查询评论用户信息,并设置需要返回的评论用户名
|
||||||
|
List<Long> userIds = bookComments.stream().map(BookComment::getUserId).toList();
|
||||||
|
List<UserInfo> userInfos = userDaoManager.listUsers(userIds);
|
||||||
|
Map<Long, String> userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, UserInfo::getUsername));
|
||||||
|
List<BookCommentRespDto.CommentInfo> commentInfos = bookComments.stream()
|
||||||
|
.map(v -> BookCommentRespDto.CommentInfo.builder()
|
||||||
|
.commentUser(userInfoMap.get(v.getUserId()))
|
||||||
|
.commentContent(v.getCommentContent())
|
||||||
|
.commentTime(v.getCreateTime()).build()).toList();
|
||||||
|
bookCommentRespDto.setComments(commentInfos);
|
||||||
|
} else {
|
||||||
|
bookCommentRespDto.setComments(Collections.emptyList());
|
||||||
|
}
|
||||||
|
return RestResp.ok(bookCommentRespDto);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
|
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
|
||||||
// 查询章节信息
|
// 查询章节信息
|
||||||
|
Loading…
x
Reference in New Issue
Block a user