diff --git a/src/main/java/io/github/xxyopen/novel/controller/front/NewsController.java b/src/main/java/io/github/xxyopen/novel/controller/front/NewsController.java new file mode 100644 index 0000000..af602c6 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/controller/front/NewsController.java @@ -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> listLatestNews(){ + return newsService.listLatestNews(); + } +} diff --git a/src/main/java/io/github/xxyopen/novel/core/common/config/CorsConfig.java b/src/main/java/io/github/xxyopen/novel/core/common/config/CorsConfig.java index 337a636..aa67b89 100644 --- a/src/main/java/io/github/xxyopen/novel/core/common/config/CorsConfig.java +++ b/src/main/java/io/github/xxyopen/novel/core/common/config/CorsConfig.java @@ -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("*"); // 允许的请求方式 diff --git a/src/main/java/io/github/xxyopen/novel/core/common/constant/ApiRouterConsts.java b/src/main/java/io/github/xxyopen/novel/core/common/constant/ApiRouterConsts.java index a92fd84..cd6eaa5 100644 --- a/src/main/java/io/github/xxyopen/novel/core/common/constant/ApiRouterConsts.java +++ b/src/main/java/io/github/xxyopen/novel/core/common/constant/ApiRouterConsts.java @@ -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; } diff --git a/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java b/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java index fd1a2b4..6508610 100644 --- a/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java +++ b/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java @@ -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) ; /** diff --git a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java index 32960f1..bd5946b 100644 --- a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java +++ b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java @@ -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"; + } diff --git a/src/main/java/io/github/xxyopen/novel/dto/resp/NewsInfoRespDto.java b/src/main/java/io/github/xxyopen/novel/dto/resp/NewsInfoRespDto.java new file mode 100644 index 0000000..5c9d681 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/dto/resp/NewsInfoRespDto.java @@ -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; + + +} diff --git a/src/main/java/io/github/xxyopen/novel/manager/NewsCacheManager.java b/src/main/java/io/github/xxyopen/novel/manager/NewsCacheManager.java new file mode 100644 index 0000000..215d056 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/manager/NewsCacheManager.java @@ -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 listLatestNews() { + // 从新闻信息表中查询出最新发布的两条新闻 + QueryWrapper 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(); + } + +} diff --git a/src/main/java/io/github/xxyopen/novel/service/NewsService.java b/src/main/java/io/github/xxyopen/novel/service/NewsService.java new file mode 100644 index 0000000..c469273 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/service/NewsService.java @@ -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> listLatestNews(); +} diff --git a/src/main/java/io/github/xxyopen/novel/service/impl/NewsServiceImpl.java b/src/main/java/io/github/xxyopen/novel/service/impl/NewsServiceImpl.java new file mode 100644 index 0000000..733c9c1 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/service/impl/NewsServiceImpl.java @@ -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> listLatestNews() { + return RestResp.ok(newsCacheManager.listLatestNews()); + } +} diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index ecb1a0b..40569f6 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -15,9 +15,6 @@ - ${CONSOLE_LOG_PATTERN} UTF-8