feat: 新增 上/下章ID查询接口 & 数据库常量类

This commit is contained in:
xiongxiaoyang 2022-05-16 06:44:39 +08:00
parent 63bfacccb7
commit cd39bade6d
12 changed files with 219 additions and 19 deletions

View File

@ -66,6 +66,22 @@ public class BookController {
return bookService.getBookContentAbout(chapterId); return bookService.getBookContentAbout(chapterId);
} }
/**
* 获取上一章节ID接口
* */
@GetMapping("preChapterId/{chapterId}")
public RestResp<Long> getPreChapterId(@PathVariable("chapterId") Long chapterId){
return bookService.getPreChapterId(chapterId);
}
/**
* 获取下一章节ID接口
* */
@GetMapping("nextChapterId/{chapterId}")
public RestResp<Long> nextChapterId(@PathVariable("chapterId") Long chapterId){
return bookService.nextChapterId(chapterId);
}
/** /**
* 小说点击榜查询接口 * 小说点击榜查询接口
* */ * */

View File

@ -0,0 +1,120 @@
package io.github.xxyopen.novel.core.constant;
import lombok.Getter;
/**
* 数据库 常量
*
* @author xiongxiaoyang
* @date 2022/5/12
*/
public class DatabaseConsts {
/**
* 小说表
*/
public static class BookTable {
@Getter
public enum ColumnEnum {
CATEGORY_ID("category_id"),
VISIT_COUNT("visit_count"),
LAST_CHAPTER_UPDATE_TIME("last_chapter_update_time")
;
private String name;
ColumnEnum(String name) {
this.name = name;
}
}
}
/**
* 小说章节表
*/
public static class BookChapterTable {
@Getter
public enum ColumnEnum {
BOOK_ID("book_id"),
CHAPTER_NUM("chapter_num"),
LAST_CHAPTER_UPDATE_TIME("last_chapter_update_time")
;
private String name;
ColumnEnum(String name) {
this.name = name;
}
}
}
/**
* 小说内容表
*/
public static class BookContentTable {
@Getter
public enum ColumnEnum {
CHAPTER_ID("chapter_id")
;
private String name;
ColumnEnum(String name) {
this.name = name;
}
}
}
/**
* 通用列枚举类
* */
@Getter
public enum CommonColumnEnum{
ID("id"),
SORT("sort"),
CREATE_TIME("create_time"),
UPDATE_TIME("update_time");
private String name;
CommonColumnEnum(String name){
this.name = name;
}
}
/**
* SQL语句枚举类
* */
@Getter
public enum SqlEnum {
LIMIT_1("limit 1"),
LIMIT_2("limit 2"),
LIMIT_30("limit 30"),
LIMIT_500("limit 500")
;
private String sql;
SqlEnum(String sql) {
this.sql = sql;
}
}
}

View File

