Refactor option source

pull/137/head
johnniang 2019-04-01 12:02:11 +08:00
parent c3739ff8b8
commit 1a3ea785b8
8 changed files with 68 additions and 21 deletions

View File

@ -1,13 +1,14 @@
package cc.ryanc.halo.model.entity; package cc.ryanc.halo.model.entity;
import cc.ryanc.halo.model.enums.OptionSource;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where; import org.hibernate.annotations.Where;
import javax.persistence.*; import javax.persistence.*;
import lombok.NoArgsConstructor;
/** /**
* Setting entity. * Setting entity.
@ -43,8 +44,8 @@ public class Option extends BaseEntity {
/** /**
* source,default is system * source,default is system
*/ */
@Column(name = "option_source", columnDefinition = "varchar(127) default 'system'") @Column(name = "option_source", columnDefinition = "int default 0")
private String optionSource; private OptionSource source;
public Option(String optionKey, String optionValue) { public Option(String optionKey, String optionValue) {
this.optionKey = optionKey; this.optionKey = optionKey;
@ -55,6 +56,9 @@ public class Option extends BaseEntity {
public void prePersist() { public void prePersist() {
super.prePersist(); super.prePersist();
id = null; id = null;
optionSource = "system";
if (source == null) {
source = OptionSource.SYSTEM;
}
} }
} }

View File

@ -0,0 +1,23 @@
package cc.ryanc.halo.model.enums;
/**
* Option source.
*
* @author johnniang
* @date 4/1/19
*/
public enum OptionSource implements ValueEnum<Integer> {
SYSTEM(0),
THEME(1);
private final int value;
OptionSource(int value) {
this.value = value;
}
@Override
public Integer getValue() {
return null;
}}

View File

@ -0,0 +1,19 @@
package cc.ryanc.halo.model.enums.converter;
import cc.ryanc.halo.model.enums.OptionSource;
import javax.persistence.Converter;
/**
* OptionSource converter.
*
* @author johnniang
* @date 4/1/19
*/
@Converter(autoApply = true)
public class OptionSourceConverter extends AbstractConverter<OptionSource, Integer> {
public OptionSourceConverter() {
super(OptionSource.class);
}
}

View File

@ -3,6 +3,7 @@ package cc.ryanc.halo.service;
import cc.ryanc.halo.exception.MissingPropertyException; import cc.ryanc.halo.exception.MissingPropertyException;
import cc.ryanc.halo.model.dto.OptionOutputDTO; import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.entity.Option; import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.OptionSource;
import cc.ryanc.halo.model.enums.PropertyEnum; import cc.ryanc.halo.model.enums.PropertyEnum;
import cc.ryanc.halo.model.enums.ValueEnum; import cc.ryanc.halo.model.enums.ValueEnum;
import cc.ryanc.halo.model.params.OptionParam; import cc.ryanc.halo.model.params.OptionParam;
@ -36,7 +37,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param value value * @param value value
* @param source source * @param source source
*/ */
void save(@NonNull String key, String value, String source); void save(@NonNull String key, String value, @NonNull OptionSource source);
/** /**
* Save multiple options * Save multiple options
@ -44,7 +45,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param options options * @param options options
* @param source source * @param source source
*/ */
void save(@NonNull Map<String, String> options, String source); void save(@NonNull Map<String, String> options, @NonNull OptionSource source);
/** /**
* SAve multiple options * SAve multiple options
@ -52,7 +53,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param optionParams option params * @param optionParams option params
* @param source source * @param source source
*/ */
void save(List<OptionParam> optionParams, String source); void save(List<OptionParam> optionParams, @NonNull OptionSource source);
/** /**
* Saves blog properties. * Saves blog properties.
@ -60,7 +61,7 @@ public interface OptionService extends CrudService<Option, Integer> {
* @param properties blog properties * @param properties blog properties
* @param source source * @param source source
*/ */
void saveProperties(@NonNull Map<? extends PropertyEnum, String> properties, String source); void saveProperties(@NonNull Map<? extends PropertyEnum, String> properties, @NonNull OptionSource source);
/** /**
* Get all options * Get all options

View File

@ -3,10 +3,7 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.exception.MissingPropertyException; import cc.ryanc.halo.exception.MissingPropertyException;
import cc.ryanc.halo.model.dto.OptionOutputDTO; import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.entity.Option; import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.*;
import cc.ryanc.halo.model.enums.PropertyEnum;
import cc.ryanc.halo.model.enums.QnYunProperties;
import cc.ryanc.halo.model.enums.ValueEnum;
import cc.ryanc.halo.model.params.OptionParam; import cc.ryanc.halo.model.params.OptionParam;
import cc.ryanc.halo.repository.OptionRepository; import cc.ryanc.halo.repository.OptionRepository;
import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.OptionService;
@ -50,7 +47,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
* @param source source * @param source source
*/ */
@Override @Override
public void save(String key, String value, String source) { public void save(String key, String value, OptionSource source) {
Assert.hasText(key, "Option key must not be blank"); Assert.hasText(key, "Option key must not be blank");
if (StringUtils.isBlank(value)) { if (StringUtils.isBlank(value)) {
@ -68,7 +65,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); anOption.setSource(source);
return anOption; return anOption;
}); });
@ -83,7 +80,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
* @param source source * @param source source
*/ */
@Override @Override
public void save(Map<String, String> options, String source) { public void save(Map<String, String> options, OptionSource source) {
if (CollectionUtils.isEmpty(options)) { if (CollectionUtils.isEmpty(options)) {
return; return;
} }
@ -92,7 +89,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
} }
@Override @Override
public void save(List<OptionParam> optionParams, String source) { public void save(List<OptionParam> optionParams, OptionSource source) {
if (CollectionUtils.isEmpty(optionParams)) { if (CollectionUtils.isEmpty(optionParams)) {
return; return;
} }
@ -102,7 +99,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
} }
@Override @Override
public void saveProperties(Map<? extends PropertyEnum, String> properties, String source) { public void saveProperties(Map<? extends PropertyEnum, String> properties, OptionSource source) {
if (CollectionUtils.isEmpty(properties)) { if (CollectionUtils.isEmpty(properties)) {
return; return;
} }

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.web.controller.admin.api; package cc.ryanc.halo.web.controller.admin.api;
import cc.ryanc.halo.model.dto.OptionOutputDTO; import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.enums.OptionSource;
import cc.ryanc.halo.model.params.OptionParam; import cc.ryanc.halo.model.params.OptionParam;
import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.OptionService;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -33,7 +34,7 @@ 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, "system"); optionService.save(optionParams, OptionSource.SYSTEM);
} }
@GetMapping("map_view") @GetMapping("map_view")
@ -45,6 +46,6 @@ public class OptionController {
@PostMapping("map_view/saving") @PostMapping("map_view/saving")
@ApiOperation("Saves options by option map") @ApiOperation("Saves options by option map")
public void saveOptionsWithMapView(@RequestBody Map<String, String> optionMap) { public void saveOptionsWithMapView(@RequestBody Map<String, String> optionMap) {
optionService.save(optionMap, "system"); optionService.save(optionMap, OptionSource.SYSTEM);
} }
} }

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.web.controller.admin.api; package cc.ryanc.halo.web.controller.admin.api;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.OptionSource;
import cc.ryanc.halo.model.support.Theme; import cc.ryanc.halo.model.support.Theme;
import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.service.ThemeService; import cc.ryanc.halo.service.ThemeService;
@ -61,7 +62,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, "system"); optionService.saveProperties(properties, OptionSource.SYSTEM);
BaseContentController.THEME = themeName; BaseContentController.THEME = themeName;
configuration.setSharedVariable("themeName", themeName); configuration.setSharedVariable("themeName", themeName);
configuration.setSharedVariable("options", optionService.listOptions()); configuration.setSharedVariable("options", optionService.listOptions());

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.exception.BadRequestException;
import cc.ryanc.halo.model.entity.*; import cc.ryanc.halo.model.entity.*;
import cc.ryanc.halo.model.enums.AttachmentType; import cc.ryanc.halo.model.enums.AttachmentType;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.OptionSource;
import cc.ryanc.halo.model.params.InstallParam; import cc.ryanc.halo.model.params.InstallParam;
import cc.ryanc.halo.model.support.BaseResponse; import cc.ryanc.halo.model.support.BaseResponse;
import cc.ryanc.halo.service.*; import cc.ryanc.halo.service.*;
@ -175,7 +176,7 @@ public class InstallController {
properties.put(BlogProperties.ATTACHMENT_TYPE, AttachmentType.LOCAL.getValue().toString()); properties.put(BlogProperties.ATTACHMENT_TYPE, AttachmentType.LOCAL.getValue().toString());
// Create properties // Create properties
optionService.saveProperties(properties, "system"); optionService.saveProperties(properties, OptionSource.SYSTEM);
} }
// /** // /**