Skip to content

Commit

Permalink
Merge pull request #30 from Na-o-man/feature/#28/get-photoList-api
Browse files Browse the repository at this point in the history
[FEAT]  특정 공유 그룹의 전체 사진 조회 API 구현
  • Loading branch information
bflykky authored Jul 31, 2024
2 parents ba94630 + 544915b commit 63a47eb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -26,12 +34,20 @@ public class PhotoController {
@PostMapping("/preSignedUrl")
public ResultResponse<PhotoResponse.PreSignedUrlListInfo> getPreSignedUrlList(@Valid @RequestBody PhotoRequest.PreSignedUrlRequest request) {
List<PhotoResponse.PreSignedUrlInfo> 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<PhotoResponse.PhotoUploadInfo> upload(@Valid @RequestBody PhotoRequest.PhotoUploadRequest request) {
public ResultResponse<PhotoResponse.PhotoUploadInfo> 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<PhotoResponse.PagedPhotoInfo> getAllPhotoListByShareGroup(@RequestParam Long shareGroupId,
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
Page<Photo> allPhotoListByShareGroup = photoService.getAllPhotoList(shareGroupId, pageable);
return ResultResponse.of(RETRIEVE_PHOTO, photoConverter.toPhotoListInfo(allPhotoListByShareGroup));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,4 +41,33 @@ public Photo toEntity(String photoUrl, String photoName, ShareGroup shareGroup)
.shareGroup(shareGroup)
.build();
}

public PhotoResponse.PagedPhotoInfo toPhotoListInfo(Page<Photo> photoList) {
List<PhotoResponse.PhotoInfo> 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");
}

}
24 changes: 24 additions & 0 deletions src/main/java/com/umc/naoman/domain/photo/dto/PhotoResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

public abstract class PhotoResponse {
Expand Down Expand Up @@ -35,4 +36,27 @@ public static class PhotoUploadInfo {
private Long shareGroupId;
private int uploadCount;
}

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class PagedPhotoInfo {
private List<PhotoInfo> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Photo, Long> {
Page<Photo> findAllByShareGroupId(Long shareGroupId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -10,4 +13,6 @@ public interface PhotoService {
List<PhotoResponse.PreSignedUrlInfo> getPreSignedUrlList(PhotoRequest.PreSignedUrlRequest request);

PhotoResponse.PhotoUploadInfo uploadPhotoList(PhotoRequest.PhotoUploadRequest request);

Page<Photo> getAllPhotoList(Long shareGroupId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -58,10 +59,6 @@ public List<PhotoResponse.PreSignedUrlInfo> 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()) {
Expand All @@ -74,6 +71,13 @@ public PhotoResponse.PhotoUploadInfo uploadPhotoList(PhotoRequest.PhotoUploadReq
return new PhotoResponse.PhotoUploadInfo(shareGroup.getId(), uploadCount);
}

@Override
@Transactional(readOnly = true)
public Page<Photo> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 63a47eb

Please sign in to comment.