feat: 增加小说排行榜相关查询接口

This commit is contained in:
xiongxiaoyang 2022-05-14 18:19:43 +08:00
parent 11dcf3f9a5
commit 7e0062c488
5 changed files with 272 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package io.github.xxyopen.novel.controller.front;
import io.github.xxyopen.novel.core.common.constant.ApiRouterConsts;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
import io.github.xxyopen.novel.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 小说模块 API 接口
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
@RestController
@RequestMapping(ApiRouterConsts.API_FRONT_BOOK_URL_PREFIX)
@RequiredArgsConstructor
public class BookController {
private final BookService bookService;
/**
* 小说点击榜查询接口
* */
@GetMapping("visitRank")
public RestResp<List<BookRankRespDto>> listVisitRankBooks(){
return bookService.listVisitRankBooks();
}
/**
* 小说新书榜查询接口
* */
@GetMapping("newestRank")
public RestResp<List<BookRankRespDto>> listNewestRankBooks(){
return bookService.listNewestRankBooks();
}
/**
* 小说更新榜查询接口
* */
@GetMapping("updateRank")
public RestResp<List<BookRankRespDto>> listUpdateRankBooks(){
return bookService.listUpdateRankBooks();
}
}

View File

@ -0,0 +1,73 @@
package io.github.xxyopen.novel.dto.resp;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 小说排行榜 响应DTO
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
@Data
public class BookRankRespDto implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* ID
*/
private Long id;
/**
* 类别ID
*/
private Long categoryId;
/**
* 类别名
*/
private String categoryName;
/**
* 小说封面地址
*/
private String picUrl;
/**
* 小说名
*/
private String bookName;
/**
* 作家名
*/
private String authorName;
/**
* 书籍描述
*/
private String bookDesc;
/**
* 总字数
*/
private Integer wordCount;
/**
* 最新章节名
*/
private String lastChapterName;
/**
* 最新章节更新时间
*/
@JsonFormat(pattern = "MM/dd HH:mm")
private LocalDateTime lastChapterUpdateTime;
}

View File

@ -0,0 +1,79 @@
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.dao.entity.BookInfo;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 小说排行榜 缓存管理类
*
* @author xiongxiaoyang
* @date 2022/5/12
*/
@Component
@RequiredArgsConstructor
public class BookRankCacheManager {
private final BookInfoMapper bookInfoMapper;
/**
* 查询小说点击榜列表并放入缓存中
*/
@Cacheable(cacheManager = CacheConsts.REDIS_CACHE_MANAGER
, value = CacheConsts.BOOK_VISIT_RANK_CACHE_NAME)
public List<BookRankRespDto> listVisitRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper
.orderByDesc("visit_count");
return getBookRankRespDtos(bookInfoQueryWrapper);
}
/**
* 查询小说新书榜列表并放入缓存中
*/
@Cacheable(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER
, value = CacheConsts.BOOK_NEWEST_RANK_CACHE_NAME)
public List<BookRankRespDto> listNewestRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper
.orderByDesc("create_time");
return getBookRankRespDtos(bookInfoQueryWrapper);
}
/**
* 查询小说更新榜列表并放入缓存中
*/
@Cacheable(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER
, value = CacheConsts.BOOK_UPDATE_RANK_CACHE_NAME)
public List<BookRankRespDto> listUpdateRankBooks() {
QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
bookInfoQueryWrapper
.orderByDesc("update_time");
return getBookRankRespDtos(bookInfoQueryWrapper);
}
private List<BookRankRespDto> getBookRankRespDtos(QueryWrapper<BookInfo> bookInfoQueryWrapper) {
bookInfoQueryWrapper.last("limit 30");
return bookInfoMapper.selectList(bookInfoQueryWrapper).stream().map(v -> {
BookRankRespDto respDto = new BookRankRespDto();
respDto.setId(v.getId());
respDto.setCategoryId(v.getCategoryId());
respDto.setCategoryName(v.getCategoryName());
respDto.setBookName(v.getBookName());
respDto.setPicUrl(v.getPicUrl());
respDto.setBookDesc(v.getBookDesc());
respDto.setLastChapterName(v.getLastChapterName());
respDto.setLastChapterUpdateTime(v.getLastChapterUpdateTime());
respDto.setWordCount(v.getWordCount());
return respDto;
}).toList();
}
}

View File

@ -0,0 +1,30 @@
package io.github.xxyopen.novel.service;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
import java.util.List;
/**
* 小说模块 服务类
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
public interface BookService {
/**
* 小说点击榜查询
* */
RestResp<List<BookRankRespDto>> listVisitRankBooks();
/**
* 小说新书榜查询
* */
RestResp<List<BookRankRespDto>> listNewestRankBooks();
/**
* 小说更新榜查询
* */
RestResp<List<BookRankRespDto>> listUpdateRankBooks();
}

View File

@ -0,0 +1,38 @@
package io.github.xxyopen.novel.service.impl;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.resp.BookRankRespDto;
import io.github.xxyopen.novel.manager.BookRankCacheManager;
import io.github.xxyopen.novel.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 小说模块 服务实现类
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
@Service
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
private final BookRankCacheManager bookRankCacheManager;
@Override
public RestResp<List<BookRankRespDto>> listVisitRankBooks() {
return RestResp.ok(bookRankCacheManager.listVisitRankBooks());
}
@Override
public RestResp<List<BookRankRespDto>> listNewestRankBooks() {
return RestResp.ok(bookRankCacheManager.listNewestRankBooks());
}
@Override
public RestResp<List<BookRankRespDto>> listUpdateRankBooks() {
return RestResp.ok(bookRankCacheManager.listUpdateRankBooks());
}
}