mirror of https://github.com/halo-dev/halo
Refactor OptionService#save
parent
55ab7815a9
commit
a6c3ed48f0
|
@ -14,7 +14,7 @@ import java.util.Optional;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wechat cache store.
|
* String cache store.
|
||||||
*
|
*
|
||||||
* @author johnniang
|
* @author johnniang
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -2,6 +2,7 @@ package cc.ryanc.halo.service;
|
||||||
|
|
||||||
import cc.ryanc.halo.model.entity.Option;
|
import cc.ryanc.halo.model.entity.Option;
|
||||||
import cc.ryanc.halo.service.base.CrudService;
|
import cc.ryanc.halo.service.base.CrudService;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -15,17 +16,17 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
/**
|
/**
|
||||||
* Save one option
|
* Save one option
|
||||||
*
|
*
|
||||||
* @param key key
|
* @param key key must not be blank
|
||||||
* @param value value
|
* @param value value
|
||||||
*/
|
*/
|
||||||
void save(String key, String value);
|
void save(@NonNull String key, String value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save multiple options
|
* Save multiple options
|
||||||
*
|
*
|
||||||
* @param options options
|
* @param options options
|
||||||
*/
|
*/
|
||||||
void save(Map<String, String> options);
|
void save(@NonNull Map<String, String> options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all options
|
* Get all options
|
||||||
|
|
|
@ -6,7 +6,9 @@ import cc.ryanc.halo.service.OptionService;
|
||||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||||
import cc.ryanc.halo.utils.ServiceUtils;
|
import cc.ryanc.halo.utils.ServiceUtils;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -35,9 +37,14 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void save(String key, String value) {
|
public void save(String key, String value) {
|
||||||
if (StrUtil.equals(value, "")) {
|
Assert.hasText(key, "Option key must not be blank");
|
||||||
|
|
||||||
|
if (StringUtils.isNotBlank(value)) {
|
||||||
|
// If the value is blank, remove the key
|
||||||
optionRepository.removeByOptionKey(key);
|
optionRepository.removeByOptionKey(key);
|
||||||
} else if (StrUtil.isNotEmpty(key)) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Option options = optionRepository.findByOptionKey(key).map(option -> {
|
Option options = optionRepository.findByOptionKey(key).map(option -> {
|
||||||
// Exist
|
// Exist
|
||||||
option.setOptionValue(value);
|
option.setOptionValue(value);
|
||||||
|
@ -53,7 +60,6 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
// Save or update the options
|
// Save or update the options
|
||||||
optionRepository.save(options);
|
optionRepository.save(options);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save multiple options
|
* Save multiple options
|
||||||
|
|
Loading…
Reference in New Issue