Skip to content

Commit

Permalink
Merge pull request #101 from grida-diary/main
Browse files Browse the repository at this point in the history
Docs
  • Loading branch information
wwan13 authored Aug 2, 2024
2 parents 13359a5 + 8c96afa commit d48f06f
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.grida.api.ApiResponse
import org.grida.api.dto.IdResponse
import org.grida.domain.diary.DiaryScope
import org.grida.domain.diary.DiaryService
import org.grida.domain.diaryimage.DiaryImageService
import org.grida.presentation.v1.diary.dto.DiaryModifyRequest
import org.grida.presentation.v1.diary.dto.DiaryRequest
import org.grida.presentation.v1.diary.dto.DiaryResponse
Expand All @@ -20,7 +21,8 @@ import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/api/v1/diary")
class DiaryController(
private val diaryService: DiaryService
private val diaryService: DiaryService,
private val diaryImageService: DiaryImageService
) {

@PostMapping
Expand All @@ -39,7 +41,8 @@ class DiaryController(
@PathVariable diaryId: Long
): ApiResponse<DiaryResponse> {
val diary = diaryService.readDiary(diaryId, userId)
val response = DiaryResponse.from(diary)
val remainCount = diaryImageService.countRemainImageGenerateAttempt(diaryId)
val response = DiaryResponse.from(diary, remainCount)
return ApiResponse.success(response)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ data class DiaryResponse(
val content: String,
val targetDate: String,
val scope: String,
val createdAt: String
val createdAt: String,
val remainAttempt: Long
) {

companion object {
fun from(diary: Diary): DiaryResponse {
fun from(
diary: Diary,
remainAttempt: Long
): DiaryResponse {
return DiaryResponse(
content = diary.content,
targetDate = DateTimeUtil.toDefaultDateFormat(diary.targetDate),
scope = diary.scope.name,
createdAt = DateTimeUtil.toDefaultDateTimeFormat(diary.timestamp.createdAt)
createdAt = DateTimeUtil.toDefaultDateTimeFormat(diary.timestamp.createdAt),
remainAttempt = remainAttempt
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.grida.docs.ApiDocsTest
import org.grida.domain.diary.Diary
import org.grida.domain.diary.DiaryScope
import org.grida.domain.diary.DiaryService
import org.grida.domain.diaryimage.DiaryImageService
import org.grida.presentation.v1.diary.DiaryController
import org.grida.presentation.v1.diary.dto.DiaryModifyRequest
import org.grida.presentation.v1.diary.dto.DiaryRequest
Expand All @@ -32,6 +33,9 @@ class DiaryApiDocsTest(
@MockkBean
private lateinit var diaryService: DiaryService

@MockkBean
private lateinit var diaryImageService: DiaryImageService

@Test
fun `일기 생성 API`() {
every { diaryService.appendDiary(any()) } returns 1L
Expand Down Expand Up @@ -74,6 +78,7 @@ class DiaryApiDocsTest(
)

every { diaryService.readDiary(any(), any()) } returns diary
every { diaryImageService.countRemainImageGenerateAttempt(any()) } returns 2L

val api = api.get("/api/v1/diary/{diaryId}", 1L) {
withBearerToken()
Expand All @@ -89,6 +94,7 @@ class DiaryApiDocsTest(
"data.targetDate" isTypeOf DATE whichMeans "대상 날짜",
"data.scope" isTypeOf ENUM(DiaryScope::class) whichMeans "일기 공개 범위",
"data.createdAt" isTypeOf DATETIME whichMeans "생성 시간",
"data.remainAttempt" isTypeOf NUMBER whichMeans "일기 이미지 재생성 가능 횟수",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ data class DiaryImage(
override fun isOwner(accessorId: Long): Boolean {
return userId == accessorId
}

companion object {
const val IMAGE_GENERATE_MAX_ATTEMPT_COUNT = 3L
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ class DiaryImageReader(
) {

@Transactional(readOnly = true)
fun read(diaryId: Long): DiaryImage {
return diaryImageRepository.findById(diaryId)
fun read(diaryImageId: Long): DiaryImage {
return diaryImageRepository.findById(diaryImageId)
}

@Transactional(readOnly = true)
fun countGeneratedImages(diaryId: Long): Long {
return diaryImageRepository.countByDiaryId(diaryId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface DiaryImageRepository {

fun findById(id: Long): DiaryImage

fun countByDiaryId(diaryId: Long): Long

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

fun updateStatus(diaryImageId: Long, status: ImageStatus): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import org.springframework.stereotype.Service
@Service
class DiaryImageService(
private val diaryImageAppender: DiaryImageAppender,
private val diaryImageGenerator: DiaryImageGenerator,
private val diaryImageReader: DiaryImageReader,
private val diaryImageModifier: DiaryImageModifier,
private val diaryImageValidator: DiaryImageValidator,
private val diaryImageGenerator: DiaryImageGenerator,
private val diaryReader: DiaryReader,
private val accessManager: AccessManager
) {
Expand All @@ -20,18 +21,26 @@ class DiaryImageService(
diaryId: Long,
userId: Long
): Long {
diaryImageValidator.validateRemainingAttemptCount(diaryId)

val diary = diaryReader.read(diaryId)
accessManager.ownerOnly(diary, userId)
val generatedImageUrl = diaryImageGenerator.generate(diary.content)

val generatedImageUrl = diaryImageGenerator.generate(diary.content)
val diaryImage = DiaryImage(
userId = userId,
diaryId = diaryId,
image = Image(generatedImageUrl, ImageStatus.DEACTIVATE)
)

return diaryImageAppender.append(diaryImage, diaryId, userId)
}

fun countRemainImageGenerateAttempt(diaryId: Long): Long {
val generatedImagesCount = diaryImageReader.countGeneratedImages(diaryId)
return DiaryImage.IMAGE_GENERATE_MAX_ATTEMPT_COUNT - generatedImagesCount
}

fun applyDiaryImage(
diaryImageId: Long,
diaryId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package org.grida.domain.diaryimage
import org.grida.domain.image.ImageStatus
import org.grida.error.ActivateImageAlreadyExists
import org.grida.error.GridaException
import org.grida.error.ImageGenerateAttemptOver
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
class DiaryImageValidator(
private val diaryImageRepository: DiaryImageRepository
private val diaryImageRepository: DiaryImageRepository,
private val diaryImageReader: DiaryImageReader
) {

@Transactional(readOnly = true)
Expand All @@ -17,4 +19,12 @@ class DiaryImageValidator(
throw GridaException(ActivateImageAlreadyExists)
}
}

@Transactional(readOnly = true)
fun validateRemainingAttemptCount(diaryId: Long) {
val generatedImagesCount = diaryImageReader.countGeneratedImages(diaryId)
if (generatedImagesCount >= DiaryImage.IMAGE_GENERATE_MAX_ATTEMPT_COUNT) {
throw GridaException(ImageGenerateAttemptOver)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ data object ActivateImageAlreadyExists : CoreDomainErrorType {
override val logLevel: LogLevel = INFO
}

data object ImageGenerateAttemptOver : CoreDomainErrorType {
override val httpStatusCode: Int = BAD_REQUEST
override val errorCode: String = "IMAGE_400_2"
override val message: String = "이미지 생성 시도 횟수를 초과하였습니다."
override val logLevel: LogLevel = INFO
}

data object PasswordConfirmNotMatched : CoreDomainErrorType {
override val httpStatusCode: Int = BAD_REQUEST
override val errorCode: String = "AUTH_400_1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class DiaryImageEntityRepository(
return diaryImageEntity.toDomain()
}

override fun countByDiaryId(diaryId: Long): Long {
return diaryImageJpaEntityRepository.countByDiaryId(diaryId)
}

override fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean {
return diaryImageJpaEntityRepository.existsByDiaryIdAndStatus(diaryId, status)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ fun DiaryImageJpaEntityRepository.findByIdOrException(

interface DiaryImageJpaEntityRepository : JpaRepository<DiaryImageEntity, Long> {

fun countByDiaryId(diaryId: Long): Long

fun existsByDiaryIdAndStatus(diaryId: Long, status: ImageStatus): Boolean
}

0 comments on commit d48f06f

Please sign in to comment.