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