mirror of https://github.com/halo-dev/halo
Create page api for gallery.
parent
8b80ed69b1
commit
cf3301d15a
|
@ -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<GalleryDTO> listGalleries(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort) {
|
||||
return galleryService.listDtos(sort);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public Page<GalleryDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
|
||||
GalleryQuery galleryQuery) {
|
||||
return galleryService.pageDtosBy(pageable, galleryQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get gallery by id.
|
||||
*
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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<Gallery, Integer> {
|
||||
public interface GalleryRepository extends BaseRepository<Gallery, Integer>, JpaSpecificationExecutor<Gallery> {
|
||||
|
||||
/**
|
||||
* Query galleries by team
|
||||
|
|
|
@ -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<Gallery, Integer> {
|
|||
* @return list of galleries
|
||||
*/
|
||||
List<GalleryDTO> 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<GalleryDTO> pageDtosBy(@NonNull Pageable pageable, GalleryQuery galleryQuery);
|
||||
}
|
||||
|
|
|
@ -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<Gallery, Integer> im
|
|||
List<Gallery> galleries = galleryRepository.findByTeam(team, sort);
|
||||
return galleries.stream().map(gallery -> (GalleryDTO) new GalleryDTO().convertFrom(gallery)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<GalleryDTO> pageDtosBy(Pageable pageable, GalleryQuery galleryQuery) {
|
||||
Assert.notNull(pageable, "Page info must not be null");
|
||||
|
||||
// List all
|
||||
Page<Gallery> galleryPage = galleryRepository.findAll(buildSpecByQuery(galleryQuery), pageable);
|
||||
|
||||
// Convert and return
|
||||
return galleryPage.map(gallery -> new GalleryDTO().convertFrom(gallery));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private Specification<Gallery> buildSpecByQuery(@NonNull GalleryQuery galleryQuery) {
|
||||
Assert.notNull(galleryQuery, "Attachment query must not be null");
|
||||
|
||||
return (Specification<Gallery>) (root, query, criteriaBuilder) -> {
|
||||
List<Predicate> 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();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue