提交 8930f9d0 编写于 作者: J johnniang

Refactor some theme services

上级 cbabebff
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");
}
......
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<ThemeProperty> getThemeBy(@NonNull String themeId);
Optional<ThemeProperty> 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.
......
......@@ -105,7 +105,9 @@ public class ThemeServiceImpl implements ThemeService {
@Override
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
Set<ThemeProperty> 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);
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册