diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/research/dto/LabDto.kt b/src/main/kotlin/com/wafflestudio/csereal/core/research/dto/LabDto.kt index 19126f2b..3250be51 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/research/dto/LabDto.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/research/dto/LabDto.kt @@ -2,6 +2,7 @@ package com.wafflestudio.csereal.core.research.dto import com.wafflestudio.csereal.common.properties.LanguageType import com.wafflestudio.csereal.core.research.database.LabEntity +import com.wafflestudio.csereal.core.resource.attachment.dto.AttachmentResponse data class LabDto( val id: Long, @@ -11,14 +12,14 @@ data class LabDto( val location: String?, val tel: String?, val acronym: String?, - val pdf: String?, + val pdf: AttachmentResponse?, val youtube: String?, val group: String, val description: String?, val websiteURL: String? ) { companion object { - fun of(entity: LabEntity, pdfURL: String): LabDto = entity.run { + fun of(entity: LabEntity, pdf: AttachmentResponse?): LabDto = entity.run { LabDto( id = this.id, language = LanguageType.makeLowercase(entity.language), @@ -27,7 +28,7 @@ data class LabDto( location = this.location, tel = this.tel, acronym = this.acronym, - pdf = pdfURL, + pdf = pdf, youtube = this.youtube, group = this.research.name, description = this.description, diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/research/service/ResearchService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/research/service/ResearchService.kt index 4e700af0..023eac57 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/research/service/ResearchService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/research/service/ResearchService.kt @@ -209,28 +209,27 @@ class ResearchServiceImpl( } } - var pdfURL = "" if (pdf != null) { - val attachmentDto = attachmentService.uploadAttachmentInLabEntity(newLab, pdf) - pdfURL = "${endpointProperties.backend}/v1/file/${attachmentDto.filename}" + attachmentService.uploadAttachmentInLabEntity(newLab, pdf) } newLab.researchSearch = ResearchSearchEntity.create(newLab) labRepository.save(newLab) - return LabDto.of(newLab, pdfURL) + val attachmentResponse = + attachmentService.createOneAttachmentResponse(newLab.pdf) + + return LabDto.of(newLab, attachmentResponse) } @Transactional(readOnly = true) override fun readAllLabs(language: String): List { val enumLanguageType = LanguageType.makeStringToLanguageType(language) val labs = labRepository.findAllByLanguageOrderByName(enumLanguageType).map { - var pdfURL = "" - if (it.pdf != null) { - pdfURL = createPdfURL(it.pdf!!) - } - LabDto.of(it, pdfURL) + val attachmentResponse = + attachmentService.createOneAttachmentResponse(it.pdf) + LabDto.of(it, attachmentResponse) } return labs @@ -240,12 +239,11 @@ class ResearchServiceImpl( override fun readLab(labId: Long): LabDto { val lab = labRepository.findByIdOrNull(labId) ?: throw CserealException.Csereal404("해당 연구실을 찾을 수 없습니다.(labId=$labId)") - var pdfURL = "" - if (lab.pdf != null) { - pdfURL = createPdfURL(lab.pdf!!) - } - return LabDto.of(lab, pdfURL) + val attachmentResponse = + attachmentService.createOneAttachmentResponse(lab.pdf) + + return LabDto.of(lab, attachmentResponse) } private fun createPdfURL(pdf: AttachmentEntity): String { @@ -296,12 +294,10 @@ class ResearchServiceImpl( labEntity.researchSearch = ResearchSearchEntity.create(labEntity) } - return LabDto.of( - labEntity, - labEntity.pdf?.let { - createPdfURL(it) - } ?: "" - ) + val attachmentResponse = + attachmentService.createOneAttachmentResponse(labEntity.pdf) + + return LabDto.of(labEntity, attachmentResponse) } @Transactional @@ -339,7 +335,7 @@ class ResearchServiceImpl( labRepository.save(newLab) - list.add(LabDto.of(newLab, "")) + list.add(LabDto.of(newLab, null)) } return list } @@ -372,12 +368,13 @@ class ResearchServiceImpl( val lab = labRepository.findByIdOrNull(labId) ?: throw CserealException.Csereal404("해당 연구실을 찾을 수 없습니다.") - var pdfURL = "" if (pdf != null) { val attachmentDto = attachmentService.uploadAttachmentInLabEntity(lab, pdf) - pdfURL = "${endpointProperties.backend}/v1/file/${attachmentDto.filename}" } - return LabDto.of(lab, pdfURL) + val attachmentResponse = + attachmentService.createOneAttachmentResponse(lab.pdf) + + return LabDto.of(lab, attachmentResponse) } } diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/resource/attachment/service/AttachmentService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/resource/attachment/service/AttachmentService.kt index 4a1f64cf..15d7410e 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/resource/attachment/service/AttachmentService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/resource/attachment/service/AttachmentService.kt @@ -33,7 +33,7 @@ interface AttachmentService { contentEntityType: AttachmentContentEntityType, requestAttachments: List ): List - + fun createOneAttachmentResponse(attachment: AttachmentEntity?): AttachmentResponse? fun createAttachmentResponses(attachments: List?): List fun deleteAttachments(ids: List?) @@ -111,6 +111,23 @@ class AttachmentServiceImpl( return attachmentsList } + @Transactional + override fun createOneAttachmentResponse(attachment: AttachmentEntity?): AttachmentResponse? { + var attachmentDto: AttachmentResponse? = null + if (attachment != null) { + if (attachment.isDeleted == false) { + attachmentDto = AttachmentResponse( + id = attachment.id, + name = attachment.filename.substringAfter("_"), + url = "${endpointProperties.backend}/v1/file/${attachment.filename}", + bytes = attachment.size + ) + } + } + + return attachmentDto + } + @Transactional override fun createAttachmentResponses(attachments: List?): List { val list = mutableListOf()