mirror of https://github.com/halo-dev/halo
Complete theme setting api
parent
cd82468496
commit
a68431b09f
|
@ -18,9 +18,9 @@ public class OptionParam implements InputConverter<Option> {
|
|||
|
||||
@NotBlank(message = "Option key must not be blank")
|
||||
@Size(max = 100, message = "Length of option key must not be more than {max}")
|
||||
private String optionKey;
|
||||
private String key;
|
||||
|
||||
|
||||
@Size(max = 1023, message = "Length of option value must not be more than {max}")
|
||||
private String optionValue;
|
||||
private String value;
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package run.halo.app.repository;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import run.halo.app.model.entity.ThemeSetting;
|
||||
import run.halo.app.repository.base.BaseRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Theme setting repository interface.
|
||||
*
|
||||
|
@ -11,4 +15,31 @@ import run.halo.app.repository.base.BaseRepository;
|
|||
*/
|
||||
public interface ThemeSettingRepository extends BaseRepository<ThemeSetting, Integer> {
|
||||
|
||||
/**
|
||||
* Finds all theme settings by theme id.
|
||||
*
|
||||
* @param themeId theme id must not be blank
|
||||
* @return a list of theme setting
|
||||
*/
|
||||
@NonNull
|
||||
List<ThemeSetting> findAllByThemeId(@NonNull String themeId);
|
||||
|
||||
/**
|
||||
* Deletes theme setting by theme id and setting key.
|
||||
*
|
||||
* @param themeId theme id must not be blank
|
||||
* @param key setting key must not be blank
|
||||
* @return affected row(s)
|
||||
*/
|
||||
long deleteByThemeIdAndKey(@NonNull String themeId, @NonNull String key);
|
||||
|
||||
/**
|
||||
* Finds theme settings by theme id and setting key.
|
||||
*
|
||||
* @param themeId theme id must not be blank
|
||||
* @param key setting key must not be blank
|
||||
* @return an optional theme setting
|
||||
*/
|
||||
@NonNull
|
||||
Optional<ThemeSetting> findByThemeIdAndKey(@NonNull String themeId, @NonNull String key);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
package run.halo.app.service;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import run.halo.app.model.entity.ThemeSetting;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Theme setting service interface.
|
||||
*
|
||||
|
@ -8,4 +16,43 @@ package run.halo.app.service;
|
|||
*/
|
||||
public interface ThemeSettingService {
|
||||
|
||||
|
||||
/**
|
||||
* Saves theme setting.
|
||||
*
|
||||
* @param key setting key must not be blank
|
||||
* @param value setting value
|
||||
* @param themeId theme id must not be blank
|
||||
* @return theme setting or null if the key does not exist
|
||||
*/
|
||||
@Nullable
|
||||
@Transactional
|
||||
ThemeSetting save(@NonNull String key, @Nullable String value, @NonNull String themeId);
|
||||
|
||||
/**
|
||||
* Saves theme settings.
|
||||
*
|
||||
* @param settings theme setting map
|
||||
* @param themeId theme id must not be blank
|
||||
*/
|
||||
@Transactional
|
||||
void save(@Nullable Map<String, String> settings, @NonNull String themeId);
|
||||
|
||||
/**
|
||||
* Lists theme settings by theme id.
|
||||
*
|
||||
* @param themeId theme id must not be blank
|
||||
* @return a list of theme setting
|
||||
*/
|
||||
@NonNull
|
||||
List<ThemeSetting> listBy(String themeId);
|
||||
|
||||
/**
|
||||
* Lists theme settings as map.
|
||||
*
|
||||
* @param themeId theme id must not be blank
|
||||
* @return theme setting map
|
||||
*/
|
||||
@NonNull
|
||||
Map<String, String> listAsMapBy(@NonNull String themeId);
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
}
|
||||
|
||||
// TODO Optimize the query
|
||||
optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue()));
|
||||
optionParams.forEach(optionParam -> save(optionParam.getKey(), optionParam.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -108,11 +108,6 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
properties.forEach((property, value) -> save(property.getValue(), value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all options
|
||||
*
|
||||
* @return Map
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> listOptions() {
|
||||
return ServiceUtils.convertToMap(listAll(), Option::getKey, Option::getValue);
|
||||
|
@ -123,12 +118,6 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
return listAll().stream().map(option -> new OptionOutputDTO().<OptionOutputDTO>convertFrom(option)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets option by key
|
||||
*
|
||||
* @param key key
|
||||
* @return String
|
||||
*/
|
||||
@Override
|
||||
public String getByKeyOfNullable(String key) {
|
||||
return getByKey(key).orElse(null);
|
||||
|
|
|
@ -1,10 +1,18 @@
|
|||
package run.halo.app.service.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.model.entity.ThemeSetting;
|
||||
import run.halo.app.repository.ThemeSettingRepository;
|
||||
import run.halo.app.service.ThemeSettingService;
|
||||
import run.halo.app.service.base.AbstractCrudService;
|
||||
import run.halo.app.utils.ServiceUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Theme setting service implementation.
|
||||
|
@ -12,6 +20,7 @@ import run.halo.app.service.base.AbstractCrudService;
|
|||
* @author johnniang
|
||||
* @date 4/8/19
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, Integer> implements ThemeSettingService {
|
||||
|
||||
|
@ -22,4 +31,66 @@ public class ThemeSettingServiceImpl extends AbstractCrudService<ThemeSetting, I
|
|||
this.themeSettingRepository = themeSettingRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ThemeSetting save(String key, String value, String themeId) {
|
||||
Assert.notNull(key, "Setting key must not be null");
|
||||
assertThemeIdHasText(themeId);
|
||||
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return themeSettingRepository.findByThemeIdAndKey(themeId, key)
|
||||
.map(setting -> {
|
||||
themeSettingRepository.delete(setting);
|
||||
log.debug("Removed theme setting: [{}]", setting);
|
||||
return setting;
|
||||
}).orElse(null);
|
||||
}
|
||||
|
||||
ThemeSetting themeSetting = themeSettingRepository.findByThemeIdAndKey(themeId, key)
|
||||
.map(setting -> {
|
||||
setting.setValue(value);
|
||||
return setting;
|
||||
}).orElseGet(() -> {
|
||||
ThemeSetting setting = new ThemeSetting();
|
||||
setting.setKey(key);
|
||||
setting.setValue(value);
|
||||
setting.setThemeId(themeId);
|
||||
return setting;
|
||||
});
|
||||
|
||||
// Save the theme setting
|
||||
return themeSettingRepository.save(themeSetting);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Map<String, String> settings, String themeId) {
|
||||
assertThemeIdHasText(themeId);
|
||||
|
||||
if (CollectionUtils.isEmpty(settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Save the settings
|
||||
settings.forEach((key, value) -> save(key, value, themeId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ThemeSetting> listBy(String themeId) {
|
||||
assertThemeIdHasText(themeId);
|
||||
|
||||
return themeSettingRepository.findAllByThemeId(themeId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> listAsMapBy(String themeId) {
|
||||
return ServiceUtils.convertToMap(listBy(themeId), ThemeSetting::getKey, ThemeSetting::getValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts theme id has text.
|
||||
*
|
||||
* @param themeId theme id to be checked
|
||||
*/
|
||||
private void assertThemeIdHasText(String themeId) {
|
||||
Assert.hasText(themeId, "Theme id must not be null");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,10 @@ import run.halo.app.model.support.BaseResponse;
|
|||
import run.halo.app.model.support.ThemeFile;
|
||||
import run.halo.app.model.support.ThemeProperty;
|
||||
import run.halo.app.service.ThemeService;
|
||||
import run.halo.app.service.ThemeSettingService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Theme controller.
|
||||
|
@ -21,8 +23,12 @@ public class ThemeController {
|
|||
|
||||
private final ThemeService themeService;
|
||||
|
||||
public ThemeController(ThemeService themeService) {
|
||||
private final ThemeSettingService themeSettingService;
|
||||
|
||||
public ThemeController(ThemeService themeService,
|
||||
ThemeSettingService themeSettingService) {
|
||||
this.themeService = themeService;
|
||||
this.themeSettingService = themeSettingService;
|
||||
}
|
||||
|
||||
@GetMapping("{themeId}")
|
||||
|
@ -31,22 +37,12 @@ public class ThemeController {
|
|||
return themeService.getThemeOfNonNullBy(themeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all themes
|
||||
*
|
||||
* @return themes
|
||||
*/
|
||||
@GetMapping
|
||||
@ApiOperation("List all themes")
|
||||
public List<ThemeProperty> listAll() {
|
||||
return themeService.getThemes();
|
||||
}
|
||||
|
||||
/**
|
||||
* List all of theme files.
|
||||
*
|
||||
* @return List<ThemeFile>
|
||||
*/
|
||||
@GetMapping("files")
|
||||
public List<ThemeFile> listFiles() {
|
||||
return themeService.listThemeFolderBy(themeService.getActivatedThemeId());
|
||||
|
@ -69,30 +65,49 @@ public class ThemeController {
|
|||
return themeService.getCustomTpl(themeService.getActivatedThemeId());
|
||||
}
|
||||
|
||||
@PostMapping("{themeId}/activate")
|
||||
@PostMapping("{themeId}/activation")
|
||||
@ApiOperation("Activates a theme")
|
||||
public ThemeProperty active(@RequestParam("themeId") String themeId) {
|
||||
return themeService.activeTheme(themeId);
|
||||
}
|
||||
|
||||
@GetMapping("activate")
|
||||
@GetMapping("activation")
|
||||
@ApiOperation("Gets activate theme")
|
||||
public ThemeProperty getActivateTheme() {
|
||||
return themeService.getThemeOfNonNullBy(themeService.getActivatedThemeId());
|
||||
}
|
||||
|
||||
@GetMapping("activate/configurations")
|
||||
@ApiOperation("Fetches theme configuration")
|
||||
@GetMapping("activation/configurations")
|
||||
@ApiOperation("Fetches activated theme configuration")
|
||||
public BaseResponse<Object> fetchConfig() {
|
||||
return BaseResponse.ok(themeService.fetchConfig(themeService.getActivatedThemeId()));
|
||||
}
|
||||
|
||||
@GetMapping("activation/settings")
|
||||
@ApiOperation("Lists activated theme settings")
|
||||
public Map<String, String> listSettingsBy() {
|
||||
return themeSettingService.listAsMapBy(themeService.getActivatedThemeId());
|
||||
}
|
||||
|
||||
@PostMapping("activation/settings")
|
||||
@ApiOperation("Saves theme settings")
|
||||
public void saveSettingsBy(@RequestBody Map<String, String> settings) {
|
||||
themeSettingService.save(settings, themeService.getActivatedThemeId());
|
||||
}
|
||||
|
||||
@GetMapping("{themeId}/configurations")
|
||||
@ApiOperation("Fetches theme configuration by theme id")
|
||||
public BaseResponse<Object> fetchConfig(@PathVariable("themeId") String themeId) {
|
||||
return BaseResponse.ok(themeService.fetchConfig(themeId));
|
||||
}
|
||||
|
||||
@PostMapping("{themeId}/settings")
|
||||
@ApiOperation("Saves theme settings")
|
||||
public void saveSettingsBy(@PathVariable("themeId") String themeId,
|
||||
@RequestBody Map<String, String> settings) {
|
||||
themeSettingService.save(settings, themeId);
|
||||
}
|
||||
|
||||
@DeleteMapping("{themeId}")
|
||||
@ApiOperation("Deletes a theme")
|
||||
public void deleteBy(@PathVariable("themeId") String themeId) {
|
||||
|
|
Loading…
Reference in New Issue