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

Feat: Conference page 수정 API #99

Merged
merged 18 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 17 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
@@ -1,11 +1,11 @@
package com.wafflestudio.csereal.core.conference.api

import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.core.conference.dto.ConferenceModifyRequest
import com.wafflestudio.csereal.core.conference.dto.ConferencePage
import com.wafflestudio.csereal.core.conference.service.ConferenceService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@RequestMapping("/conference")
@RestController
Expand All @@ -18,4 +18,15 @@ class ConferenceController(
return ResponseEntity.ok(conferenceService.getConferencePage())
}

@AuthenticatedStaff
@PatchMapping("/page/conferences")
fun modifyConferencePage(
@RequestBody conferenceModifyRequest: ConferenceModifyRequest
): ResponseEntity<ConferencePage> {
return ResponseEntity.ok(
conferenceService.modifyConferences(
conferenceModifyRequest
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package com.wafflestudio.csereal.core.conference.database

import com.wafflestudio.csereal.common.config.BaseTimeEntity
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import com.wafflestudio.csereal.core.conference.dto.ConferenceCreateDto
import com.wafflestudio.csereal.core.conference.dto.ConferenceDto
import com.wafflestudio.csereal.core.research.database.ResearchSearchEntity
import jakarta.persistence.*

@Entity(name = "conference")
class ConferenceEntity(
val code: String,
val abbreviation: String,
val name: String,
var isDeleted: Boolean = false,
var code: String,
var abbreviation: String,
var name: String,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "conference_page_id")
val conferencePage: ConferencePageEntity
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "conference_page_id")
val conferencePage: ConferencePageEntity,

) : BaseTimeEntity()
@OneToOne(mappedBy = "conferenceElement", cascade = [CascadeType.ALL], orphanRemoval = true)
var researchSearch: ResearchSearchEntity? = null,
) : BaseTimeEntity() {
companion object {
fun of(
conferenceCreateDto: ConferenceCreateDto,
conferencePage: ConferencePageEntity,
) = ConferenceEntity(
code = conferenceCreateDto.code,
abbreviation = conferenceCreateDto.abbreviation,
name = conferenceCreateDto.name,
conferencePage = conferencePage,
)
}

fun update(conferenceDto: ConferenceDto) {
this.code = conferenceDto.code
this.abbreviation = conferenceDto.abbreviation
this.name = conferenceDto.name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class ConferencePageEntity(

@OneToOne
@JoinColumn(name = "author_id")
val author: UserEntity,
var author: UserEntity,

@OneToMany(mappedBy = "conferencePage")
@OneToMany(mappedBy = "conferencePage", cascade = [CascadeType.ALL], orphanRemoval = true)
@OrderBy("code ASC")
val conferences: List<ConferenceEntity> = mutableListOf()
val conferences: MutableSet<ConferenceEntity> = mutableSetOf()

) : BaseTimeEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.wafflestudio.csereal.core.conference.database

import org.springframework.data.jpa.repository.JpaRepository

interface ConferenceRepository: JpaRepository<ConferenceEntity, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.wafflestudio.csereal.core.conference.dto

data class ConferenceCreateDto (
val code: String,
val abbreviation: String,
val name: String,
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package com.wafflestudio.csereal.core.conference.dto
import com.wafflestudio.csereal.core.conference.database.ConferenceEntity

data class ConferenceDto(
val id: Long,
val code: String,
val abbreviation: String,
val name: String
val name: String,
) {
companion object {
fun of(conferenceEntity: ConferenceEntity): ConferenceDto {
return ConferenceDto(
id = conferenceEntity.id,
code = conferenceEntity.code,
abbreviation = conferenceEntity.abbreviation,
name = conferenceEntity.name
name = conferenceEntity.name,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wafflestudio.csereal.core.conference.dto

data class ConferenceModifyRequest(
val newConferenceList: List<ConferenceCreateDto>,
val modifiedConferenceList: List<ConferenceDto>,
val deleteConfereceIdList: List<Long>
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ data class ConferencePage(
createdAt = conferencePageEntity.createdAt!!,
modifiedAt = conferencePageEntity.modifiedAt!!,
author = conferencePageEntity.author.name,
conferenceList = conferencePageEntity.conferences.map { ConferenceDto.of(it) }
conferenceList = conferencePageEntity.conferences.map {
ConferenceDto.of(it)
}.sortedBy { it.code }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
package com.wafflestudio.csereal.core.conference.service

import com.wafflestudio.csereal.common.CserealException
import com.wafflestudio.csereal.core.conference.database.ConferenceEntity
import com.wafflestudio.csereal.core.conference.database.ConferencePageEntity
import com.wafflestudio.csereal.core.conference.database.ConferencePageRepository
import com.wafflestudio.csereal.core.conference.database.ConferenceRepository
import com.wafflestudio.csereal.core.conference.dto.ConferenceCreateDto
import com.wafflestudio.csereal.core.conference.dto.ConferenceDto
import com.wafflestudio.csereal.core.conference.dto.ConferenceModifyRequest
import com.wafflestudio.csereal.core.conference.dto.ConferencePage
import com.wafflestudio.csereal.core.research.database.ResearchSearchEntity
import com.wafflestudio.csereal.core.research.service.ResearchSearchService
import com.wafflestudio.csereal.core.user.database.UserEntity
import com.wafflestudio.csereal.core.user.database.UserRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.oauth2.core.oidc.user.OidcUser
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.context.request.RequestAttributes
import org.springframework.web.context.request.RequestContextHolder


interface ConferenceService {
fun getConferencePage(): ConferencePage
fun modifyConferences(conferenceModifyRequest: ConferenceModifyRequest): ConferencePage
}

@Service
@Transactional
class ConferenceServiceImpl(
private val conferencePageRepository: ConferencePageRepository
private val conferencePageRepository: ConferencePageRepository,
private val conferenceRepository: ConferenceRepository,
private val userRepository: UserRepository,
private val researchSearchService: ResearchSearchService,
) : ConferenceService {

@Transactional(readOnly = true)
Expand All @@ -23,4 +42,84 @@ class ConferenceServiceImpl(
return ConferencePage.of(conferencePage)
}

}
@Transactional
override fun modifyConferences(conferenceModifyRequest: ConferenceModifyRequest): ConferencePage {
var user = RequestContextHolder.getRequestAttributes()?.getAttribute(
"loggedInUser",
RequestAttributes.SCOPE_REQUEST
) as UserEntity?

if (user == null) {
val oidcUser = SecurityContextHolder.getContext().authentication.principal as OidcUser
val username = oidcUser.idToken.getClaim<String>("username")

user = userRepository.findByUsername(username) ?: throw CserealException.Csereal404("재로그인이 필요합니다.")
}

val conferencePage = conferencePageRepository.findAll()[0]

val newConferenceList = conferenceModifyRequest.newConferenceList.map {
createConferenceWithoutSave(it, conferencePage)
}

val modifiedConferenceList = conferenceModifyRequest.modifiedConferenceList.map {
modifyConferenceWithoutSave(it)
}

val deleteConferenceList = conferenceModifyRequest.deleteConfereceIdList.map {
deleteConference(it, conferencePage)
}

conferencePage.author = user

return ConferencePage.of(conferencePage)
}

@Transactional
fun createConferenceWithoutSave(
conferenceCreateDto: ConferenceCreateDto,
conferencePage: ConferencePageEntity,
): ConferenceEntity {
val newConference = ConferenceEntity.of(
conferenceCreateDto,
conferencePage
)
conferencePage.conferences.add(newConference)

newConference.researchSearch = ResearchSearchEntity.create(newConference)

return newConference
}

@Transactional
fun modifyConferenceWithoutSave(
conferenceDto: ConferenceDto,
): ConferenceEntity {
val conferenceEntity = conferenceRepository.findByIdOrNull(conferenceDto.id)
?: throw CserealException.Csereal404("Conference id:${conferenceDto.id} 가 존재하지 않습니다.")

conferenceEntity.update(conferenceDto)

conferenceEntity.researchSearch?.update(conferenceEntity)
?: let {
conferenceEntity.researchSearch = ResearchSearchEntity.create(conferenceEntity)
}

return conferenceEntity
}

@Transactional
fun deleteConference(
id: Long,
conferencePage: ConferencePageEntity,
) = conferenceRepository.findByIdOrNull(id)
?. let {
it.isDeleted = true
conferencePage.conferences.remove(it)

it.researchSearch?.let {
researchSearchService.deleteResearchSearch(it)
}
it.researchSearch = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.springframework.transaction.annotation.Transactional
interface MemberSearchService {
fun searchTopMember(keyword: String, number: Int): MemberSearchTopResponse
fun searchMember(keyword: String, pageSize: Int, pageNum: Int): MemberSearchPageResponse

}

@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.wafflestudio.csereal.core.research.api

import com.wafflestudio.csereal.common.aop.AuthenticatedStaff
import com.wafflestudio.csereal.core.research.dto.LabDto
import com.wafflestudio.csereal.core.research.dto.LabUpdateRequest
import com.wafflestudio.csereal.core.research.dto.ResearchDto
import com.wafflestudio.csereal.core.research.dto.ResearchGroupResponse
import com.wafflestudio.csereal.core.research.service.ResearchService
Expand Down Expand Up @@ -62,4 +64,17 @@ class ResearchController(
): ResponseEntity<LabDto> {
return ResponseEntity.ok(researchService.readLab(labId))
}

/**
* Research Group 수정은 일단 제외하였음.
*/
@AuthenticatedStaff
@PatchMapping("/lab/{labId}")
fun updateLab(
@PathVariable labId: Long,
@Valid @RequestPart("request") request: LabUpdateRequest,
@RequestPart("pdf") pdf: MultipartFile?
): ResponseEntity<LabDto> {
return ResponseEntity.ok(researchService.updateLab(labId, request, pdf))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@ import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.core.member.database.ProfessorEntity
import com.wafflestudio.csereal.core.research.dto.LabDto
import com.wafflestudio.csereal.core.research.dto.LabUpdateRequest
import com.wafflestudio.csereal.core.resource.attachment.database.AttachmentEntity
import jakarta.persistence.*

@Entity(name = "lab")
class LabEntity(
val name: String,
var name: String,

@OneToMany(mappedBy = "lab")
val professors: MutableSet<ProfessorEntity> = mutableSetOf(),

val location: String?,
val tel: String?,
val acronym: String?,
var location: String?,
var tel: String?,
var acronym: String?,

@OneToOne
var pdf: AttachmentEntity? = null,

val youtube: String?,
var youtube: String?,

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

val description: String?,
val websiteURL: String?,
var description: String?,
var websiteURL: String?,

@OneToOne(mappedBy = "lab", cascade = [CascadeType.ALL], orphanRemoval = true)
var researchSearch: ResearchSearchEntity? = null,

) : BaseTimeEntity() {
companion object {
Expand All @@ -45,4 +49,14 @@ class LabEntity(
)
}
}

fun updateWithoutProfessor(labUpdateRequest: LabUpdateRequest) {
this.name = labUpdateRequest.name
this.location = labUpdateRequest.location
this.tel = labUpdateRequest.tel
this.acronym = labUpdateRequest.acronym
this.youtube = labUpdateRequest.youtube
this.description = labUpdateRequest.description
this.websiteURL = labUpdateRequest.websiteURL
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ResearchEntity(
@OneToMany(mappedBy = "research", cascade = [CascadeType.ALL], orphanRemoval = true)
var attachments: MutableList<AttachmentEntity> = mutableListOf(),

@OneToOne(mappedBy = "research", cascade = [CascadeType.ALL], orphanRemoval = true)
var researchSearch: ResearchSearchEntity? = null,
) : BaseTimeEntity(), MainImageContentEntityType, AttachmentContentEntityType {
override fun bringMainImage() = mainImage
override fun bringAttachments() = attachments
Expand All @@ -38,4 +40,11 @@ class ResearchEntity(
)
}
}

fun updateWithoutLabImageAttachment(researchDto: ResearchDto) {
this.postType = researchDto.postType
this.name = researchDto.name
this.description = researchDto.description
}

}
Loading