Skip to content

Commit

Permalink
feat: 가져오기 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
CChuYong committed Dec 12, 2024
1 parent 9953094 commit 04bf4e3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ public Mono<AlbumResponse> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public record AlbumCreateRequest(

@MatchEnum(enumClass = AlbumType.class)
@Schema(description = "앨범 타입")
String type
String type,

@Schema(description = "초대 코드")
String sumoneInviteCode
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public interface AlbumRepository extends R2dbcRepository<AlbumEntity, String> {
Flux<AlbumEntity> findAllByOwnerMemberIdOrderByAlbumIdDesc(String ownerMemberId, Pageable pageable);

Mono<Long> countAlbumEntityByType(AlbumType albumType);

Flux<AlbumEntity> findAllByExternalId(String externalId);
}
104 changes: 70 additions & 34 deletions photo-service/src/main/java/kr/mafoo/photo/service/AlbumService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,84 +31,115 @@ 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;

private final SharedMemberQuery sharedMemberQuery;
private final MemberService memberService;
private final SharedMemberCommand sharedMemberCommand;

private final SumoneEventMappingRepository sumoneEventMappingRepository;

@Transactional(readOnly = true)
public Flux<AlbumDto> 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<AlbumDto> 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<AlbumDto> 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<SharedAlbumDto> 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
public Mono<AlbumEntity> addAlbum(String albumName, String albumType, String requestMemberId) {
return albumCommand.addAlbum(albumName, albumType, requestMemberId, null);
}

@Transactional
public Mono<AlbumEntity> 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<AlbumEntity> 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<AlbumEntity> 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<Void> removeAlbum(String albumId, String requestMemberId) {
return albumPermissionVerifier.verifyOwnership(albumId, requestMemberId)
.flatMap(albumCommand::removeAlbum);
.flatMap(albumCommand::removeAlbum);
}

public Mono<Long> countAlbumByAlbumType(AlbumType albumType) {
Expand Down

0 comments on commit 04bf4e3

Please sign in to comment.