Skip to content

Commit

Permalink
fix: researchDetail에 사진, 첨부파일 업로드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
skfotakf committed Sep 1, 2023
1 parent b5f6a6b commit 2975ebd
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.wafflestudio.csereal.common.controller

import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity

interface ImageContentEntityType {
interface MainImageContentEntityType {
fun bringMainImage(): MainImageEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.wafflestudio.csereal.core.about.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.common.controller.ImageContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.core.about.dto.AboutDto
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
Expand All @@ -26,7 +26,7 @@ class AboutEntity(
@OneToOne
var mainImage: MainImageEntity? = null,

) : BaseTimeEntity(), ImageContentEntityType, AttachmentContentEntityType {
) : BaseTimeEntity(), MainImageContentEntityType, AttachmentContentEntityType {
override fun bringMainImage(): MainImageEntity? = mainImage
override fun bringAttachments(): List<AttachmentEntity> = attachments

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wafflestudio.csereal.core.member.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.ImageContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.core.member.dto.ProfessorDto
import com.wafflestudio.csereal.core.research.database.LabEntity
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
Expand Down Expand Up @@ -44,7 +44,7 @@ class ProfessorEntity(
@OneToOne
var mainImage: MainImageEntity? = null,

) : BaseTimeEntity(), ImageContentEntityType {
) : BaseTimeEntity(), MainImageContentEntityType {
override fun bringMainImage(): MainImageEntity? = mainImage

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.wafflestudio.csereal.core.member.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.ImageContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.core.member.dto.StaffDto
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
import jakarta.persistence.CascadeType
Expand All @@ -24,7 +24,7 @@ class StaffEntity(
@OneToOne
var mainImage: MainImageEntity? = null,

) : BaseTimeEntity(), ImageContentEntityType {
) : BaseTimeEntity(), MainImageContentEntityType {
override fun bringMainImage(): MainImageEntity? = mainImage

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.wafflestudio.csereal.core.news.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.common.controller.ImageContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.core.news.dto.NewsDto
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
Expand Down Expand Up @@ -30,7 +30,7 @@ class NewsEntity(
@OneToMany(mappedBy = "news", cascade = [CascadeType.ALL])
var newsTags: MutableSet<NewsTagEntity> = mutableSetOf()

): BaseTimeEntity(), ImageContentEntityType, AttachmentContentEntityType {
): BaseTimeEntity(), MainImageContentEntityType, AttachmentContentEntityType {
override fun bringMainImage() = mainImage
override fun bringAttachments() = attachments

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,50 @@ class ResearchController(
) {
@PostMapping
fun createResearchDetail(
@Valid @RequestBody request: ResearchDto
) : ResponseEntity<ResearchDto> {
return ResponseEntity.ok(researchService.createResearchDetail(request))
@Valid @RequestPart("request") request: ResearchDto,
@RequestPart("mainImage") mainImage: MultipartFile?,
@RequestPart("attachments") attachments: List<MultipartFile>?
): ResponseEntity<ResearchDto> {
return ResponseEntity.ok(researchService.createResearchDetail(request, mainImage, attachments))
}

@GetMapping("/groups")
fun readAllResearchGroups() : ResponseEntity<ResearchGroupResponse> {
fun readAllResearchGroups(): ResponseEntity<ResearchGroupResponse> {
return ResponseEntity.ok(researchService.readAllResearchGroups())
}

@GetMapping("/centers")
fun readAllResearchCenters() : ResponseEntity<List<ResearchDto>> {
fun readAllResearchCenters(): ResponseEntity<List<ResearchDto>> {
return ResponseEntity.ok(researchService.readAllResearchCenters())
}

@PatchMapping("/{researchId}")
fun updateResearchDetail(
@PathVariable researchId: Long,
@Valid @RequestBody request: ResearchDto
) : ResponseEntity<ResearchDto> {
return ResponseEntity.ok(researchService.updateResearchDetail(researchId, request))
@Valid @RequestPart("request") request: ResearchDto,
@RequestPart("mainImage") mainImage: MultipartFile?,
@RequestPart("attachments") attachments: List<MultipartFile>?
): ResponseEntity<ResearchDto> {
return ResponseEntity.ok(researchService.updateResearchDetail(researchId, request, mainImage, attachments))
}

@PostMapping("/lab")
fun createLab(
@Valid @RequestPart("request") request: LabDto,
@RequestPart("pdf") pdf: MultipartFile?
) : ResponseEntity<LabDto> {
): ResponseEntity<LabDto> {
return ResponseEntity.ok(researchService.createLab(request, pdf))
}

@GetMapping("/labs")
fun readAllLabs() : ResponseEntity<List<LabDto>> {
fun readAllLabs(): ResponseEntity<List<LabDto>> {
return ResponseEntity.ok(researchService.readAllLabs())
}

@GetMapping("/lab/{labId}")
fun readLab(
@PathVariable labId: Long,
) : ResponseEntity<LabDto> {
): ResponseEntity<LabDto> {
return ResponseEntity.ok(researchService.readLab(labId))
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.wafflestudio.csereal.core.research.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
import com.wafflestudio.csereal.core.research.dto.ResearchDto
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.mainImage.database.MainImageEntity
import jakarta.persistence.*

@Entity(name = "research")
Expand All @@ -11,18 +15,26 @@ class ResearchEntity(

var name: String,
var description: String?,
var websiteURL: String?,

@OneToMany(mappedBy = "research", cascade = [CascadeType.ALL], orphanRemoval = true)
var labs: MutableList<LabEntity> = mutableListOf()
): BaseTimeEntity() {
var labs: MutableList<LabEntity> = mutableListOf(),

@OneToOne
var mainImage: MainImageEntity? = null,

@OneToMany(mappedBy = "research", cascade = [CascadeType.ALL], orphanRemoval = true)
var attachments: MutableList<AttachmentEntity> = mutableListOf(),

) : BaseTimeEntity(), MainImageContentEntityType, AttachmentContentEntityType {
override fun bringMainImage() = mainImage
override fun bringAttachments() = attachments

companion object {
fun of(researchDto: ResearchDto) : ResearchEntity {
fun of(researchDto: ResearchDto): ResearchEntity {
return ResearchEntity(
postType = researchDto.postType,
name = researchDto.name,
description = researchDto.description,
websiteURL = researchDto.websiteURL,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@ package com.wafflestudio.csereal.core.research.dto

import com.wafflestudio.csereal.core.research.database.ResearchEntity
import com.wafflestudio.csereal.core.research.database.ResearchPostType
import com.wafflestudio.csereal.core.resource.attachment.dto.AttachmentResponse
import java.time.LocalDateTime

data class ResearchDto(
val id: Long,
val postType: ResearchPostType,
val name: String,
val description: String?,
val websiteURL: String?,
val createdAt: LocalDateTime?,
val modifiedAt: LocalDateTime?,
val labs: List<ResearchLabResponse>?
val labs: List<ResearchLabResponse>?,
val imageURL: String?,
val attachments: List<AttachmentResponse>?,
) {
companion object {
fun of(entity: ResearchEntity) = entity.run {
fun of(entity: ResearchEntity, imageURL: String?, attachmentResponse: List<AttachmentResponse>) = entity.run {
ResearchDto(
id = this.id,
postType = this.postType,
name = this.name,
description = this.description,
websiteURL = this.websiteURL,
createdAt = this.createdAt,
modifiedAt = this.modifiedAt,
labs = this.labs.map { ResearchLabResponse(id = it.id, name = it.name) }
labs = this.labs.map { ResearchLabResponse(id = it.id, name = it.name) },
imageURL = imageURL,
attachments = attachmentResponse
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import com.wafflestudio.csereal.core.research.database.*
import com.wafflestudio.csereal.core.research.dto.*
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.attachment.service.AttachmentService
import com.wafflestudio.csereal.core.resource.mainImage.service.MainImageService
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile

interface ResearchService {
fun createResearchDetail(request: ResearchDto): ResearchDto
fun createResearchDetail(request: ResearchDto, mainImage: MultipartFile?, attachments: List<MultipartFile>?): ResearchDto
fun readAllResearchGroups(): ResearchGroupResponse
fun readAllResearchCenters(): List<ResearchDto>
fun updateResearchDetail(researchId: Long, request: ResearchDto): ResearchDto
fun updateResearchDetail(researchId: Long, request: ResearchDto, mainImage: MultipartFile?, attachments: List<MultipartFile>?): ResearchDto
fun createLab(request: LabDto, pdf: MultipartFile?): LabDto
fun readAllLabs(): List<LabDto>
fun readLab(labId: Long): LabDto
Expand All @@ -26,11 +27,13 @@ class ResearchServiceImpl(
private val researchRepository: ResearchRepository,
private val labRepository: LabRepository,
private val professorRepository: ProfessorRepository,
private val mainImageService: MainImageService,
private val attachmentService: AttachmentService,
) : ResearchService {
@Transactional
override fun createResearchDetail(request: ResearchDto): ResearchDto {
override fun createResearchDetail(request: ResearchDto, mainImage: MultipartFile?, attachments: List<MultipartFile>?): ResearchDto {
val newResearch = ResearchEntity.of(request)

if(request.labs != null) {

for(lab in request.labs) {
Expand All @@ -41,9 +44,20 @@ class ResearchServiceImpl(
}
}

if(mainImage != null) {
mainImageService.uploadMainImage(newResearch, mainImage)
}

if(attachments != null) {
attachmentService.uploadAllAttachments(newResearch, attachments)
}

researchRepository.save(newResearch)

return ResearchDto.of(newResearch)
val imageURL = mainImageService.createImageURL(newResearch.mainImage)
val attachmentResponses = attachmentService.createAttachmentResponses(newResearch.attachments)

return ResearchDto.of(newResearch, imageURL, attachmentResponses)
}

@Transactional(readOnly = true)
Expand All @@ -55,7 +69,10 @@ class ResearchServiceImpl(
"오늘도 인류가 꿈꾸는 행복하고 편리한 세상을 위해 변화와 혁신, 연구와 도전을 계속하고 있습니다."

val researchGroups = researchRepository.findAllByPostTypeOrderByName(ResearchPostType.GROUPS).map {
ResearchDto.of(it)
val imageURL = mainImageService.createImageURL(it.mainImage)
val attachmentResponses = attachmentService.createAttachmentResponses(it.attachments)

ResearchDto.of(it, imageURL, attachmentResponses)
}

return ResearchGroupResponse(description, researchGroups)
Expand All @@ -64,13 +81,16 @@ class ResearchServiceImpl(
@Transactional(readOnly = true)
override fun readAllResearchCenters(): List<ResearchDto> {
val researchCenters = researchRepository.findAllByPostTypeOrderByName(ResearchPostType.CENTERS).map {
ResearchDto.of(it)
val imageURL = mainImageService.createImageURL(it.mainImage)
val attachmentResponses = attachmentService.createAttachmentResponses(it.attachments)

ResearchDto.of(it, imageURL, attachmentResponses)
}

return researchCenters
}
@Transactional
override fun updateResearchDetail(researchId: Long, request: ResearchDto): ResearchDto {
override fun updateResearchDetail(researchId: Long, request: ResearchDto, mainImage: MultipartFile?, attachments: List<MultipartFile>?): ResearchDto {
val research = researchRepository.findByIdOrNull(researchId)
?: throw CserealException.Csereal404("해당 게시글을 찾을 수 없습니다.(researchId=$researchId)")

Expand All @@ -96,7 +116,19 @@ class ResearchServiceImpl(
}
}

return ResearchDto.of(research)
if(mainImage != null) {
mainImageService.uploadMainImage(research, mainImage)
}

if(attachments != null) {
attachmentService.uploadAllAttachments(research, attachments)
}

val imageURL = mainImageService.createImageURL(research.mainImage)
val attachmentResponses = attachmentService.createAttachmentResponses(research.attachments)


return ResearchDto.of(research, imageURL, attachmentResponses)
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.wafflestudio.csereal.core.academics.database.AcademicsEntity
import com.wafflestudio.csereal.core.academics.database.CourseEntity
import com.wafflestudio.csereal.core.news.database.NewsEntity
import com.wafflestudio.csereal.core.research.database.LabEntity
import com.wafflestudio.csereal.core.research.database.ResearchEntity
import com.wafflestudio.csereal.core.seminar.database.SeminarEntity
import jakarta.persistence.*

Expand Down Expand Up @@ -42,6 +43,10 @@ class AttachmentEntity(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "lab_id")
var lab: LabEntity? = null,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "research_id")
var research: ResearchEntity? = null,
) : BaseTimeEntity() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.wafflestudio.csereal.core.academics.database.AcademicsEntity
import com.wafflestudio.csereal.core.academics.database.CourseEntity
import com.wafflestudio.csereal.core.news.database.NewsEntity
import com.wafflestudio.csereal.core.research.database.LabEntity
import com.wafflestudio.csereal.core.research.database.ResearchEntity
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentRepository
import com.wafflestudio.csereal.core.resource.attachment.dto.AttachmentDto
Expand Down Expand Up @@ -142,6 +143,10 @@ class AttachmentServiceImpl(
contentEntity.attachments.add(attachment)
attachment.course = contentEntity
}
is ResearchEntity -> {
contentEntity.attachments.add(attachment)
attachment.research = contentEntity
}
}
}
}
Loading

0 comments on commit 2975ebd

Please sign in to comment.