From 04bf4e315ced2a45a248c4fc993ef483153df355 Mon Sep 17 00:00:00 2001 From: ChuYong Date: Thu, 12 Dec 2024 23:14:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../photo/controller/AlbumController.java | 7 +- .../dto/request/AlbumCreateRequest.java | 5 +- .../photo/repository/AlbumRepository.java | 2 + .../kr/mafoo/photo/service/AlbumService.java | 104 ++++++++++++------ 4 files changed, 82 insertions(+), 36 deletions(-) diff --git a/photo-service/src/main/java/kr/mafoo/photo/controller/AlbumController.java b/photo-service/src/main/java/kr/mafoo/photo/controller/AlbumController.java index 6fe41d1..c573304 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/controller/AlbumController.java +++ b/photo-service/src/main/java/kr/mafoo/photo/controller/AlbumController.java @@ -50,9 +50,14 @@ public Mono createAlbum( String memberId, AlbumCreateRequest request ){ - if (request.name().equals("SUMONE")) { + if (request.type().equals("SUMONE")) { throw new RuntimeException(); // should not be happened } + if(request.sumoneInviteCode() != null) { + return albumService + .addSumoneAlbum(request.name(), request.type(), memberId, request.sumoneInviteCode()) + .map(AlbumResponse::fromEntity); + } return albumService .addAlbum(request.name(), request.type(), memberId) .map(AlbumResponse::fromEntity); diff --git a/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/AlbumCreateRequest.java b/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/AlbumCreateRequest.java index f308893..5e08b3f 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/AlbumCreateRequest.java +++ b/photo-service/src/main/java/kr/mafoo/photo/controller/dto/request/AlbumCreateRequest.java @@ -15,6 +15,9 @@ public record AlbumCreateRequest( @MatchEnum(enumClass = AlbumType.class) @Schema(description = "앨범 타입") - String type + String type, + + @Schema(description = "초대 코드") + String sumoneInviteCode ) { } diff --git a/photo-service/src/main/java/kr/mafoo/photo/repository/AlbumRepository.java b/photo-service/src/main/java/kr/mafoo/photo/repository/AlbumRepository.java index 6bbbe4a..2183b30 100644 --- a/photo-service/src/main/java/kr/mafoo/photo/repository/AlbumRepository.java +++ b/photo-service/src/main/java/kr/mafoo/photo/repository/AlbumRepository.java @@ -34,4 +34,6 @@ public interface AlbumRepository extends R2dbcRepository { Flux findAllByOwnerMemberIdOrderByAlbumIdDesc(String ownerMemberId, Pageable pageable); Mono countAlbumEntityByType(AlbumType albumType); + + Flux findAllByExternalId(String externalId); } 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 953db87..5a770b3 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 @@ -6,11 +6,16 @@ import java.util.Comparator; import java.util.Optional; + import kr.mafoo.photo.domain.AlbumEntity; import kr.mafoo.photo.domain.enums.AlbumType; +import kr.mafoo.photo.domain.enums.BrandType; import kr.mafoo.photo.exception.AlbumNotFoundException; import kr.mafoo.photo.exception.AlbumOwnerChangeDeniedException; import kr.mafoo.photo.exception.SharedMemberNotFoundException; +import kr.mafoo.photo.repository.AlbumRepository; +import kr.mafoo.photo.repository.PhotoRepository; +import kr.mafoo.photo.repository.SumoneEventMappingRepository; import kr.mafoo.photo.service.dto.AlbumDto; import kr.mafoo.photo.service.dto.SharedAlbumDto; import kr.mafoo.photo.service.dto.SharedMemberDto; @@ -26,6 +31,11 @@ public class AlbumService { private final AlbumQuery albumQuery; private final AlbumCommand albumCommand; + private final AlbumRepository albumRepository; + + private final PhotoCommand photoCommand; + private final PhotoRepository photoRepository; + private final AlbumPermissionVerifier albumPermissionVerifier; @@ -33,46 +43,48 @@ public class AlbumService { private final MemberService memberService; private final SharedMemberCommand sharedMemberCommand; + private final SumoneEventMappingRepository sumoneEventMappingRepository; + @Transactional(readOnly = true) public Flux findAlbumListByMemberId(String memberId, String token) { return Flux.merge( - findOwnedAlbumListByMemberId(memberId), - findSharedAlbumListByMemberId(memberId, token) + findOwnedAlbumListByMemberId(memberId), + findSharedAlbumListByMemberId(memberId, token) ).sort(Comparator.comparing(AlbumDto::createdAt).reversed()); } private Flux findOwnedAlbumListByMemberId(String memberId) { return albumQuery.findByMemberId(memberId) - .onErrorResume(AlbumNotFoundException.class, ex -> Mono.empty()) - .map(AlbumDto::fromOwnedAlbum); + .onErrorResume(AlbumNotFoundException.class, ex -> Mono.empty()) + .map(AlbumDto::fromOwnedAlbum); } private Flux findSharedAlbumListByMemberId(String memberId, String token) { return sharedMemberQuery.findAllByMemberIdWhereStatusNotRejected(memberId) - .onErrorResume(SharedMemberNotFoundException.class, ex -> Mono.empty()) - .concatMap(sharedMember -> albumQuery.findById(sharedMember.getAlbumId()) - .flatMap(album -> memberService.getMemberInfoById(album.getOwnerMemberId(), token) - .flatMap(member -> Mono.just(AlbumDto.fromSharedAlbum(album, sharedMember, member))) - ) - ); + .onErrorResume(SharedMemberNotFoundException.class, ex -> Mono.empty()) + .concatMap(sharedMember -> albumQuery.findById(sharedMember.getAlbumId()) + .flatMap(album -> memberService.getMemberInfoById(album.getOwnerMemberId(), token) + .flatMap(member -> Mono.just(AlbumDto.fromSharedAlbum(album, sharedMember, member))) + ) + ); } @Transactional(readOnly = true) public Mono findAlbumDetailById(String albumId, String requestMemberId, String token) { return albumPermissionVerifier.verifyOwnershipOrAccessPermission(albumId, requestMemberId, VIEW_ACCESS) - .flatMap(album -> sharedMemberQuery.findAllByAlbumIdWhereStatusNotRejected(albumId) - .onErrorResume(SharedMemberNotFoundException.class, ex -> Mono.empty()) - - .flatMap(sharedMember -> memberService.getMemberInfoById(sharedMember.getMemberId(), token) - .map(memberInfo -> SharedMemberDto.fromSharedMember(sharedMember, memberInfo))) - .sort(Comparator.comparing(SharedMemberDto::shareStatus)) - .collectList() - .flatMap(sharedMembers -> memberService.getMemberInfoById(album.getOwnerMemberId(), token) - .map(ownerMember -> SharedAlbumDto.fromSharedAlbum(album, ownerMember, sharedMembers)) - ) - - .switchIfEmpty(Mono.just(SharedAlbumDto.fromOwnedAlbum(album))) - ); + .flatMap(album -> sharedMemberQuery.findAllByAlbumIdWhereStatusNotRejected(albumId) + .onErrorResume(SharedMemberNotFoundException.class, ex -> Mono.empty()) + + .flatMap(sharedMember -> memberService.getMemberInfoById(sharedMember.getMemberId(), token) + .map(memberInfo -> SharedMemberDto.fromSharedMember(sharedMember, memberInfo))) + .sort(Comparator.comparing(SharedMemberDto::shareStatus)) + .collectList() + .flatMap(sharedMembers -> memberService.getMemberInfoById(album.getOwnerMemberId(), token) + .map(ownerMember -> SharedAlbumDto.fromSharedAlbum(album, ownerMember, sharedMembers)) + ) + + .switchIfEmpty(Mono.just(SharedAlbumDto.fromOwnedAlbum(album))) + ); } @Transactional @@ -80,30 +92,54 @@ public Mono addAlbum(String albumName, String albumType, String req return albumCommand.addAlbum(albumName, albumType, requestMemberId, null); } + @Transactional + public Mono addSumoneAlbum(String albumName, String albumType, String requestMemberId, String inviteCode) { + return albumCommand + .addAlbum(albumName, albumType, requestMemberId, null) + .flatMap(album -> sumoneEventMappingRepository + .findByInviteCode(inviteCode) + .flatMap(sumoneEventMappingEntity -> + sumoneEventMappingRepository.delete(sumoneEventMappingEntity).then(Mono.just(sumoneEventMappingEntity))) + .map(entity -> "SUMONE_" + entity.getId()) + .flatMapMany(albumRepository::findAllByExternalId) + .flatMap(sumoneAlbum -> + photoRepository.findAllByAlbumIdOrderByCreatedAtAsc(sumoneAlbum.getAlbumId()).flatMap(photo -> + photoCommand.addPhoto( + photo.getPhotoUrl(), + BrandType.EXTERNAL, + album.getAlbumId(), + 0, + requestMemberId + ) + ) + ) + .then(Mono.just(album))); + } + @Transactional public Mono modifyAlbumNameAndType(String albumId, String newAlbumName, String newAlbumType, String requestMemberId) { return albumPermissionVerifier.verifyOwnershipOrAccessPermission(albumId, requestMemberId, FULL_ACCESS) - .flatMap(album -> albumCommand.modifyAlbumNameAndType(album, newAlbumName, newAlbumType)); + .flatMap(album -> albumCommand.modifyAlbumNameAndType(album, newAlbumName, newAlbumType)); } @Transactional public Mono modifyAlbumOwnership(String albumId, String newOwnerMemberId, String requestMemberId) { return albumPermissionVerifier.verifyOwnership(albumId, requestMemberId) - .flatMap(album -> sharedMemberQuery.findByAlbumIdAndMemberIdWhereStatusAccepted(albumId, newOwnerMemberId) - .onErrorResume(SharedMemberNotFoundException.class, ex -> - Mono.error(new AlbumOwnerChangeDeniedException()) - ) - .flatMap(sharedMemberCommand::removeSharedMember) - .then(albumCommand.modifyAlbumOwnership(album, newOwnerMemberId)) - .then(sharedMemberCommand.addSharedMember(albumId, String.valueOf(FULL_ACCESS), Optional.of(ACCEPTED), requestMemberId)) - .thenReturn(album) - ); + .flatMap(album -> sharedMemberQuery.findByAlbumIdAndMemberIdWhereStatusAccepted(albumId, newOwnerMemberId) + .onErrorResume(SharedMemberNotFoundException.class, ex -> + Mono.error(new AlbumOwnerChangeDeniedException()) + ) + .flatMap(sharedMemberCommand::removeSharedMember) + .then(albumCommand.modifyAlbumOwnership(album, newOwnerMemberId)) + .then(sharedMemberCommand.addSharedMember(albumId, String.valueOf(FULL_ACCESS), Optional.of(ACCEPTED), requestMemberId)) + .thenReturn(album) + ); } @Transactional public Mono removeAlbum(String albumId, String requestMemberId) { return albumPermissionVerifier.verifyOwnership(albumId, requestMemberId) - .flatMap(albumCommand::removeAlbum); + .flatMap(albumCommand::removeAlbum); } public Mono countAlbumByAlbumType(AlbumType albumType) {