mirror of https://github.com/halo-dev/halo
Complete saveOptions api
parent
63f87204bb
commit
603be43a6a
|
@ -0,0 +1,20 @@
|
|||
package cc.ryanc.halo.model.dto;
|
||||
|
||||
import cc.ryanc.halo.model.dto.base.OutputConverter;
|
||||
import cc.ryanc.halo.model.entity.Option;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Option output dto.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 3/20/19
|
||||
*/
|
||||
@Data
|
||||
public class OptionOutputDTO implements OutputConverter<OptionOutputDTO, Option> {
|
||||
|
||||
private String optionKey;
|
||||
|
||||
private String optionValue;
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package cc.ryanc.halo.model.params;
|
||||
|
||||
import cc.ryanc.halo.model.dto.base.InputConverter;
|
||||
import cc.ryanc.halo.model.entity.Option;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
/**
|
||||
* Optiona param.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 3/20/19
|
||||
*/
|
||||
@Data
|
||||
public class OptionParam implements InputConverter<Option> {
|
||||
|
||||
@NotBlank(message = "Option key must not be blank")
|
||||
@Size(max = 100, message = "Length of option key must not be more than {max}")
|
||||
private String optionKey;
|
||||
|
||||
|
||||
@Size(max = 1023, message = "Length of option value must not be more than {max}")
|
||||
private String optionValue;
|
||||
}
|
|
@ -1,11 +1,14 @@
|
|||
package cc.ryanc.halo.service;
|
||||
|
||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Option;
|
||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
||||
import cc.ryanc.halo.model.params.OptionParam;
|
||||
import cc.ryanc.halo.service.base.CrudService;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -31,6 +34,13 @@ public interface OptionService extends CrudService<Option, Integer> {
|
|||
*/
|
||||
void save(@NonNull Map<String, String> options);
|
||||
|
||||
/**
|
||||
* SAve multiple options
|
||||
*
|
||||
* @param optionParams option params
|
||||
*/
|
||||
void save(List<OptionParam> optionParams);
|
||||
|
||||
/**
|
||||
* Saves blog properties.
|
||||
*
|
||||
|
@ -45,6 +55,13 @@ public interface OptionService extends CrudService<Option, Integer> {
|
|||
*/
|
||||
Map<String, String> listOptions();
|
||||
|
||||
/**
|
||||
* Lists all option dtos.
|
||||
*
|
||||
* @return a list of option dto
|
||||
*/
|
||||
List<OptionOutputDTO> listDtos();
|
||||
|
||||
/**
|
||||
* Get option by key
|
||||
*
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package cc.ryanc.halo.service.impl;
|
||||
|
||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||
import cc.ryanc.halo.model.entity.Option;
|
||||
import cc.ryanc.halo.model.enums.BlogProperties;
|
||||
import cc.ryanc.halo.model.params.OptionParam;
|
||||
import cc.ryanc.halo.repository.OptionRepository;
|
||||
import cc.ryanc.halo.service.OptionService;
|
||||
import cc.ryanc.halo.service.base.AbstractCrudService;
|
||||
|
@ -11,8 +13,10 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* OptionService implementation class
|
||||
|
@ -78,6 +82,16 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
options.forEach((key, value) -> save(key, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(List<OptionParam> optionParams) {
|
||||
if (CollectionUtils.isEmpty(optionParams)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO Optimize the query
|
||||
optionParams.forEach(optionParam -> save(optionParam.getOptionKey(), optionParam.getOptionValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveProperties(Map<BlogProperties, String> properties) {
|
||||
if (CollectionUtils.isEmpty(properties)) {
|
||||
|
@ -97,6 +111,11 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
return ServiceUtils.convertToMap(listAll(), Option::getOptionKey, Option::getOptionValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OptionOutputDTO> listDtos() {
|
||||
return listAll().stream().map(option -> (OptionOutputDTO) new OptionOutputDTO().convertFrom(option)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets option by key
|
||||
*
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package cc.ryanc.halo.web.controller.admin.api;
|
||||
|
||||
import cc.ryanc.halo.model.dto.OptionOutputDTO;
|
||||
import cc.ryanc.halo.model.params.OptionParam;
|
||||
import cc.ryanc.halo.service.OptionService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Option Controller.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 3/20/19
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/admin/api/options")
|
||||
public class OptionController {
|
||||
|
||||
private final OptionService optionService;
|
||||
|
||||
public OptionController(OptionService optionService) {
|
||||
this.optionService = optionService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OptionOutputDTO> listAll() {
|
||||
return optionService.listDtos();
|
||||
}
|
||||
|
||||
@PostMapping("saving")
|
||||
public void saveOptions(@Valid @RequestBody List<OptionParam> optionParams) {
|
||||
optionService.save(optionParams);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue