Support attachment search.

pull/146/head
ruibaby 2019-04-19 00:47:58 +08:00
parent 9e9bd1a377
commit 4a2ac0bf29
5 changed files with 53 additions and 7 deletions

View File

@ -0,0 +1,18 @@
package run.halo.app.model.params;
import lombok.Data;
/**
* Attachment query params.
*
* @author : RYAN0UP
* @date : 2019/04/18
*/
@Data
public class AttachmentQuery {
/**
* Keyword.
*/
private String keyword;
}

View File

@ -1,13 +1,13 @@
package run.halo.app.repository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import run.halo.app.model.entity.Attachment;
import run.halo.app.repository.base.BaseRepository;
import run.halo.app.repository.base.BaseRepository;
/**
* Attachment repository
*
* @author johnniang
*/
public interface AttachmentRepository extends BaseRepository<Attachment, Integer> {
public interface AttachmentRepository extends BaseRepository<Attachment, Integer>, JpaSpecificationExecutor<Attachment> {
}

View File

@ -7,6 +7,7 @@ import org.springframework.web.multipart.MultipartFile;
import run.halo.app.exception.FileOperationException;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.model.params.AttachmentQuery;
import run.halo.app.service.base.CrudService;
@ -24,7 +25,7 @@ public interface AttachmentService extends CrudService<Attachment, Integer> {
* @return a page of attachment output dto
*/
@NonNull
Page<AttachmentOutputDTO> pageDtosBy(@NonNull Pageable pageable);
Page<AttachmentOutputDTO> pageDtosBy(@NonNull Pageable pageable, AttachmentQuery attachmentQuery);
/**
* Uploads file.

View File

@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.stereotype.Service;
import org.springframework.util.Assert;
@ -12,6 +13,7 @@ import run.halo.app.handler.file.FileHandlers;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.model.enums.AttachmentType;
import run.halo.app.model.params.AttachmentQuery;
import run.halo.app.model.properties.AttachmentProperties;
import run.halo.app.model.support.UploadResult;
import run.halo.app.repository.AttachmentRepository;
@ -19,6 +21,9 @@ import run.halo.app.service.AttachmentService;
import run.halo.app.service.OptionService;
import run.halo.app.service.base.AbstractCrudService;
import javax.persistence.criteria.Predicate;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
/**
@ -47,16 +52,36 @@ public class AttachmentServiceImpl extends AbstractCrudService<Attachment, Integ
}
@Override
public Page<AttachmentOutputDTO> pageDtosBy(Pageable pageable) {
public Page<AttachmentOutputDTO> pageDtosBy(@NonNull Pageable pageable, AttachmentQuery attachmentQuery) {
Assert.notNull(pageable, "Page info must not be null");
// List all
Page<Attachment> attachmentPage = listAll(pageable);
Page<Attachment> attachmentPage = attachmentRepository.findAll(buildSpecByQuery(attachmentQuery), pageable);
// Convert and return
return attachmentPage.map(this::convertToDto);
}
@NonNull
private Specification<Attachment> buildSpecByQuery(@NonNull AttachmentQuery attachmentQuery) {
Assert.notNull(attachmentQuery, "Attachment query must not be null");
return (Specification<Attachment>) (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (attachmentQuery.getKeyword() != null) {
String likeCondition = String.format("%%%s%%", StringUtils.strip(attachmentQuery.getKeyword()));
Predicate nameLike = criteriaBuilder.like(root.get("name"), likeCondition);
predicates.add(criteriaBuilder.or(nameLike));
}
return query.where(predicates.toArray(new Predicate[0])).getRestriction();
};
}
@Override
public Attachment upload(MultipartFile file) {
Assert.notNull(file, "Multipart file must not be null");

View File

@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import run.halo.app.model.dto.AttachmentOutputDTO;
import run.halo.app.model.entity.Attachment;
import run.halo.app.model.params.AttachmentQuery;
import run.halo.app.service.AttachmentService;
import java.util.LinkedList;
@ -39,8 +40,9 @@ public class AttachmentController {
* @return Page<AttachmentOutputDTO>
*/
@GetMapping
public Page<AttachmentOutputDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable) {
return attachmentService.pageDtosBy(pageable);
public Page<AttachmentOutputDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
AttachmentQuery attachmentQuery) {
return attachmentService.pageDtosBy(pageable, attachmentQuery);
}
/**