From 8930f9d0d23056e3fe64432f1ae0c93e35bd2af8 Mon Sep 17 00:00:00 2001 From: johnniang Date: Sat, 4 May 2019 10:35:34 +0800 Subject: [PATCH] Refactor some theme services --- .../controller/content/ContentSheetController.java | 9 ++++----- src/main/java/run/halo/app/service/ThemeService.java | 11 ++++++----- .../run/halo/app/service/impl/ThemeServiceImpl.java | 10 +++++++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/run/halo/app/controller/content/ContentSheetController.java b/src/main/java/run/halo/app/controller/content/ContentSheetController.java index 8d9305093..1b6e84a4d 100644 --- a/src/main/java/run/halo/app/controller/content/ContentSheetController.java +++ b/src/main/java/run/halo/app/controller/content/ContentSheetController.java @@ -1,6 +1,5 @@ package run.halo.app.controller.content; -import cn.hutool.core.util.StrUtil; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -13,6 +12,8 @@ import run.halo.app.service.SheetService; import run.halo.app.service.ThemeService; /** + * Content sheet controller. + * * @author ryanwang * @date : 2019-03-21 */ @@ -68,10 +69,8 @@ public class ContentSheetController { model.addAttribute("post", sheetService.convertToDetail(sheet)); model.addAttribute("is_sheet", true); - if (StrUtil.isNotEmpty(sheet.getTemplate())) { - if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { - return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); - } + if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { + return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); } return themeService.render("sheet"); } diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index c187c1b0b..0204573f9 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -1,6 +1,7 @@ package run.halo.app.service; import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; import org.springframework.web.multipart.MultipartFile; import run.halo.app.handler.theme.config.support.Group; import run.halo.app.handler.theme.config.support.ThemeProperty; @@ -82,11 +83,11 @@ public interface ThemeService { /** * Get theme property by theme id. * - * @param themeId must not be blank + * @param themeId theme id * @return a optional theme property */ @NonNull - Optional getThemeBy(@NonNull String themeId); + Optional getThemeBy(@Nullable String themeId); /** * Gets all themes @@ -126,15 +127,15 @@ public interface ThemeService { * @param template template must not be blank * @return boolean */ - boolean templateExists(@NonNull String template); + boolean templateExists(@Nullable String template); /** * Checks whether theme exists under template path * - * @param themeId theme name + * @param themeId theme id * @return boolean */ - boolean themeExists(@NonNull String themeId); + boolean themeExists(@Nullable String themeId); /** * Gets theme base path. 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 a967a39c4..f16dad8c6 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -105,7 +105,9 @@ public class ThemeServiceImpl implements ThemeService { @Override public Optional getThemeBy(String themeId) { - Assert.hasText(themeId, "Theme id must not be blank"); + if (StringUtils.isBlank(themeId)) { + return Optional.empty(); + } // Get all themes Set themes = getThemes(); @@ -181,7 +183,9 @@ public class ThemeServiceImpl implements ThemeService { @Override public boolean templateExists(String template) { - Assert.hasText(template, "Template must not be blank"); + if (StringUtils.isBlank(template)) { + return false; + } // Resolve template path Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template); @@ -507,7 +511,7 @@ public class ThemeServiceImpl implements ThemeService { */ private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) { this.activatedTheme = activatedTheme; - this.activatedThemeId = activatedTheme.getId(); + this.activatedThemeId = Optional.ofNullable(activatedTheme).map(ThemeProperty::getId).orElse(null); } /**