diff --git a/src/main/java/run/halo/app/controller/admin/api/ThemeController.java b/src/main/java/run/halo/app/controller/admin/api/ThemeController.java index 4fe33a4b6..6da4ce4b7 100644 --- a/src/main/java/run/halo/app/controller/admin/api/ThemeController.java +++ b/src/main/java/run/halo/app/controller/admin/api/ThemeController.java @@ -3,6 +3,7 @@ package run.halo.app.controller.admin.api; import io.swagger.annotations.ApiOperation; import java.util.List; import java.util.Map; +import java.util.Set; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -18,17 +19,20 @@ import org.springframework.web.multipart.MultipartFile; import run.halo.app.annotation.DisableOnCondition; import run.halo.app.cache.lock.CacheLock; import run.halo.app.handler.theme.config.support.Group; +import run.halo.app.handler.theme.config.support.Item; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.model.params.ThemeContentParam; import run.halo.app.model.support.BaseResponse; import run.halo.app.model.support.ThemeFile; import run.halo.app.service.ThemeService; import run.halo.app.service.ThemeSettingService; +import run.halo.app.utils.ServiceUtils; /** * Theme controller. * * @author ryanwang + * @author guqing * @date 2019-03-20 */ @RestController @@ -137,6 +141,19 @@ public class ThemeController { return themeService.fetchConfig(themeId); } + @GetMapping("{themeId:.+}/configurations/groups/{group}") + @ApiOperation("Fetches theme configuration by theme id and group name") + public Set fetchConfigByGroup(@PathVariable("themeId") String themeId, + @PathVariable String group) { + return themeService.fetchConfigItemsBy(themeId, group); + } + + @GetMapping("{themeId:.+}/configurations/groups") + @ApiOperation("Fetches theme configuration group names by theme id") + public Set fetchConfigGroups(@PathVariable("themeId") String themeId) { + return ServiceUtils.fetchProperty(themeService.fetchConfig(themeId), Group::getName); + } + @GetMapping("activation/settings") @ApiOperation("Lists activated theme settings") public Map listSettingsBy() { @@ -149,6 +166,13 @@ public class ThemeController { return themeSettingService.listAsMapBy(themeId); } + @GetMapping("{themeId:.+}/groups/{group}/settings") + @ApiOperation("Lists theme settings by theme id and group name") + public Map listSettingsBy(@PathVariable("themeId") String themeId, + @PathVariable String group) { + return themeSettingService.listAsMapBy(themeId, group); + } + @PostMapping("activation/settings") @ApiOperation("Saves theme settings") public void saveSettingsBy(@RequestBody Map settings) { diff --git a/src/main/java/run/halo/app/service/ThemeService.java b/src/main/java/run/halo/app/service/ThemeService.java index 509469b08..a379887bc 100644 --- a/src/main/java/run/halo/app/service/ThemeService.java +++ b/src/main/java/run/halo/app/service/ThemeService.java @@ -3,10 +3,12 @@ package run.halo.app.service; import java.nio.file.Path; import java.util.List; import java.util.Optional; +import java.util.Set; 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.Item; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.model.support.ThemeFile; @@ -14,6 +16,7 @@ import run.halo.app.model.support.ThemeFile; * Theme service interface. * * @author ryanwang + * @author guqing * @date 2019-03-26 */ public interface ThemeService { @@ -67,7 +70,7 @@ public interface ThemeService { Optional fetchThemePropertyBy(@Nullable String themeId); /** - * Gets all themes + * Gets all themes. * * @return set of themes */ @@ -95,7 +98,7 @@ public interface ThemeService { List listCustomTemplates(@NonNull String themeId, @NonNull String prefix); /** - * Judging whether template exists under the specified theme + * Judging whether template exists under the specified theme. * * @param template template must not be blank * @return boolean @@ -103,7 +106,7 @@ public interface ThemeService { boolean templateExists(@Nullable String template); /** - * Checks whether theme exists under template path + * Checks whether theme exists under template path. * * @param themeId theme id * @return boolean @@ -169,6 +172,15 @@ public interface ThemeService { @NonNull List fetchConfig(@NonNull String themeId); + /** + * Fetch config items by themeId and group. + * + * @param themeId theme id must not be blank + * @param group group name must not be blank + * @return config items + */ + Set fetchConfigItemsBy(@NonNull String themeId, String group); + /** * Renders a theme page. * diff --git a/src/main/java/run/halo/app/service/ThemeSettingService.java b/src/main/java/run/halo/app/service/ThemeSettingService.java index 283aed09f..92066bae1 100644 --- a/src/main/java/run/halo/app/service/ThemeSettingService.java +++ b/src/main/java/run/halo/app/service/ThemeSettingService.java @@ -2,9 +2,11 @@ package run.halo.app.service; import java.util.List; import java.util.Map; +import java.util.Set; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.transaction.annotation.Transactional; +import run.halo.app.handler.theme.config.support.Item; import run.halo.app.model.entity.ThemeSetting; import run.halo.app.service.base.CrudService; @@ -55,6 +57,16 @@ public interface ThemeSettingService extends CrudService @NonNull Map listAsMapBy(@NonNull String themeId); + /** + * Lists theme settings as map by themeId and group name. + * + * @param themeId theme id must not be blank. + * @param group theme group name must not be blank. + * @return theme setting map(key: item name, value: item) + */ + @NonNull + Map listAsMapBy(String themeId, String group); + /** * Delete unused theme setting. */ 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 4ce8ebf91..4e71b35ec 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeServiceImpl.java @@ -6,8 +6,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import lombok.extern.slf4j.Slf4j; @@ -33,7 +35,7 @@ import run.halo.app.exception.ThemePropertyMissingException; import run.halo.app.exception.ThemeUpdateException; import run.halo.app.handler.theme.config.ThemeConfigResolver; import run.halo.app.handler.theme.config.support.Group; -import run.halo.app.handler.theme.config.support.Option; +import run.halo.app.handler.theme.config.support.Item; import run.halo.app.handler.theme.config.support.ThemeProperty; import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.ThemeFile; @@ -54,6 +56,7 @@ import run.halo.app.utils.FileUtils; * Theme service implementation. * * @author ryanwang + * @author guqing * @date 2019-03-26 */ @Slf4j @@ -296,6 +299,16 @@ public class ThemeServiceImpl implements ThemeService { } } + @Override + public Set fetchConfigItemsBy(@NonNull String themeId, @NonNull String group) { + return fetchConfig(themeId).stream() + .filter(g -> StringUtils.equals(g.getName(), group)) + .findFirst() + .map(Group::getItems) + .map(items -> (Set) new LinkedHashSet<>(items)) + .orElseGet(Collections::emptySet); + } + @Override public String render(String pageName) { var folderName = getActivatedTheme().getFolderName(); diff --git a/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java b/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java index 0a22ab63f..f055bb0fb 100644 --- a/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/ThemeSettingServiceImpl.java @@ -10,6 +10,7 @@ import java.util.Optional; import java.util.Set; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; import org.springframework.data.domain.Example; import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; @@ -121,6 +122,7 @@ public class ThemeSettingServiceImpl extends AbstractCrudService listBy(String themeId) { assertThemeIdHasText(themeId); @@ -128,11 +130,30 @@ public class ThemeSettingServiceImpl extends AbstractCrudService listAsMapBy(String themeId) { + public Map listAsMapBy(@NonNull String themeId) { // Convert to item map(key: item name, value: item) Map itemMap = getConfigItemMap(themeId); + return listAsMapBy(themeId, itemMap); + } + + @NonNull + @Override + public Map listAsMapBy(String themeId, String group) { + // Convert to item map(key: item name, value: item) + Set items = themeService.fetchConfigItemsBy(themeId, group); + Map itemMap = ServiceUtils.convertToMap(items, Item::getName); + + return listAsMapBy(themeId, itemMap); + } + + @NotNull + private Map listAsMapBy(String themeId, Map itemMap) { + Assert.notNull(themeId, "The themeId must not be null."); + Assert.notNull(itemMap, "The itemMap must not be null."); + // Get theme setting List themeSettings = listBy(themeId); @@ -170,7 +191,6 @@ public class ThemeSettingServiceImpl extends AbstractCrudService