diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/AcademicsController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/AcademicsController.kt index 2c5e6146..36cd8fc3 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/AcademicsController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/AcademicsController.kt @@ -2,6 +2,7 @@ package com.wafflestudio.csereal.core.academics.api import com.wafflestudio.csereal.common.aop.AuthenticatedStaff import com.wafflestudio.csereal.common.enums.LanguageType +import com.wafflestudio.csereal.core.academics.api.req.UpdateSingleReq import com.wafflestudio.csereal.core.academics.dto.* import com.wafflestudio.csereal.core.academics.service.AcademicsService import com.wafflestudio.csereal.core.academics.dto.ScholarshipDto @@ -41,6 +42,15 @@ class AcademicsController( return ResponseEntity.ok(academicsService.readGuide(language, studentType)) } + @AuthenticatedStaff + @PutMapping("/{studentType}/guide") + fun updateGuide( + @RequestParam(required = false, defaultValue = "ko") language: String, + @PathVariable studentType: String, + @RequestPart request: UpdateSingleReq, + @RequestPart newAttachments: List? + ) = academicsService.updateGuide(language, studentType, request, newAttachments) + @GetMapping("/undergraduate/general-studies-requirements") fun readGeneralStudiesRequirements( @RequestParam(required = false, defaultValue = "ko") language: String @@ -95,6 +105,14 @@ class AcademicsController( return ResponseEntity.ok(academicsService.readDegreeRequirements(language)) } + @AuthenticatedStaff + @PutMapping("/undergraduate/degree-requirements") + fun updateDegreeRequirements( + @RequestParam(required = false, defaultValue = "ko") language: String, + @RequestPart request: UpdateSingleReq, + @RequestPart newAttachments: List? + ) = academicsService.updateDegreeRequirements(language, request, newAttachments) + @AuthenticatedStaff @PostMapping("/{studentType}/scholarshipDetail") fun createScholarshipDetail( diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/req/UpdateSingleReq.kt b/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/req/UpdateSingleReq.kt new file mode 100644 index 00000000..4131f8b3 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/academics/api/req/UpdateSingleReq.kt @@ -0,0 +1,6 @@ +package com.wafflestudio.csereal.core.academics.api.req + +data class UpdateSingleReq( + val description: String, + val deleteIds: List +) diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/academics/service/AcademicsService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/academics/service/AcademicsService.kt index 97caf9b4..75e0e157 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/academics/service/AcademicsService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/academics/service/AcademicsService.kt @@ -2,6 +2,7 @@ package com.wafflestudio.csereal.core.academics.service import com.wafflestudio.csereal.common.CserealException import com.wafflestudio.csereal.common.enums.LanguageType +import com.wafflestudio.csereal.core.academics.api.req.UpdateSingleReq import com.wafflestudio.csereal.core.academics.database.* import com.wafflestudio.csereal.core.academics.dto.* import com.wafflestudio.csereal.core.resource.attachment.service.AttachmentService @@ -29,6 +30,7 @@ interface AcademicsService { fun readGeneralStudiesRequirements(language: String): GeneralStudiesRequirementsPageResponse fun readDegreeRequirements(language: String): DegreeRequirementsPageResponse + fun updateDegreeRequirements(language: String, request: UpdateSingleReq, newAttachments: List?) fun createCourse( studentType: String, request: CourseDto, @@ -44,6 +46,12 @@ interface AcademicsService { fun readAllScholarship(language: String, studentType: String): ScholarshipPageResponse fun readScholarship(scholarshipId: Long): ScholarshipDto + fun updateGuide( + language: String, + studentType: String, + request: UpdateSingleReq, + newAttachments: List? + ) } // TODO: add Update, Delete method @@ -103,6 +111,34 @@ class AcademicsServiceImpl( return GuidePageResponse.of(academicsEntity, attachmentResponses) } + @Transactional + override fun updateGuide( + language: String, + studentType: String, + request: UpdateSingleReq, + newAttachments: List? + ) { + val languageType = LanguageType.makeStringToLanguageType(language) + val enumStudentType = makeStringToAcademicsStudentType(studentType) + + val academicsEntity = + academicsRepository.findByLanguageAndStudentTypeAndPostType( + languageType, + enumStudentType, + AcademicsPostType.GUIDE + ) + + academicsEntity.description = request.description + academicsEntity.academicsSearch?.update(academicsEntity) ?: let { + academicsEntity.academicsSearch = AcademicsSearchEntity.create(academicsEntity) + } + + attachmentService.deleteAttachments(request.deleteIds) + if (newAttachments != null) { + attachmentService.uploadAllAttachments(academicsEntity, newAttachments) + } + } + @Transactional(readOnly = true) override fun readAcademicsYearResponses( language: String, @@ -162,6 +198,32 @@ class AcademicsServiceImpl( return DegreeRequirementsPageResponse.of(academicsEntity, attachments) } + @Transactional + override fun updateDegreeRequirements( + language: String, + request: UpdateSingleReq, + newAttachments: List? + ) { + val enumLanguageType = LanguageType.makeStringToLanguageType(language) + + val academicsEntity = + academicsRepository.findByLanguageAndStudentTypeAndPostType( + enumLanguageType, + AcademicsStudentType.UNDERGRADUATE, + AcademicsPostType.DEGREE_REQUIREMENTS + ) + + academicsEntity.description = request.description + academicsEntity.academicsSearch?.update(academicsEntity) ?: let { + academicsEntity.academicsSearch = AcademicsSearchEntity.create(academicsEntity) + } + + attachmentService.deleteAttachments(request.deleteIds) + if (newAttachments != null) { + attachmentService.uploadAllAttachments(academicsEntity, newAttachments) + } + } + @Transactional override fun createCourse( studentType: String, 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 d0c2fbca..fecb4a42 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 @@ -35,10 +35,12 @@ interface AttachmentService { contentEntityType: AttachmentContentEntityType, requestAttachments: List ): List + fun createOneAttachmentResponse(attachment: AttachmentEntity?): AttachmentResponse? fun createAttachmentResponses(attachments: List?): List fun deleteAttachment(attachment: AttachmentEntity) + fun deleteAttachments(ids: List?) fun deleteAttachmentsDeprecated(ids: List?) fun deleteAttachmentDeprecated(attachment: AttachmentEntity) } @@ -174,6 +176,17 @@ class AttachmentServiceImpl( eventPublisher.publishEvent(FileDeleteEvent(fileDirectory)) } + @Transactional + override fun deleteAttachments(ids: List?) { + if (ids != null) { + for (id in ids) { + val attachment = attachmentRepository.findByIdOrNull(id) + ?: throw CserealException.Csereal404("id:${id}인 첨부파일을 찾을 수 없습니다.") + deleteAttachment(attachment) + } + } + } + private fun connectAttachmentToEntity(contentEntity: AttachmentContentEntityType, attachment: AttachmentEntity) { when (contentEntity) { is NewsEntity -> {