Change gallery to photo.

pull/146/head
ruibaby 2019-04-25 22:39:50 +08:00
parent da52405843
commit f541d470de
15 changed files with 334 additions and 330 deletions

View File

@ -59,7 +59,7 @@ public class FreeMarkerAutoConfiguration {
private TagTagDirective tagTagDirective; private TagTagDirective tagTagDirective;
@Autowired @Autowired
private GalleryTagDirective galleryTagDirective; private PhotoTagDirective photoTagDirective;
@Autowired @Autowired
private RandomMethod randomMethod; private RandomMethod randomMethod;
@ -83,7 +83,7 @@ public class FreeMarkerAutoConfiguration {
configuration.setSharedVariable("menuTag", menuTagDirective); configuration.setSharedVariable("menuTag", menuTagDirective);
configuration.setSharedVariable("tagTag", tagTagDirective); configuration.setSharedVariable("tagTag", tagTagDirective);
configuration.setSharedVariable("postTag", postTagDirective); configuration.setSharedVariable("postTag", postTagDirective);
configuration.setSharedVariable("galleryTag", galleryTagDirective); configuration.setSharedVariable("photoTag", photoTagDirective);
configuration.setSharedVariable("randomMethod", randomMethod); configuration.setSharedVariable("randomMethod", randomMethod);
configuration.setSharedVariable("recentPostsMethod", recentPostsMethod); configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod); configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);

View File

@ -1,81 +0,0 @@
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.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.entity.Gallery;
import run.halo.app.model.params.GalleryParam;
import run.halo.app.model.params.GalleryQuery;
import run.halo.app.service.GalleryService;
import javax.validation.Valid;
import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Gallery controller
*
* @author : RYAN0UP
* @date : 2019/3/21
*/
@RestController
@RequestMapping("/api/admin/galleries")
public class GalleryController {
private final GalleryService galleryService;
public GalleryController(GalleryService galleryService) {
this.galleryService = galleryService;
}
/**
* List all galleries
*
* @param sort sort
* @return all of galleries
*/
@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.
*
* @param galleryId gallery id
* @return GalleryDTO
*/
@GetMapping("{galleryId:\\d+}")
@ApiOperation("Get gallery detail by id")
public GalleryDTO getBy(@PathVariable("galleryId") Integer galleryId) {
return new GalleryDTO().convertFrom(galleryService.getById(galleryId));
}
/**
* Delete gallery by id.
*
* @param galleryId gallery id
*/
@DeleteMapping("{galleryId:\\d+}")
@ApiOperation("Delete gallery by id")
public void deletePermanently(@PathVariable("galleryId") Integer galleryId) {
galleryService.removeById(galleryId);
}
@PostMapping
public Gallery createBy(@Valid @RequestBody GalleryParam galleryParam) {
return galleryService.createBy(galleryParam);
}
}

View File

@ -0,0 +1,81 @@
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.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.PhotoDTO;
import run.halo.app.model.entity.Photo;
import run.halo.app.model.params.PhotoParam;
import run.halo.app.model.params.PhotoQuery;
import run.halo.app.service.PhotoService;
import javax.validation.Valid;
import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC;
/**
* Photo controller
*
* @author : RYAN0UP
* @date : 2019/3/21
*/
@RestController
@RequestMapping("/api/admin/photos")
public class PhotoController {
private final PhotoService photoService;
public PhotoController(PhotoService photoService) {
this.photoService = photoService;
}
/**
* List all photos
*
* @param sort sort
* @return all of photos
*/
@GetMapping(value = "latest")
public List<PhotoDTO> listPhotos(@SortDefault(sort = "updateTime", direction = Sort.Direction.DESC) Sort sort) {
return photoService.listDtos(sort);
}
@GetMapping
public Page<PhotoDTO> pageBy(@PageableDefault(sort = "updateTime", direction = DESC) Pageable pageable,
PhotoQuery photoQuery) {
return photoService.pageDtosBy(pageable, photoQuery);
}
/**
* Get photo by id.
*
* @param photoId photo id
* @return PhotoDTO
*/
@GetMapping("{photoId:\\d+}")
@ApiOperation("Get photo detail by id")
public PhotoDTO getBy(@PathVariable("photoId") Integer photoId) {
return new PhotoDTO().convertFrom(photoService.getById(photoId));
}
/**
* Delete photo by id.
*
* @param photoId photo id
*/
@DeleteMapping("{photoId:\\d+}")
@ApiOperation("Delete photo by id")
public void deletePermanently(@PathVariable("photoId") Integer photoId) {
photoService.removeById(photoId);
}
@PostMapping
public Photo createBy(@Valid @RequestBody PhotoParam photoParam) {
return photoService.createBy(photoParam);
}
}

