From 43114c1b7205e62c10a1e8e840853f8608c5aecd Mon Sep 17 00:00:00 2001 From: wwan13 Date: Sat, 3 Aug 2024 11:39:33 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat=20:=20=EB=8C=80=ED=91=9C=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=B0=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v1/diaryimage/DiaryImageController.kt | 11 +++++++++++ .../domain/diaryimage/DiaryImageModifier.kt | 16 +++++++++++++++- .../domain/diaryimage/DiaryImageRepository.kt | 2 ++ .../grida/domain/diaryimage/DiaryImageService.kt | 9 +++++++++ .../diaryimage/DiaryImageEntityRepository.kt | 6 ++++++ .../diaryimage/DiaryImageJpaEntityRepository.kt | 11 +++++++++++ 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diaryimage/DiaryImageController.kt b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diaryimage/DiaryImageController.kt index abe3986..8c59412 100644 --- a/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diaryimage/DiaryImageController.kt +++ b/grida-core/core-api/src/main/kotlin/org/grida/presentation/v1/diaryimage/DiaryImageController.kt @@ -35,4 +35,15 @@ class DiaryImageController( val response = IdResponse(diaryId) return ApiResponse.success(response) } + + @PostMapping("/{diaryId}/image/{diaryImageId}/change") + fun changeDiaryImage( + @RequestUserId userId: Long, + @PathVariable diaryId: Long, + @PathVariable diaryImageId: Long + ): ApiResponse { + diaryImageService.changeDiaryImage(diaryImageId, diaryId, userId) + val response = IdResponse(diaryId) + return ApiResponse.success(response) + } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageModifier.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageModifier.kt index 8b19889..dfd896c 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageModifier.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageModifier.kt @@ -13,10 +13,24 @@ class DiaryImageModifier( ) { @Transactional - fun modifyAsActivate(diaryImageId: Long, userId: Long) { + fun modifyAsActivate( + diaryImageId: Long, + userId: Long + ) { val diaryImage = diaryImageReader.read(diaryImageId) accessManager.ownerOnly(diaryImage, userId) diaryImageRepository.updateStatus(diaryImageId, ImageStatus.ACTIVATE) } + + @Transactional + fun modifyOriginalDiaryImageAsDeactivate( + diaryId: Long, + userId: Long + ) { + val originalDiaryImage = + diaryImageRepository.findByDiaryIdAndStatus(diaryId, ImageStatus.ACTIVATE) + + diaryImageRepository.updateStatus(originalDiaryImage.id, ImageStatus.DEACTIVATE) + } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageRepository.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageRepository.kt index ad92a80..274dc08 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageRepository.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageRepository.kt @@ -9,6 +9,8 @@ interface DiaryImageRepository { fun findById(id: Long): DiaryImage + fun findByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): DiaryImage + fun countByDiaryId(diaryId: Long): Long fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageService.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageService.kt index ee8979a..0aa2530 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageService.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageService.kt @@ -49,4 +49,13 @@ class DiaryImageService( diaryImageValidator.validateAlreadyHasActivateDiaryImage(diaryId) diaryImageModifier.modifyAsActivate(diaryImageId, userId) } + + fun changeDiaryImage( + diaryImageId: Long, + diaryId: Long, + userId: Long + ) { + diaryImageModifier.modifyOriginalDiaryImageAsDeactivate(diaryId, userId) + diaryImageModifier.modifyAsActivate(diaryImageId, userId) + } } \ No newline at end of file diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntityRepository.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntityRepository.kt index 2863d98..6f165dd 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntityRepository.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntityRepository.kt @@ -30,6 +30,12 @@ class DiaryImageEntityRepository( return diaryImageEntity.toDomain() } + override fun findByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): DiaryImage { + val diaryImageEntity = + diaryImageJpaEntityRepository.findByDiaryIdAndStatusOrException(diaryId, status) + return diaryImageEntity.toDomain() + } + override fun countByDiaryId(diaryId: Long): Long { return diaryImageJpaEntityRepository.countByDiaryId(diaryId) } diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageJpaEntityRepository.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageJpaEntityRepository.kt index 187debd..5477102 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageJpaEntityRepository.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageJpaEntityRepository.kt @@ -4,6 +4,7 @@ import org.grida.domain.image.ImageStatus import org.grida.error.GridaException import org.grida.error.NoSuchData import org.springframework.data.jpa.repository.JpaRepository +import java.util.Optional fun DiaryImageJpaEntityRepository.findByIdOrException( id: Long @@ -11,8 +12,18 @@ fun DiaryImageJpaEntityRepository.findByIdOrException( return this.findById(id).orElseThrow { GridaException(NoSuchData) } } +fun DiaryImageJpaEntityRepository.findByDiaryIdAndStatusOrException( + diaryId: Long, + status: ImageStatus +): DiaryImageEntity { + return this.findByDiaryIdAndStatus(diaryId, status) + .orElseThrow { GridaException(NoSuchData) } +} + interface DiaryImageJpaEntityRepository : JpaRepository { + fun findByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Optional + fun countByDiaryId(diaryId: Long): Long fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean From 3ab5f58237df59a7f1cfe34d5b4140c11947ad7d Mon Sep 17 00:00:00 2001 From: wwan13 Date: Sat, 3 Aug 2024 11:41:11 +0900 Subject: [PATCH 2/2] =?UTF-8?q?docs=20:=20=EB=8C=80=ED=91=9C=20=EC=9D=BC?= =?UTF-8?q?=EA=B8=B0=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/diaryimage/DiaryImageApiDocsTest.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/grida-core/core-api/src/test/kotlin/org/grida/docs/diaryimage/DiaryImageApiDocsTest.kt b/grida-core/core-api/src/test/kotlin/org/grida/docs/diaryimage/DiaryImageApiDocsTest.kt index 84bb9c3..cdfc7e5 100644 --- a/grida-core/core-api/src/test/kotlin/org/grida/docs/diaryimage/DiaryImageApiDocsTest.kt +++ b/grida-core/core-api/src/test/kotlin/org/grida/docs/diaryimage/DiaryImageApiDocsTest.kt @@ -68,4 +68,27 @@ class DiaryImageApiDocsTest( ) } } + + @Test + fun `대표 일기 이미지 수정 API`() { + every { diaryImageService.changeDiaryImage(any(), any(), any()) } just runs + + val api = api.post("/api/v1/diary/{diaryId}/image/{diaryImageId}/change", 1L, 1L) { + withBearerToken() + } + + documentFor(api, "change-diary-image") { + summary("대표 일기 이미지 수정 API") + requestHeaders( + "Authorization" whichMeans "인증 토큰" + ) + pathParameters( + "diaryId" whichMeans "적용하려는 일기의 ID", + "diaryImageId" whichMeans "수정하려는 일기 이미지의 ID", + ) + responseFields( + "data.id" isTypeOf NUMBER whichMeans "적용된 일기 ID" + ) + } + } }