mirror of https://github.com/halo-dev/halo
Add some APIs for group obtaining theme configurations and setting items (#1582)
parent
dbf954e20c
commit
514ccdb2a1
|
@ -3,6 +3,7 @@ package run.halo.app.controller.admin.api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
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.annotation.DisableOnCondition;
|
||||||
import run.halo.app.cache.lock.CacheLock;
|
import run.halo.app.cache.lock.CacheLock;
|
||||||
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.Item;
|
||||||
import run.halo.app.handler.theme.config.support.ThemeProperty;
|
import run.halo.app.handler.theme.config.support.ThemeProperty;
|
||||||
import run.halo.app.model.params.ThemeContentParam;
|
import run.halo.app.model.params.ThemeContentParam;
|
||||||
import run.halo.app.model.support.BaseResponse;
|
import run.halo.app.model.support.BaseResponse;
|
||||||
import run.halo.app.model.support.ThemeFile;
|
import run.halo.app.model.support.ThemeFile;
|
||||||
import run.halo.app.service.ThemeService;
|
import run.halo.app.service.ThemeService;
|
||||||
import run.halo.app.service.ThemeSettingService;
|
import run.halo.app.service.ThemeSettingService;
|
||||||
|
import run.halo.app.utils.ServiceUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Theme controller.
|
* Theme controller.
|
||||||
*
|
*
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
|
* @author guqing
|
||||||
* @date 2019-03-20
|
* @date 2019-03-20
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -137,6 +141,19 @@ public class ThemeController {
|
||||||
return themeService.fetchConfig(themeId);
|
return themeService.fetchConfig(themeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("{themeId:.+}/configurations/groups/{group}")
|
||||||
|
@ApiOperation("Fetches theme configuration by theme id and group name")
|
||||||
|
public Set<Item> 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<String> fetchConfigGroups(@PathVariable("themeId") String themeId) {
|
||||||
|
return ServiceUtils.fetchProperty(themeService.fetchConfig(themeId), Group::getName);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("activation/settings")
|
@GetMapping("activation/settings")
|
||||||
@ApiOperation("Lists activated theme settings")
|
@ApiOperation("Lists activated theme settings")
|
||||||
public Map<String, Object> listSettingsBy() {
|
public Map<String, Object> listSettingsBy() {
|
||||||
|
@ -149,6 +166,13 @@ public class ThemeController {
|
||||||
return themeSettingService.listAsMapBy(themeId);
|
return themeSettingService.listAsMapBy(themeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("{themeId:.+}/groups/{group}/settings")
|
||||||
|
@ApiOperation("Lists theme settings by theme id and group name")
|
||||||
|
public Map<String, Object> listSettingsBy(@PathVariable("themeId") String themeId,
|
||||||
|
@PathVariable String group) {
|
||||||
|
return themeSettingService.listAsMapBy(themeId, group);
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("activation/settings")
|
@PostMapping("activation/settings")
|
||||||
@ApiOperation("Saves theme settings")
|
@ApiOperation("Saves theme settings")
|
||||||
public void saveSettingsBy(@RequestBody Map<String, Object> settings) {
|
public void saveSettingsBy(@RequestBody Map<String, Object> settings) {
|
||||||
|
|
|
@ -3,10 +3,12 @@ package run.halo.app.service;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
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.Item;
|
||||||
import run.halo.app.handler.theme.config.support.ThemeProperty;
|
import run.halo.app.handler.theme.config.support.ThemeProperty;
|
||||||
import run.halo.app.model.support.ThemeFile;
|
import run.halo.app.model.support.ThemeFile;
|
||||||
|
|
||||||
|
@ -14,6 +16,7 @@ import run.halo.app.model.support.ThemeFile;
|
||||||
* Theme service interface.
|
* Theme service interface.
|
||||||
*
|
*
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
|
* @author guqing
|
||||||
* @date 2019-03-26
|
* @date 2019-03-26
|
||||||
*/
|
*/
|
||||||
public interface ThemeService {
|
public interface ThemeService {
|
||||||
|
@ -67,7 +70,7 @@ public interface ThemeService {
|
||||||
Optional<ThemeProperty> fetchThemePropertyBy(@Nullable String themeId);
|
Optional<ThemeProperty> fetchThemePropertyBy(@Nullable String themeId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all themes
|
* Gets all themes.
|
||||||
*
|
*
|
||||||
* @return set of themes
|
* @return set of themes
|
||||||
*/
|
*/
|
||||||
|
@ -95,7 +98,7 @@ public interface ThemeService {
|
||||||
List<String> listCustomTemplates(@NonNull String themeId, @NonNull String prefix);
|
List<String> 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
|
* @param template template must not be blank
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
@ -103,7 +106,7 @@ public interface ThemeService {
|
||||||
boolean templateExists(@Nullable String template);
|
boolean templateExists(@Nullable String template);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether theme exists under template path
|
* Checks whether theme exists under template path.
|
||||||
*
|
*
|
||||||
* @param themeId theme id
|
* @param themeId theme id
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
@ -169,6 +172,15 @@ public interface ThemeService {
|
||||||
@NonNull
|
@NonNull
|
||||||
List<Group> fetchConfig(@NonNull String themeId);
|
List<Group> fetchConfig(@NonNull String themeId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch config items by <code>themeId</code> and <code>group</code>.
|
||||||
|
*
|
||||||
|
* @param themeId theme id must not be blank
|
||||||
|
* @param group group name must not be blank
|
||||||
|
* @return config items
|
||||||
|
*/
|
||||||
|
Set<Item> fetchConfigItemsBy(@NonNull String themeId, String group);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders a theme page.
|
* Renders a theme page.
|
||||||
*
|
*
|
||||||
|
|
|
@ -2,9 +2,11 @@ package run.halo.app.service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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.model.entity.ThemeSetting;
|
||||||
import run.halo.app.service.base.CrudService;
|
import run.halo.app.service.base.CrudService;
|
||||||
|
|
||||||
|
@ -55,6 +57,16 @@ public interface ThemeSettingService extends CrudService<ThemeSetting, Integer>
|
||||||
@NonNull
|
@NonNull
|
||||||
Map<String, Object> listAsMapBy(@NonNull String themeId);
|
Map<String, Object> listAsMapBy(@NonNull String themeId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists theme settings as map by <code>themeId</code> and <code>group</code> 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<String, Object> listAsMapBy(String themeId, String group);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete unused theme setting.
|
* Delete unused theme setting.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -6,8 +6,10 @@ import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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.exception.ThemeUpdateException;
|
||||||
import run.halo.app.handler.theme.config.ThemeConfigResolver;
|
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.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.handler.theme.config.support.ThemeProperty;
|
||||||
import run.halo.app.model.support.HaloConst;
|
import run.halo.app.model.support.HaloConst;
|
||||||
import run.halo.app.model.support.ThemeFile;
|
import run.halo.app.model.support.ThemeFile;
|
||||||
|
@ -54,6 +56,7 @@ import run.halo.app.utils.FileUtils;
|
||||||
* Theme service implementation.
|
* Theme service implementation.
|
||||||
*
|
*
|
||||||
* @author ryanwang
|
* @author ryanwang
|
||||||
|
* @author guqing
|
||||||
* @date 2019-03-26
|
* @date 2019-03-26
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@ -296,6 +299,16 @@ public class ThemeServiceImpl implements ThemeService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Item> 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<Item>) new LinkedHashSet<>(items))
|
||||||
|
.orElseGet(Collections::emptySet);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String render(String pageName) {
|
public String render(String pageName) {
|
||||||
var folderName = getActivatedTheme().getFolderName();
|
var folderName = getActivatedTheme().getFolderName();
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.data.domain.Example;
|
import org.springframework.data.domain.Example;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
@ -121,6 +122,7 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public List<ThemeSetting> listBy(String themeId) {
|
public List<ThemeSetting> listBy(String themeId) {
|
||||||
assertThemeIdHasText(themeId);
|
assertThemeIdHasText(themeId);
|
||||||
|
@ -128,11 +130,30 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
|
||||||
return themeSettingRepository.findAllByThemeId(themeId);
|
return themeSettingRepository.findAllByThemeId(themeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> listAsMapBy(String themeId) {
|
public Map<String, Object> listAsMapBy(@NonNull String themeId) {
|
||||||
// Convert to item map(key: item name, value: item)
|
// Convert to item map(key: item name, value: item)
|
||||||
Map<String, Item> itemMap = getConfigItemMap(themeId);
|
Map<String, Item> itemMap = getConfigItemMap(themeId);
|
||||||
|
|
||||||
|
return listAsMapBy(themeId, itemMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> listAsMapBy(String themeId, String group) {
|
||||||
|
// Convert to item map(key: item name, value: item)
|
||||||
|
Set<Item> items = themeService.fetchConfigItemsBy(themeId, group);
|
||||||
|
Map<String, Item> itemMap = ServiceUtils.convertToMap(items, Item::getName);
|
||||||
|
|
||||||
|
return listAsMapBy(themeId, itemMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Map<String, Object> listAsMapBy(String themeId, Map<String, Item> itemMap) {
|
||||||
|
Assert.notNull(themeId, "The themeId must not be null.");
|
||||||
|
Assert.notNull(itemMap, "The itemMap must not be null.");
|
||||||
|
|
||||||
// Get theme setting
|
// Get theme setting
|
||||||
List<ThemeSetting> themeSettings = listBy(themeId);
|
List<ThemeSetting> themeSettings = listBy(themeId);
|
||||||
|
|
||||||
|
@ -170,7 +191,6 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
|
||||||
|
|
||||||
result.put(name, convertedDefaultValue);
|
result.put(name, convertedDefaultValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue