feat: 新增小说搜索接口

This commit is contained in:
xiongxiaoyang 2022-05-16 09:36:16 +08:00
parent 403624f043
commit 06ef610424
10 changed files with 205 additions and 0 deletions

View File

@ -1,7 +1,9 @@
package io.github.xxyopen.novel.controller.front; package io.github.xxyopen.novel.controller.front;
import io.github.xxyopen.novel.core.common.resp.PageRespDto;
import io.github.xxyopen.novel.core.constant.ApiRouterConsts; import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
import io.github.xxyopen.novel.core.common.resp.RestResp; import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.req.BookSearchReqDto;
import io.github.xxyopen.novel.dto.resp.*; import io.github.xxyopen.novel.dto.resp.*;
import io.github.xxyopen.novel.service.BookService; import io.github.xxyopen.novel.service.BookService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -23,6 +25,14 @@ public class BookController {
private final BookService bookService; private final BookService bookService;
/**
* 小说搜索接口
* */
@GetMapping("search")
public RestResp<PageRespDto<BookInfoRespDto>> searchBooks(BookSearchReqDto condition){
return bookService.searchBooks(condition);
}
/** /**
* 小说信息查询接口 * 小说信息查询接口
* */ * */

View File

@ -0,0 +1,28 @@
package io.github.xxyopen.novel.core.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Mybatis-Plus 配置类
*
* @author xiongxiaoyang
* @date 2022/5/16
*/
@Configuration
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

View File

@ -1,9 +1,14 @@
package io.github.xxyopen.novel.dao.mapper; package io.github.xxyopen.novel.dao.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.xxyopen.novel.dao.entity.BookInfo; import io.github.xxyopen.novel.dao.entity.BookInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.github.xxyopen.novel.dto.req.BookSearchReqDto;
import io.github.xxyopen.novel.dto.resp.BookInfoRespDto;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
* <p> * <p>
* 小说信息 Mapper 接口 * 小说信息 Mapper 接口
@ -20,4 +25,13 @@ public interface BookInfoMapper extends BaseMapper<BookInfo> {
* @param bookId 小说ID * @param bookId 小说ID
*/ */
void addVisitCount(@Param("bookId") Long bookId); void addVisitCount(@Param("bookId") Long bookId);
/**
* 小说搜索
* @param page mybatis-plus 分页对象
* @param condition 搜索条件
* @return 返回结果
* */
List<BookInfo> searchBooks(IPage<BookInfoRespDto> page, BookSearchReqDto condition);
} }

View File

@ -0,0 +1,63 @@
package io.github.xxyopen.novel.dto.req;
import io.github.xxyopen.novel.core.common.req.PageReqDto;
import lombok.Data;
import java.util.Date;
/**
* 小说搜索 请求DTO
*
* @author xiongxiaoyang
* @date 2022/5/16
*/
@Data
public class BookSearchReqDto extends PageReqDto {
/**
* 搜索关键字
*/
private String keyword;
/**
* 作品方向
*/
private Byte workDirection;
/**
* 分类ID
*/
private Integer categoryId;
/**
* 是否收费1收费0免费
*/
private Byte isVip;
/**
* 小说更新状态0连载中1已完结
*/
private Byte bookStatus;
/**
* 字数最小值
*/
private Integer wordCountMin;
/**
* 字数最大值
*/
private Integer wordCountMax;
private Date updateTimeMin;
/**
* 更新时间单位
*/
private Long updatePeriod;
/**
* 排序字段
*/
private String sort = "last_chapter_update_time desc";
}

View File

@ -83,5 +83,10 @@ public class BookInfoRespDto {
*/ */
private Long lastChapterId; private Long lastChapterId;
/**
* 最新章节名
*/
private String lastChapterName;
} }

View File

