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);
}
/**
* 获取上一章节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 Integer chapterNum;
/**
* 章节名
*/

View File

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

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.mapper.BookContentMapper;
import lombok.RequiredArgsConstructor;
@ -27,7 +28,7 @@ public class BookContentCacheManager {
, value = CacheConsts.BOOK_CONTENT_CACHE_NAME)
public String getBookContent(Long chapterId) {
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);
return bookContent.getContent();
}

View File

@ -2,6 +2,7 @@ package io.github.xxyopen.novel.manager;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.BookInfo;
import io.github.xxyopen.novel.dao.mapper.BookChapterMapper;
@ -38,9 +39,9 @@ public class BookInfoCacheManager {
// 查询首章ID
QueryWrapper<BookChapter> queryWrapper = new QueryWrapper<>();
queryWrapper
.eq("book_id", id)
.orderByAsc("chapter_num")
.last("limit 1");
.eq(DatabaseConsts.BookChapterTable.ColumnEnum.BOOK_ID.getName(), id)
.orderByAsc(DatabaseConsts.BookChapterTable.ColumnEnum.CHAPTER_NUM.getName())
.last(DatabaseConsts.SqlEnum.LIMIT_1.getSql());
BookChapter firstBookChapter = bookChapterMapper.selectOne(queryWrapper);
// 组装响应对象
return BookInfoRespDto.builder()
@ -68,9 +69,9 @@ public class BookInfoCacheManager {
, value = CacheConsts.LAST_UPDATE_BOOK_ID_LIST_CACHE_NAME)
public List<Long> getLastUpdateIdList(Long categoryId) {
QueryWrapper<BookInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("category_id", categoryId)
.orderByDesc("last_chapter_update_time")
.last("limit 500");
queryWrapper.eq(DatabaseConsts.BookTable.ColumnEnum.CATEGORY_ID.getName(), categoryId)
.orderByDesc(DatabaseConsts.BookTable.ColumnEnum.LAST_CHAPTER_UPDATE_TIME.getName())
.last(DatabaseConsts.SqlEnum.LIMIT_500.getSql());
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 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.mapper.BookInfoMapper;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
@ -31,7 +32,7 @@ public class BookRankCacheManager {
public List<BookRankRespDto> listVisitRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper
.orderByDesc("visit_count");
.orderByDesc(DatabaseConsts.BookTable.ColumnEnum.VISIT_COUNT.getName());
return getBookRankRespDtos(bookInfoQueryWrapper);
}
@ -43,7 +44,7 @@ public class BookRankCacheManager {
public List<BookRankRespDto> listNewestRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper
.orderByDesc("create_time");
.orderByDesc(DatabaseConsts.CommonColumnEnum.CREATE_TIME.getName());
return getBookRankRespDtos(bookInfoQueryWrapper);
}
@ -55,12 +56,12 @@ public class BookRankCacheManager {
public List<BookRankRespDto> listUpdateRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper
.orderByDesc("update_time");
.orderByDesc(DatabaseConsts.CommonColumnEnum.UPDATE_TIME.getName());
return getBookRankRespDtos(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 -> {
BookRankRespDto respDto = new BookRankRespDto();
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 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.mapper.HomeFriendLinkMapper;
import io.github.xxyopen.novel.dto.resp.HomeFriendLinkRespDto;
@ -31,7 +32,7 @@ public class FriendLinkCacheManager {
public List<HomeFriendLinkRespDto> listFriendLinks() {
// 从友情链接表中查询出友情链接列表
QueryWrapper<HomeFriendLink> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("sort");
queryWrapper.orderByAsc(DatabaseConsts.CommonColumnEnum.SORT.getName());
return friendLinkMapper.selectList(queryWrapper).stream().map(v -> {
HomeFriendLinkRespDto respDto = new HomeFriendLinkRespDto();
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 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.HomeBook;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
@ -40,7 +41,7 @@ public class HomeBookCacheManager {
public List<HomeBookRespDto> listHomeBooks() {
// 从首页小说推荐表中查询出需要推荐的小说
QueryWrapper<HomeBook> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("sort");
queryWrapper.orderByAsc(DatabaseConsts.CommonColumnEnum.SORT.getName());
List<HomeBook> homeBooks = homeBookMapper.selectList(queryWrapper);
// 获取推荐小说ID列表
@ -51,7 +52,7 @@ public class HomeBookCacheManager {
// 根据小说ID列表查询相关的小说信息列表
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper.in("id", bookIds);
bookInfoQueryWrapper.in(DatabaseConsts.CommonColumnEnum.ID.getName(), bookIds);
List<BookInfo> bookInfos = bookInfoMapper.selectList(bookInfoQueryWrapper);
// 组装 HomeBookRespDto 列表数据并返回

View File

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

View File

@ -56,4 +56,14 @@ public interface BookService {
* 增加小说点击量
* */
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 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.mapper.BookChapterMapper;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
@ -19,6 +20,7 @@ import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
/**
@ -79,7 +81,7 @@ public class BookServiceImpl implements BookService {
// 查询章节总数
QueryWrapper<BookChapter> chapterQueryWrapper = new QueryWrapper<>();
chapterQueryWrapper.eq("book_id", bookId);
chapterQueryWrapper.eq(DatabaseConsts.BookChapterTable.ColumnEnum.BOOK_ID.getName(), bookId);
Long chapterTotal = bookChapterMapper.selectCount(chapterQueryWrapper);
// 组装数据并返回
@ -117,6 +119,46 @@ public class BookServiceImpl implements BookService {
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
public RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId) {
// 查询章节信息