Skip to content

Commit

Permalink
Merge pull request #104 from grida-diary/main
Browse files Browse the repository at this point in the history
Docs
  • Loading branch information
wwan13 authored Aug 3, 2024
2 parents d48f06f + 374bc9f commit ef15f42
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IdResponse> {
diaryImageService.changeDiaryImage(diaryImageId, diaryId, userId)
val response = IdResponse(diaryId)
return ApiResponse.success(response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ 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
): DiaryImageEntity {
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<DiaryImageEntity, Long> {

fun findByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Optional<DiaryImageEntity>

fun countByDiaryId(diaryId: Long): Long

fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean
Expand Down

0 comments on commit ef15f42

Please sign in to comment.