@ -1,6 +1,8 @@
package io.github.xxyopen.novel.service; package io.github.xxyopen.novel.service;
import io.github.xxyopen.novel.core.common.resp.PageRespDto;
import io.github.xxyopen.novel.core.common.resp.RestResp; import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.req.BookSearchReqDto;
import io.github.xxyopen.novel.dto.resp.*; import io.github.xxyopen.novel.dto.resp.*;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -14,58 +16,84 @@ import java.util.List;
*/ */
public interface BookService { public interface BookService {
/**
* 小说搜索
* @param condition 搜索条件
* @return 搜索结果
* */
RestResp<PageRespDto<BookInfoRespDto>> searchBooks(BookSearchReqDto condition);
/** /**
* 小说点击榜查询 * 小说点击榜查询
* @return 小说点击排行列表
* */ * */
RestResp<List<BookRankRespDto>> listVisitRankBooks(); RestResp<List<BookRankRespDto>> listVisitRankBooks();
/** /**
* 小说新书榜查询 * 小说新书榜查询
* @return 小说新书排行列表
* */ * */
RestResp<List<BookRankRespDto>> listNewestRankBooks(); RestResp<List<BookRankRespDto>> listNewestRankBooks();
/** /**
* 小说更新榜查询 * 小说更新榜查询
* @return 小说更新排行列表
* */ * */
RestResp<List<BookRankRespDto>> listUpdateRankBooks(); RestResp<List<BookRankRespDto>> listUpdateRankBooks();
/** /**
* 小说信息查询 * 小说信息查询
* @param bookId 小说ID
* @return 小说信息
* */ * */
RestResp<BookInfoRespDto> getBookById(Long bookId); RestResp<BookInfoRespDto> getBookById(Long bookId);
/** /**
* 小说内容相关信息查询 * 小说内容相关信息查询
* @param chapterId 章节ID
* @return 内容相关联的信息
* */ * */
RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId); RestResp<BookContentAboutRespDto> getBookContentAbout(Long chapterId);
/** /**
* 小说最新章节相关信息查询 * 小说最新章节相关信息查询
* @param bookId 小说ID
* @return 章节相关联的信息
* */ * */
RestResp<BookChapterAboutRespDto> getLastChapterAbout(Long bookId); RestResp<BookChapterAboutRespDto> getLastChapterAbout(Long bookId);
/** /**
* 小说推荐列表查询 * 小说推荐列表查询
* @param bookId 小说ID
* @return 小说信息列表
* */ * */
RestResp<List<BookInfoRespDto>> listRecBooks(Long bookId) throws NoSuchAlgorithmException; RestResp<List<BookInfoRespDto>> listRecBooks(Long bookId) throws NoSuchAlgorithmException;
/** /**
* 增加小说点击量 * 增加小说点击量
* @param bookId 小说ID
* @return 成功状态
* */ * */
RestResp<Void> addVisitCount(Long bookId); RestResp<Void> addVisitCount(Long bookId);
/** /**
* 获取上一章节ID * 获取上一章节ID
* @param chapterId 章节ID
* @return 上一章节ID
* */ * */
RestResp<Long> getPreChapterId(Long chapterId); RestResp<Long> getPreChapterId(Long chapterId);
/** /**
* 获取下一章节ID * 获取下一章节ID
* @param chapterId 章节ID
* @return 下一章节ID
* */ * */
RestResp<Long> nextChapterId(Long chapterId); RestResp<Long> nextChapterId(Long chapterId);
/** /**
* 小说章节列表查询 * 小说章节列表查询
* @param bookId 小说ID
* @return 小说章节列表
* */ * */
RestResp<List<BookChapterRespDto>> listChapters(Long bookId); RestResp<List<BookChapterRespDto>> listChapters(Long bookId);
} }

View File

@ -23,6 +23,7 @@ public interface HomeService {
/** /**
* 首页友情链接列表查询 * 首页友情链接列表查询
* @return 友情链接列表
* */ * */
RestResp<List<HomeFriendLinkRespDto>> listHomeFriendLinks(); RestResp<List<HomeFriendLinkRespDto>> listHomeFriendLinks();
} }

