From d35b6e7da32b9f1570caa81215d000fc7e19afb4 Mon Sep 17 00:00:00 2001 From: johnniang Date: Mon, 8 Apr 2019 19:12:30 +0800 Subject: [PATCH] Refactor controllers and HaloConst --- .../app/config/WebMvcAutoConfiguration.java | 18 ++++----- .../halo/app/listener/StartedListener.java | 28 ++++---------- .../run/halo/app/model/support/HaloConst.java | 5 --- .../run/halo/app/service/OptionService.java | 28 ++++++++++---- .../run/halo/app/service/PostService.java | 1 + .../java/run/halo/app/service/TagService.java | 3 +- .../run/halo/app/service/ThemeService.java | 10 +++++ .../app/service/impl/OptionServiceImpl.java | 15 ++++++++ .../app/service/impl/ThemeServiceImpl.java | 36 +++++++++++++++++- .../java/run/halo/app/utils/HaloUtils.java | 8 +++- .../controller/admin/api/ThemeController.java | 19 +++------- .../content/ContentCategoryController.java | 17 +++++---- .../content/ContentIndexController.java | 19 +++++----- .../content/ContentPageController.java | 37 ++++++++++--------- .../content/ContentTagController.java | 33 ++++++++--------- .../content/base/BaseContentController.java | 26 ------------- .../web/controller/core/CommonController.java | 24 +++++++----- 17 files changed, 178 insertions(+), 149 deletions(-) diff --git a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java index 4c2fdfbec..09a67cc67 100644 --- a/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java +++ b/src/main/java/run/halo/app/config/WebMvcAutoConfiguration.java @@ -1,10 +1,5 @@ package run.halo.app.config; -import run.halo.app.config.properties.HaloProperties; -import run.halo.app.factory.StringToEnumConverterFactory; -import run.halo.app.model.support.HaloConst; -import run.halo.app.security.resolver.AuthenticationArgumentResolver; -import run.halo.app.web.controller.support.PageJacksonSerializer; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -25,8 +20,11 @@ import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; +import run.halo.app.config.properties.HaloProperties; import run.halo.app.factory.StringToEnumConverterFactory; +import run.halo.app.model.support.HaloConst; import run.halo.app.security.resolver.AuthenticationArgumentResolver; +import run.halo.app.web.controller.support.PageJacksonSerializer; import java.util.List; @@ -43,6 +41,8 @@ import java.util.List; @PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8") public class WebMvcAutoConfiguration implements WebMvcConfigurer { + private static final String FILE_PROTOCOL = "file:///"; + @Autowired private HaloProperties haloProperties; @@ -76,13 +76,13 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { .addResourceLocations("classpath:/static/"); registry.addResourceHandler("/**") .addResourceLocations("classpath:/templates/themes/") - .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/templates/themes/"); + .addResourceLocations(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/themes/"); registry.addResourceHandler("/upload/**") - .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/upload/"); + .addResourceLocations(FILE_PROTOCOL + haloProperties.getWorkDir() + "upload/"); registry.addResourceHandler("/favicon.ico") .addResourceLocations("classpath:/static/halo-admin/images/favicon.ico"); registry.addResourceHandler("/backup/**") - .addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/halo/backup/"); + .addResourceLocations(FILE_PROTOCOL + haloProperties.getWorkDir() + "backup/"); registry.addResourceHandler("/admin/**") .addResourceLocations("classpath:/static/admin/"); @@ -108,7 +108,7 @@ public class WebMvcAutoConfiguration implements WebMvcConfigurer { @Bean public FreeMarkerConfigurer freemarkerConfig() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); - configurer.setTemplateLoaderPaths("file:///" + System.getProperties().getProperty("user.home") + "/halo/templates/", "classpath:/templates/"); + configurer.setTemplateLoaderPaths(FILE_PROTOCOL + haloProperties.getWorkDir() + "templates/", "classpath:/templates/"); configurer.setDefaultEncoding("UTF-8"); return configurer; } diff --git a/src/main/java/run/halo/app/listener/StartedListener.java b/src/main/java/run/halo/app/listener/StartedListener.java index e21514551..fddd7fcbf 100644 --- a/src/main/java/run/halo/app/listener/StartedListener.java +++ b/src/main/java/run/halo/app/listener/StartedListener.java @@ -1,16 +1,5 @@ package run.halo.app.listener; -import run.halo.app.config.properties.HaloProperties; -import run.halo.app.model.entity.User; -import run.halo.app.model.params.UserParam; -import run.halo.app.model.properties.BlogProperties; -import run.halo.app.model.properties.PrimaryProperties; -import run.halo.app.model.support.HaloConst; -import run.halo.app.model.support.Theme; -import run.halo.app.service.OptionService; -import run.halo.app.service.ThemeService; -import run.halo.app.service.UserService; -import run.halo.app.utils.HaloUtils; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import com.fasterxml.jackson.databind.ObjectMapper; @@ -22,12 +11,16 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Configuration; import org.springframework.util.ResourceUtils; +import run.halo.app.config.properties.HaloProperties; import run.halo.app.model.entity.User; import run.halo.app.model.params.UserParam; import run.halo.app.model.properties.BlogProperties; import run.halo.app.model.properties.PrimaryProperties; +import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.Theme; +import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; +import run.halo.app.service.UserService; import run.halo.app.utils.HaloUtils; import java.io.File; @@ -36,9 +29,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import static run.halo.app.model.support.HaloConst.ACTIVATED_THEME_NAME; -import static run.halo.app.model.support.HaloConst.DEFAULT_THEME_NAME; - /** * The method executed after the application is started. * @@ -75,7 +65,7 @@ public class StartedListener implements ApplicationListener { @Transactional void save(List optionParams, @NonNull OptionSource source); + /** + * Saves a property. + * + * @param property must not be null + * @param value could be null + * @param source must not be null + */ + @Transactional + void saveProperty(@NonNull PropertyEnum property, String value, @NonNull OptionSource source); + /** * Saves blog properties. * @@ -276,4 +279,13 @@ public interface OptionService extends CrudService { */ @NonNull Locale getLocale(); + + /** + * Gets current active theme. + * + * @return current active theme + */ + @NonNull + String getTheme(); + } diff --git a/src/main/java/run/halo/app/service/PostService.java b/src/main/java/run/halo/app/service/PostService.java index ff5d4d909..33dbc3a07 100755 --- a/src/main/java/run/halo/app/service/PostService.java +++ b/src/main/java/run/halo/app/service/PostService.java @@ -124,6 +124,7 @@ public interface PostService extends CrudService { * @param url post url. * @return Post */ + @NonNull Post getByUrl(@NonNull String url); /** diff --git a/src/main/java/run/halo/app/service/TagService.java b/src/main/java/run/halo/app/service/TagService.java index cbab4cd41..5dcd5fce9 100644 --- a/src/main/java/run/halo/app/service/TagService.java +++ b/src/main/java/run/halo/app/service/TagService.java @@ -1,10 +1,9 @@ package run.halo.app.service; +import org.springframework.lang.NonNull; import run.halo.app.model.dto.TagOutputDTO; import run.halo.app.model.entity.Tag; import run.halo.app.service.base.CrudService; -import org.springframework.lang.NonNull; -import run.halo.app.service.base.CrudService; import java.util.List; diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index 2570883bb..1911e2fa7 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -119,4 +119,14 @@ public interface ThemeService { */ @Nullable Object fetchConfig(@NonNull String themeName); + + /** + * Render a theme page. + * + * @param pageName must not be blank + * @return full path of the theme page + */ + @NonNull + String render(@NonNull String pageName); + } diff --git a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java index 1ed2db6ac..2e8d11a07 100644 --- a/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/OptionServiceImpl.java @@ -24,6 +24,8 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; +import static run.halo.app.model.support.HaloConst.DEFAULT_THEME_NAME; + /** * OptionService implementation class * @@ -102,6 +104,14 @@ public class OptionServiceImpl extends AbstractCrudService impl optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue(), source)); } + @Override + public void saveProperty(PropertyEnum property, String value, OptionSource source) { + Assert.notNull(property, "Property must not be null"); + Assert.notNull(source, "Option source must not be null"); + + save(property.getValue(), value, source); + } + @Override public void saveProperties(Map properties, OptionSource source) { if (CollectionUtils.isEmpty(properties)) { @@ -280,4 +290,9 @@ public class OptionServiceImpl extends AbstractCrudService impl } }).orElseGet(Locale::getDefault); } + + @Override + public String getTheme() { + return getByProperty(PrimaryProperties.THEME).orElse(DEFAULT_THEME_NAME); + } } diff --git a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java index 528a9ef79..092c75699 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -9,6 +9,7 @@ import cn.hutool.setting.dialect.Props; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import lombok.extern.slf4j.Slf4j; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import run.halo.app.config.properties.HaloProperties; @@ -17,6 +18,7 @@ import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.Theme; import run.halo.app.model.support.ThemeFile; import run.halo.app.model.support.ThemeProperties; +import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; import run.halo.app.utils.FilenameUtils; @@ -47,15 +49,30 @@ public class ThemeServiceImpl implements ThemeService { */ private static String[] FILTER_FILES = {".git", ".DS_Store", "theme.properties"}; + /** + * Theme folder location. + */ private final static String THEME_FOLDER = "templates/themes"; + /** + * Configuration file name. + */ private final static String[] OPTIONS_NAMES = {"options.yaml", "options.yml"}; + /** + * Render template. + */ + private final static String RENDER_TEMPLATE = "themes/%s/%s"; + private final Path workDir; private final ObjectMapper yamlMapper; - public ThemeServiceImpl(HaloProperties haloProperties) { + private final OptionService optionService; + + public ThemeServiceImpl(HaloProperties haloProperties, + OptionService optionService) { + this.optionService = optionService; yamlMapper = new ObjectMapper(new YAMLFactory()); workDir = Paths.get(haloProperties.getWorkDir(), THEME_FOLDER); } @@ -181,7 +198,7 @@ public class ThemeServiceImpl implements ThemeService { */ @Override public boolean isTemplateExist(String template) { - StrBuilder templatePath = new StrBuilder(HaloConst.ACTIVATED_THEME_NAME); + StrBuilder templatePath = new StrBuilder(getThemeName()); templatePath.append("/"); templatePath.append(template); File file = new File(getThemeBasePath(), templatePath.toString()); @@ -304,4 +321,19 @@ public class ThemeServiceImpl implements ThemeService { return null; } } + + @Override + public String render(String pageName) { + return String.format(RENDER_TEMPLATE, optionService.getTheme(), pageName); + } + + /** + * Gets theme name. + * + * @return theme name. + */ + @NonNull + private String getThemeName() { + return optionService.getTheme(); + } } diff --git a/src/main/java/run/halo/app/utils/HaloUtils.java b/src/main/java/run/halo/app/utils/HaloUtils.java index 51736606c..5d5d9e924 100755 --- a/src/main/java/run/halo/app/utils/HaloUtils.java +++ b/src/main/java/run/halo/app/utils/HaloUtils.java @@ -1,7 +1,6 @@ package run.halo.app.utils; import cn.hutool.core.text.StrBuilder; -import io.github.biezhi.ome.OhMyEmail; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.lang.NonNull; @@ -19,7 +18,6 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.attribute.BasicFileAttributes; import java.util.Date; -import java.util.Properties; import java.util.UUID; /** @@ -33,6 +31,12 @@ import java.util.UUID; @Slf4j public class HaloUtils { + /** + * Time format. + * + * @param totalSeconds seconds + * @return formatted time + */ @NonNull public static String timeFormat(long totalSeconds) { if (totalSeconds <= 0) { diff --git a/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java b/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java index d70af1e80..1b83e447a 100644 --- a/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java +++ b/src/main/java/run/halo/app/web/controller/admin/api/ThemeController.java @@ -6,17 +6,13 @@ import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; import run.halo.app.model.enums.OptionSource; import run.halo.app.model.properties.PrimaryProperties; -import run.halo.app.model.properties.PropertyEnum; import run.halo.app.model.support.BaseResponse; -import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.Theme; import run.halo.app.model.support.ThemeFile; import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * Theme controller. @@ -60,7 +56,7 @@ public class ThemeController { */ @GetMapping("files") public List listFiles() { - return themeService.listThemeFolderBy(HaloConst.ACTIVATED_THEME_NAME); + return themeService.listThemeFolderBy(optionService.getTheme()); } @GetMapping("files/content") @@ -76,7 +72,7 @@ public class ThemeController { @GetMapping("files/custom") public List customTemplate() { - return themeService.getCustomTpl(HaloConst.ACTIVATED_THEME_NAME); + return themeService.getCustomTpl(optionService.getTheme()); } /** @@ -86,13 +82,10 @@ public class ThemeController { * @throws TemplateModelException TemplateModelException */ @GetMapping(value = "active") - @ApiOperation("Active theme") - public void active(@RequestParam(name = "theme", defaultValue = "anatole") String theme) throws TemplateModelException { - Map properties = new HashMap<>(1); - properties.put(PrimaryProperties.THEME, theme); - // TODO Refactor: saveProperties => saveProperty - optionService.saveProperties(properties, OptionSource.SYSTEM); - HaloConst.ACTIVATED_THEME_NAME = theme; + @ApiOperation("Active a theme") + public void active(String theme) throws TemplateModelException { + // TODO Check existence of the theme + optionService.saveProperty(PrimaryProperties.THEME, theme, OptionSource.SYSTEM); configuration.setSharedVariable("themeName", theme); configuration.setSharedVariable("options", optionService.listOptions()); } diff --git a/src/main/java/run/halo/app/web/controller/content/ContentCategoryController.java b/src/main/java/run/halo/app/web/controller/content/ContentCategoryController.java index 4f0f404fe..c15b3f9ea 100644 --- a/src/main/java/run/halo/app/web/controller/content/ContentCategoryController.java +++ b/src/main/java/run/halo/app/web/controller/content/ContentCategoryController.java @@ -1,9 +1,5 @@ package run.halo.app.web.controller.content; -import run.halo.app.model.entity.Category; -import run.halo.app.service.CategoryService; -import run.halo.app.service.PostService; -import run.halo.app.web.controller.content.base.BaseContentController; import org.springframework.data.domain.Sort; import org.springframework.data.web.SortDefault; import org.springframework.stereotype.Controller; @@ -14,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import run.halo.app.model.entity.Category; import run.halo.app.service.CategoryService; import run.halo.app.service.PostService; -import run.halo.app.web.controller.content.base.BaseContentController; +import run.halo.app.service.ThemeService; import java.util.List; @@ -26,16 +22,20 @@ import static org.springframework.data.domain.Sort.Direction.DESC; */ @Controller @RequestMapping(value = "/categories") -public class ContentCategoryController extends BaseContentController { +public class ContentCategoryController { private final CategoryService categoryService; private final PostService postService; + private final ThemeService themeService; + public ContentCategoryController(CategoryService categoryService, - PostService postService) { + PostService postService, + ThemeService themeService) { this.categoryService = categoryService; this.postService = postService; + this.themeService = themeService; } /** @@ -48,7 +48,7 @@ public class ContentCategoryController extends BaseContentController { public String categories(Model model) { final List categories = categoryService.listAll(); model.addAttribute("categories", categories); - return this.render("categories"); + return themeService.render("categories"); } /** @@ -77,6 +77,7 @@ public class ContentCategoryController extends BaseContentController { @PathVariable("slugName") String slugName, @PathVariable("page") Integer page, @SortDefault(sort = "postDate", direction = DESC) Sort sort) { + // TODO Complete this api in the future return ""; } } diff --git a/src/main/java/run/halo/app/web/controller/content/ContentIndexController.java b/src/main/java/run/halo/app/web/controller/content/ContentIndexController.java index abe42de4c..069112b5e 100644 --- a/src/main/java/run/halo/app/web/controller/content/ContentIndexController.java +++ b/src/main/java/run/halo/app/web/controller/content/ContentIndexController.java @@ -1,11 +1,5 @@ package run.halo.app.web.controller.content; -import run.halo.app.model.enums.PostStatus; -import run.halo.app.model.enums.PostType; -import run.halo.app.model.vo.PostListVO; -import run.halo.app.service.OptionService; -import run.halo.app.service.PostService; -import run.halo.app.web.controller.content.base.BaseContentController; import cn.hutool.core.util.PageUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; @@ -20,8 +14,9 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import run.halo.app.model.enums.PostStatus; import run.halo.app.model.vo.PostListVO; +import run.halo.app.service.OptionService; import run.halo.app.service.PostService; -import run.halo.app.web.controller.content.base.BaseContentController; +import run.halo.app.service.ThemeService; import static org.springframework.data.domain.Sort.Direction.DESC; @@ -34,16 +29,20 @@ import static org.springframework.data.domain.Sort.Direction.DESC; @Slf4j @Controller @RequestMapping -public class ContentIndexController extends BaseContentController { +public class ContentIndexController { private final PostService postService; private final OptionService optionService; + private final ThemeService themeService; + public ContentIndexController(PostService postService, - OptionService optionService) { + OptionService optionService, + ThemeService themeService) { this.postService = postService; this.optionService = optionService; + this.themeService = themeService; } @@ -80,6 +79,6 @@ public class ContentIndexController extends BaseContentController { model.addAttribute("is_index", true); model.addAttribute("posts", posts); model.addAttribute("rainbow", rainbow); - return this.render("index"); + return themeService.render("index"); } } diff --git a/src/main/java/run/halo/app/web/controller/content/ContentPageController.java b/src/main/java/run/halo/app/web/controller/content/ContentPageController.java index 179604d1a..c115bf3b9 100644 --- a/src/main/java/run/halo/app/web/controller/content/ContentPageController.java +++ b/src/main/java/run/halo/app/web/controller/content/ContentPageController.java @@ -1,19 +1,11 @@ package run.halo.app.web.controller.content; -import run.halo.app.model.entity.Comment; -import run.halo.app.model.entity.Gallery; -import run.halo.app.model.entity.Post; -import run.halo.app.model.enums.PostStatus; -import run.halo.app.model.enums.PostType; -import run.halo.app.service.CommentService; -import run.halo.app.service.GalleryService; -import run.halo.app.service.PostService; -import run.halo.app.web.controller.content.base.BaseContentController; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; +import run.halo.app.exception.NotFoundException; import run.halo.app.model.entity.Comment; import run.halo.app.model.entity.Gallery; import run.halo.app.model.entity.Post; @@ -21,7 +13,7 @@ import run.halo.app.model.enums.PostStatus; import run.halo.app.service.CommentService; import run.halo.app.service.GalleryService; import run.halo.app.service.PostService; -import run.halo.app.web.controller.content.base.BaseContentController; +import run.halo.app.service.ThemeService; import java.util.List; @@ -30,7 +22,7 @@ import java.util.List; * @date : 2019-03-21 */ @Controller -public class ContentPageController extends BaseContentController { +public class ContentPageController { private final GalleryService galleryService; @@ -38,10 +30,16 @@ public class ContentPageController extends BaseContentController { private final CommentService commentService; - public ContentPageController(GalleryService galleryService, PostService postService, CommentService commentService) { + private final ThemeService themeService; + + public ContentPageController(GalleryService galleryService, + PostService postService, + CommentService commentService, + ThemeService themeService) { this.galleryService = galleryService; this.postService = postService; this.commentService = commentService; + this.themeService = themeService; } /** @@ -53,7 +51,7 @@ public class ContentPageController extends BaseContentController { public String gallery(Model model) { final List galleries = galleryService.listAll(); model.addAttribute("galleries", galleries); - return this.render("gallery"); + return themeService.render("gallery"); } /** @@ -63,7 +61,7 @@ public class ContentPageController extends BaseContentController { */ @GetMapping(value = "/links") public String links() { - return this.render("links"); + return themeService.render("links"); } /** @@ -78,10 +76,15 @@ public class ContentPageController extends BaseContentController { @RequestParam(value = "cp", defaultValue = "1") Integer cp, Model model) { final Post post = postService.getByUrl(url); - if (null == post || !post.getStatus().equals(PostStatus.PUBLISHED)) { - return this.renderNotFound(); + + if (!post.getStatus().equals(PostStatus.PUBLISHED)) { + throw new NotFoundException("The post isn't published").setErrorData(url); } + List comments; + + // TODO Complete this api + // if (StrUtil.equals(OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()), "true") || OPTIONS.get(BlogProperties.NEW_COMMENT_NEED_CHECK.getValue()) == null) { // comments = commentService.findCommentsByPostAndCommentStatus(post, CommentStatus.PUBLISHED.getValue()); // } else { @@ -106,6 +109,6 @@ public class ContentPageController extends BaseContentController { // if (StrUtil.isNotEmpty(post.getCustomTpl())) { // return this.render(post.getCustomTpl()); // } - return this.render("page"); + return themeService.render("page"); } } diff --git a/src/main/java/run/halo/app/web/controller/content/ContentTagController.java b/src/main/java/run/halo/app/web/controller/content/ContentTagController.java index f7580d9d4..95a65d5a2 100644 --- a/src/main/java/run/halo/app/web/controller/content/ContentTagController.java +++ b/src/main/java/run/halo/app/web/controller/content/ContentTagController.java @@ -1,11 +1,5 @@ package run.halo.app.web.controller.content; -import run.halo.app.model.entity.Tag; -import run.halo.app.model.vo.PostListVO; -import run.halo.app.service.OptionService; -import run.halo.app.service.PostService; -import run.halo.app.service.TagService; -import run.halo.app.web.controller.content.base.BaseContentController; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -18,9 +12,10 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import run.halo.app.model.entity.Tag; import run.halo.app.model.vo.PostListVO; +import run.halo.app.service.OptionService; import run.halo.app.service.PostService; import run.halo.app.service.TagService; -import run.halo.app.web.controller.content.base.BaseContentController; +import run.halo.app.service.ThemeService; import static org.springframework.data.domain.Sort.Direction.DESC; @@ -32,7 +27,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC; */ @Controller @RequestMapping(value = "/tags") -public class ContentTagController extends BaseContentController { +public class ContentTagController { private final TagService tagService; @@ -40,12 +35,16 @@ public class ContentTagController extends BaseContentController { private final OptionService optionService; + private final ThemeService themeService; + public ContentTagController(TagService tagService, PostService postService, - OptionService optionService) { + OptionService optionService, + ThemeService themeService) { this.tagService = tagService; this.postService = postService; this.optionService = optionService; + this.themeService = themeService; } /** @@ -55,7 +54,7 @@ public class ContentTagController extends BaseContentController { */ @GetMapping public String tags() { - return this.render("tags"); + return themeService.render("tags"); } /** @@ -84,20 +83,18 @@ public class ContentTagController extends BaseContentController { @PathVariable("slugName") String slugName, @PathVariable("page") Integer page, @SortDefault(sort = "postDate", direction = DESC) Sort sort) { - final Tag tag = tagService.getBySlugNameOfNonNull(slugName); - if (null == tag) { - return this.renderNotFound(); - } - int size = optionService.getPostPageSize(); - final Pageable pageable = PageRequest.of(page - 1, size, sort); + Tag tag = tagService.getBySlugNameOfNonNull(slugName); + int size = optionService.getPostPageSize(); + Pageable pageable = PageRequest.of(page - 1, size, sort); + + Page posts; // TODO get posts by tag - final Page posts; //final int[] rainbow = PageUtil.rainbow(page, posts.getTotalPages(), 3); // model.addAttribute("is_tags", true); // model.addAttribute("posts", posts); // model.addAttribute("rainbow", rainbow); // model.addAttribute("tag", tag); - return this.render("tag"); + return themeService.render("tag"); } } diff --git a/src/main/java/run/halo/app/web/controller/content/base/BaseContentController.java b/src/main/java/run/halo/app/web/controller/content/base/BaseContentController.java index e3b571249..f14e3a6f1 100644 --- a/src/main/java/run/halo/app/web/controller/content/base/BaseContentController.java +++ b/src/main/java/run/halo/app/web/controller/content/base/BaseContentController.java @@ -1,9 +1,5 @@ package run.halo.app.web.controller.content.base; -import cn.hutool.core.text.StrBuilder; - -import static run.halo.app.model.support.HaloConst.ACTIVATED_THEME_NAME; - /** * Content base Controller * @@ -12,26 +8,4 @@ import static run.halo.app.model.support.HaloConst.ACTIVATED_THEME_NAME; */ public abstract class BaseContentController { - /** - * Render page by template name - * - * @param pageName pageName - * @return template path - */ - public String render(String pageName) { - final StrBuilder themeStr = new StrBuilder("themes/"); - themeStr.append(ACTIVATED_THEME_NAME); - themeStr.append("/"); - themeStr.append(pageName); - return themeStr.toString(); - } - - /** - * Redirect to 404 - * - * @return redirect:/404 - */ - public String renderNotFound() { - return "redirect:/404"; - } } diff --git a/src/main/java/run/halo/app/web/controller/core/CommonController.java b/src/main/java/run/halo/app/web/controller/core/CommonController.java index c75465cd1..ea5422170 100644 --- a/src/main/java/run/halo/app/web/controller/core/CommonController.java +++ b/src/main/java/run/halo/app/web/controller/core/CommonController.java @@ -1,16 +1,14 @@ package run.halo.app.web.controller.core; -import run.halo.app.model.entity.User; -import run.halo.app.model.support.HaloConst; -import run.halo.app.service.ThemeService; import cn.hutool.core.text.StrBuilder; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.stereotype.Controller; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import run.halo.app.model.entity.User; +import run.halo.app.model.support.HaloConst; +import run.halo.app.service.OptionService; import run.halo.app.service.ThemeService; import javax.servlet.http.HttpServletRequest; @@ -35,8 +33,16 @@ public class CommonController implements ErrorController { private static final String ADMIN_URL = "/admin"; - @Autowired - private ThemeService themeService; + private final ThemeService themeService; + + private final OptionService optionService; + + public CommonController(ThemeService themeService, + OptionService optionService) { + this.themeService = themeService; + this.optionService = optionService; + } + /** * Handle error @@ -108,7 +114,7 @@ public class CommonController implements ErrorController { return "common/error/404"; } StrBuilder path = new StrBuilder("themes/"); - path.append(HaloConst.ACTIVATED_THEME_NAME); + path.append(optionService.getTheme()); path.append("/404"); return path.toString(); } @@ -119,12 +125,12 @@ public class CommonController implements ErrorController { * @return template path: */ @GetMapping(value = "/500") - public String contentInternalError() throws FileNotFoundException { + public String contentInternalError() { if (!themeService.isTemplateExist(INTERNAL_ERROR_TEMPLATE)) { return "common/error/500"; } StrBuilder path = new StrBuilder("themes/"); - path.append(HaloConst.ACTIVATED_THEME_NAME); + path.append(optionService.getTheme()); path.append("/500"); return path.toString(); }