mirror of https://github.com/halo-dev/halo
feat: add some api for option.
parent
3676a77be2
commit
bc06a49651
|
@ -7,7 +7,8 @@ import org.springframework.data.web.PageableDefault;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import run.halo.app.model.dto.OptionDTO;
|
import run.halo.app.model.dto.OptionDTO;
|
||||||
import run.halo.app.model.dto.OptionListDTO;
|
import run.halo.app.model.dto.OptionSimpleDTO;
|
||||||
|
import run.halo.app.model.entity.Option;
|
||||||
import run.halo.app.model.params.OptionParam;
|
import run.halo.app.model.params.OptionParam;
|
||||||
import run.halo.app.model.params.OptionQuery;
|
import run.halo.app.model.params.OptionQuery;
|
||||||
import run.halo.app.service.OptionService;
|
import run.halo.app.service.OptionService;
|
||||||
|
@ -57,11 +58,37 @@ public class OptionController {
|
||||||
|
|
||||||
@GetMapping("list_view")
|
@GetMapping("list_view")
|
||||||
@ApiOperation("Lists all options with list view")
|
@ApiOperation("Lists all options with list view")
|
||||||
public Page<OptionListDTO> listAllWithListView(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
|
public Page<OptionSimpleDTO> listAllWithListView(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
|
||||||
OptionQuery optionQuery) {
|
OptionQuery optionQuery) {
|
||||||
return optionService.pageDtosBy(pageable, optionQuery);
|
return optionService.pageDtosBy(pageable, optionQuery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("{id:\\d+}")
|
||||||
|
@ApiOperation("Get option detail by id")
|
||||||
|
public OptionSimpleDTO getBy(@PathVariable("id") Integer id) {
|
||||||
|
Option option = optionService.getById(id);
|
||||||
|
return optionService.convertToDto(option);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
@ApiOperation("Creates option")
|
||||||
|
public void createBy(@RequestBody @Valid OptionParam optionParam) {
|
||||||
|
optionService.save(optionParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("{optionId:\\d+}")
|
||||||
|
@ApiOperation("Updates option")
|
||||||
|
public void updateBy(@PathVariable("optionId") Integer optionId,
|
||||||
|
@RequestBody @Valid OptionParam optionParam) {
|
||||||
|
optionService.update(optionId, optionParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("{optionId:\\d+}")
|
||||||
|
@ApiOperation("Deletes option")
|
||||||
|
public void deletePermanently(@PathVariable("optionId") Integer optionId) {
|
||||||
|
optionService.removePermanently(optionId);
|
||||||
|
}
|
||||||
|
|
||||||
@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, Object> optionMap) {
|
public void saveOptionsWithMapView(@RequestBody Map<String, Object> optionMap) {
|
||||||
|
|
|
@ -14,7 +14,9 @@ import java.util.Date;
|
||||||
*/
|
*/
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Data
|
@Data
|
||||||
public class OptionListDTO extends OptionDTO {
|
public class OptionSimpleDTO extends OptionDTO {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
private OptionType type;
|
private OptionType type;
|
||||||
|
|
|
@ -8,7 +8,7 @@ import org.springframework.lang.Nullable;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import run.halo.app.exception.MissingPropertyException;
|
import run.halo.app.exception.MissingPropertyException;
|
||||||
import run.halo.app.model.dto.OptionDTO;
|
import run.halo.app.model.dto.OptionDTO;
|
||||||
import run.halo.app.model.dto.OptionListDTO;
|
import run.halo.app.model.dto.OptionSimpleDTO;
|
||||||
import run.halo.app.model.entity.Option;
|
import run.halo.app.model.entity.Option;
|
||||||
import run.halo.app.model.enums.ValueEnum;
|
import run.halo.app.model.enums.ValueEnum;
|
||||||
import run.halo.app.model.params.OptionParam;
|
import run.halo.app.model.params.OptionParam;
|
||||||
|
@ -46,13 +46,28 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
void save(@Nullable Map<String, Object> options);
|
void save(@Nullable Map<String, Object> options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SAve multiple options
|
* Save multiple options
|
||||||
*
|
*
|
||||||
* @param optionParams option params
|
* @param optionParams option params
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
void save(@Nullable List<OptionParam> optionParams);
|
void save(@Nullable List<OptionParam> optionParams);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save single option.
|
||||||
|
*
|
||||||
|
* @param optionParam option param
|
||||||
|
*/
|
||||||
|
void save(@Nullable OptionParam optionParam);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update option by id.
|
||||||
|
*
|
||||||
|
* @param optionId option id must not be null.
|
||||||
|
* @param optionParam option param must not be null.
|
||||||
|
*/
|
||||||
|
void update(@NonNull Integer optionId, @NonNull OptionParam optionParam);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves a property.
|
* Saves a property.
|
||||||
*
|
*
|
||||||
|
@ -103,7 +118,16 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @param optionQuery optionQuery
|
* @param optionQuery optionQuery
|
||||||
* @return a page of option output dto
|
* @return a page of option output dto
|
||||||
*/
|
*/
|
||||||
Page<OptionListDTO> pageDtosBy(@NonNull Pageable pageable, OptionQuery optionQuery);
|
Page<OptionSimpleDTO> pageDtosBy(@NonNull Pageable pageable, OptionQuery optionQuery);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes option permanently.
|
||||||
|
*
|
||||||
|
* @param id option id must not be null
|
||||||
|
* @return option detail deleted
|
||||||
|
*/
|
||||||
|
@NonNull
|
||||||
|
Option removePermanently(@NonNull Integer id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get option by key
|
* Get option by key
|
||||||
|
@ -321,5 +345,5 @@ public interface OptionService extends CrudService<Option, Integer> {
|
||||||
* @return an option output dto
|
* @return an option output dto
|
||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
OptionListDTO convertToDto(@NonNull Option option);
|
OptionSimpleDTO convertToDto(@NonNull Option option);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ import run.halo.app.config.properties.HaloProperties;
|
||||||
import run.halo.app.event.options.OptionUpdatedEvent;
|
import run.halo.app.event.options.OptionUpdatedEvent;
|
||||||
import run.halo.app.exception.MissingPropertyException;
|
import run.halo.app.exception.MissingPropertyException;
|
||||||
import run.halo.app.model.dto.OptionDTO;
|
import run.halo.app.model.dto.OptionDTO;
|
||||||
import run.halo.app.model.dto.OptionListDTO;
|
import run.halo.app.model.dto.OptionSimpleDTO;
|
||||||
import run.halo.app.model.entity.Option;
|
import run.halo.app.model.entity.Option;
|
||||||
import run.halo.app.model.enums.ValueEnum;
|
import run.halo.app.model.enums.ValueEnum;
|
||||||
import run.halo.app.model.params.OptionParam;
|
import run.halo.app.model.params.OptionParam;
|
||||||
|
@ -130,6 +130,21 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
save(optionMap);
|
save(optionMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void save(OptionParam optionParam) {
|
||||||
|
Option option = optionParam.convertTo();
|
||||||
|
create(option);
|
||||||
|
publishOptionUpdatedEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Integer optionId, OptionParam optionParam) {
|
||||||
|
Option optionToUpdate = getById(optionId);
|
||||||
|
optionParam.update(optionToUpdate);
|
||||||
|
update(optionToUpdate);
|
||||||
|
publishOptionUpdatedEvent();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void saveProperty(PropertyEnum property, String value) {
|
public void saveProperty(PropertyEnum property, String value) {
|
||||||
Assert.notNull(property, "Property must not be null");
|
Assert.notNull(property, "Property must not be null");
|
||||||
|
@ -221,7 +236,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<OptionListDTO> pageDtosBy(Pageable pageable, OptionQuery optionQuery) {
|
public Page<OptionSimpleDTO> pageDtosBy(Pageable pageable, OptionQuery optionQuery) {
|
||||||
Assert.notNull(pageable, "Page info must not be null");
|
Assert.notNull(pageable, "Page info must not be null");
|
||||||
|
|
||||||
Page<Option> optionPage = optionRepository.findAll(buildSpecByQuery(optionQuery), pageable);
|
Page<Option> optionPage = optionRepository.findAll(buildSpecByQuery(optionQuery), pageable);
|
||||||
|
@ -229,6 +244,13 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
return optionPage.map(this::convertToDto);
|
return optionPage.map(this::convertToDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Option removePermanently(Integer id) {
|
||||||
|
Option deletedOption = removeById(id);
|
||||||
|
publishOptionUpdatedEvent();
|
||||||
|
return deletedOption;
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Specification<Option> buildSpecByQuery(@NonNull OptionQuery optionQuery) {
|
private Specification<Option> buildSpecByQuery(@NonNull OptionQuery optionQuery) {
|
||||||
Assert.notNull(optionQuery, "Option query must not be null");
|
Assert.notNull(optionQuery, "Option query must not be null");
|
||||||
|
@ -439,10 +461,10 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OptionListDTO convertToDto(Option option) {
|
public OptionSimpleDTO convertToDto(Option option) {
|
||||||
Assert.notNull(option, "Option must not be null");
|
Assert.notNull(option, "Option must not be null");
|
||||||
|
|
||||||
return new OptionListDTO().convertFrom(option);
|
return new OptionSimpleDTO().convertFrom(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cleanCache() {
|
private void cleanCache() {
|
||||||
|
|
Loading…
Reference in New Issue