From 620a9d0f0b910aa1f2f84cb74903ff3fb8766eec Mon Sep 17 00:00:00 2001 From: wwan13 Date: Thu, 1 Aug 2024 16:06:31 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat=20:=20=EC=86=8C=EC=9C=A0=EA=B0=80?= =?UTF-8?q?=EB=8A=A5=ED=95=9C=20Ownable=20=EC=9D=B8=ED=84=B0=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EC=A0=95=EC=9D=98,=20=EC=86=8C=EC=9C=A0?= =?UTF-8?q?=EC=9E=90=EB=A7=8C=20=EC=A0=91=EA=B7=BC=20=EA=B0=80=EB=8A=A5?= =?UTF-8?q?=ED=95=9C=20access=20logic=20=EC=A4=91=EB=B3=B5=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/grida/domain/base/AccessManager.kt | 18 ++++++++++++++++++ .../kotlin/org/grida/domain/base/Ownable.kt | 6 ++++++ 2 files changed, 24 insertions(+) create mode 100644 grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt create mode 100644 grida-core/core-domain/src/main/kotlin/org/grida/domain/base/Ownable.kt diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt new file mode 100644 index 00000000..68601cfa --- /dev/null +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt @@ -0,0 +1,18 @@ +package org.grida.domain.base + +import org.grida.error.AccessFailed +import org.grida.error.GridaException +import org.springframework.stereotype.Component + +@Component +class AccessManager { + + fun ownerOnly( + target: Ownable, + accessorId: Long + ) { + if (target.isOwner(accessorId)) { + throw GridaException(AccessFailed) + } + } +} \ No newline at end of file diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/Ownable.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/Ownable.kt new file mode 100644 index 00000000..ceea72bc --- /dev/null +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/Ownable.kt @@ -0,0 +1,6 @@ +package org.grida.domain.base + +interface Ownable { + + fun isOwner(accessorId: Long): Boolean +} \ No newline at end of file From a98284f9d9a82db1a130c55709eb6366e375db6f Mon Sep 17 00:00:00 2001 From: wwan13 Date: Thu, 1 Aug 2024 16:08:40 +0900 Subject: [PATCH 2/6] =?UTF-8?q?refactor=20:=20Ownable,=20AccessManager=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/org/grida/domain/diary/Diary.kt | 8 +++++--- .../kotlin/org/grida/domain/diary/DiaryModifier.kt | 7 ++++--- .../org/grida/domain/profileimage/ProfileImage.kt | 6 ++++-- .../grida/domain/profileimage/ProfileImageModifier.kt | 5 +++-- .../grida/domain/profileimage/ProfileImageValidator.kt | 10 ---------- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/Diary.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/Diary.kt index 67d704c2..31d8e22d 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/Diary.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/Diary.kt @@ -1,5 +1,6 @@ package org.grida.domain.diary +import org.grida.domain.base.Ownable import org.grida.domain.base.Timestamp import java.time.LocalDate @@ -10,8 +11,9 @@ data class Diary( val targetDate: LocalDate, val content: String, val scope: DiaryScope -) { - fun isOwner(assessorId: Long): Boolean { - return userId == assessorId +) : Ownable { + + override fun isOwner(accessorId: Long): Boolean { + return userId == accessorId } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt index aebe7078..5211f230 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diary/DiaryModifier.kt @@ -1,5 +1,6 @@ package org.grida.domain.diary +import org.grida.domain.base.AccessManager import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional @@ -7,7 +8,7 @@ import org.springframework.transaction.annotation.Transactional class DiaryModifier( private val diaryRepository: DiaryRepository, private val diaryReader: DiaryReader, - private val diaryValidator: DiaryValidator + private val accessManager: AccessManager ) { @Transactional @@ -18,7 +19,7 @@ class DiaryModifier( scope: DiaryScope ) { val diary = diaryReader.read(diaryId) - diaryValidator.validateIsOwner(diary, userId) + accessManager.ownerOnly(diary, userId) diaryRepository.updateContent(diaryId, content) diaryRepository.updateScope(diaryId, scope) @@ -31,7 +32,7 @@ class DiaryModifier( scope: DiaryScope ) { val diary = diaryReader.read(diaryId) - diaryValidator.validateIsOwner(diary, userId) + accessManager.ownerOnly(diary, userId) diaryRepository.updateScope(diaryId, scope) } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt index ace4889a..138d6878 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImage.kt @@ -1,5 +1,6 @@ package org.grida.domain.profileimage +import org.grida.domain.base.Ownable import org.grida.domain.image.Image data class ProfileImage( @@ -7,8 +8,9 @@ data class ProfileImage( val userId: Long, val image: Image, val appearance: Appearance -) { - fun isOwner(accessorId: Long): Boolean { +) : Ownable { + + override fun isOwner(accessorId: Long): Boolean { return userId == accessorId } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt index ae450493..0940960c 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageModifier.kt @@ -1,5 +1,6 @@ package org.grida.domain.profileimage +import org.grida.domain.base.AccessManager import org.grida.domain.image.ImageStatus import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional @@ -8,7 +9,7 @@ import org.springframework.transaction.annotation.Transactional class ProfileImageModifier( private val profileImageRepository: ProfileImageRepository, private val profileImageReader: ProfileImageReader, - private val profileImageValidator: ProfileImageValidator + private val accessManager: AccessManager ) { @Transactional @@ -17,7 +18,7 @@ class ProfileImageModifier( profileImageId: Long ) { val profileImage = profileImageReader.read(profileImageId) - profileImageValidator.validateIsOwner(profileImage, userId) + accessManager.ownerOnly(profileImage, userId) profileImageRepository.updateStatue(profileImageId, ImageStatus.ACTIVATE) } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt index be30ba3d..f1166290 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt @@ -1,7 +1,6 @@ package org.grida.domain.profileimage import org.grida.domain.image.ImageStatus -import org.grida.error.AccessFailed import org.grida.error.ActivateProfileImageAlreadyExists import org.grida.error.GridaException import org.springframework.stereotype.Component @@ -12,15 +11,6 @@ class ProfileImageValidator( private val profileImageRepository: ProfileImageRepository ) { - fun validateIsOwner( - profileImage: ProfileImage, - userId: Long - ) { - if (!profileImage.isOwner(userId)) { - throw GridaException(AccessFailed) - } - } - @Transactional(readOnly = true) fun validateAlreadyHasActivateProfileImage(userId: Long) { if (profileImageRepository.existsByUserIdAndStatus(userId, ImageStatus.ACTIVATE)) { From 5664a327c87c094a4adaec000f4f5719c710178a Mon Sep 17 00:00:00 2001 From: wwan13 Date: Thu, 1 Aug 2024 18:45:22 +0900 Subject: [PATCH 3/6] =?UTF-8?q?fix=20:=20=EC=A1=B0=EA=B1=B4=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/org/grida/domain/base/AccessManager.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt index 68601cfa..d859627c 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/base/AccessManager.kt @@ -11,7 +11,7 @@ class AccessManager { target: Ownable, accessorId: Long ) { - if (target.isOwner(accessorId)) { + if (!target.isOwner(accessorId)) { throw GridaException(AccessFailed) } } From 756563195e921e3e4510bbfe8d2b4cc67a862f1d Mon Sep 17 00:00:00 2001 From: wwan13 Date: Thu, 1 Aug 2024 19:08:57 +0900 Subject: [PATCH 4/6] =?UTF-8?q?refactor=20:=20=EC=97=90=EB=9F=AC=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=AC=EC=82=AC=EC=9A=A9=20=EA=B0=80=EB=8A=A5?= =?UTF-8?q?=ED=95=98=EA=B2=8C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/grida/domain/profileimage/ProfileImageValidator.kt | 4 ++-- .../src/main/kotlin/org/grida/error/CoreDomainErrorType.kt | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt index f1166290..1a9b3e57 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/profileimage/ProfileImageValidator.kt @@ -1,7 +1,7 @@ package org.grida.domain.profileimage import org.grida.domain.image.ImageStatus -import org.grida.error.ActivateProfileImageAlreadyExists +import org.grida.error.ActivateImageAlreadyExists import org.grida.error.GridaException import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional @@ -14,7 +14,7 @@ class ProfileImageValidator( @Transactional(readOnly = true) fun validateAlreadyHasActivateProfileImage(userId: Long) { if (profileImageRepository.existsByUserIdAndStatus(userId, ImageStatus.ACTIVATE)) { - throw GridaException(ActivateProfileImageAlreadyExists) + throw GridaException(ActivateImageAlreadyExists) } } } diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/error/CoreDomainErrorType.kt b/grida-core/core-domain/src/main/kotlin/org/grida/error/CoreDomainErrorType.kt index a28f360e..c4e930a8 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/error/CoreDomainErrorType.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/error/CoreDomainErrorType.kt @@ -26,10 +26,10 @@ data object UnusableUsername : CoreDomainErrorType { override val logLevel: LogLevel = INFO } -data object ActivateProfileImageAlreadyExists : CoreDomainErrorType { +data object ActivateImageAlreadyExists : CoreDomainErrorType { override val httpStatusCode: Int = BAD_REQUEST - override val errorCode: String = "PROFILE_IMAGE_400_1" - override val message: String = "이미 활성화된 프로필 이미지가 존재합니다." + override val errorCode: String = "IMAGE_400_1" + override val message: String = "이미 활성화된 이미지가 존재합니다." override val logLevel: LogLevel = INFO } From 101cb3d6090ab3d81280c0b4aa5412065e6f3c80 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Thu, 1 Aug 2024 19:10:55 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat=20:=20=EC=9D=BC=EA=B8=B0=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EC=A0=81=EC=9A=A9=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v1/diaryimage/DiaryImageController.kt | 11 ++++++++++ .../org/grida/domain/diaryimage/DiaryImage.kt | 8 ++++++- .../domain/diaryimage/DiaryImageModifier.kt | 22 +++++++++++++++++++ .../domain/diaryimage/DiaryImageReader.kt | 15 +++++++++++++ .../domain/diaryimage/DiaryImageRepository.kt | 7 ++++++ .../domain/diaryimage/DiaryImageService.kt | 17 +++++++++++--- .../domain/diaryimage/DiaryImageValidator.kt | 20 +++++++++++++++++ .../diaryimage/DiaryImageEntity.kt | 7 +++++- .../diaryimage/DiaryImageEntityRepository.kt | 22 +++++++++++++++++-- .../DiaryImageJpaEntityRepository.kt | 11 ++++++++++ 10 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageModifier.kt create mode 100644 grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageReader.kt create mode 100644 grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageValidator.kt 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 5ec48196..abe3986e 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 @@ -24,4 +24,15 @@ class DiaryImageController( val response = IdResponse(generatedDiaryImageId) return ApiResponse.success(response) } + + @PostMapping("/{diaryId}/image/{diaryImageId}") + fun applyDiaryImage( + @RequestUserId userId: Long, + @PathVariable diaryId: Long, + @PathVariable diaryImageId: Long + ): ApiResponse { + diaryImageService.applyDiaryImage(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/DiaryImage.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImage.kt index 55f48a1a..b8211931 100644 --- a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImage.kt +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImage.kt @@ -1,5 +1,6 @@ package org.grida.domain.diaryimage +import org.grida.domain.base.Ownable import org.grida.domain.image.Image data class DiaryImage( @@ -7,4 +8,9 @@ data class DiaryImage( val userId: Long, val diaryId: Long, val image: Image -) +) : Ownable { + + override fun isOwner(accessorId: Long): Boolean { + return userId == accessorId + } +} 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 new file mode 100644 index 00000000..8b19889c --- /dev/null +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageModifier.kt @@ -0,0 +1,22 @@ +package org.grida.domain.diaryimage + +import org.grida.domain.base.AccessManager +import org.grida.domain.image.ImageStatus +import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional + +@Component +class DiaryImageModifier( + private val diaryImageRepository: DiaryImageRepository, + private val diaryImageReader: DiaryImageReader, + private val accessManager: AccessManager +) { + + @Transactional + fun modifyAsActivate(diaryImageId: Long, userId: Long) { + val diaryImage = diaryImageReader.read(diaryImageId) + accessManager.ownerOnly(diaryImage, userId) + + diaryImageRepository.updateStatus(diaryImageId, ImageStatus.ACTIVATE) + } +} diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageReader.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageReader.kt new file mode 100644 index 00000000..c64220c8 --- /dev/null +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageReader.kt @@ -0,0 +1,15 @@ +package org.grida.domain.diaryimage + +import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional + +@Component +class DiaryImageReader( + private val diaryImageRepository: DiaryImageRepository +) { + + @Transactional(readOnly = true) + fun read(diaryId: Long): DiaryImage { + return diaryImageRepository.findById(diaryId) + } +} 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 8157809c..7690b054 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 @@ -1,8 +1,15 @@ package org.grida.domain.diaryimage import org.grida.domain.diary.Diary +import org.grida.domain.image.ImageStatus import org.grida.domain.user.User interface DiaryImageRepository { fun save(diaryImage: DiaryImage, diary: Diary, user: User): Long + + fun findById(id: Long): DiaryImage + + fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean + + fun updateStatus(diaryImageId: Long, status: ImageStatus): Long } \ No newline at end of file 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 034543b4..17d947ca 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 @@ -1,7 +1,7 @@ package org.grida.domain.diaryimage +import org.grida.domain.base.AccessManager import org.grida.domain.diary.DiaryReader -import org.grida.domain.diary.DiaryValidator import org.grida.domain.image.Image import org.grida.domain.image.ImageStatus import org.springframework.stereotype.Service @@ -10,8 +10,10 @@ import org.springframework.stereotype.Service class DiaryImageService( private val diaryImageAppender: DiaryImageAppender, private val diaryImageGenerator: DiaryImageGenerator, + private val diaryImageModifier: DiaryImageModifier, + private val diaryImageValidator: DiaryImageValidator, private val diaryReader: DiaryReader, - private val diaryValidator: DiaryValidator + private val accessManager: AccessManager ) { fun generateDiaryImage( @@ -19,7 +21,7 @@ class DiaryImageService( userId: Long ): Long { val diary = diaryReader.read(diaryId) - diaryValidator.validateIsOwner(diary, userId) + accessManager.ownerOnly(diary, userId) val generatedImageUrl = diaryImageGenerator.generate(diary.content) val diaryImage = DiaryImage( @@ -29,4 +31,13 @@ class DiaryImageService( ) return diaryImageAppender.append(diaryImage, diaryId, userId) } + + fun applyDiaryImage( + diaryImageId: Long, + diaryId: Long, + userId: Long + ) { + diaryImageValidator.validateAlreadyHasActivateDiaryImage(diaryId) + diaryImageModifier.modifyAsActivate(diaryImageId, userId) + } } \ No newline at end of file diff --git a/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageValidator.kt b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageValidator.kt new file mode 100644 index 00000000..f354f350 --- /dev/null +++ b/grida-core/core-domain/src/main/kotlin/org/grida/domain/diaryimage/DiaryImageValidator.kt @@ -0,0 +1,20 @@ +package org.grida.domain.diaryimage + +import org.grida.domain.image.ImageStatus +import org.grida.error.ActivateImageAlreadyExists +import org.grida.error.GridaException +import org.springframework.stereotype.Component +import org.springframework.transaction.annotation.Transactional + +@Component +class DiaryImageValidator( + private val diaryImageRepository: DiaryImageRepository +) { + + @Transactional(readOnly = true) + fun validateAlreadyHasActivateDiaryImage(diaryId: Long) { + if (diaryImageRepository.existsByDiaryIdAndStatus(diaryId, ImageStatus.ACTIVATE)) { + throw GridaException(ActivateImageAlreadyExists) + } + } +} diff --git a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntity.kt b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntity.kt index b701b369..d54734a4 100644 --- a/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntity.kt +++ b/grida-database/database-rds/src/main/kotlin/org/grida/persistence/diaryimage/DiaryImageEntity.kt @@ -39,4 +39,9 @@ class DiaryImageEntity( @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "diary_id") var diary: DiaryEntity, -) : BaseEntity() \ No newline at end of file +) : BaseEntity() { + + fun updateStatue(status: ImageStatus) { + this.status = status + } +} \ 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 2be8d18d..fdd46cb5 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 @@ -3,9 +3,8 @@ package org.grida.persistence.diaryimage import org.grida.domain.diary.Diary import org.grida.domain.diaryimage.DiaryImage import org.grida.domain.diaryimage.DiaryImageRepository +import org.grida.domain.image.ImageStatus import org.grida.domain.user.User -import org.grida.persistence.diary.DiaryEntity -import org.grida.persistence.user.UserEntity import org.springframework.stereotype.Repository import org.springframework.transaction.annotation.Transactional @@ -25,4 +24,23 @@ class DiaryImageEntityRepository( diaryImageJpaEntityRepository.save(diaryEntity) return diaryEntity.id } + + override fun findById(id: Long): DiaryImage { + val diaryImageEntity = diaryImageJpaEntityRepository.findByIdOrException(id) + return diaryImageEntity.toDomain() + } + + override fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean { + return diaryImageJpaEntityRepository.existsByDiaryIdAndStatus(diaryId, status) + } + + @Transactional + override fun updateStatus( + diaryImageId: Long, + status: ImageStatus + ): Long { + val diaryImageEntity = diaryImageJpaEntityRepository.findByIdOrException(diaryImageId) + diaryImageEntity.updateStatue(status) + return diaryImageEntity.id + } } \ No newline at end of file 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 157bfe2a..0cb3492a 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 @@ -1,6 +1,17 @@ package org.grida.persistence.diaryimage +import org.grida.domain.image.ImageStatus +import org.grida.error.GridaException +import org.grida.error.NoSuchData import org.springframework.data.jpa.repository.JpaRepository +fun DiaryImageJpaEntityRepository.findByIdOrException( + id: Long +): DiaryImageEntity { + return this.findById(id).orElseThrow { GridaException(NoSuchData) } +} + interface DiaryImageJpaEntityRepository : JpaRepository { + + fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean } \ No newline at end of file From 240e7d1fecba5232c4d336e2f788008fbfb43897 Mon Sep 17 00:00:00 2001 From: wwan13 Date: Thu, 1 Aug 2024 19:11:13 +0900 Subject: [PATCH 6/6] =?UTF-8?q?docs=20:=20=EC=9D=BC=EA=B8=B0=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EC=A0=81=EC=9A=A9=20API=20=EB=AC=B8?= =?UTF-8?q?=EC=84=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/diaryimage/DiaryImageApiDocsTest.kt | 25 +++++++++++++++++++ 1 file changed, 25 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 17e2aaef..84bb9c35 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 @@ -2,6 +2,8 @@ package org.grida.docs.diaryimage import com.ninjasquad.springmockk.MockkBean import io.mockk.every +import io.mockk.just +import io.mockk.runs import io.wwan13.api.document.snippets.NUMBER import io.wwan13.api.document.snippets.isTypeOf import io.wwan13.api.document.snippets.whichMeans @@ -43,4 +45,27 @@ class DiaryImageApiDocsTest( ) } } + + @Test + fun `일기 이미지 적용 API`() { + every { diaryImageService.applyDiaryImage(any(), any(), any()) } just runs + + val api = api.post("/api/v1/diary/{diaryId}/image/{diaryImageId}", 1L, 1L) { + withBearerToken() + } + + documentFor(api, "apply-diary-image") { + summary("일기 이미지 적용 API") + requestHeaders( + "Authorization" whichMeans "인증 토큰" + ) + pathParameters( + "diaryId" whichMeans "적용하려는 일기의 ID", + "diaryImageId" whichMeans "적용하려는 일기 이미지의 ID", + ) + responseFields( + "data.id" isTypeOf NUMBER whichMeans "적용된 일기 ID" + ) + } + } }