Refactor OptionService#save

pull/137/head
johnniang 2019-03-18 22:17:47 +08:00
parent 55ab7815a9
commit a6c3ed48f0
3 changed files with 27 additions and 20 deletions

View File

@ -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
*/ */

View File

@ -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

View File

@ -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,24 +37,28 @@ 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");
optionRepository.removeByOptionKey(key);
} else if (StrUtil.isNotEmpty(key)) {
Option options = optionRepository.findByOptionKey(key).map(option -> {
// Exist
option.setOptionValue(value);
return option;
}).orElseGet(() -> {
// Not exist
Option option = new Option();
option.setOptionKey(key);
option.setOptionValue(value);
return option;
});
// Save or update the options if (StringUtils.isNotBlank(value)) {
optionRepository.save(options); // If the value is blank, remove the key
optionRepository.removeByOptionKey(key);
return;
} }
Option options = optionRepository.findByOptionKey(key).map(option -> {
// Exist
option.setOptionValue(value);
return option;
}).orElseGet(() -> {
// Not exist
Option option = new Option();
option.setOptionKey(key);
option.setOptionValue(value);
return option;
});
// Save or update the options
optionRepository.save(options);
} }
/** /**