diff --git a/src/main/java/com/umc/naoman/domain/photo/controller/PhotoController.java b/src/main/java/com/umc/naoman/domain/photo/controller/PhotoController.java index 2210e97..28e0945 100644 --- a/src/main/java/com/umc/naoman/domain/photo/controller/PhotoController.java +++ b/src/main/java/com/umc/naoman/domain/photo/controller/PhotoController.java @@ -3,18 +3,26 @@ import com.umc.naoman.domain.photo.converter.PhotoConverter; import com.umc.naoman.domain.photo.dto.PhotoRequest; import com.umc.naoman.domain.photo.dto.PhotoResponse; +import com.umc.naoman.domain.photo.entity.Photo; import com.umc.naoman.domain.photo.service.PhotoService; import com.umc.naoman.global.result.ResultResponse; -import com.umc.naoman.global.result.code.PhotoResultCode; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +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.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; +import static com.umc.naoman.global.result.code.PhotoResultCode.*; + @RestController @RequiredArgsConstructor @RequestMapping("/photos") @@ -26,12 +34,20 @@ public class PhotoController { @PostMapping("/preSignedUrl") public ResultResponse getPreSignedUrlList(@Valid @RequestBody PhotoRequest.PreSignedUrlRequest request) { List preSignedUrlList = photoService.getPreSignedUrlList(request); - return ResultResponse.of(PhotoResultCode.CREATE_PRESIGNED_URL, photoConverter.toPreSignedUrlListInfo(preSignedUrlList)); + return ResultResponse.of(CREATE_PRESIGNED_URL, photoConverter.toPreSignedUrlListInfo(preSignedUrlList)); } @PostMapping("/upload") - public ResultResponse upload(@Valid @RequestBody PhotoRequest.PhotoUploadRequest request) { + public ResultResponse uploadPhotoList(@Valid @RequestBody PhotoRequest.PhotoUploadRequest request) { PhotoResponse.PhotoUploadInfo photoUploadInfo = photoService.uploadPhotoList(request); - return ResultResponse.of(PhotoResultCode.UPLOAD_PHOTO, photoUploadInfo); + return ResultResponse.of(UPLOAD_PHOTO, photoUploadInfo); + } + + @GetMapping("/all") + public ResultResponse getAllPhotoListByShareGroup(@RequestParam Long shareGroupId, + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) { + Page allPhotoListByShareGroup = photoService.getAllPhotoList(shareGroupId, pageable); + return ResultResponse.of(RETRIEVE_PHOTO, photoConverter.toPhotoListInfo(allPhotoListByShareGroup)); } + } diff --git a/src/main/java/com/umc/naoman/domain/photo/converter/PhotoConverter.java b/src/main/java/com/umc/naoman/domain/photo/converter/PhotoConverter.java index 565989e..3a22467 100644 --- a/src/main/java/com/umc/naoman/domain/photo/converter/PhotoConverter.java +++ b/src/main/java/com/umc/naoman/domain/photo/converter/PhotoConverter.java @@ -3,6 +3,7 @@ import com.umc.naoman.domain.photo.dto.PhotoResponse; import com.umc.naoman.domain.photo.entity.Photo; import com.umc.naoman.domain.shareGroup.entity.ShareGroup; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Component; import java.util.List; @@ -40,4 +41,33 @@ public Photo toEntity(String photoUrl, String photoName, ShareGroup shareGroup) .shareGroup(shareGroup) .build(); } + + public PhotoResponse.PagedPhotoInfo toPhotoListInfo(Page photoList) { + List photoInfoList = photoList.stream() + .map(this::toPhotoInfo) + .collect(Collectors.toList()); + + return PhotoResponse.PagedPhotoInfo.builder() + .isLast(photoList.isLast()) + .isFirst(photoList.isFirst()) + .totalPage(photoList.getTotalPages()) + .totalElements(photoList.getTotalElements()) + .photoInfoList(photoInfoList) + .build(); + } + + public PhotoResponse.PhotoInfo toPhotoInfo(Photo photo) { + return PhotoResponse.PhotoInfo.builder() + .photoUrl(photo.getUrl()) + .photoName(photo.getName()) + .resizedPhotoName(convertExtension(photo.getName())) + .createdAt(photo.getCreatedAt()) + .build(); + } + + // HEIC에서 JPG로 확장자를 변환하는 메서드 + private String convertExtension(String photoUrl) { + return photoUrl.replace(".HEIC", ".jpg"); + } + } diff --git a/src/main/java/com/umc/naoman/domain/photo/dto/PhotoResponse.java b/src/main/java/com/umc/naoman/domain/photo/dto/PhotoResponse.java index 1191c26..9d6de77 100644 --- a/src/main/java/com/umc/naoman/domain/photo/dto/PhotoResponse.java +++ b/src/main/java/com/umc/naoman/domain/photo/dto/PhotoResponse.java @@ -5,6 +5,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.time.LocalDateTime; import java.util.List; public abstract class PhotoResponse { @@ -35,4 +36,27 @@ public static class PhotoUploadInfo { private Long shareGroupId; private int uploadCount; } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class PagedPhotoInfo { + private List photoInfoList; + Integer totalPage; + Long totalElements; + Boolean isFirst; + Boolean isLast; + } + + @Getter + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class PhotoInfo { + private String photoUrl; + private String photoName; + private String resizedPhotoName; + private LocalDateTime createdAt; + } } diff --git a/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java b/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java index 259dd1d..2e5899d 100644 --- a/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java +++ b/src/main/java/com/umc/naoman/domain/photo/repository/PhotoRepository.java @@ -1,9 +1,13 @@ package com.umc.naoman.domain.photo.repository; import com.umc.naoman.domain.photo.entity.Photo; +import com.umc.naoman.domain.shareGroup.entity.ShareGroup; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface PhotoRepository extends JpaRepository { + Page findAllByShareGroupId(Long shareGroupId, Pageable pageable); } diff --git a/src/main/java/com/umc/naoman/domain/photo/service/PhotoService.java b/src/main/java/com/umc/naoman/domain/photo/service/PhotoService.java index 93cf4b1..e6b3f48 100644 --- a/src/main/java/com/umc/naoman/domain/photo/service/PhotoService.java +++ b/src/main/java/com/umc/naoman/domain/photo/service/PhotoService.java @@ -2,6 +2,9 @@ import com.umc.naoman.domain.photo.dto.PhotoRequest; import com.umc.naoman.domain.photo.dto.PhotoResponse; +import com.umc.naoman.domain.photo.entity.Photo; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import java.util.List; @@ -10,4 +13,6 @@ public interface PhotoService { List getPreSignedUrlList(PhotoRequest.PreSignedUrlRequest request); PhotoResponse.PhotoUploadInfo uploadPhotoList(PhotoRequest.PhotoUploadRequest request); + + Page getAllPhotoList(Long shareGroupId, Pageable pageable); } diff --git a/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java b/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java index 0362097..ebdf018 100644 --- a/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java +++ b/src/main/java/com/umc/naoman/domain/photo/service/PhotoServiceImpl.java @@ -17,6 +17,8 @@ import com.umc.naoman.global.error.BusinessException; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -27,7 +29,6 @@ import java.util.stream.Collectors; import static com.umc.naoman.global.error.code.S3ErrorCode.PHOTO_NOT_FOUND_S3; -import static com.umc.naoman.global.error.code.ShareGroupErrorCode.SHARE_GROUP_NOT_FOUND; @Service @RequiredArgsConstructor @@ -58,10 +59,6 @@ public List getPreSignedUrlList(PhotoRequest.Pre @Transactional public PhotoResponse.PhotoUploadInfo uploadPhotoList(PhotoRequest.PhotoUploadRequest request) { ShareGroup shareGroup = shareGroupService.findShareGroup(request.getShareGroupId()); - if (shareGroup == null) { - throw new BusinessException(SHARE_GROUP_NOT_FOUND); - } - int uploadCount = 0; for (String photoUrl : request.getPhotoUrlList()) { @@ -74,6 +71,13 @@ public PhotoResponse.PhotoUploadInfo uploadPhotoList(PhotoRequest.PhotoUploadReq return new PhotoResponse.PhotoUploadInfo(shareGroup.getId(), uploadCount); } + @Override + @Transactional(readOnly = true) + public Page getAllPhotoList(Long shareGroupId, Pageable pageable) { + ShareGroup shareGroup = shareGroupService.findShareGroup(shareGroupId); + return photoRepository.findAllByShareGroupId(shareGroup.getId(), pageable); + } + private PhotoResponse.PreSignedUrlInfo getPreSignedUrl(String originalFilename) { String fileName = createPath(originalFilename); diff --git a/src/main/java/com/umc/naoman/global/result/code/PhotoResultCode.java b/src/main/java/com/umc/naoman/global/result/code/PhotoResultCode.java index f4568c2..44bf274 100644 --- a/src/main/java/com/umc/naoman/global/result/code/PhotoResultCode.java +++ b/src/main/java/com/umc/naoman/global/result/code/PhotoResultCode.java @@ -9,7 +9,8 @@ @RequiredArgsConstructor public enum PhotoResultCode implements ResultCode { CREATE_PRESIGNED_URL(200, "SP000", "성공적으로 Presigned URL을 요청하였습니다."), - UPLOAD_PHOTO(200, "SP000", "성공적으로 이미지를 업로드하였습니다.") + UPLOAD_PHOTO(200, "SP000", "성공적으로 이미지를 업로드하였습니다."), + RETRIEVE_PHOTO(200, "SP000", "성공적으로 이미지를 조회하였습니다.") ; private final int status;