mirror of https://github.com/halo-dev/halo
feat: create list_view for option api.
parent
316e618dee
commit
3676a77be2
|
@ -1,21 +1,29 @@
|
|||
package run.halo.app.controller.admin.api;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import run.halo.app.model.dto.OptionDTO;
|
||||
import run.halo.app.model.dto.OptionListDTO;
|
||||
import run.halo.app.model.params.OptionParam;
|
||||
import run.halo.app.model.params.OptionQuery;
|
||||
import run.halo.app.service.OptionService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.data.domain.Sort.Direction.DESC;
|
||||
|
||||
/**
|
||||
* Option Controller.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 3/20/19
|
||||
* @author ryanwang
|
||||
* @date 2019-03-20
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/options")
|
||||
|
@ -47,6 +55,13 @@ public class OptionController {
|
|||
return optionService.listOptions(keys);
|
||||
}
|
||||
|
||||
@GetMapping("list_view")
|
||||
@ApiOperation("Lists all options with list view")
|
||||
public Page<OptionListDTO> listAllWithListView(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
|
||||
OptionQuery optionQuery) {
|
||||
return optionService.pageDtosBy(pageable, optionQuery);
|
||||
}
|
||||
|
||||
@PostMapping("map_view/saving")
|
||||
@ApiOperation("Saves options by option map")
|
||||
public void saveOptionsWithMapView(@RequestBody Map<String, Object> optionMap) {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package run.halo.app.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import run.halo.app.model.enums.OptionType;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Option list output dto.
|
||||
*
|
||||
* @author ryanwang
|
||||
* @date 2019-12-02
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class OptionListDTO extends OptionDTO {
|
||||
|
||||
private OptionType type;
|
||||
|
||||
private Date createTime;
|
||||
|
||||
private Date updateTime;
|
||||
}
|
|
@ -4,6 +4,7 @@ import lombok.Data;
|
|||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
import run.halo.app.model.enums.OptionType;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
|
@ -11,6 +12,8 @@ import javax.persistence.*;
|
|||
* Setting entity.
|
||||
*
|
||||
* @author johnniang
|
||||
* @author ryanwang
|
||||
* @date 2019-03-20
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
|
@ -24,6 +27,12 @@ public class Option extends BaseEntity {
|
|||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* option type
|
||||
*/
|
||||
@Column(name = "type", columnDefinition = "int default 0")
|
||||
private OptionType type;
|
||||
|
||||
/**
|
||||
* option key
|
||||
*/
|
||||
|
@ -36,7 +45,8 @@ public class Option extends BaseEntity {
|
|||
@Column(name = "option_value", columnDefinition = "varchar(1023) not null")
|
||||
private String value;
|
||||
|
||||
public Option(String key, String value) {
|
||||
public Option(OptionType type, String key, String value) {
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
@ -45,5 +55,9 @@ public class Option extends BaseEntity {
|
|||
public void prePersist() {
|
||||
super.prePersist();
|
||||
id = null;
|
||||
|
||||
if (type == null) {
|
||||
type = OptionType.INTERNAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package run.halo.app.model.enums;
|
||||
|
||||
/**
|
||||
* Option Type.
|
||||
*
|
||||
* @author ryanwang
|
||||
* @date : 2019-12-02
|
||||
*/
|
||||
public enum OptionType implements ValueEnum<Integer> {
|
||||
|
||||
/**
|
||||
* internal option
|
||||
*/
|
||||
INTERNAL(0),
|
||||
|
||||
/**
|
||||
* custom option
|
||||
*/
|
||||
CUSTOM(1);
|
||||
|
||||
private Integer value;
|
||||
|
||||
OptionType(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ package run.halo.app.model.params;
|
|||
import lombok.Data;
|
||||
import run.halo.app.model.dto.base.InputConverter;
|
||||
import run.halo.app.model.entity.Option;
|
||||
import run.halo.app.model.enums.OptionType;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.Size;
|
||||
|
@ -11,7 +12,8 @@ import javax.validation.constraints.Size;
|
|||
* Optional param.
|
||||
*
|
||||
* @author johnniang
|
||||
* @date 3/20/19
|
||||
* @author ryanwang
|
||||
* @date 2019-03-20
|
||||
*/
|
||||
@Data
|
||||
public class OptionParam implements InputConverter<Option> {
|
||||
|
@ -23,4 +25,6 @@ public class OptionParam implements InputConverter<Option> {
|
|||
|
||||
@Size(max = 1023, message = "Length of option value must not be more than {max}")
|
||||
private String value;
|
||||
|
||||
private OptionType type;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package run.halo.app.model.params;
|
||||
|
||||
import lombok.Data;
|
||||
import run.halo.app.model.enums.OptionType;
|
||||
|
||||
/**
|
||||
* Option query params.
|
||||
*
|
||||
* @author ryanwang
|
||||
* @date : 2019-12-02
|
||||
*/
|
||||
@Data
|
||||
public class OptionQuery {
|
||||
|
||||
private String keyword;
|
||||
|
||||
private OptionType type;
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package run.halo.app.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import run.halo.app.model.entity.Option;
|
||||
import run.halo.app.repository.base.BaseRepository;
|
||||
|
||||
|
@ -9,8 +10,10 @@ import java.util.Optional;
|
|||
* Option repository.
|
||||
*
|
||||
* @author johnniang
|
||||
* @author ryanwang
|
||||
* @date 2019-03-20
|
||||
*/
|
||||
public interface OptionRepository extends BaseRepository<Option, Integer> {
|
||||
public interface OptionRepository extends BaseRepository<Option, Integer>, JpaSpecificationExecutor<Option> {
|
||||
|
||||
/**
|
||||
* Query option by key
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package run.halo.app.service;
|
||||
|
||||
import com.qiniu.common.Zone;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import run.halo.app.exception.MissingPropertyException;
|
||||
import run.halo.app.model.dto.OptionDTO;
|
||||
import run.halo.app.model.dto.OptionListDTO;
|
||||
import run.halo.app.model.entity.Option;
|
||||
import run.halo.app.model.enums.ValueEnum;
|
||||
import run.halo.app.model.params.OptionParam;
|
||||
import run.halo.app.model.params.OptionQuery;
|
||||
import run.halo.app.model.properties.PropertyEnum;
|
||||
import run.halo.app.service.base.CrudService;
|
||||
|
||||
|
@ -92,6 +96,15 @@ public interface OptionService extends CrudService<Option, Integer> {
|
|||
@NonNull
|
||||
List<OptionDTO> listDtos();
|
||||
|
||||
/**
|
||||
* Pages option output dtos.
|
||||
*
|
||||
* @param pageable page info must not be null
|
||||
* @param optionQuery optionQuery
|
||||
* @return a page of option output dto
|
||||
*/
|
||||
Page<OptionListDTO> pageDtosBy(@NonNull Pageable pageable, OptionQuery optionQuery);
|
||||
|
||||
/**
|
||||
* Get option by key
|
||||
*
|
||||
|
@ -301,4 +314,12 @@ public interface OptionService extends CrudService<Option, Integer> {
|
|||
*/
|
||||
long getBirthday();
|
||||
|
||||
/**
|
||||
* Converts to option output dto.
|
||||
*
|
||||
* @param option option must not be null
|
||||
* @return an option output dto
|
||||
*/
|
||||
@NonNull
|
||||
OptionListDTO convertToDto(@NonNull Option option);
|
||||
}
|
||||
|
|
|
@ -6,6 +6,9 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -17,9 +20,11 @@ import run.halo.app.config.properties.HaloProperties;
|
|||
import run.halo.app.event.options.OptionUpdatedEvent;
|
||||
import run.halo.app.exception.MissingPropertyException;
|
||||
import run.halo.app.model.dto.OptionDTO;
|
||||
import run.halo.app.model.dto.OptionListDTO;
|
||||
import run.halo.app.model.entity.Option;
|
||||
import run.halo.app.model.enums.ValueEnum;
|
||||
import run.halo.app.model.params.OptionParam;
|
||||
import run.halo.app.model.params.OptionQuery;
|
||||
import run.halo.app.model.properties.*;
|
||||
import run.halo.app.repository.OptionRepository;
|
||||
import run.halo.app.service.OptionService;
|
||||
|
@ -29,6 +34,7 @@ import run.halo.app.utils.HaloUtils;
|
|||
import run.halo.app.utils.ServiceUtils;
|
||||
import run.halo.app.utils.ValidationUtils;
|
||||
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
|
@ -214,6 +220,41 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<OptionListDTO> pageDtosBy(Pageable pageable, OptionQuery optionQuery) {
|
||||
Assert.notNull(pageable, "Page info must not be null");
|
||||
|
||||
Page<Option> optionPage = optionRepository.findAll(buildSpecByQuery(optionQuery), pageable);
|
||||
|
||||
return optionPage.map(this::convertToDto);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Specification<Option> buildSpecByQuery(@NonNull OptionQuery optionQuery) {
|
||||
Assert.notNull(optionQuery, "Option query must not be null");
|
||||
|
||||
return (Specification<Option>) (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new LinkedList<>();
|
||||
|
||||
if (optionQuery.getType() != null) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("type"), optionQuery.getType()));
|
||||
}
|
||||
|
||||
if (optionQuery.getKeyword() != null) {
|
||||
|
||||
String likeCondition = String.format("%%%s%%", StringUtils.strip(optionQuery.getKeyword()));
|
||||
|
||||
Predicate keyLike = criteriaBuilder.like(root.get("key"), likeCondition);
|
||||
|
||||
Predicate valueLike = criteriaBuilder.like(root.get("value"), likeCondition);
|
||||
|
||||
predicates.add(criteriaBuilder.or(keyLike, valueLike));
|
||||
}
|
||||
|
||||
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getByKeyOfNullable(String key) {
|
||||
return getByKey(key).orElse(null);
|
||||
|
@ -397,6 +438,13 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
|
|||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public OptionListDTO convertToDto(Option option) {
|
||||
Assert.notNull(option, "Option must not be null");
|
||||
|
||||
return new OptionListDTO().convertFrom(option);
|
||||
}
|
||||
|
||||
private void cleanCache() {
|
||||
cacheStore.delete(OPTIONS_KEY);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue