mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
feat(news): 增加最新新闻列表查询接口
This commit is contained in:
parent
f5101be5fc
commit
56c596c5ee
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package io.github.xxyopen.novel.core.common.config;
|
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.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.cors.CorsConfiguration;
|
import org.springframework.web.cors.CorsConfiguration;
|
||||||
@ -19,7 +20,7 @@ public class CorsConfig {
|
|||||||
public CorsFilter corsFilter() {
|
public CorsFilter corsFilter() {
|
||||||
CorsConfiguration config = new CorsConfiguration();
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
// 允许的域,不要写*,否则cookie就无法使用了
|
// 允许的域,不要写*,否则cookie就无法使用了
|
||||||
config.addAllowedOrigin("http://localhost:1024");
|
config.addAllowedOrigin(SystemConfigConsts.NOVEL_FRONT_WEB_ORIGIN);
|
||||||
// 允许的头信息
|
// 允许的头信息
|
||||||
config.addAllowedHeader("*");
|
config.addAllowedHeader("*");
|
||||||
// 允许的请求方式
|
// 允许的请求方式
|
||||||
|
@ -6,56 +6,70 @@ package io.github.xxyopen.novel.core.common.constant;
|
|||||||
* @author xiongxiaoyang
|
* @author xiongxiaoyang
|
||||||
* @date 2022/5/12
|
* @date 2022/5/12
|
||||||
*/
|
*/
|
||||||
public interface ApiRouterConsts {
|
public class ApiRouterConsts {
|
||||||
|
|
||||||
|
private ApiRouterConsts() {
|
||||||
|
throw new IllegalStateException("Constant class");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API请求路径前缀
|
* 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请求路径前缀
|
* 前台门户首页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请求路径前缀
|
* 前台门户小说相关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请求路径前缀
|
* 前台门户会员相关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;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,20 +29,48 @@ public class CacheConsts {
|
|||||||
* */
|
* */
|
||||||
public static final String HOME_BOOK_CACHE_NAME = "homeBookCache";
|
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 static final String HOME_FRIEND_LINK_CACHE_NAME = "homeFriendLinkCache";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 缓存配置常量
|
* 缓存配置常量
|
||||||
*/
|
*/
|
||||||
public enum CacheEnum {
|
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)
|
||||||
;
|
;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,4 +7,14 @@ package io.github.xxyopen.novel.core.constant;
|
|||||||
* @date 2022/5/12
|
* @date 2022/5/12
|
||||||
*/
|
*/
|
||||||
public class SystemConfigConsts {
|
public class SystemConfigConsts {
|
||||||
|
|
||||||
|
private SystemConfigConsts() {
|
||||||
|
throw new IllegalStateException("Constant class");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小说前台门户系统域
|
||||||
|
* */
|
||||||
|
public static final String NOVEL_FRONT_WEB_ORIGIN = "http://localhost:1024";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
@ -15,9 +15,6 @@
|
|||||||
<!-- ConsoleAppender:把日志输出到控制台 -->
|
<!-- ConsoleAppender:把日志输出到控制台 -->
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
<encoder>
|
<encoder>
|
||||||
<!--
|
|
||||||
<pattern>%d %p (%file:%line\)- %m%n</pattern>
|
|
||||||
-->
|
|
||||||
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
|
||||||
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
|
<!-- 控制台也要使用UTF-8,不要使用GBK,否则会中文乱码 -->
|
||||||
<charset>UTF-8</charset>
|
<charset>UTF-8</charset>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user