View File

@ -15,6 +15,7 @@ public interface NewsService {
/** /**
* 最新新闻列表查询 * 最新新闻列表查询
* @return 新闻列表
* */ * */
RestResp<List<NewsInfoRespDto>> listLatestNews(); RestResp<List<NewsInfoRespDto>> listLatestNews();
} }

View File

@ -1,11 +1,15 @@
package io.github.xxyopen.novel.service.impl; package io.github.xxyopen.novel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.xxyopen.novel.core.common.resp.PageRespDto;
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.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.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;
import io.github.xxyopen.novel.dto.req.BookSearchReqDto;
import io.github.xxyopen.novel.dto.resp.*; import io.github.xxyopen.novel.dto.resp.*;
import io.github.xxyopen.novel.manager.BookChapterCacheManager; import io.github.xxyopen.novel.manager.BookChapterCacheManager;
import io.github.xxyopen.novel.manager.BookContentCacheManager; import io.github.xxyopen.novel.manager.BookContentCacheManager;
@ -48,6 +52,25 @@ public class BookServiceImpl implements BookService {
private static final Integer REC_BOOK_COUNT = 4; private static final Integer REC_BOOK_COUNT = 4;
@Override
public RestResp<PageRespDto<BookInfoRespDto>> searchBooks(BookSearchReqDto condition) {
Page<BookInfoRespDto> page = new Page<>();
page.setCurrent(condition.getPageNum());
page.setSize(condition.getPageSize());
List<BookInfo> bookInfos = bookInfoMapper.searchBooks(page, condition);
return RestResp.ok(PageRespDto.of(condition.getPageNum(),condition.getPageSize(),page.getTotal()
,bookInfos.stream().map(v -> BookInfoRespDto.builder()
.id(v.getId())
.bookName(v.getBookName())
.categoryId(v.getCategoryId())
.categoryName(v.getCategoryName())
.authorId(v.getAuthorId())
.authorName(v.getAuthorName())
.wordCount(v.getWordCount())
.lastChapterName(v.getLastChapterName())
.build()).toList()));
}
@Override @Override
public RestResp<List<BookRankRespDto>> listVisitRankBooks() { public RestResp<List<BookRankRespDto>> listVisitRankBooks() {
return RestResp.ok(bookRankCacheManager.listVisitRankBooks()); return RestResp.ok(bookRankCacheManager.listVisitRankBooks());

View File

@ -2,6 +2,38 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.github.xxyopen.novel.dao.mapper.BookInfoMapper"> <mapper namespace="io.github.xxyopen.novel.dao.mapper.BookInfoMapper">
<select id="searchBooks" resultType="io.github.xxyopen.novel.dao.entity.BookInfo">
select
id,category_id,category_name,book_name,author_id,author_name,word_count,last_chapter_name
from book_info where word_count > 0
<if test="condition.keyword != null and condition.keyword != ''">
and (book_name like concat('%',#{condition.keyword},'%') or author_name like concat('%',#{condition.keyword},'%'))
</if>
<if test="condition.workDirection != null">
and work_direction = #{condition.workDirection}
</if>
<if test="condition.categoryId != null">
and condition.category_id = #{condition.categoryId}
</if>
<if test="condition.isVip != null">
and condition.is_vip = #{condition.isVip}
</if>
<if test="condition.bookStatus != null">
and condition.book_status = #{condition.bookStatus}
</if>
<if test="condition.wordCountMin != null">
and condition.word_count >= #{condition.wordCountMin}
</if>
<if test="condition.wordCountMax != null">
and condition.word_count <![CDATA[ < ]]> #{condition.wordCountMax}
</if>
<if test="condition.updateTimeMin != null">
and condition.last_chapter_update_time >= #{condition.updateTimeMin}
</if>
order by #{condition.sort}
</select>
<update id="addVisitCount"> <update id="addVisitCount">
update book_info update book_info
set visit_count = visit_count + 1 set visit_count = visit_count + 1