diff --git a/src/main/java/com/guttery/madii/common/exception/ErrorDetails.java b/src/main/java/com/guttery/madii/common/exception/ErrorDetails.java index aea08c9..d928070 100644 --- a/src/main/java/com/guttery/madii/common/exception/ErrorDetails.java +++ b/src/main/java/com/guttery/madii/common/exception/ErrorDetails.java @@ -52,6 +52,7 @@ public enum ErrorDetails { JOY_NOT_FOUND("J001", HttpStatus.NOT_FOUND.value(), "소확행을 찾을 수 없습니다."), TODAY_JOY_NOT_FOUND("J100", HttpStatus.NOT_FOUND.value(), "오늘의 소확행을 찾을 수 없습니다."), + SAVING_JOY_NOT_FOUND("J001", HttpStatus.NOT_FOUND.value(), "앨범에 저장된 소확행 기록을 찾을 수 없습니다."), ALBUM_NOT_FOUND("A001", HttpStatus.NOT_FOUND.value(), "앨범을 찾을 수 없습니다."), ALREADY_EXIST_BOOKMARK("A002", HttpStatus.BAD_REQUEST.value(), "이미 저장한 앨범입니다."), diff --git a/src/main/java/com/guttery/madii/domain/albums/application/service/AlbumService.java b/src/main/java/com/guttery/madii/domain/albums/application/service/AlbumService.java index 74be7db..0eeb3c3 100644 --- a/src/main/java/com/guttery/madii/domain/albums/application/service/AlbumService.java +++ b/src/main/java/com/guttery/madii/domain/albums/application/service/AlbumService.java @@ -97,7 +97,11 @@ public void addJoyToAlbum(Long joyId, AlbumSaveJoyRequest albumSaveJoyRequest, U .orElseThrow(() -> CustomException.of(ErrorDetails.JOY_NOT_FOUND)); final Album album = albumRepository.findById(albumId) .orElseThrow(() -> CustomException.of(ErrorDetails.ALBUM_NOT_FOUND)); - final SavingJoy savingJoy = SavingJoy.createSavingJoy(joy, album); + + // 앨범 내에서 가장 높은 order 값을 조회하여 1을 더해줌 + Integer maxOrder = savingJoyRepository.findMaxOrderByAlbum(album); + Integer joyOrder = (maxOrder == null) ? 1 : maxOrder + 1; + final SavingJoy savingJoy = SavingJoy.createSavingJoy(joy, album, joyOrder); savingJoyRepository.save(savingJoy); } } @@ -308,4 +312,51 @@ public void reportAlbum(AlbumReportRequest albumReportRequest, Long albumId, Use album.makeBlocked(); } + public void putAllAlbum(AlbumPutAllRequest albumPutAllRequest, Long albumId, UserPrincipal userPrincipal) { + final User user = UserServiceHelper.findExistingUser(userRepository, userPrincipal); + Album album = albumRepository.findById(albumId) + .orElseThrow(() -> CustomException.of(ErrorDetails.ALBUM_NOT_FOUND)); + + String name = albumPutAllRequest.name(); + String description = albumPutAllRequest.description(); + List joys = albumPutAllRequest.joys(); + List deletedJoyIds = albumPutAllRequest.deletedJoyIds(); + + // 1. 삭제된 소확행 처리 + if (deletedJoyIds != null && !deletedJoyIds.isEmpty()) { + savingJoyRepository.deleteAllByJoyJoyIdIn(deletedJoyIds); + } + + for (SavingJoyDto joyDto :joys) { + // 2. 수정된 소확행 처리 + if (joyDto.joyId() != null) { + // 소확행명 수정 - joy table update + Joy joy = joyRepository.findById(joyDto.joyId()) + .orElseThrow(() -> CustomException.of(ErrorDetails.JOY_NOT_FOUND)); + joy.modifyContents(joyDto.contents()); + + // 순서 세팅 - saving_joy table update + SavingJoy savingJoy = savingJoyRepository.findByJoyAndAlbum(joy, album) + .orElseThrow(() -> CustomException.of(ErrorDetails.SAVING_JOY_NOT_FOUND)); + savingJoy.modifyJoyOrder(joyDto.joyOrder()); + } else { + // 3. 새로 추가된 소확행 처리 + Random random = new Random(); + int bound = 24; // 소확행 썸네일 아이콘 번호 1 ~ 24 중 랜덤 생성 + + Joy joy; + if (album.getAlbumStatus().getIsOfficial()) { + joy = Joy.createOfficialJoy(user, 1 + random.nextInt(bound), joyDto.contents()); + } else { + joy = Joy.createPersonalJoy(user, 1 + random.nextInt(bound), joyDto.contents()); + } + + joyRepository.save(joy); + SavingJoy savingJoy = SavingJoy.createSavingJoy(joy, album, joyDto.joyOrder()); + savingJoyRepository.save(savingJoy); + } + } + + album.modifyNameAndDes(name, description); + } } diff --git a/src/main/java/com/guttery/madii/domain/albums/domain/repository/SavingJoyRepository.java b/src/main/java/com/guttery/madii/domain/albums/domain/repository/SavingJoyRepository.java index 81bbb6d..cb08766 100644 --- a/src/main/java/com/guttery/madii/domain/albums/domain/repository/SavingJoyRepository.java +++ b/src/main/java/com/guttery/madii/domain/albums/domain/repository/SavingJoyRepository.java @@ -5,9 +5,17 @@ import com.guttery.madii.domain.joy.domain.model.Joy; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + public interface SavingJoyRepository extends JpaRepository { - SavingJoy findByJoyAndAlbum(Joy joy, Album album); + Optional findByJoyAndAlbum(Joy joy, Album album); void deleteByAlbumAlbumId(Long albumId); + + Integer findMaxOrderByAlbum(Album album); + + void deleteAllByJoyJoyIdIn(List deletedJoyIds); + } diff --git a/src/main/java/com/guttery/madii/domain/albums/presentation/AlbumController.java b/src/main/java/com/guttery/madii/domain/albums/presentation/AlbumController.java index b578ebf..dc2232e 100644 --- a/src/main/java/com/guttery/madii/domain/albums/presentation/AlbumController.java +++ b/src/main/java/com/guttery/madii/domain/albums/presentation/AlbumController.java @@ -280,4 +280,22 @@ public void reportAlbum(@Valid @RequestBody AlbumReportRequest albumReportReques albumService.reportAlbum(albumReportRequest, albumId, userPrincipal); } + + @PutMapping("/{albumId}/all") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "앨범 편집 성공", + useReturnTypeSchema = true + ) + } + ) + @Operation(summary = "앨범 편집 API", description = "앨범 편집 API입니다.") + public void putAllAlbum(@Valid @RequestBody AlbumPutAllRequest albumPutAllRequest, + @PathVariable Long albumId, + @AuthenticationPrincipal final UserPrincipal userPrincipal) { + albumService.putAllAlbum(albumPutAllRequest, albumId, userPrincipal); + } + } diff --git a/src/main/java/com/guttery/madii/domain/joy/domain/model/Joy.java b/src/main/java/com/guttery/madii/domain/joy/domain/model/Joy.java index 97f543b..db36ddf 100644 --- a/src/main/java/com/guttery/madii/domain/joy/domain/model/Joy.java +++ b/src/main/java/com/guttery/madii/domain/joy/domain/model/Joy.java @@ -58,6 +58,10 @@ public static Joy createPersonalJoy(User user, Integer joyIconNum, String conten return new Joy(user, JoyType.PERSONAL, joyIconNum, contents); } + public static Joy createOfficialJoy(User user, Integer joyIconNum, String contents) { + return new Joy(user, JoyType.OFFICIAL, joyIconNum, contents); + } + public void modifyContents(String contents) { this.contents = contents; }