mirror of https://github.com/halo-dev/halo
Support querying multi post statuses (#1527)
* feat: post page list supports multiple status queriespull/1534/head
parent
62c84e9215
commit
641e03e401
|
@ -5,6 +5,7 @@ import static org.springframework.data.domain.Sort.Direction.DESC;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
@ -44,6 +45,7 @@ import run.halo.app.service.PostService;
|
|||
*
|
||||
* @author johnniang
|
||||
* @author ryanwang
|
||||
* @author guqing
|
||||
* @date 2019-04-02
|
||||
*/
|
||||
@RestController("ApiContentPostController")
|
||||
|
@ -83,7 +85,7 @@ public class PostController {
|
|||
PostQuery postQuery = new PostQuery();
|
||||
postQuery.setKeyword(keyword);
|
||||
postQuery.setCategoryId(categoryId);
|
||||
postQuery.setStatus(PostStatus.PUBLISHED);
|
||||
postQuery.setStatuses(Set.of(PostStatus.PUBLISHED));
|
||||
Page<Post> postPage = postService.pageBy(postQuery, pageable);
|
||||
return postService.convertToListVo(postPage, true);
|
||||
}
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
package run.halo.app.model.params;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import lombok.Data;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import run.halo.app.model.enums.PostStatus;
|
||||
|
||||
/**
|
||||
* Post query.
|
||||
*
|
||||
* @author johnniang
|
||||
* @author guqing
|
||||
* @date 4/10/19
|
||||
*/
|
||||
@Data
|
||||
|
@ -20,11 +24,46 @@ public class PostQuery {
|
|||
/**
|
||||
* Post status.
|
||||
*/
|
||||
@Deprecated(forRemoval = true, since = "1.5.0")
|
||||
private PostStatus status;
|
||||
|
||||
/**
|
||||
* Post statuses.
|
||||
*/
|
||||
private Set<PostStatus> statuses;
|
||||
|
||||
/**
|
||||
* Category id.
|
||||
*/
|
||||
private Integer categoryId;
|
||||
|
||||
/**
|
||||
* This method is deprecated in version 1.5.0, and it is recommended to use
|
||||
* <code>getStatuses()</code> method.
|
||||
*
|
||||
* @see #getStatuses()
|
||||
* @return post status.
|
||||
*/
|
||||
@Deprecated
|
||||
public PostStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* In order to be compatible with status, this method will return the combined results
|
||||
* of status and statuses before status is removed.
|
||||
*
|
||||
* @return a combined status set of status and statues
|
||||
*/
|
||||
public Set<PostStatus> getStatuses() {
|
||||
Set<PostStatus> statuses = new HashSet<>();
|
||||
// Need to be compatible with status parameter values due to historical reasons.
|
||||
if (this.status != null) {
|
||||
statuses.add(this.status);
|
||||
}
|
||||
if (!CollectionUtils.isEmpty(this.statuses)) {
|
||||
statuses.addAll(this.statuses);
|
||||
}
|
||||
return statuses;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,11 +16,13 @@ import java.util.Objects;
|
|||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.persistence.criteria.CriteriaBuilder.In;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.Subquery;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
@ -149,7 +151,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
|
||||
PostQuery postQuery = new PostQuery();
|
||||
postQuery.setKeyword(keyword);
|
||||
postQuery.setStatus(PostStatus.PUBLISHED);
|
||||
postQuery.setStatuses(Set.of(PostStatus.PUBLISHED));
|
||||
|
||||
// Build specification and find all
|
||||
return postRepository.findAll(buildSpecByQuery(postQuery), pageable);
|
||||
|
@ -808,8 +810,9 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
|
|||
return (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> predicates = new LinkedList<>();
|
||||
|
||||
if (postQuery.getStatus() != null) {
|
||||
predicates.add(criteriaBuilder.equal(root.get("status"), postQuery.getStatus()));
|
||||
Set<PostStatus> statuses = postQuery.getStatuses();
|
||||
if (!CollectionUtils.isEmpty(statuses)) {
|
||||
predicates.add(root.get("status").in(statuses));
|
||||
}
|
||||
|
||||
if (postQuery.getCategoryId() != null) {
|
||||
|
|
Loading…
Reference in New Issue