feat(news): 增加最新新闻列表查询接口

This commit is contained in:
xiongxiaoyang 2022-05-14 14:36:38 +08:00
parent f5101be5fc
commit 56c596c5ee
10 changed files with 246 additions and 17 deletions

View File

@ -0,0 +1,34 @@
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.NewsInfoRespDto;
import io.github.xxyopen.novel.service.NewsService;
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/12
*/
@RestController
@RequestMapping(ApiRouterConsts.API_FRONT_NEWS_URL_PREFIX)
@RequiredArgsConstructor
public class NewsController {
private final NewsService newsService;
/**
* 最新新闻列表查询接口
* */
@GetMapping("latestList")
public RestResp<List<NewsInfoRespDto>> listLatestNews(){
return newsService.listLatestNews();
}
}

View File

@ -1,5 +1,6 @@
package io.github.xxyopen.novel.core.common.config;
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
@ -19,7 +20,7 @@ public class CorsConfig {
public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
// 允许的域,不要写*否则cookie就无法使用了
config.addAllowedOrigin("http://localhost:1024");
config.addAllowedOrigin(SystemConfigConsts.NOVEL_FRONT_WEB_ORIGIN);
// 允许的头信息
config.addAllowedHeader("*");
// 允许的请求方式

View File

@ -6,56 +6,70 @@ package io.github.xxyopen.novel.core.common.constant;
* @author xiongxiaoyang
* @date 2022/5/12
*/
public interface ApiRouterConsts {
public class ApiRouterConsts {
private ApiRouterConsts() {
throw new IllegalStateException("Constant class");
}
/**
* API请求路径前缀
*/
String API_URL_PREFIX = "/api";
public static final String API_URL_PREFIX = "/api";
/**
* 前台门户系统请求路径前缀
*/
String API_FRONT_URL_PREFIX = API_URL_PREFIX + "/front";
public static final String API_FRONT_URL_PREFIX = API_URL_PREFIX + "/front";
/**
* 作家管理系统请求路径前缀
*/
String API_AUTHOR_URL_PREFIX = API_URL_PREFIX + "/author";
public static final String API_AUTHOR_URL_PREFIX = API_URL_PREFIX + "/author";
/**
* 平台后台管理系统请求路径前缀
*/
String API_ADMIN_URL_PREFIX = API_URL_PREFIX + "/admin";
public static final String API_ADMIN_URL_PREFIX = API_URL_PREFIX + "/admin";
/**
* 首页模块请求路径前缀
* */
String HOME_URL_PREFIX = "/home";
public static final String HOME_URL_PREFIX = "/home";
/**
* 首页模块请求路径前缀
* */
public static final String NEWS_URL_PREFIX = "/news";
/**
* 小说模块请求路径前缀
* */
String BOOK_URL_PREFIX = "/book";
public static final String BOOK_URL_PREFIX = "/book";
/**
* 会员模块请求路径前缀
* */
String USER_URL_PREFIX = "/user";
public static final String USER_URL_PREFIX = "/user";
/**
* 前台门户首页API请求路径前缀
*/
String API_FRONT_HOME_URL_PREFIX = API_FRONT_URL_PREFIX + HOME_URL_PREFIX;
public static final String API_FRONT_HOME_URL_PREFIX = API_FRONT_URL_PREFIX + HOME_URL_PREFIX;
/**
* 前台门户新闻相关API请求路径前缀
*/
public static final String API_FRONT_NEWS_URL_PREFIX = API_FRONT_URL_PREFIX + NEWS_URL_PREFIX;
/**
* 前台门户小说相关API请求路径前缀
*/
String API_FRONT_BOOK_URL_PREFIX = API_FRONT_URL_PREFIX + BOOK_URL_PREFIX;
public static final String API_FRONT_BOOK_URL_PREFIX = API_FRONT_URL_PREFIX + BOOK_URL_PREFIX;
/**
* 前台门户会员相关API请求路径前缀
*/
String API_FRONT_USER_URL_PREFIX = API_FRONT_URL_PREFIX + USER_URL_PREFIX;
public static final String API_FRONT_USER_URL_PREFIX = API_FRONT_URL_PREFIX + USER_URL_PREFIX;
}

View File

@ -29,20 +29,48 @@ public class CacheConsts {
* */
public static final String HOME_BOOK_CACHE_NAME = "homeBookCache";
/**
* 最新新闻缓存
* */
public static final String LATEST_NEWS_CACHE_NAME = "latestNewsCache";
/**
* 小说点击榜缓存
* */
public static final String BOOK_VISIT_RANK_CACHE_NAME = "bookVisitRankCache";
/**
* 小说新书榜缓存
* */
public static final String BOOK_NEWEST_RANK_CACHE_NAME = "bookNewestRankCache";
/**
* 小说更新榜缓存
* */
public static final String BOOK_UPDATE_RANK_CACHE_NAME = "bookUpdateRankCache";
/**
* 首页友情链接缓存
* */
public static final String HOME_FRIEND_LINK_CACHE_NAME = "homeFriendLinkCache";
/**
* 缓存配置常量
*/
public enum CacheEnum {
HOME_BOOK_CACHE(1,HOME_BOOK_CACHE_NAME,0,1),
HOME_BOOK_CACHE(0,HOME_BOOK_CACHE_NAME,60 * 60 * 24,1),
HOME_FRIEND_LINK_CACHE(2,HOME_FRIEND_LINK_CACHE_NAME,1000,1)
LATEST_NEWS_CACHE(0,LATEST_NEWS_CACHE_NAME,60 * 10,1),
BOOK_VISIT_RANK_CACHE(2,BOOK_VISIT_RANK_CACHE_NAME,60 * 60 * 6,1),
BOOK_NEWEST_RANK_CACHE(0,BOOK_NEWEST_RANK_CACHE_NAME,60 * 30,1),
BOOK_UPDATE_RANK_CACHE(0,BOOK_UPDATE_RANK_CACHE_NAME,60,1),
HOME_FRIEND_LINK_CACHE(2,HOME_FRIEND_LINK_CACHE_NAME,0,1)
;
/**

View File

@ -7,4 +7,14 @@ package io.github.xxyopen.novel.core.constant;
* @date 2022/5/12
*/
public class SystemConfigConsts {
private SystemConfigConsts() {
throw new IllegalStateException("Constant class");
}
/**
* 小说前台门户系统域
* */
public static final String NOVEL_FRONT_WEB_ORIGIN = "http://localhost:1024";
}

View File

@ -0,0 +1,49 @@
package io.github.xxyopen.novel.dto.resp;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 新闻信息 响应DTO
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
@Data
public class NewsInfoRespDto {
/**
* ID
*/
private Long id;
/**
* 类别ID
*/
private Long categoryId;
/**
* 类别名
*/
private String categoryName;
/**
* 新闻来源
*/
private String sourceName;
/**
* 新闻标题
*/
private String title;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDateTime updateTime;
}

View File

@ -0,0 +1,48 @@
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.NewsInfo;
import io.github.xxyopen.novel.dao.mapper.NewsInfoMapper;
import io.github.xxyopen.novel.dto.resp.NewsInfoRespDto;
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 NewsCacheManager {
private final NewsInfoMapper newsInfoMapper;
/**
* 最新新闻列表查询并放入缓存中
*/
@Cacheable(cacheManager = CacheConsts.CAFFEINE_CACHE_MANAGER
, value = CacheConsts.LATEST_NEWS_CACHE_NAME)
public List<NewsInfoRespDto> listLatestNews() {
// 从新闻信息表中查询出最新发布的两条新闻
QueryWrapper<NewsInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("create_time")
.last("limit 2");
return newsInfoMapper.selectList(queryWrapper).stream().map(v -> {
NewsInfoRespDto respDto = new NewsInfoRespDto();
respDto.setId(v.getId());
respDto.setCategoryId(v.getCategoryId());
respDto.setCategoryName(v.getCategoryName());
respDto.setTitle(v.getTitle());
respDto.setSourceName(v.getSourceName());
respDto.setUpdateTime(v.getUpdateTime());
return respDto;
}).toList();
}
}

View File

@ -0,0 +1,20 @@
package io.github.xxyopen.novel.service;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.resp.NewsInfoRespDto;
import java.util.List;
/**
* 新闻模块 服务类
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
public interface NewsService {
/**
* 最新新闻列表查询
* */
RestResp<List<NewsInfoRespDto>> listLatestNews();
}

View File

@ -0,0 +1,28 @@
package io.github.xxyopen.novel.service.impl;
import io.github.xxyopen.novel.core.common.resp.RestResp;
import io.github.xxyopen.novel.dto.resp.NewsInfoRespDto;
import io.github.xxyopen.novel.manager.NewsCacheManager;
import io.github.xxyopen.novel.service.NewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 新闻模块 服务实现类
*
* @author xiongxiaoyang
* @date 2022/5/14
*/
@Service
@RequiredArgsConstructor
public class NewsServiceImpl implements NewsService {
private final NewsCacheManager newsCacheManager;
@Override
public RestResp<List<NewsInfoRespDto>> listLatestNews() {
return RestResp.ok(newsCacheManager.listLatestNews());
}
}

View File

@ -15,9 +15,6 @@
<!-- ConsoleAppender把日志输出到控制台 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!--
<pattern>%d %p (%file:%line\)- %m%n</pattern>
-->
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<!-- 控制台也要使用UTF-8不要使用GBK否则会中文乱码 -->
<charset>UTF-8</charset>