mirror of https://github.com/halo-dev/halo
Refactor OptionService
parent
3100c9eb29
commit
1018481bd9
|
@ -41,13 +41,13 @@ public class Option extends BaseEntity {
|
|||
/**
|
||||
* source,default is system
|
||||
*/
|
||||
@Column(name = "source", columnDefinition = "varchar(127) default 'system'")
|
||||
private String source;
|
||||
@Column(name = "option_source", columnDefinition = "varchar(127) default 'system'")
|
||||
private String optionSource;
|
||||
|
||||
@Override
|
||||
public void prePersist() {
|
||||
super.prePersist();
|
||||
id = null;
|
||||
source = "system";
|
||||
optionSource = "system";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,31 +22,35 @@ public interface OptionService extends CrudService<Option, Integer> {
|
|||
/**
|
||||
* Save one option
|
||||
*
|
||||
* @param key key must not be blank
|
||||
* @param value value
|
||||
* @param key key must not be blank
|
||||
* @param value value
|
||||
* @param source source
|
||||
*/
|
||||
void save(@NonNull String key, String value);
|
||||
void save(@NonNull String key, String value, String source);
|
||||
|
||||
/**
|
||||
* Save multiple options
|
||||
*
|
||||
* @param options options
|
||||
* @param source source
|
||||
*/
|
||||
void save(@NonNull Map<String, String> options);
|
||||
void save(@NonNull Map<String, String> options, String source);
|
||||
|
||||
/**
|
||||
* SAve multiple options
|
||||
*
|
||||
* @param optionParams option params
|
||||
* @param source source
|
||||
*/
|
||||
void save(List<OptionParam> optionParams);
|
||||
void save(List<OptionParam> optionParams, String source);
|
||||
|
||||
/**
|
||||
* Saves blog properties.
|
||||
*
|
||||
* @param properties blog properties
|
||||
* @param source source
|
||||
*/
|
||||
void saveProperties(@NonNull Map<BlogProperties, String> properties);
|
||||
void saveProperties(@NonNull Map<BlogProperties, String> properties, String source);
|
||||
|
||||
/**
|
||||
* Get all options
|
||||
|
|
|
@ -37,11 +37,12 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
/**
|
||||
* Saves one option
|
||||
*
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param key key
|
||||
* @param value value
|
||||
* @param source source
|
||||
*/
|
||||
@Override
|
||||
public void save(String key, String value) {
|
||||
public void save(String key, String value, String source) {
|
||||
Assert.hasText(key, "Option key must not be blank");
|
||||
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
|
@ -59,6 +60,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
Option anOption = new Option();
|
||||
anOption.setOptionKey(key);
|
||||
anOption.setOptionValue(value);
|
||||
anOption.setOptionSource(source);
|
||||
return anOption;
|
||||
});
|
||||
|
||||
|
@ -70,35 +72,36 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
* Saves multiple options
|
||||
*
|
||||
* @param options options
|
||||
* @param source source
|
||||
*/
|
||||
@Override
|
||||
public void save(Map<String, String> options) {
|
||||
public void save(Map<String, String> options, String source) {
|
||||
if (CollectionUtils.isEmpty(options)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// (Not recommended) Don't write "this::save" here
|
||||
// Types of key and value are String
|
||||
options.forEach((key, value) -> save(key, value));
|
||||
options.forEach((key, value) -> save(key, value, source));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(List<OptionParam> optionParams) {
|
||||
public void save(List<OptionParam> optionParams, String source) {
|
||||
if (CollectionUtils.isEmpty(optionParams)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO Optimize the query
|
||||
optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue()));
|
||||
optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue(), source));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveProperties(Map<BlogProperties, String> properties) {
|
||||
public void saveProperties(Map<BlogProperties, String> properties, String source) {
|
||||
if (CollectionUtils.isEmpty(properties)) {
|
||||
return;
|
||||
}
|
||||
|
||||
properties.forEach((property, value) -> save(property.getValue(), value));
|
||||
properties.forEach((property, value) -> save(property.getValue(), value, source));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,6 +31,6 @@ public class OptionController {
|
|||
|
||||
@PostMapping("saving")
|
||||
public void saveOptions(@Valid @RequestBody List<OptionParam> optionParams) {
|
||||
optionService.save(optionParams);
|
||||
optionService.save(optionParams,"system");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public class ThemeController {
|
|||
public void active(@RequestParam(name = "themeName", defaultValue = "anatole") String themeName) throws TemplateModelException {
|
||||
Map<BlogProperties, String> properties = new HashMap<>(1);
|
||||
properties.put(BlogProperties.THEME, themeName);
|
||||
optionService.saveProperties(properties);
|
||||
optionService.saveProperties(properties,"system");
|
||||
BaseContentController.THEME = themeName;
|
||||
OPTIONS.clear();
|
||||
OPTIONS.putAll(optionService.listOptions());
|
||||
|
|
|
@ -191,7 +191,7 @@ public class InstallController {
|
|||
properties.put(BlogProperties.ATTACH_LOC, AttachOrigin.SERVER.getValue().toString());
|
||||
|
||||
// Create properties
|
||||
optionService.saveProperties(properties);
|
||||
optionService.saveProperties(properties,"system");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue