Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#34] 슬라이드 이미지 관련 로직 추가 #35

Merged
merged 1 commit into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ class JwtAuthFilter(
} catch (e: PregenException) {
log.error("Filter error: ${e.message}")
handleException(request, response, e)
} catch (e: Exception) {
log.error("Filter error: ${e.message}")
handleException(request, response, e, "인증 과정에서 예외가 발생했습니다.")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class File(
return File(
null,
fileType = IMAGE,
path = path,
path = path.replace(".", ""),
originalName = originalName,
generatedName = generatedName,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ data class FileProperties(
val fullThumbnailPath: String
get() = "$basePath/$thumbnailPath"
.replace("//", "/")
.replace(".", "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.kkeunkkeun.pregen.presentation.file.infrastructure

import org.kkeunkkeun.pregen.presentation.file.domain.File
import org.kkeunkkeun.pregen.presentation.file.service.FileRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Repository

@Repository
Expand All @@ -12,4 +13,8 @@ class FileRepositoryImpl(
override fun save(file: File) {
fileJpaRepository.save(file)
}

override fun findById(fileId: Long?): File? = fileId?.let {
fileJpaRepository.findByIdOrNull(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ import org.kkeunkkeun.pregen.presentation.file.domain.File
interface FileRepository {

fun save(file: File)

fun findById(fileId: Long?): File?
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ data class PresentationListResponse(

val timeLimit: PresentationTime,

val thumbnailPath: String?,

val createdAt: LocalDateTime,

val modifiedAt: LocalDateTime,
) {

companion object {
fun from(presentation: Presentation): ListItem {
fun from(presentation: Presentation, thumbnailPath: String?): ListItem {
val id = presentation.id ?: throw RuntimeException()
val createdAt = presentation.createdAt ?: throw RuntimeException()
val modifiedAt = presentation.modifiedAt ?: throw RuntimeException()

return ListItem(
id,
presentation.title,
presentation.getDDay(),
PresentationTime.from(presentation.timeLimit),
createdAt
thumbnailPath,
createdAt,
modifiedAt,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PresentationResponse {
) {
companion object {
fun from(slides: List<Slide>): List<SlideDetail> {
return slides.map { slide -> SlideDetail(slide.id!!, slide.imageFilePath(), slide.script, slide.memo) }
return slides.map { slide -> SlideDetail(slide.id!!, slide.imageFilePath, slide.script, slide.memo) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PresentationService(
private val presentationRepository: PresentationRepository,
private val accountRepository: AccountRepository,
private val presentationAccessChecker: PresentationAccessChecker,
private val thumbnailResolver: PresentationThumbnailResolver
) {

fun findListByAccountId(email: String, pageable: Pageable): Page<PresentationListResponse.ListItem> {
Expand Down Expand Up @@ -63,6 +64,7 @@ class PresentationService(
}

private fun generateListItem(presentation: Presentation): PresentationListResponse.ListItem {
return PresentationListResponse.ListItem.from(presentation)
val thumbnailPath = thumbnailResolver.findThumbnail(presentation.id!!)
return PresentationListResponse.ListItem.from(presentation, thumbnailPath)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.kkeunkkeun.pregen.presentation.presentation.service

import org.kkeunkkeun.pregen.presentation.practice.service.PracticeRepository
import org.kkeunkkeun.pregen.presentation.slide.service.SlideRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
@Transactional(readOnly = true)
class PresentationThumbnailResolver(
private val practiceRepository: PracticeRepository,
private val slideRepository: SlideRepository,
) {

fun findThumbnail(presentationId: Long): String? {
return practiceRepository.findLatestByPresentationId(presentationId)
.let { practice ->
slideRepository.findFirstByPracticeId(practice.id!!)
}?.imageFilePath
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ class Slide(
var practiceId: Long? = practiceId
protected set

val imageFilePath: String?
get() = imageFile?.absolutePath

companion object {
fun from(practiceId: Long, imageFile: File?, request: PresentationRequest.SlideRequest): Slide {
return Slide(practiceId, imageFile, request.script, request.memo)
}
}

fun imageFilePath(): String? {
return imageFile?.absolutePath
}

fun unmap() {
this.practiceId = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import org.springframework.data.jpa.repository.JpaRepository
interface SlideJpaRepository: JpaRepository<Slide, Long> {

fun findByPracticeId(practiceId: Long): List<Slide>

fun findFirstByPracticeId(practiceId: Long): Slide?
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class SlideRepositoryImpl(
return slideJpaRepository.findByPracticeId(practiceId)
}

override fun findFirstByPracticeId(practiceId: Long): Slide? {
return slideJpaRepository.findFirstByPracticeId(practiceId)
}

override fun saveAll(slides: List<Slide>) {
slideJpaRepository.saveAll(slides)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ interface SlideRepository {

fun findByPracticeId(practiceId: Long): List<Slide>

fun findFirstByPracticeId(practiceId: Long): Slide?

fun saveAll(slides: List<Slide>)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kkeunkkeun.pregen.presentation.slide.service

import org.kkeunkkeun.pregen.presentation.file.service.FileRepository
import org.kkeunkkeun.pregen.presentation.presentation.presentation.PresentationRequest
import org.kkeunkkeun.pregen.presentation.slide.domain.Slide
import org.springframework.stereotype.Service
Expand All @@ -9,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional
@Transactional(readOnly = true)
class SlideService(
private val slideRepository: SlideRepository,
private val fileRepository: FileRepository,
) {

fun findByPracticeId(practiceId: Long): List<Slide> {
Expand All @@ -17,11 +19,19 @@ class SlideService(

@Transactional
fun saveAll(practiceId: Long, slideRequests: List<PresentationRequest.SlideRequest>) {
val slides = slideRequests.map { request -> Slide.from(practiceId, null, request) } // TODO: add file
val slides = slideRequests.map { request -> mapToSlideEntity(practiceId, request) }

slideRepository.saveAll(slides)
}

private fun mapToSlideEntity(practiceId: Long, slideRequest: PresentationRequest.SlideRequest): Slide {
return Slide.from(
practiceId = practiceId,
imageFile = fileRepository.findById(slideRequest.imageFileId),
request = slideRequest
)
}

@Transactional
fun overwrite(practiceId: Long, slideRequests: List<PresentationRequest.SlideRequest>) {
this.findByPracticeId(practiceId)
Expand Down