Refactor some theme services

pull/146/head
johnniang 2019-05-04 10:35:34 +08:00
parent cbabebff68
commit 8930f9d0d2
3 changed files with 17 additions and 13 deletions

View File

@ -1,6 +1,5 @@
package run.halo.app.controller.content; package run.halo.app.controller.content;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -13,6 +12,8 @@ import run.halo.app.service.SheetService;
import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeService;
/** /**
* Content sheet controller.
*
* @author ryanwang * @author ryanwang
* @date : 2019-03-21 * @date : 2019-03-21
*/ */
@ -68,11 +69,9 @@ public class ContentSheetController {
model.addAttribute("post", sheetService.convertToDetail(sheet)); model.addAttribute("post", sheetService.convertToDetail(sheet));
model.addAttribute("is_sheet", true); model.addAttribute("is_sheet", true);
if (StrUtil.isNotEmpty(sheet.getTemplate())) {
if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) { if (themeService.templateExists(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate() + HaloConst.SUFFIX_FTL)) {
return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate()); return themeService.render(ThemeService.CUSTOM_SHEET_PREFIX + sheet.getTemplate());
} }
}
return themeService.render("sheet"); return themeService.render("sheet");
} }
} }

View File

@ -1,6 +1,7 @@
package run.halo.app.service; package run.halo.app.service;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import run.halo.app.handler.theme.config.support.Group; import run.halo.app.handler.theme.config.support.Group;
import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.handler.theme.config.support.ThemeProperty;
@ -82,11 +83,11 @@ public interface ThemeService {
/** /**
* Get theme property by theme id. * Get theme property by theme id.
* *
* @param themeId must not be blank * @param themeId theme id
* @return a optional theme property * @return a optional theme property
*/ */
@NonNull @NonNull
Optional<ThemeProperty> getThemeBy(@NonNull String themeId); Optional<ThemeProperty> getThemeBy(@Nullable String themeId);
/** /**
* Gets all themes * Gets all themes
@ -126,15 +127,15 @@ public interface ThemeService {
* @param template template must not be blank * @param template template must not be blank
* @return boolean * @return boolean
*/ */
boolean templateExists(@NonNull String template); boolean templateExists(@Nullable String template);
/** /**
* Checks whether theme exists under template path * Checks whether theme exists under template path
* *
* @param themeId theme name * @param themeId theme id
* @return boolean * @return boolean
*/ */
boolean themeExists(@NonNull String themeId); boolean themeExists(@Nullable String themeId);
/** /**
* Gets theme base path. * Gets theme base path.

View File

@ -105,7 +105,9 @@ public class ThemeServiceImpl implements ThemeService {
@Override @Override
public Optional<ThemeProperty> getThemeBy(String themeId) { public Optional<ThemeProperty> getThemeBy(String themeId) {
Assert.hasText(themeId, "Theme id must not be blank"); if (StringUtils.isBlank(themeId)) {
return Optional.empty();
}
// Get all themes // Get all themes
Set<ThemeProperty> themes = getThemes(); Set<ThemeProperty> themes = getThemes();
@ -181,7 +183,9 @@ public class ThemeServiceImpl implements ThemeService {
@Override @Override
public boolean templateExists(String template) { public boolean templateExists(String template) {
Assert.hasText(template, "Template must not be blank"); if (StringUtils.isBlank(template)) {
return false;
}
// Resolve template path // Resolve template path
Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template); Path templatePath = Paths.get(getActivatedTheme().getThemePath(), template);
@ -507,7 +511,7 @@ public class ThemeServiceImpl implements ThemeService {
*/ */
private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) { private void setActivatedTheme(@Nullable ThemeProperty activatedTheme) {
this.activatedTheme = activatedTheme; this.activatedTheme = activatedTheme;
this.activatedThemeId = activatedTheme.getId(); this.activatedThemeId = Optional.ofNullable(activatedTheme).map(ThemeProperty::getId).orElse(null);
} }
/** /**