@ -31,6 +31,11 @@ public class BookChapterRespDto implements Serializable {
*/ */
private Long bookId; private Long bookId;
/**
* 章节号
*/
private Integer chapterNum;
/** /**
* 章节名 * 章节名
*/ */

View File

@ -30,6 +30,7 @@ public class BookChapterCacheManager {
return BookChapterRespDto.builder() return BookChapterRespDto.builder()
.id(chapterId) .id(chapterId)
.bookId(bookChapter.getBookId()) .bookId(bookChapter.getBookId())
.chapterNum(bookChapter.getChapterNum())
.chapterName(bookChapter.getChapterName()) .chapterName(bookChapter.getChapterName())
.chapterWordCount(bookChapter.getWordCount()) .chapterWordCount(bookChapter.getWordCount())
.chapterUpdateTime(bookChapter.getUpdateTime()) .chapterUpdateTime(bookChapter.getUpdateTime())

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.constant.CacheConsts; import io.github.xxyopen.novel.core.constant.CacheConsts;
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
import io.github.xxyopen.novel.dao.entity.BookContent; import io.github.xxyopen.novel.dao.entity.BookContent;
import io.github.xxyopen.novel.dao.mapper.BookContentMapper; import io.github.xxyopen.novel.dao.mapper.BookContentMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -27,7 +28,7 @@ public class BookContentCacheManager {
, value = CacheConsts.BOOK_CONTENT_CACHE_NAME) , value = CacheConsts.BOOK_CONTENT_CACHE_NAME)
public String getBookContent(Long chapterId) { public String getBookContent(Long chapterId) {
QueryWrapper<BookContent> contentQueryWrapper = new QueryWrapper<>(); QueryWrapper<BookContent> contentQueryWrapper = new QueryWrapper<>();
contentQueryWrapper.eq("chapter_id",chapterId).last("limit 1"); contentQueryWrapper.eq(DatabaseConsts.BookContentTable.ColumnEnum.CHAPTER_ID.getName(), chapterId).last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
BookContent bookContent = bookContentMapper.selectOne(contentQueryWrapper); BookContent bookContent = bookContentMapper.selectOne(contentQueryWrapper);
return bookContent.getContent(); return bookContent.getContent();
} }

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.constant.CacheConsts; import io.github.xxyopen.novel.core.constant.CacheConsts;
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.BookInfo; import io.github.xxyopen.novel.dao.entity.BookInfo;
import io.github.xxyopen.novel.dao.mapper.BookChapterMapper; import io.github.xxyopen.novel.dao.mapper.BookChapterMapper;
@ -38,9 +39,9 @@ public class BookInfoCacheManager {
// 查询首章ID // 查询首章ID
QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>(); QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
queryWrapper queryWrapper
.eq("book_id", id) .eq(DatabaseConsts.BookChapterTable.ColumnEnum.BOOK_ID.getName(), id)
.orderByAsc("chapter_num") .orderByAsc(DatabaseConsts.BookChapterTable.ColumnEnum.CHAPTER_NUM.getName())
.last("limit 1"); .last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
BookChapter firstBookChapter = bookChapterMapper.selectOne(queryWrapper); BookChapter firstBookChapter = bookChapterMapper.selectOne(queryWrapper);
// 组装响应对象 // 组装响应对象
return BookInfoRespDto.builder() return BookInfoRespDto.builder()
@ -68,9 +69,9 @@ public class BookInfoCacheManager {
, value = CacheConsts.LAST_UPDATE_BOOK_ID_LIST_CACHE_NAME) , value = CacheConsts.LAST_UPDATE_BOOK_ID_LIST_CACHE_NAME)
public List<Long> getLastUpdateIdList(Long categoryId) { public List<Long> getLastUpdateIdList(Long categoryId) {
QueryWrapper<BookInfo> queryWrapper = new QueryWrapper<>(); QueryWrapper<BookInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("category_id", categoryId) queryWrapper.eq(DatabaseConsts.BookTable.ColumnEnum.CATEGORY_ID.getName(), categoryId)
.orderByDesc("last_chapter_update_time") .orderByDesc(DatabaseConsts.BookTable.ColumnEnum.LAST_CHAPTER_UPDATE_TIME.getName())
.last("limit 500"); .last(DatabaseConsts.SqlEnum.LIMIT_500.getSql());
return bookInfoMapper.selectList(queryWrapper).stream().map(BookInfo::getId).toList(); return bookInfoMapper.selectList(queryWrapper).stream().map(BookInfo::getId).toList();
} }

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.constant.CacheConsts; import io.github.xxyopen.novel.core.constant.CacheConsts;
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
import io.github.xxyopen.novel.dao.entity.BookInfo; import io.github.xxyopen.novel.dao.entity.BookInfo;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper; import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto; import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
@ -31,7 +32,7 @@ public class BookRankCacheManager {
public List<BookRankRespDto> listVisitRankBooks() { public List<BookRankRespDto> listVisitRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>(); QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper bookInfoQueryWrapper
.orderByDesc("visit_count"); .orderByDesc(DatabaseConsts.BookTable.ColumnEnum.VISIT_COUNT.getName());
return getBookRankRespDtos(bookInfoQueryWrapper); return getBookRankRespDtos(bookInfoQueryWrapper);
} }
@ -43,7 +44,7 @@ public class BookRankCacheManager {
public List<BookRankRespDto> listNewestRankBooks() { public List<BookRankRespDto> listNewestRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>(); QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper bookInfoQueryWrapper
.orderByDesc("create_time"); .orderByDesc(DatabaseConsts.CommonColumnEnum.CREATE_TIME.getName());
return getBookRankRespDtos(bookInfoQueryWrapper); return getBookRankRespDtos(bookInfoQueryWrapper);
} }
@ -55,12 +56,12 @@ public class BookRankCacheManager {
public List<BookRankRespDto> listUpdateRankBooks() { public List<BookRankRespDto> listUpdateRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>(); QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper bookInfoQueryWrapper
.orderByDesc("update_time"); .orderByDesc(DatabaseConsts.CommonColumnEnum.UPDATE_TIME.getName());
return getBookRankRespDtos(bookInfoQueryWrapper); return getBookRankRespDtos(bookInfoQueryWrapper);
} }
private List<BookRankRespDto> getBookRankRespDtos(QueryWrapper<BookInfo> bookInfoQueryWrapper) { private List<BookRankRespDto> getBookRankRespDtos(QueryWrapper<BookInfo> bookInfoQueryWrapper) {
bookInfoQueryWrapper.last("limit 30"); bookInfoQueryWrapper.last(DatabaseConsts.SqlEnum.LIMIT_30.getSql());
return bookInfoMapper.selectList(bookInfoQueryWrapper).stream().map(v -> { return bookInfoMapper.selectList(bookInfoQueryWrapper).stream().map(v -> {
BookRankRespDto respDto = new BookRankRespDto(); BookRankRespDto respDto = new BookRankRespDto();
respDto.setId(v.getId()); respDto.setId(v.getId());

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.constant.CacheConsts; import io.github.xxyopen.novel.core.constant.CacheConsts;
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
import io.github.xxyopen.novel.dao.entity.HomeFriendLink; import io.github.xxyopen.novel.dao.entity.HomeFriendLink;
import io.github.xxyopen.novel.dao.mapper.HomeFriendLinkMapper; import io.github.xxyopen.novel.dao.mapper.HomeFriendLinkMapper;
import io.github.xxyopen.novel.dto.resp.HomeFriendLinkRespDto; import io.github.xxyopen.novel.dto.resp.HomeFriendLinkRespDto;
@ -31,7 +32,7 @@ public class FriendLinkCacheManager {
public List<HomeFriendLinkRespDto> listFriendLinks() { public List<HomeFriendLinkRespDto> listFriendLinks() {
// 从友情链接表中查询出友情链接列表 // 从友情链接表中查询出友情链接列表
QueryWrapper<HomeFriendLink> queryWrapper = new QueryWrapper<>(); QueryWrapper<HomeFriendLink> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("sort"); queryWrapper.orderByAsc(DatabaseConsts.CommonColumnEnum.SORT.getName());
return friendLinkMapper.selectList(queryWrapper).stream().map(v -> { return friendLinkMapper.selectList(queryWrapper).stream().map(v -> {
HomeFriendLinkRespDto respDto = new HomeFriendLinkRespDto(); HomeFriendLinkRespDto respDto = new HomeFriendLinkRespDto();
respDto.setLinkName(v.getLinkName()); respDto.setLinkName(v.getLinkName());

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.constant.CacheConsts; import io.github.xxyopen.novel.core.constant.CacheConsts;
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
import io.github.xxyopen.novel.dao.entity.BookInfo; import io.github.xxyopen.novel.dao.entity.BookInfo;
import io.github.xxyopen.novel.dao.entity.HomeBook; import io.github.xxyopen.novel.dao.entity.HomeBook;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper; import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
@ -40,7 +41,7 @@ public class HomeBookCacheManager {
public List<HomeBookRespDto> listHomeBooks() { public List<HomeBookRespDto> listHomeBooks() {
// 从首页小说推荐表中查询出需要推荐的小说 // 从首页小说推荐表中查询出需要推荐的小说
QueryWrapper<HomeBook> queryWrapper = new QueryWrapper<>(); QueryWrapper<HomeBook> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("sort"); queryWrapper.orderByAsc(DatabaseConsts.CommonColumnEnum.SORT.getName());
List<HomeBook> homeBooks = homeBookMapper.selectList(queryWrapper); List<HomeBook> homeBooks = homeBookMapper.selectList(queryWrapper);
// 获取推荐小说ID列表 // 获取推荐小说ID列表
@ -51,7 +52,7 @@ public class HomeBookCacheManager {
// 根据小说ID列表查询相关的小说信息列表 // 根据小说ID列表查询相关的小说信息列表
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>(); QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper.in("id", bookIds); bookInfoQueryWrapper.in(DatabaseConsts.CommonColumnEnum.ID.getName(), bookIds);
List<BookInfo> bookInfos = bookInfoMapper.selectList(bookInfoQueryWrapper); List<BookInfo> bookInfos = bookInfoMapper.selectList(bookInfoQueryWrapper);
// 组装 HomeBookRespDto 列表数据并返回 // 组装 HomeBookRespDto 列表数据并返回

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.constant.CacheConsts; import io.github.xxyopen.novel.core.constant.CacheConsts;
import io.github.xxyopen.novel.core.constant.DatabaseConsts;
import io.github.xxyopen.novel.dao.entity.NewsInfo; import io.github.xxyopen.novel.dao.entity.NewsInfo;
import io.github.xxyopen.novel.dao.mapper.NewsInfoMapper; import io.github.xxyopen.novel.dao.mapper.NewsInfoMapper;
import io.github.xxyopen.novel.dto.resp.NewsInfoRespDto; import io.github.xxyopen.novel.dto.resp.NewsInfoRespDto;
@ -31,8 +32,8 @@ public class NewsCacheManager {
public List<NewsInfoRespDto> listLatestNews() { public List<NewsInfoRespDto> listLatestNews() {
// 从新闻信息表中查询出最新发布的两条新闻 // 从新闻信息表中查询出最新发布的两条新闻
QueryWrapper<NewsInfo> queryWrapper = new QueryWrapper<>(); QueryWrapper<NewsInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("create_time") queryWrapper.orderByDesc(DatabaseConsts.CommonColumnEnum.CREATE_TIME.getName())
.last("limit 2"); .last(DatabaseConsts.SqlEnum.LIMIT_2.getSql());
return newsInfoMapper.selectList(queryWrapper).stream().map(v -> { return newsInfoMapper.selectList(queryWrapper).stream().map(v -> {
NewsInfoRespDto respDto = new NewsInfoRespDto(); NewsInfoRespDto respDto = new NewsInfoRespDto();
respDto.setId(v.getId()); respDto.setId(v.getId());

View File

@ -56,4 +56,14 @@ public interface BookService {
* 增加小说点击量 * 增加小说点击量
* */ * */
RestResp<Void> addVisitCount(Long bookId); RestResp<Void> addVisitCount(Long bookId);
/**
* 获取上一章节ID
* */
RestResp<Long> getPreChapterId(Long chapterId);
/**
* 获取下一章节ID
* */
RestResp<Long> nextChapterId(Long chapterId);
} }

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.github.xxyopen.novel.core.common.resp.RestResp; import io.github.xxyopen.novel.core.common.resp.RestResp;
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.mapper.BookChapterMapper; import io.github.xxyopen.novel.dao.mapper.BookChapterMapper;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper; import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
@ -19,6 +20,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Random; import java.util.Random;
/** /**
@ -79,7 +81,7 @@ public class BookServiceImpl implements BookService {
// 查询章节总数 // 查询章节总数
QueryWrapper<BookChapter> chapterQueryWrapper = new QueryWrapper<>(); QueryWrapper<BookChapter> chapterQueryWrapper = new QueryWrapper<>();
chapterQueryWrapper.eq("book_id", bookId); chapterQueryWrapper.eq(DatabaseConsts.BookChapterTable.ColumnEnum.BOOK_ID.getName(), bookId);
Long chapterTotal = bookChapterMapper.selectCount(chapterQueryWrapper); Long chapterTotal = bookChapterMapper.selectCount(chapterQueryWrapper);
// 组装数据并返回 // 组装数据并返回
@ -98,14 +100,14 @@ public class BookServiceImpl implements BookService {
List<Integer> recIdIndexList = new ArrayList<>(); List<Integer> recIdIndexList = new ArrayList<>();
int count = 0; int count = 0;
Random rand = SecureRandom.getInstanceStrong(); Random rand = SecureRandom.getInstanceStrong();
while (count < REC_BOOK_COUNT){ while (count < REC_BOOK_COUNT) {
int recIdIndex = rand.nextInt(lastUpdateIdList.size()); int recIdIndex = rand.nextInt(lastUpdateIdList.size());
if (!recIdIndexList.contains(recIdIndex)) { if (!recIdIndexList.contains(recIdIndex)) {
recIdIndexList.add(recIdIndex); recIdIndexList.add(recIdIndex);
bookId = lastUpdateIdList.get(recIdIndex); bookId = lastUpdateIdList.get(recIdIndex);
BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(bookId); BookInfoRespDto bookInfo = bookInfoCacheManager.getBookInfo(bookId);
respDtoList.add(bookInfo); respDtoList.add(bookInfo);
count ++ ; count++;
} }
} }
return RestResp.ok(respDtoList); return RestResp.ok(respDtoList);
@ -117,6 +119,46 @@ public class BookServiceImpl implements BookService {
return RestResp.ok(); return RestResp.ok();
} }
@Override
public RestResp<Long> getPreChapterId(Long chapterId) {
// 查询小说ID 章节号
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
Long bookId = chapter.getBookId();
Integer chapterNum = chapter.getChapterNum();
// 查询上一章信息并返回章节ID
QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(DatabaseConsts.BookChapterTable.ColumnEnum.BOOK_ID.getName(), bookId)
.lt(DatabaseConsts.BookChapterTable.ColumnEnum.CHAPTER_NUM.getName(), chapterNum)
.orderByDesc(DatabaseConsts.BookChapterTable.ColumnEnum.CHAPTER_NUM.getName())
.last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
return RestResp.ok(
Optional.ofNullable(bookChapterMapper.selectOne(queryWrapper))
.map(BookChapter::getId)
.orElse(null)
);
}
@Override
public RestResp<Long> nextChapterId(Long chapterId) {
// 查询小说ID 章节号
BookChapterRespDto chapter = bookChapterCacheManager.getChapter(chapterId);
Long bookId = chapter.getBookId();
Integer chapterNum = chapter.getChapterNum();
// 查询下一章信息并返回章节ID
QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(DatabaseConsts.BookChapterTable.ColumnEnum.BOOK_ID.getName(), bookId)
.gt(DatabaseConsts.BookChapterTable.ColumnEnum.CHAPTER_NUM.getName(), chapterNum)
.orderByAsc(DatabaseConsts.BookChapterTable.ColumnEnum.CHAPTER_NUM.getName())
.last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
return RestResp.ok(
Optional.ofNullable(bookChapterMapper.selectOne(queryWrapper))
.map(BookChapter::getId)
.orElse(null)
);
}
@Override @Override
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) { public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
// 查询章节信息 // 查询章节信息