From 655b9f90a6c988b5df787c3d3a49018e10b24457 Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 00:07:43 +0900 Subject: [PATCH 01/10] feat: add albumId saving & photoCount updating logic for uploadFileUrlPhoto api --- .../photo/controller/PhotoController.java | 2 +- .../request/PhotoFileUrlUploadRequest.java | 7 ++++- .../kr/mafoo/photo/domain/AlbumEntity.java | 8 +++--- .../kr/mafoo/photo/domain/PhotoEntity.java | 3 ++- .../kr/mafoo/photo/service/AlbumService.java | 8 +++--- .../kr/mafoo/photo/service/PhotoService.java | 27 +++++++++++-------- 6 files changed, 33 insertions(+), 22 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java b/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java index dcb7981..fb41db2 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java +++ b/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java @@ -53,7 +53,7 @@ public Flux uploadFileUrlPhoto( PhotoFileUrlUploadRequest request ){ return photoService - .createNewPhotoFileUrl(request.fileUrls(), memberId) + .createNewPhotoFileUrl(request.fileUrls(), request.albumId(), memberId) .map(PhotoResponse::fromEntity); } diff --git a/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/PhotoFileUrlUploadRequest.java b/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/PhotoFileUrlUploadRequest.java index 1787366..442e8b9 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/PhotoFileUrlUploadRequest.java +++ b/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/PhotoFileUrlUploadRequest.java @@ -2,6 +2,7 @@ import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Schema; +import kr.mafoo.photo.annotation.ULID; @Schema(description = "파일(url) 사진 n건 업로드 요청") public record PhotoFileUrlUploadRequest( @@ -9,6 +10,10 @@ public record PhotoFileUrlUploadRequest( schema = @Schema(description = "파일 URL 목록"), arraySchema = @Schema(example = "[\"file_url_1\", \"file_url_2\", \"file_url_3\"]") ) - String[] fileUrls + String[] fileUrls, + + @ULID + @Schema(description = "앨범 ID", example = "test_album_id") + String albumId ) { } diff --git a/photo-service/src/main/java/kr/mafoo/photo/domain/AlbumEntity.java b/photo-service/src/main/java/kr/mafoo/photo/domain/AlbumEntity.java index 467e76e..312addf 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/domain/AlbumEntity.java +++ b/photo-service/src/main/java/kr/mafoo/photo/domain/AlbumEntity.java @@ -77,13 +77,13 @@ public AlbumEntity updateType(AlbumType newType) { return this; } - public AlbumEntity increasePhotoCount() { - this.photoCount += 1; + public AlbumEntity increasePhotoCount(int count) { + this.photoCount += count; return this; } - public AlbumEntity decreasePhotoCount() { - this.photoCount -= 1; + public AlbumEntity decreasePhotoCount(int count) { + this.photoCount -= count; return this; } diff --git a/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java b/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java index 5084cb5..91b79da 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java +++ b/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java @@ -82,12 +82,13 @@ public PhotoEntity updateDisplayIndex(Integer displayIndex) { return this; } - public static PhotoEntity newPhoto(String photoId, String photoUrl, BrandType brandType, String ownerMemberId) { + public static PhotoEntity newPhoto(String photoId, String photoUrl, BrandType brandType, String albumId, String ownerMemberId) { PhotoEntity photo = new PhotoEntity(); photo.photoId = photoId; photo.photoUrl = photoUrl; photo.brand = brandType; photo.ownerMemberId = ownerMemberId; + photo.albumId = albumId; photo.displayIndex = 0; photo.isNew = true; photo.createdAt = LocalDateTime.now(); diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java b/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java index 1b80189..9cefe85 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java @@ -114,7 +114,7 @@ public Mono updateAlbumType(String albumId, AlbumType albumType, St } @Transactional - public Mono increaseAlbumPhotoCount(String albumId, String requestMemberId) { + public Mono increaseAlbumPhotoCount(String albumId, int count, String requestMemberId) { return albumRepository .findById(albumId) .switchIfEmpty(Mono.error(new AlbumNotFoundException())) @@ -123,13 +123,13 @@ public Mono increaseAlbumPhotoCount(String albumId, String requestMemberId // 내 앨범이 아니면 그냥 없는 앨범 처리 return Mono.error(new AlbumNotFoundException()); } else { - return albumRepository.save(albumEntity.increasePhotoCount()).then(); + return albumRepository.save(albumEntity.increasePhotoCount(count)).then(); } }); } @Transactional - public Mono decreaseAlbumPhotoCount(String albumId, String requestMemberId) { + public Mono decreaseAlbumPhotoCount(String albumId, int count, String requestMemberId) { if (albumId == null) { return Mono.empty(); @@ -143,7 +143,7 @@ public Mono decreaseAlbumPhotoCount(String albumId, String requestMemberId // 내 앨범이 아니면 그냥 없는 앨범 처리 return Mono.error(new AlbumNotFoundException()); } else { - return albumRepository.save(albumEntity.decreasePhotoCount()).then(); + return albumRepository.save(albumEntity.decreasePhotoCount(count)).then(); } }); } diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index b2a681f..2a4580a 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -38,20 +38,25 @@ public Mono createNewPhotoByQrUrl(String qrUrl, String requestMembe return qrService .getFileFromQrUrl(qrUrl) .flatMap(fileDto -> objectStorageService.uploadFile(fileDto.fileByte()) - .flatMap(photoUrl -> createNewPhoto(photoUrl, fileDto.type(), requestMemberId)) + .flatMap(photoUrl -> createNewPhoto(photoUrl, fileDto.type(), null, requestMemberId)) ); } @Transactional - public Flux createNewPhotoFileUrl(String[] fileUrls, String requestMemberId) { - return Flux.fromArray(fileUrls) - .flatMap(fileUrl -> objectStorageService.setObjectPublicRead(fileUrl) - .flatMap(fileLink -> createNewPhoto(fileLink, BrandType.EXTERNAL, requestMemberId)) + public Flux createNewPhotoFileUrl(String[] fileUrls, String albumId, String requestMemberId) { + + return albumService.findByAlbumId(albumId, requestMemberId) + .flatMap(albumEntity -> albumService.increaseAlbumPhotoCount(albumId, fileUrls.length, requestMemberId)) + .thenMany( + Flux.fromArray(fileUrls) + .flatMap(fileUrl -> objectStorageService.setObjectPublicRead(fileUrl) + .flatMap(fileLink -> createNewPhoto(fileLink, BrandType.EXTERNAL, albumId, requestMemberId)) + ) ); } - private Mono createNewPhoto(String photoUrl, BrandType type, String requestMemberId) { - PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, type, requestMemberId); + private Mono createNewPhoto(String photoUrl, BrandType type, String albumId, String requestMemberId) { + PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, type, albumId, requestMemberId); return photoRepository.save(photoEntity); } @@ -73,7 +78,7 @@ public Flux uploadPhoto(Flux files, String requestMemberI }) .flatMap(bytes -> objectStorageService.uploadFile(bytes) .flatMap(photoUrl -> { - PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, BrandType.EXTERNAL, requestMemberId); + PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, BrandType.EXTERNAL, null, requestMemberId); return photoRepository.save(photoEntity); })) .subscribeOn(Schedulers.boundedElastic()) @@ -111,7 +116,7 @@ public Mono deletePhotoById(String photoId, String requestMemberId) { // 내 사진이 아니면 그냥 없는 사진 처리 return Mono.error(new PhotoNotFoundException()); } else { - return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), requestMemberId) + return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) .then(photoRepository.popDisplayIndexGreaterThan(photoEntity.getAlbumId(), photoEntity.getDisplayIndex())) .then(photoRepository.deleteById(photoId)); } @@ -149,9 +154,9 @@ public Mono updatePhotoAlbumId(String photoId, String albumId, Stri // 내 앨범이 아니면 그냥 없는 앨범 처리 return Mono.error(new AlbumNotFoundException()); } else { - return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), requestMemberId) + return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) .then(photoRepository.popDisplayIndexGreaterThan(photoEntity.getAlbumId(), photoEntity.getDisplayIndex())) - .then(albumService.increaseAlbumPhotoCount(albumId, requestMemberId)) + .then(albumService.increaseAlbumPhotoCount(albumId, 1, requestMemberId)) .then(photoRepository.save( photoEntity .updateAlbumId(albumId) From 6f1847adc8428b8d5c26ca26e4dafaaaf4d573ad Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 00:56:01 +0900 Subject: [PATCH 02/10] feat: create findByPhotoId method --- .../java/kr/mafoo/photo/service/PhotoService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 2a4580a..9fe2497 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -106,6 +106,20 @@ public Flux findAllByAlbumId(String albumId, String requestMemberId }); } + public Mono findByPhotoId(String photoId, String requestMemberId) { + return photoRepository + .findById(photoId) + .switchIfEmpty(Mono.error(new PhotoNotFoundException())) + .flatMap(photoEntity -> { + if (!photoEntity.getOwnerMemberId().equals(requestMemberId)) { + // 내 사진이 아니면 그냥 없는 사진 처리 + return Mono.error(new PhotoNotFoundException()); + } else { + return Mono.just(photoEntity); + } + }); + } + @Transactional public Mono deletePhotoById(String photoId, String requestMemberId) { return photoRepository From 3c51ab3b2befcb65a49255c9e2849471a6c51f97 Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 01:09:18 +0900 Subject: [PATCH 03/10] refactor: remove album repository from photo service --- .../kr/mafoo/photo/service/PhotoService.java | 52 +++++-------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 9fe2497..1e4e4ad 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -2,11 +2,9 @@ import kr.mafoo.photo.domain.BrandType; import kr.mafoo.photo.domain.PhotoEntity; -import kr.mafoo.photo.exception.AlbumNotFoundException; import kr.mafoo.photo.exception.PhotoDisplayIndexIsSameException; import kr.mafoo.photo.exception.PhotoDisplayIndexNotValidException; import kr.mafoo.photo.exception.PhotoNotFoundException; -import kr.mafoo.photo.repository.AlbumRepository; import kr.mafoo.photo.repository.PhotoRepository; import kr.mafoo.photo.util.IdGenerator; import lombok.RequiredArgsConstructor; @@ -27,7 +25,6 @@ @Service public class PhotoService { private final PhotoRepository photoRepository; - private final AlbumRepository albumRepository; private final AlbumService albumService; private final QrService qrService; @@ -87,23 +84,14 @@ public Flux uploadPhoto(Flux files, String requestMemberI } public Flux findAllByAlbumId(String albumId, String requestMemberId, String sort) { - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMapMany(albumEntity -> { - if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - String sortMethod = (sort == null) ? "CUSTOM" : sort.toUpperCase(); - - return switch (sortMethod) { + return albumService.findByAlbumId(albumId, requestMemberId) + .thenMany( + switch (sort) { case "ASC" -> photoRepository.findAllByAlbumIdOrderByCreatedAtAsc(albumId); case "DESC" -> photoRepository.findAllByAlbumIdOrderByCreatedAtDesc(albumId); default -> photoRepository.findAllByAlbumIdOrderByDisplayIndexDesc(albumId); - }; - } - }); + } + ); } public Mono findByPhotoId(String photoId, String requestMemberId) { @@ -160,24 +148,17 @@ public Mono updatePhotoAlbumId(String photoId, String albumId, Stri // 내 사진이 아니면 그냥 없는 사진 처리 return Mono.error(new PhotoNotFoundException()); } else { - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMap(albumEntity -> { - if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) + return albumService.findByAlbumId(albumId, requestMemberId) + .flatMap(albumEntity -> + albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) .then(photoRepository.popDisplayIndexGreaterThan(photoEntity.getAlbumId(), photoEntity.getDisplayIndex())) .then(albumService.increaseAlbumPhotoCount(albumId, 1, requestMemberId)) .then(photoRepository.save( photoEntity .updateAlbumId(albumId) .updateDisplayIndex(albumEntity.getPhotoCount()) - )); - } - }); + )) + ); } }); } @@ -187,17 +168,10 @@ public Mono updatePhotoDisplayIndex(String photoId, Integer newInde return photoRepository .findById(photoId) .switchIfEmpty(Mono.error(new PhotoNotFoundException())) - .flatMap(photoEntity -> albumRepository - .findById(photoEntity.getAlbumId()) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) + .flatMap(photoEntity -> albumService.findByAlbumId(photoEntity.getAlbumId(), requestMemberId) .flatMap(albumEntity -> { - int targetIndex = albumEntity.getPhotoCount() - newIndex - 1; - if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - return Mono.error(new AlbumNotFoundException()); - } - if (photoEntity.getDisplayIndex().equals(targetIndex)) { return Mono.error(new PhotoDisplayIndexIsSameException()); } @@ -215,8 +189,8 @@ public Mono updatePhotoDisplayIndex(String photoId, Integer newInde .pushDisplayIndexBetween(photoEntity.getAlbumId(), targetIndex, photoEntity.getDisplayIndex() - 1) .then(photoRepository.save(photoEntity.updateDisplayIndex(targetIndex))); } - - })); + }) + ); } } From 45228f4970b52c4fde4f9df789de4f9a4a999ce9 Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 01:23:23 +0900 Subject: [PATCH 04/10] refactor: refactor deletePhotoById to use findByPhotoId instead of photoRepository --- .../kr/mafoo/photo/service/PhotoService.java | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 1e4e4ad..6d57e02 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -110,19 +110,12 @@ public Mono findByPhotoId(String photoId, String requestMemberId) { @Transactional public Mono deletePhotoById(String photoId, String requestMemberId) { - return photoRepository - .findById(photoId) - .switchIfEmpty(Mono.error(new PhotoNotFoundException())) - .flatMap(photoEntity -> { - if (!photoEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 사진이 아니면 그냥 없는 사진 처리 - return Mono.error(new PhotoNotFoundException()); - } else { - return albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) + return findByPhotoId(photoId, requestMemberId) + .flatMap(photoEntity -> + albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) .then(photoRepository.popDisplayIndexGreaterThan(photoEntity.getAlbumId(), photoEntity.getDisplayIndex())) - .then(photoRepository.deleteById(photoId)); - } - }); + .then(photoRepository.deleteById(photoId)) + ); } @Transactional From 2236e5418c7c89af0f5da315e6b7d6df7f2f03cc Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 02:34:40 +0900 Subject: [PATCH 05/10] refactor: refactor updatePhotoDisplayIndex to use findByPhotoId instead of photoRepository --- .../src/main/java/kr/mafoo/photo/service/PhotoService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 6d57e02..586ebbc 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -41,7 +41,6 @@ public Mono createNewPhotoByQrUrl(String qrUrl, String requestMembe @Transactional public Flux createNewPhotoFileUrl(String[] fileUrls, String albumId, String requestMemberId) { - return albumService.findByAlbumId(albumId, requestMemberId) .flatMap(albumEntity -> albumService.increaseAlbumPhotoCount(albumId, fileUrls.length, requestMemberId)) .thenMany( @@ -158,9 +157,7 @@ public Mono updatePhotoAlbumId(String photoId, String albumId, Stri @Transactional public Mono updatePhotoDisplayIndex(String photoId, Integer newIndex, String requestMemberId) { - return photoRepository - .findById(photoId) - .switchIfEmpty(Mono.error(new PhotoNotFoundException())) + return findByPhotoId(photoId, requestMemberId) .flatMap(photoEntity -> albumService.findByAlbumId(photoEntity.getAlbumId(), requestMemberId) .flatMap(albumEntity -> { int targetIndex = albumEntity.getPhotoCount() - newIndex - 1; From 9ca74cc17c168a8083d0aecec04ed5c571d60e2e Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 04:54:36 +0900 Subject: [PATCH 06/10] fix: fix updatePhotoBulkAlbumId photoCount & displayIndex updating error --- .../kr/mafoo/photo/service/PhotoService.java | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 586ebbc..00f1261 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -98,7 +98,10 @@ public Mono findByPhotoId(String photoId, String requestMemberId) { .findById(photoId) .switchIfEmpty(Mono.error(new PhotoNotFoundException())) .flatMap(photoEntity -> { - if (!photoEntity.getOwnerMemberId().equals(requestMemberId)) { + if (!photoEntity.hasOwnerMemberId()) { + return photoRepository.save(photoEntity.updateOwnerMemberId(requestMemberId)); + } + else if (!photoEntity.getOwnerMemberId().equals(requestMemberId)) { // 내 사진이 아니면 그냥 없는 사진 처리 return Mono.error(new PhotoNotFoundException()); } else { @@ -120,27 +123,14 @@ public Mono deletePhotoById(String photoId, String requestMemberId) { @Transactional public Flux updatePhotoBulkAlbumId(String[] photoIds, String albumId, String requestMemberId) { return Flux.fromArray(photoIds) - .flatMap(photoId -> - this.updatePhotoAlbumId(photoId, albumId, requestMemberId) - ); + .concatMap(photoId -> this.updatePhotoAlbumId(photoId, albumId, requestMemberId)); } @Transactional public Mono updatePhotoAlbumId(String photoId, String albumId, String requestMemberId) { - return photoRepository - .findById(photoId) - .switchIfEmpty(Mono.error(new PhotoNotFoundException())) - .flatMap(photoEntity -> { - - if (!photoEntity.hasOwnerMemberId()) { - photoRepository.save(photoEntity.updateOwnerMemberId(requestMemberId)); - } - - if (!photoEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 사진이 아니면 그냥 없는 사진 처리 - return Mono.error(new PhotoNotFoundException()); - } else { - return albumService.findByAlbumId(albumId, requestMemberId) + return findByPhotoId(photoId, requestMemberId) + .flatMap(photoEntity -> + albumService.findByAlbumId(albumId, requestMemberId) .flatMap(albumEntity -> albumService.decreaseAlbumPhotoCount(photoEntity.getAlbumId(), 1, requestMemberId) .then(photoRepository.popDisplayIndexGreaterThan(photoEntity.getAlbumId(), photoEntity.getDisplayIndex())) @@ -150,9 +140,8 @@ public Mono updatePhotoAlbumId(String photoId, String albumId, Stri .updateAlbumId(albumId) .updateDisplayIndex(albumEntity.getPhotoCount()) )) - ); - } - }); + ) + ); } @Transactional From 7a4a50f5e1070f0fb1cc6359b279309734bcd5d3 Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 04:55:08 +0900 Subject: [PATCH 07/10] style: remove useless indent --- .../kr/mafoo/photo/service/PhotoService.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 00f1261..1124086 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -147,28 +147,29 @@ public Mono updatePhotoAlbumId(String photoId, String albumId, Stri @Transactional public Mono updatePhotoDisplayIndex(String photoId, Integer newIndex, String requestMemberId) { return findByPhotoId(photoId, requestMemberId) - .flatMap(photoEntity -> albumService.findByAlbumId(photoEntity.getAlbumId(), requestMemberId) - .flatMap(albumEntity -> { - int targetIndex = albumEntity.getPhotoCount() - newIndex - 1; - - if (photoEntity.getDisplayIndex().equals(targetIndex)) { - return Mono.error(new PhotoDisplayIndexIsSameException()); - } - - if (targetIndex < 0 || targetIndex >= albumEntity.getPhotoCount()) { - return Mono.error(new PhotoDisplayIndexNotValidException()); - } - - if (photoEntity.getDisplayIndex() < targetIndex) { - return photoRepository - .popDisplayIndexBetween(photoEntity.getAlbumId(), photoEntity.getDisplayIndex() + 1, targetIndex) - .then(photoRepository.save(photoEntity.updateDisplayIndex(targetIndex))); - } else { - return photoRepository - .pushDisplayIndexBetween(photoEntity.getAlbumId(), targetIndex, photoEntity.getDisplayIndex() - 1) - .then(photoRepository.save(photoEntity.updateDisplayIndex(targetIndex))); - } - }) + .flatMap(photoEntity -> + albumService.findByAlbumId(photoEntity.getAlbumId(), requestMemberId) + .flatMap(albumEntity -> { + int targetIndex = albumEntity.getPhotoCount() - newIndex - 1; + + if (photoEntity.getDisplayIndex().equals(targetIndex)) { + return Mono.error(new PhotoDisplayIndexIsSameException()); + } + + if (targetIndex < 0 || targetIndex >= albumEntity.getPhotoCount()) { + return Mono.error(new PhotoDisplayIndexNotValidException()); + } + + if (photoEntity.getDisplayIndex() < targetIndex) { + return photoRepository + .popDisplayIndexBetween(photoEntity.getAlbumId(), photoEntity.getDisplayIndex() + 1, targetIndex) + .then(photoRepository.save(photoEntity.updateDisplayIndex(targetIndex))); + } else { + return photoRepository + .pushDisplayIndexBetween(photoEntity.getAlbumId(), targetIndex, photoEntity.getDisplayIndex() - 1) + .then(photoRepository.save(photoEntity.updateDisplayIndex(targetIndex))); + } + }) ); } From 05ae694d8a514e5770aa2d59110fbd6c2c3b3577 Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 05:12:09 +0900 Subject: [PATCH 08/10] refactor: refactor methods in album service to use findByAlbumId instead of album repository --- .../kr/mafoo/photo/service/AlbumService.java | 80 ++++--------------- 1 file changed, 17 insertions(+), 63 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java b/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java index 9cefe85..b32abac 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java @@ -67,85 +67,39 @@ public Mono findByAlbumId(String albumId, String requestMemberId) { @Transactional public Mono deleteAlbumById(String albumId, String requestMemberId) { - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMap(albumEntity -> { - if(!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - return albumRepository + return findByAlbumId(albumId, requestMemberId) + .flatMap(albumEntity -> + albumRepository .deleteById(albumId) .then(albumRepository.popDisplayIndexBetween( - requestMemberId, albumEntity.getDisplayIndex(), Integer.MAX_VALUE)); - } - }); + requestMemberId, albumEntity.getDisplayIndex(), Integer.MAX_VALUE)) + ); } @Transactional public Mono updateAlbumName(String albumId, String albumName, String requestMemberId) { - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMap(albumEntity -> { - if(!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - return albumRepository.save(albumEntity.updateName(albumName)); - } - }); + return findByAlbumId(albumId, requestMemberId) + .flatMap(albumEntity -> albumRepository.save(albumEntity.updateName(albumName))); } @Transactional public Mono updateAlbumType(String albumId, AlbumType albumType, String requestMemberId) { - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMap(albumEntity -> { - if(!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - return albumRepository.save(albumEntity.updateType(albumType)); - } - }); + return findByAlbumId(albumId, requestMemberId) + .flatMap(albumEntity -> albumRepository.save(albumEntity.updateType(albumType))); } @Transactional - public Mono increaseAlbumPhotoCount(String albumId, int count, String requestMemberId) { - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMap(albumEntity -> { - if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - return albumRepository.save(albumEntity.increasePhotoCount(count)).then(); - } - }); + public Mono increaseAlbumPhotoCount(String albumId, int count, String requestMemberId) { + return findByAlbumId(albumId, requestMemberId) + .flatMap(albumEntity -> albumRepository.save(albumEntity.increasePhotoCount(count))); } @Transactional - public Mono decreaseAlbumPhotoCount(String albumId, int count, String requestMemberId) { - - if (albumId == null) { - return Mono.empty(); - } - - return albumRepository - .findById(albumId) - .switchIfEmpty(Mono.error(new AlbumNotFoundException())) - .flatMap(albumEntity -> { - if (!albumEntity.getOwnerMemberId().equals(requestMemberId)) { - // 내 앨범이 아니면 그냥 없는 앨범 처리 - return Mono.error(new AlbumNotFoundException()); - } else { - return albumRepository.save(albumEntity.decreasePhotoCount(count)).then(); - } - }); + public Mono decreaseAlbumPhotoCount(String albumId, int count, String requestMemberId) { + return Mono.justOrEmpty(albumId) + .switchIfEmpty(Mono.empty()) + .flatMap(id -> findByAlbumId(id, requestMemberId)) + .flatMap(albumEntity -> albumRepository.save(albumEntity.decreasePhotoCount(count))); } } From c1267c65b724ab2c655ebcd8cf86aedfcc6f7c54 Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 05:29:38 +0900 Subject: [PATCH 09/10] fix: add sortMethod setting logic for findAllByAlbumId --- .../src/main/java/kr/mafoo/photo/service/PhotoService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 1124086..116c2bc 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -83,9 +83,11 @@ public Flux uploadPhoto(Flux files, String requestMemberI } public Flux findAllByAlbumId(String albumId, String requestMemberId, String sort) { + String sortMethod = (sort == null) ? "CUSTOM" : sort.toUpperCase(); + return albumService.findByAlbumId(albumId, requestMemberId) .thenMany( - switch (sort) { + switch (sortMethod) { case "ASC" -> photoRepository.findAllByAlbumIdOrderByCreatedAtAsc(albumId); case "DESC" -> photoRepository.findAllByAlbumIdOrderByCreatedAtDesc(albumId); default -> photoRepository.findAllByAlbumIdOrderByDisplayIndexDesc(albumId); From 3ddada228fcf76438b1d7b22675a192f558f679c Mon Sep 17 00:00:00 2001 From: Gyoungmin Kim Date: Sat, 26 Oct 2024 06:20:00 +0900 Subject: [PATCH 10/10] feat: add display index setting logic for createNewPhotoFileUrls --- .../photo/controller/PhotoController.java | 2 +- .../kr/mafoo/photo/domain/PhotoEntity.java | 4 +-- .../kr/mafoo/photo/service/PhotoService.java | 36 ++++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java b/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java index fb41db2..7859d63 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java +++ b/photo-service/src/main/java/kr/mafoo/photo/controller/PhotoController.java @@ -53,7 +53,7 @@ public Flux uploadFileUrlPhoto( PhotoFileUrlUploadRequest request ){ return photoService - .createNewPhotoFileUrl(request.fileUrls(), request.albumId(), memberId) + .createNewPhotoFileUrls(request.fileUrls(), request.albumId(), memberId) .map(PhotoResponse::fromEntity); } diff --git a/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java b/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java index 91b79da..5b446b3 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java +++ b/photo-service/src/main/java/kr/mafoo/photo/domain/PhotoEntity.java @@ -82,14 +82,14 @@ public PhotoEntity updateDisplayIndex(Integer displayIndex) { return this; } - public static PhotoEntity newPhoto(String photoId, String photoUrl, BrandType brandType, String albumId, String ownerMemberId) { + public static PhotoEntity newPhoto(String photoId, String photoUrl, BrandType brandType, String albumId, Integer displayIndex, String ownerMemberId) { PhotoEntity photo = new PhotoEntity(); photo.photoId = photoId; photo.photoUrl = photoUrl; photo.brand = brandType; photo.ownerMemberId = ownerMemberId; photo.albumId = albumId; - photo.displayIndex = 0; + photo.displayIndex = displayIndex; photo.isNew = true; photo.createdAt = LocalDateTime.now(); photo.updatedAt = LocalDateTime.now(); diff --git a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java index 116c2bc..e5f2a67 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java +++ b/photo-service/src/main/java/kr/mafoo/photo/service/PhotoService.java @@ -19,6 +19,8 @@ import reactor.core.publisher.Mono; import reactor.core.scheduler.Schedulers; +import java.util.concurrent.atomic.AtomicInteger; + @Slf4j @RequiredArgsConstructor @@ -35,24 +37,34 @@ public Mono createNewPhotoByQrUrl(String qrUrl, String requestMembe return qrService .getFileFromQrUrl(qrUrl) .flatMap(fileDto -> objectStorageService.uploadFile(fileDto.fileByte()) - .flatMap(photoUrl -> createNewPhoto(photoUrl, fileDto.type(), null, requestMemberId)) + .flatMap(photoUrl -> createNewPhoto(photoUrl, fileDto.type(), requestMemberId)) ); } @Transactional - public Flux createNewPhotoFileUrl(String[] fileUrls, String albumId, String requestMemberId) { + public Flux createNewPhotoFileUrls(String[] fileUrls, String albumId, String requestMemberId) { return albumService.findByAlbumId(albumId, requestMemberId) - .flatMap(albumEntity -> albumService.increaseAlbumPhotoCount(albumId, fileUrls.length, requestMemberId)) - .thenMany( - Flux.fromArray(fileUrls) - .flatMap(fileUrl -> objectStorageService.setObjectPublicRead(fileUrl) - .flatMap(fileLink -> createNewPhoto(fileLink, BrandType.EXTERNAL, albumId, requestMemberId)) - ) - ); + .flatMapMany(albumEntity -> { + AtomicInteger displayIndex = new AtomicInteger(albumEntity.getPhotoCount()); + + return Flux.fromArray(fileUrls) + .concatMap(fileUrl -> + createNewPhotoFileUrl(fileUrl, BrandType.EXTERNAL, albumId, displayIndex.getAndIncrement(), requestMemberId) + ); + }); + } + + private Mono createNewPhotoFileUrl(String fileUrl, BrandType type, String albumId, Integer displayIndex, String requestMemberId) { + return objectStorageService.setObjectPublicRead(fileUrl) + .flatMap(fileLink -> { + PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), fileLink, type, albumId, displayIndex, requestMemberId); + return albumService.increaseAlbumPhotoCount(albumId, 1, requestMemberId) + .then(photoRepository.save(photoEntity)); + }); } - private Mono createNewPhoto(String photoUrl, BrandType type, String albumId, String requestMemberId) { - PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, type, albumId, requestMemberId); + private Mono createNewPhoto(String photoUrl, BrandType type, String requestMemberId) { + PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, type, null, 0, requestMemberId); return photoRepository.save(photoEntity); } @@ -74,7 +86,7 @@ public Flux uploadPhoto(Flux files, String requestMemberI }) .flatMap(bytes -> objectStorageService.uploadFile(bytes) .flatMap(photoUrl -> { - PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, BrandType.EXTERNAL, null, requestMemberId); + PhotoEntity photoEntity = PhotoEntity.newPhoto(IdGenerator.generate(), photoUrl, BrandType.EXTERNAL, null, 0, requestMemberId); return photoRepository.save(photoEntity); })) .subscribeOn(Schedulers.boundedElastic())