From 8c51eb00838544b66d055467ee1f6c51bdcc07f1 Mon Sep 17 00:00:00 2001 From: leeeryboy Date: Thu, 29 Aug 2024 00:51:56 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=8F=99=EC=95=84=EB=A6=AC=20id=20?= =?UTF-8?q?=EA=B8=B0=EC=A4=80=EC=9C=BC=EB=A1=9C=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/about/api/req/CreateClubReq.kt | 20 ++++++ .../about/api/{ => v1}/AboutController.kt | 24 +------ .../core/about/api/v2/AboutController.kt | 35 ++++++++++ .../core/about/service/AboutService.kt | 68 ++++++++++++------- 4 files changed, 101 insertions(+), 46 deletions(-) rename src/main/kotlin/com/wafflestudio/csereal/core/about/api/{ => v1}/AboutController.kt (88%) create mode 100644 src/main/kotlin/com/wafflestudio/csereal/core/about/api/v2/AboutController.kt diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/about/api/req/CreateClubReq.kt b/src/main/kotlin/com/wafflestudio/csereal/core/about/api/req/CreateClubReq.kt index dc717e4e..66257510 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/about/api/req/CreateClubReq.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/about/api/req/CreateClubReq.kt @@ -1,5 +1,7 @@ package com.wafflestudio.csereal.core.about.api.req +import com.wafflestudio.csereal.core.about.database.AboutEntity + data class CreateClubReq( val ko: ClubDto, val en: ClubDto @@ -9,3 +11,21 @@ data class ClubDto( val name: String, val description: String ) + +data class GroupedClubDto( + val ko: ClubDtoWithId, + val en: ClubDtoWithId +) + +data class ClubDtoWithId( + val id: Long, + val name: String, + val description: String, + val imageURL: String? +) { + companion object { + fun of(aboutEntity: AboutEntity, imageURL: String?): ClubDtoWithId { + return ClubDtoWithId(aboutEntity.id, aboutEntity.name!!, aboutEntity.description, imageURL) + } + } +} diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/about/api/AboutController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/about/api/v1/AboutController.kt similarity index 88% rename from src/main/kotlin/com/wafflestudio/csereal/core/about/api/AboutController.kt rename to src/main/kotlin/com/wafflestudio/csereal/core/about/api/v1/AboutController.kt index 030cd802..be70e59c 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/about/api/AboutController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/about/api/v1/AboutController.kt @@ -1,4 +1,4 @@ -package com.wafflestudio.csereal.core.about.api +package com.wafflestudio.csereal.core.about.api.v1 import com.wafflestudio.csereal.common.aop.AuthenticatedStaff import com.wafflestudio.csereal.common.enums.LanguageType @@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.* import org.springframework.web.multipart.MultipartFile @RequestMapping("/api/v1/about") -@RestController +@RestController("AboutControllerV1") class AboutController( private val aboutService: AboutService ) { @@ -41,25 +41,7 @@ class AboutController( @RequestPart newAttachments: List? ) = aboutService.updateAbout(postType, request, newMainImage, newAttachments) - @AuthenticatedStaff - @PostMapping("/student-clubs") - fun createClub( - @RequestPart request: CreateClubReq, - @RequestPart mainImage: MultipartFile? - ) = aboutService.createClub(request, mainImage) - - @AuthenticatedStaff - @PutMapping("/student-clubs/{engName}") - fun updateClub( - @PathVariable("engName") name: String, - @RequestPart request: UpdateDescriptionReq, - @RequestPart newMainImage: MultipartFile? - ) = aboutService.updateClub(name, request, newMainImage) - - @AuthenticatedStaff - @DeleteMapping("/student-clubs/{engName}") - fun deleteClub(@PathVariable("engName") name: String) = aboutService.deleteClub(name) - + @Deprecated("Use V2 API") @GetMapping("/student-clubs") fun readAllClubs( @RequestParam(required = false, defaultValue = "ko") language: String diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/about/api/v2/AboutController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/about/api/v2/AboutController.kt new file mode 100644 index 00000000..37fa1fb6 --- /dev/null +++ b/src/main/kotlin/com/wafflestudio/csereal/core/about/api/v2/AboutController.kt @@ -0,0 +1,35 @@ +package com.wafflestudio.csereal.core.about.api.v2 + +import com.wafflestudio.csereal.common.aop.AuthenticatedStaff +import com.wafflestudio.csereal.core.about.api.req.CreateClubReq +import com.wafflestudio.csereal.core.about.api.req.GroupedClubDto +import com.wafflestudio.csereal.core.about.service.AboutService +import org.springframework.web.bind.annotation.* +import org.springframework.web.multipart.MultipartFile + +@RequestMapping("/api/v2/about") +@RestController +class AboutController( + private val aboutService: AboutService +) { + @GetMapping("/student-clubs") + fun readAllClubs(): List = aboutService.readAllGroupedClubs() + + @AuthenticatedStaff + @PostMapping("/student-clubs") + fun createClub( + @RequestPart request: CreateClubReq, + @RequestPart mainImage: MultipartFile? + ) = aboutService.createClub(request, mainImage) + + @AuthenticatedStaff + @PutMapping("/student-clubs") + fun updateClub( + @RequestPart request: GroupedClubDto, + @RequestPart newMainImage: MultipartFile? + ) = aboutService.updateClub(request, newMainImage) + + @AuthenticatedStaff + @DeleteMapping("/student-clubs/{id}") + fun deleteClub(@PathVariable id: Long) = aboutService.deleteClub(id) +} diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/about/service/AboutService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/about/service/AboutService.kt index fe5f3c64..01ab319e 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/about/service/AboutService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/about/service/AboutService.kt @@ -27,10 +27,11 @@ interface AboutService { ) fun createClub(request: CreateClubReq, mainImage: MultipartFile?) - fun updateClub(name: String, request: UpdateDescriptionReq, newMainImage: MultipartFile?) - fun deleteClub(name: String) + fun updateClub(request: GroupedClubDto, newMainImage: MultipartFile?) + fun deleteClub(id: Long) fun readAllClubs(language: String): List + fun readAllGroupedClubs(): List fun createFacilities(request: CreateFacReq, mainImage: MultipartFile?) fun updateFacility(id: Long, request: CreateFacReq, newMainImage: MultipartFile?) fun deleteFacility(id: Long) @@ -122,7 +123,7 @@ class AboutServiceImpl( AboutEntity( AboutPostType.STUDENT_CLUBS, lang, - req.name + "(${request.en.name})", + req.name, req.description, searchContent = "" ).apply { syncSearchContent() } @@ -132,43 +133,50 @@ class AboutServiceImpl( clubs.forEach { mainImageService.uploadMainImage(it, mainImage) } } - clubs.forEach { aboutRepository.save(it) } + aboutLanguageRepository.save(AboutLanguageEntity(clubs[0], clubs[1])) } @Transactional - override fun updateClub(name: String, request: UpdateDescriptionReq, newMainImage: MultipartFile?) { - val lang = listOf(LanguageType.KO to request.koDescription, LanguageType.EN to request.enDescription) - val clubs = lang.map { - aboutRepository.findByLanguageAndPostTypeAndNameContaining(it.first, AboutPostType.STUDENT_CLUBS, name) - .apply { - description = it.second - syncSearchContent() - } + override fun updateClub(request: GroupedClubDto, newMainImage: MultipartFile?) { + val (ko, en) = listOf(request.ko.id, request.en.id).map { id -> + aboutRepository.findByIdOrNull(id) ?: throw CserealException.Csereal404("club not found") + } + + if (ko.language != LanguageType.KO || en.language != LanguageType.EN) { + throw CserealException.Csereal400("language doesn't match") + } + + listOf(ko to request.ko, en to request.en).forEach { (club, clubDto) -> + updateClubDetails(club, clubDto) } if (newMainImage != null) { - clubs.forEach { - it.mainImage?.let { image -> mainImageService.removeImage(image) } - mainImageService.uploadMainImage(it, newMainImage) + listOf(ko, en).forEach { club -> + club.mainImage?.let { image -> mainImageService.removeImage(image) } + mainImageService.uploadMainImage(club, newMainImage) } } } + private fun updateClubDetails(club: AboutEntity, clubDto: ClubDtoWithId) { + club.name = clubDto.name + club.description = clubDto.description + club.syncSearchContent() + } + @Transactional - override fun deleteClub(name: String) { - val lang = listOf(LanguageType.KO, LanguageType.EN) - val clubs = lang.map { - aboutRepository.findByLanguageAndPostTypeAndNameContaining( - it, - AboutPostType.STUDENT_CLUBS, - name - ) + override fun deleteClub(id: Long) { + val club = aboutRepository.findByIdOrNull(id) ?: throw CserealException.Csereal404("club not found") + val aboutLanguage = when (club.language) { + LanguageType.KO -> aboutLanguageRepository.findByKoAbout(club) + LanguageType.EN -> aboutLanguageRepository.findByEnAbout(club) } - clubs.forEach { + listOf(aboutLanguage!!.koAbout, aboutLanguage.enAbout).forEach { it.mainImage?.let { image -> mainImageService.removeImage(image) } - aboutRepository.delete(it) } + + aboutLanguageRepository.delete(aboutLanguage) } @Transactional(readOnly = true) @@ -190,6 +198,16 @@ class AboutServiceImpl( return clubs } + @Transactional(readOnly = true) + override fun readAllGroupedClubs(): List { + val clubs = aboutLanguageRepository.findAll().filter { it.koAbout.postType == AboutPostType.STUDENT_CLUBS } + .sortedBy { it.koAbout.name } + return clubs.map { + val imageURL = mainImageService.createImageURL(it.koAbout.mainImage) + GroupedClubDto(ko = ClubDtoWithId.of(it.koAbout, imageURL), en = ClubDtoWithId.of(it.enAbout, imageURL)) + } + } + @Transactional override fun createFacilities(request: CreateFacReq, mainImage: MultipartFile?) { val langToReq = listOf(