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

fix: pdf 타입 AttachmentResponse로 변경 #220

Merged
merged 1 commit into from
Mar 15, 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 @@ -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,
Expand All @@ -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),
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LabDto> {
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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -339,7 +335,7 @@ class ResearchServiceImpl(

labRepository.save(newLab)

list.add(LabDto.of(newLab, ""))
list.add(LabDto.of(newLab, null))
}
return list
}
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface AttachmentService {
contentEntityType: AttachmentContentEntityType,
requestAttachments: List<MultipartFile>
): List<AttachmentDto>

fun createOneAttachmentResponse(attachment: AttachmentEntity?): AttachmentResponse?
fun createAttachmentResponses(attachments: List<AttachmentEntity>?): List<AttachmentResponse>

fun deleteAttachments(ids: List<Long>?)
Expand Down Expand Up @@ -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
}

Comment on lines +114 to +130
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createAttachmentResponses랑 겹치는 코드가 많은거 같은데
나중에 OneAttachment만 쓰고 여러개 하는거는 list.map { createoneAttachmentResponse()... }
이런식으로 리펙토링하면 좋을듯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵!

@Transactional
override fun createAttachmentResponses(attachments: List<AttachmentEntity>?): List<AttachmentResponse> {
val list = mutableListOf<AttachmentResponse>()
Expand Down
Loading