diff --git a/src/main/java/run/halo/app/controller/admin/api/GalleryController.java b/src/main/java/run/halo/app/controller/admin/api/GalleryController.java index deab3fdfc..c70dbfacb 100644 --- a/src/main/java/run/halo/app/controller/admin/api/GalleryController.java +++ b/src/main/java/run/halo/app/controller/admin/api/GalleryController.java @@ -1,14 +1,20 @@ package run.halo.app.controller.admin.api; -import run.halo.app.model.dto.GalleryDTO; -import run.halo.app.service.GalleryService; import io.swagger.annotations.ApiOperation; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.data.web.SortDefault; import org.springframework.web.bind.annotation.*; +import run.halo.app.model.dto.GalleryDTO; +import run.halo.app.model.params.GalleryQuery; +import run.halo.app.service.GalleryService; import java.util.List; +import static org.springframework.data.domain.Sort.Direction.DESC; + /** * Gallery controller * @@ -31,11 +37,17 @@ public class GalleryController { * @param sort sort * @return all of galleries */ - @GetMapping + @GetMapping(value = "latest") public List listGalleries(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort) { return galleryService.listDtos(sort); } + @GetMapping + public Page pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable, + GalleryQuery galleryQuery) { + return galleryService.pageDtosBy(pageable, galleryQuery); + } + /** * Get gallery by id. * diff --git a/src/main/java/run/halo/app/model/params/GalleryQuery.java b/src/main/java/run/halo/app/model/params/GalleryQuery.java new file mode 100644 index 000000000..f3013c40a --- /dev/null +++ b/src/main/java/run/halo/app/model/params/GalleryQuery.java @@ -0,0 +1,20 @@ +package run.halo.app.model.params; + +import lombok.Data; + +/** + * Gallery query params. + * + * @author : RYAN0UP + * @date : 2019/04/25 + */ +@Data +public class GalleryQuery { + + /** + * Keyword. + */ + private String keyword; + + private String team; +} diff --git a/src/main/java/run/halo/app/repository/GalleryRepository.java b/src/main/java/run/halo/app/repository/GalleryRepository.java index 7000ba945..2be65f190 100644 --- a/src/main/java/run/halo/app/repository/GalleryRepository.java +++ b/src/main/java/run/halo/app/repository/GalleryRepository.java @@ -1,6 +1,7 @@ package run.halo.app.repository; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import run.halo.app.model.entity.Gallery; import run.halo.app.repository.base.BaseRepository; @@ -11,7 +12,7 @@ import java.util.List; * * @author johnniang */ -public interface GalleryRepository extends BaseRepository { +public interface GalleryRepository extends BaseRepository, JpaSpecificationExecutor { /** * Query galleries by team diff --git a/src/main/java/run/halo/app/service/GalleryService.java b/src/main/java/run/halo/app/service/GalleryService.java index 1d53d0db1..e1b773ca9 100644 --- a/src/main/java/run/halo/app/service/GalleryService.java +++ b/src/main/java/run/halo/app/service/GalleryService.java @@ -1,9 +1,12 @@ package run.halo.app.service; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.lang.NonNull; import run.halo.app.model.dto.GalleryDTO; import run.halo.app.model.entity.Gallery; +import run.halo.app.model.params.GalleryQuery; import run.halo.app.model.vo.GalleryTeamVO; import run.halo.app.service.base.CrudService; @@ -40,4 +43,14 @@ public interface GalleryService extends CrudService { * @return list of galleries */ List listByTeam(@NonNull String team, Sort sort); + + /** + * Pages gallery output dtos. + * + * @param pageable page info must not be null + * @param galleryQuery galleryQuery + * @return a page of gallery output dto + */ + @NonNull + Page pageDtosBy(@NonNull Pageable pageable, GalleryQuery galleryQuery); } diff --git a/src/main/java/run/halo/app/service/impl/GalleryServiceImpl.java b/src/main/java/run/halo/app/service/impl/GalleryServiceImpl.java index 2a2f1f045..1fb14e69b 100644 --- a/src/main/java/run/halo/app/service/impl/GalleryServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/GalleryServiceImpl.java @@ -1,16 +1,23 @@ package run.halo.app.service.impl; +import org.apache.commons.lang3.StringUtils; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; +import org.springframework.lang.NonNull; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import run.halo.app.model.dto.GalleryDTO; import run.halo.app.model.entity.Gallery; +import run.halo.app.model.params.GalleryQuery; import run.halo.app.model.vo.GalleryTeamVO; import run.halo.app.repository.GalleryRepository; import run.halo.app.service.GalleryService; import run.halo.app.service.base.AbstractCrudService; import run.halo.app.utils.ServiceUtils; +import javax.persistence.criteria.Predicate; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -92,4 +99,41 @@ public class GalleryServiceImpl extends AbstractCrudService im List galleries = galleryRepository.findByTeam(team, sort); return galleries.stream().map(gallery -> (GalleryDTO) new GalleryDTO().convertFrom(gallery)).collect(Collectors.toList()); } + + @Override + public Page pageDtosBy(Pageable pageable, GalleryQuery galleryQuery) { + Assert.notNull(pageable, "Page info must not be null"); + + // List all + Page galleryPage = galleryRepository.findAll(buildSpecByQuery(galleryQuery), pageable); + + // Convert and return + return galleryPage.map(gallery -> new GalleryDTO().convertFrom(gallery)); + } + + @NonNull + private Specification buildSpecByQuery(@NonNull GalleryQuery galleryQuery) { + Assert.notNull(galleryQuery, "Attachment query must not be null"); + + return (Specification) (root, query, criteriaBuilder) -> { + List predicates = new LinkedList<>(); + + if (galleryQuery.getTeam() != null) { + predicates.add(criteriaBuilder.equal(root.get("team"), galleryQuery.getTeam())); + } + + if (galleryQuery.getKeyword() != null) { + + String likeCondition = String.format("%%%s%%", StringUtils.strip(galleryQuery.getKeyword())); + + Predicate nameLike = criteriaBuilder.like(root.get("name"), likeCondition); + Predicate descriptionLike = criteriaBuilder.like(root.get("description"), likeCondition); + Predicate locationLike = criteriaBuilder.like(root.get("location"), likeCondition); + + predicates.add(criteriaBuilder.or(nameLike, descriptionLike, locationLike)); + } + + return query.where(predicates.toArray(new Predicate[0])).getRestriction(); + }; + } }