Skip to content

Commit

Permalink
fix: 동아리 id 기준으로 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeryboy committed Aug 28, 2024
1 parent 304a879 commit 8c51eb0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
) {
Expand Down Expand Up @@ -41,25 +41,7 @@ class AboutController(
@RequestPart newAttachments: List<MultipartFile>?
) = 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GroupedClubDto> = 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<StudentClubDto>
fun readAllGroupedClubs(): List<GroupedClubDto>
fun createFacilities(request: CreateFacReq, mainImage: MultipartFile?)
fun updateFacility(id: Long, request: CreateFacReq, newMainImage: MultipartFile?)
fun deleteFacility(id: Long)
Expand Down Expand Up @@ -122,7 +123,7 @@ class AboutServiceImpl(
AboutEntity(
AboutPostType.STUDENT_CLUBS,
lang,
req.name + "(${request.en.name})",
req.name,
req.description,
searchContent = ""
).apply { syncSearchContent() }
Expand All @@ -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)
Expand All @@ -190,6 +198,16 @@ class AboutServiceImpl(
return clubs
}

@Transactional(readOnly = true)
override fun readAllGroupedClubs(): List<GroupedClubDto> {
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(
Expand Down

0 comments on commit 8c51eb0

Please sign in to comment.