View File

@ -30,7 +30,7 @@ public class ContentSheetController {
} }
/** /**
* Render gallery page * Render photo page
* *
* @return template path: themes/{theme}/gallery.ftl * @return template path: themes/{theme}/gallery.ftl
*/ */

View File

@ -1,15 +1,17 @@
package run.halo.app.model.dto; package run.halo.app.model.dto;
import run.halo.app.model.dto.base.OutputConverter; import run.halo.app.model.dto.base.OutputConverter;
import run.halo.app.model.entity.Gallery; import run.halo.app.model.entity.Photo;
import lombok.Data; import lombok.Data;
import java.util.Date;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019/3/21 * @date : 2019/3/21
*/ */
@Data @Data
public class GalleryDTO implements OutputConverter<GalleryDTO, Gallery> { public class PhotoDTO implements OutputConverter<PhotoDTO, Photo> {
private Integer id; private Integer id;
@ -17,6 +19,8 @@ public class GalleryDTO implements OutputConverter<GalleryDTO, Gallery> {
private String thumbnail; private String thumbnail;
private Date takeTime;
private String url; private String url;
private String team; private String team;

View File

@ -10,19 +10,19 @@ import javax.persistence.*;
import java.util.Date; import java.util.Date;
/** /**
* Gallery entity * Photo entity
* *
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019-03-12 * @date : 2019-03-12
*/ */
@Data @Data
@Entity @Entity
@Table(name = "galleries") @Table(name = "photos")
@SQLDelete(sql = "update galleries set deleted = true where id = ?") @SQLDelete(sql = "update photos set deleted = true where id = ?")
@Where(clause = "deleted = false") @Where(clause = "deleted = false")
@ToString @ToString
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
public class Gallery extends BaseEntity { public class Photo extends BaseEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
@ -67,7 +67,7 @@ public class Gallery extends BaseEntity {
private String url; private String url;
/** /**
* Gallery team name. * Photo team name.
*/ */
@Column(name = "team", columnDefinition = "varchar(255) default ''") @Column(name = "team", columnDefinition = "varchar(255) default ''")
private String team; private String team;

View File

@ -5,7 +5,7 @@ import freemarker.template.*;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst; import run.halo.app.model.support.HaloConst;
import run.halo.app.service.GalleryService; import run.halo.app.service.PhotoService;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -13,18 +13,18 @@ import java.util.Map;
import static org.springframework.data.domain.Sort.Direction.DESC; import static org.springframework.data.domain.Sort.Direction.DESC;
/** /**
* Freemarker custom tag of gallery. * Freemarker custom tag of photo.
* *
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019/4/21 * @date : 2019/4/21
*/ */
@Component @Component
public class GalleryTagDirective implements TemplateDirectiveModel { public class PhotoTagDirective implements TemplateDirectiveModel {
private final GalleryService galleryService; private final PhotoService photoService;
public GalleryTagDirective(GalleryService galleryService) { public PhotoTagDirective(PhotoService photoService) {
this.galleryService = galleryService; this.photoService = photoService;
} }
@Override @Override
@ -36,16 +36,16 @@ public class GalleryTagDirective implements TemplateDirectiveModel {
String team = params.get("team").toString(); String team = params.get("team").toString();
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("galleries", builder.build().wrap(galleryService.listAll())); env.setVariable("photos", builder.build().wrap(photoService.listAll()));
break; break;
case "listTeams": case "listTeams":
env.setVariable("teams", builder.build().wrap(galleryService.listDtos(Sort.by(DESC, "createTime")))); env.setVariable("teams", builder.build().wrap(photoService.listDtos(Sort.by(DESC, "createTime"))));
break; break;
case "listByTeam": case "listByTeam":
env.setVariable("galleries", builder.build().wrap(galleryService.listByTeam(team, Sort.by(DESC, "createTime")))); env.setVariable("photos", builder.build().wrap(photoService.listByTeam(team, Sort.by(DESC, "createTime"))));
break; break;
case "count": case "count":
env.setVariable("count", builder.build().wrap(galleryService.count())); env.setVariable("count", builder.build().wrap(photoService.count()));
break; break;
default: default:
break; break;

View File

@ -2,7 +2,7 @@ package run.halo.app.model.params;
import lombok.Data; import lombok.Data;
import run.halo.app.model.dto.base.InputConverter; import run.halo.app.model.dto.base.InputConverter;
import run.halo.app.model.entity.Gallery; import run.halo.app.model.entity.Photo;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import java.util.Date; import java.util.Date;
@ -14,9 +14,9 @@ import java.util.Date;
* @date 2019/04/25 * @date 2019/04/25
*/ */
@Data @Data
public class GalleryParam implements InputConverter<Gallery> { public class PhotoParam implements InputConverter<Photo> {
@NotBlank(message = "Gallery name must not be blank") @NotBlank(message = "Photo name must not be blank")
private String name; private String name;
private String description; private String description;
@ -25,10 +25,10 @@ public class GalleryParam implements InputConverter<Gallery> {
private String location; private String location;
@NotBlank(message = "Gallery thumbnail must not be blank") @NotBlank(message = "Photo thumbnail must not be blank")
private String thumbnail; private String thumbnail;
@NotBlank(message = "Gallery url must not be blank") @NotBlank(message = "Photo url must not be blank")
private String url; private String url;
private String team; private String team;

View File

@ -3,13 +3,13 @@ package run.halo.app.model.params;
import lombok.Data; import lombok.Data;
/** /**
* Gallery query params. * Photo query params.
* *
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019/04/25 * @date : 2019/04/25
*/ */
@Data @Data
public class GalleryQuery { public class PhotoQuery {
/** /**
* Keyword. * Keyword.

View File

@ -2,7 +2,7 @@ package run.halo.app.model.vo;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import run.halo.app.model.dto.GalleryDTO; import run.halo.app.model.dto.PhotoDTO;
import java.util.List; import java.util.List;
@ -14,9 +14,9 @@ import java.util.List;
*/ */
@Data @Data
@ToString @ToString
public class GalleryTeamVO { public class PhotoTeamVO {
private String team; private String team;
private List<GalleryDTO> galleries; private List<PhotoDTO> photos;
} }

View File

@ -2,24 +2,24 @@ package run.halo.app.repository;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import run.halo.app.model.entity.Gallery; import run.halo.app.model.entity.Photo;
import run.halo.app.repository.base.BaseRepository; import run.halo.app.repository.base.BaseRepository;
import java.util.List; import java.util.List;
/** /**
* Gallery repository. * Photo repository.
* *
* @author johnniang * @author johnniang
*/ */
public interface GalleryRepository extends BaseRepository<Gallery, Integer>, JpaSpecificationExecutor<Gallery> { public interface PhotoRepository extends BaseRepository<Photo, Integer>, JpaSpecificationExecutor<Photo> {
/** /**
* Query galleries by team * Query photos by team
* *
* @param team team * @param team team
* @param sort sort * @param sort sort
* @return list of gallery * @return list of photo
*/ */
List<Gallery> findByTeam(String team, Sort sort); List<Photo> findByTeam(String team, Sort sort);
} }

View File

@ -1,66 +0,0 @@
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.GalleryParam;
import run.halo.app.model.params.GalleryQuery;
import run.halo.app.model.vo.GalleryTeamVO;
import run.halo.app.service.base.CrudService;
import java.util.List;
/**
* Gallery service.
*
* @author johnniang
*/
public interface GalleryService extends CrudService<Gallery, Integer> {
/**
* List gallery dtos.
*
* @param sort sort
* @return all galleries
*/
List<GalleryDTO> listDtos(@NonNull Sort sort);
/**
* Lists gallery team vos.
*
* @param sort must not be null
* @return a list of gallery team vo
*/
List<GalleryTeamVO> listTeamVos(@NonNull Sort sort);
/**
* List galleries by team.
*
* @param team team
* @param sort sort
* @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);
/**
* Creates gallery by gallery param.
*
* @param galleryParam must not be null
* @return create gallery
*/
@NonNull
Gallery createBy(@NonNull GalleryParam galleryParam);
}

View File

@ -0,0 +1,66 @@
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.PhotoDTO;
import run.halo.app.model.entity.Photo;
import run.halo.app.model.params.PhotoParam;
import run.halo.app.model.params.PhotoQuery;
import run.halo.app.model.vo.PhotoTeamVO;
import run.halo.app.service.base.CrudService;
import java.util.List;
/**
* Photo service.
*
* @author johnniang
*/
public interface PhotoService extends CrudService<Photo, Integer> {
/**
* List photo dtos.
*
* @param sort sort
* @return all photos
*/
List<PhotoDTO> listDtos(@NonNull Sort sort);
/**
* Lists photo team vos.
*
* @param sort must not be null
* @return a list of photo team vo
*/
List<PhotoTeamVO> listTeamVos(@NonNull Sort sort);
/**
* List photos by team.
*
* @param team team
* @param sort sort
* @return list of photos
*/
List<PhotoDTO> listByTeam(@NonNull String team, Sort sort);
/**
* Pages photo output dtos.
*
* @param pageable page info must not be null
* @param photoQuery photoQuery
* @return a page of photo output dto
*/
@NonNull
Page<PhotoDTO> pageDtosBy(@NonNull Pageable pageable, PhotoQuery photoQuery);
/**
* Creates photo by photo param.
*
* @param photoParam must not be null
* @return create photo
*/
@NonNull
Photo createBy(@NonNull PhotoParam photoParam);
}

View File

@ -1,147 +0,0 @@
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.GalleryParam;
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;
import java.util.Set;
import java.util.stream.Collectors;
/**
* GalleryService implementation class
*
* @author : RYAN0UP
* @date : 2019-03-14
*/
@Service
public class GalleryServiceImpl extends AbstractCrudService<Gallery, Integer> implements GalleryService {
private final GalleryRepository galleryRepository;
public GalleryServiceImpl(GalleryRepository galleryRepository) {
super(galleryRepository);
this.galleryRepository = galleryRepository;
}
/**
* List gallery dtos.
*
* @param sort sort
* @return all galleries
*/
@Override
public List<GalleryDTO> listDtos(Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
return listAll(sort).stream().map(gallery -> (GalleryDTO) new GalleryDTO().convertFrom(gallery)).collect(Collectors.toList());
}
/**
* Lists gallery team vos.
*
* @param sort must not be null
* @return a list of gallery team vo
*/
@Override
public List<GalleryTeamVO> listTeamVos(Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
// List all galleries
List<GalleryDTO> galleries = listDtos(sort);
// Get teams
Set<String> teams = ServiceUtils.fetchProperty(galleries, GalleryDTO::getTeam);
Map<String, List<GalleryDTO>> teamGalleryListMap = ServiceUtils.convertToListMap(teams, galleries, GalleryDTO::getTeam);
List<GalleryTeamVO> result = new LinkedList<>();
// Wrap gallery team vo list
teamGalleryListMap.forEach((team, galleryList) -> {
// Build gallery team vo
GalleryTeamVO galleryTeamVO = new GalleryTeamVO();
galleryTeamVO.setTeam(team);
galleryTeamVO.setGalleries(galleryList);
// Add it to result
result.add(galleryTeamVO);
});
return result;
}
/**
* List galleries by team.
*
* @param team team
* @param sort sort
* @return list of galleries
*/
@Override
public List<GalleryDTO> listByTeam(String team, Sort sort) {
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));
}
@Override
public Gallery createBy(GalleryParam galleryParam) {
Assert.notNull(galleryParam, "Gallery param must not be null");
return create(galleryParam.convertTo());
}
@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();
};
}
}

View File

@ -0,0 +1,147 @@
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.PhotoDTO;
import run.halo.app.model.entity.Photo;
import run.halo.app.model.params.PhotoParam;
import run.halo.app.model.params.PhotoQuery;
import run.halo.app.model.vo.PhotoTeamVO;
import run.halo.app.repository.PhotoRepository;
import run.halo.app.service.PhotoService;
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;
import java.util.Set;
import java.util.stream.Collectors;
/**
* PhotoService implementation class
*
* @author : RYAN0UP
* @date : 2019-03-14
*/
@Service
public class PhotoServiceImpl extends AbstractCrudService<Photo, Integer> implements PhotoService {
private final PhotoRepository photoRepository;
public PhotoServiceImpl(PhotoRepository photoRepository) {
super(photoRepository);
this.photoRepository = photoRepository;
}
/**
* List photo dtos.
*
* @param sort sort
* @return all photos
*/
@Override
public List<PhotoDTO> listDtos(Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
return listAll(sort).stream().map(photo -> (PhotoDTO) new PhotoDTO().convertFrom(photo)).collect(Collectors.toList());
}
/**
* Lists photo team vos.
*
* @param sort must not be null
* @return a list of photo team vo
*/
@Override
public List<PhotoTeamVO> listTeamVos(Sort sort) {
Assert.notNull(sort, "Sort info must not be null");
// List all photos
List<PhotoDTO> photos = listDtos(sort);
// Get teams
Set<String> teams = ServiceUtils.fetchProperty(photos, PhotoDTO::getTeam);
Map<String, List<PhotoDTO>> teamPhotoListMap = ServiceUtils.convertToListMap(teams, photos, PhotoDTO::getTeam);
List<PhotoTeamVO> result = new LinkedList<>();
// Wrap photo team vo list
teamPhotoListMap.forEach((team, photoList) -> {
// Build photo team vo
PhotoTeamVO photoTeamVO = new PhotoTeamVO();
photoTeamVO.setTeam(team);
photoTeamVO.setPhotos(photoList);
// Add it to result
result.add(photoTeamVO);
});
return result;
}
/**
* List photos by team.
*
* @param team team
* @param sort sort
* @return list of photos
*/
@Override
public List<PhotoDTO> listByTeam(String team, Sort sort) {
List<Photo> photos = photoRepository.findByTeam(team, sort);
return photos.stream().map(photo -> (PhotoDTO) new PhotoDTO().convertFrom(photo)).collect(Collectors.toList());
}
@Override
public Page<PhotoDTO> pageDtosBy(Pageable pageable, PhotoQuery photoQuery) {
Assert.notNull(pageable, "Page info must not be null");
// List all
Page<Photo> photoPage = photoRepository.findAll(buildSpecByQuery(photoQuery), pageable);
// Convert and return
return photoPage.map(photo -> new PhotoDTO().convertFrom(photo));
}
@Override
public Photo createBy(PhotoParam photoParam) {
Assert.notNull(photoParam, "Photo param must not be null");
return create(photoParam.convertTo());
}
@NonNull
private Specification<Photo> buildSpecByQuery(@NonNull PhotoQuery photoQuery) {
Assert.notNull(photoQuery, "Attachment query must not be null");
return (Specification<Photo>) (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new LinkedList<>();
if (photoQuery.getTeam() != null) {
predicates.add(criteriaBuilder.equal(root.get("team"), photoQuery.getTeam()));
}
if (photoQuery.getKeyword() != null) {
String likeCondition = String.format("%%%s%%", StringUtils.strip(photoQuery.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();
};
}
}