diff --git a/core/src/main/java/com/pocket/core/exception/album/AlbumErrorCode.java b/core/src/main/java/com/pocket/core/exception/album/AlbumErrorCode.java index 9206d9d..437ae3c 100644 --- a/core/src/main/java/com/pocket/core/exception/album/AlbumErrorCode.java +++ b/core/src/main/java/com/pocket/core/exception/album/AlbumErrorCode.java @@ -10,7 +10,8 @@ @AllArgsConstructor public enum AlbumErrorCode implements BaseErrorCode { ALBUM_NOT_FOUND(HttpStatus.BAD_REQUEST, "400", "해당 앨범이 존재하지 않습니다."), - ALBUM_SHARE_NOT_FOUND(HttpStatus.BAD_REQUEST, "400", "해당 공유 데이터가 존재하지 않습니다."); + ALBUM_SHARE_NOT_FOUND(HttpStatus.BAD_REQUEST, "400", "해당 공유 데이터가 존재하지 않습니다."), + USER_ALREADY_OWNED_ALBUM(HttpStatus.CONFLICT, "409", "사용자가 이미 이 앨범을 소유하고 있습니다."); private final HttpStatus httpStatus; private final String code; diff --git a/outbound/src/main/java/com/pocket/outbound/adapter/album/adapter/AlbumRegisterAdapter.java b/outbound/src/main/java/com/pocket/outbound/adapter/album/adapter/AlbumRegisterAdapter.java index 3a018ac..c8baed7 100644 --- a/outbound/src/main/java/com/pocket/outbound/adapter/album/adapter/AlbumRegisterAdapter.java +++ b/outbound/src/main/java/com/pocket/outbound/adapter/album/adapter/AlbumRegisterAdapter.java @@ -104,12 +104,23 @@ public Long saveShareTable(String email, Long albumId) { @Override public void saveNewData(String email, Long token) { - JpaAlbumShare albumShare = albumShareRepository.findById(token).orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_SHARE_NOT_FOUND)); + // albumShare 가져오기 + JpaAlbumShare albumShare = albumShareRepository.findById(token) + .orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_SHARE_NOT_FOUND)); + + // jpaUser 가져오기 JpaUser jpaUser = userRepository.findByUserEmail(email) .orElseThrow(() -> new UserCustomException(UserErrorCode.NO_USER_INFO)); + // albumShare의 user와 jpaUser가 같은지 비교 + if (albumShare.getUser().equals(jpaUser)) { + throw new AlbumCustomException(AlbumErrorCode.USER_ALREADY_OWNED_ALBUM); + } + // 앨범 저장 - JpaAlbum album = albumRepository.findById(albumShare.getAlbum().getId()).orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_NOT_FOUND)); + JpaAlbum album = albumRepository.findById(albumShare.getAlbum().getId()) + .orElseThrow(() -> new AlbumCustomException(AlbumErrorCode.ALBUM_NOT_FOUND)); + JpaAlbum newAlbum = JpaAlbum.builder() .image(album.getImage()) .jpaUser(jpaUser) @@ -123,7 +134,8 @@ public void saveNewData(String email, Long token) { List albumHashTagList = albumHashTagRepository.findByJpaAlbum_Id(album.getId()); for (JpaAlbumHashTag a : albumHashTagList) { - JpaHashTag hashTag = hashtagRepository.findById(a.getJpaHashtag().getId()).orElseThrow(() -> new HashTagCustomException(HashTagErrorCode.HASHTAG_NOT_FOUND)); + JpaHashTag hashTag = hashtagRepository.findById(a.getJpaHashtag().getId()) + .orElseThrow(() -> new HashTagCustomException(HashTagErrorCode.HASHTAG_NOT_FOUND)); JpaHashTag newTag = JpaHashTag.builder() .hashTag(hashTag.getHashTag()) @@ -133,4 +145,5 @@ public void saveNewData(String email, Long token) { hashtagRepository.save(newTag); } } + } \ No newline at end of file