diff --git a/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandCommandController.kt b/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandCommandController.kt index af8fb38..31f468e 100644 --- a/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandCommandController.kt +++ b/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandCommandController.kt @@ -1,7 +1,11 @@ package com.wap.wabi.band.controller +import com.wap.wabi.auth.admin.service.AdminService +import com.wap.wabi.auth.jwt.JwtTokenProvider import com.wap.wabi.band.payload.BandStudentDto +import com.wap.wabi.band.payload.request.BandCreateRequest import com.wap.wabi.band.payload.request.BandStudentEnrollRequest +import com.wap.wabi.band.payload.request.BandUpdateRequest import com.wap.wabi.band.service.BandCommandService import com.wap.wabi.common.payload.response.Response import io.swagger.v3.oas.annotations.Operation @@ -12,6 +16,7 @@ import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PutMapping import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController @@ -21,7 +26,58 @@ import org.springframework.web.multipart.MultipartFile @RequestMapping("/api/bands/") class BandCommandController( private val bandCommandService: BandCommandService, + private val jwtTokenProvider: JwtTokenProvider, + private val adminService: AdminService ) { + @PostMapping("create") + @Operation( + summary = "밴드 생성" + ) + fun createBand( + @RequestHeader("Authorization") token: String, + @RequestBody request: BandCreateRequest + ): ResponseEntity { + val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) + val adminId = adminService.getAdminId(adminName = adminName) + bandCommandService.createBand(adminId = adminId, bandCreateRequest = request) + + val response = Response.ok(message = "success create band") + return ResponseEntity(response, HttpStatus.OK) + } + + @PutMapping("") + @Operation( + summary = "밴드 수정" + ) + fun updateBand( + @RequestHeader("Authorization") token: String, + @RequestBody request: BandUpdateRequest + ): ResponseEntity { + val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) + val adminId = adminService.getAdminId(adminName = adminName) + bandCommandService.updateBand(adminId = adminId, bandUpdateRequest = request) + + val response = Response.ok(message = "success update band") + + return ResponseEntity(response, HttpStatus.OK) + } + + @DeleteMapping("{bandId}") + @Operation( + summary = "밴드 삭제" + ) + fun deleteBand( + @RequestHeader("Authorization") token: String, + @PathVariable bandId: Long + ): ResponseEntity { + val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) + val adminId = adminService.getAdminId(adminName = adminName) + bandCommandService.deleteBand(adminId = adminId, bandId = bandId) + + val response = Response.ok(message = "success delete band") + return ResponseEntity(response, HttpStatus.OK) + } + @PostMapping("{bandId}/members/enrollments/file") @Operation( summary = "밴드 학생 추가(excel, csv)" diff --git a/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandQueryController.kt b/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandQueryController.kt index 9a757ae..972c7c5 100644 --- a/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandQueryController.kt +++ b/wabi/src/main/kotlin/com/wap/wabi/band/controller/BandQueryController.kt @@ -2,20 +2,14 @@ package com.wap.wabi.band.controller import com.wap.wabi.auth.admin.service.AdminService import com.wap.wabi.auth.jwt.JwtTokenProvider -import com.wap.wabi.band.payload.request.BandCreateRequest -import com.wap.wabi.band.payload.request.BandUpdateRequest import com.wap.wabi.band.payload.response.BandStudentsData -import com.wap.wabi.band.service.BandService +import com.wap.wabi.band.service.BandQueryService import com.wap.wabi.common.payload.response.Response import io.swagger.v3.oas.annotations.Operation import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity -import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.PutMapping -import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @@ -23,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/api/bands/") class BandQueryController( - private val bandService: BandService, + private val bandQueryService: BandQueryService, private val jwtTokenProvider: JwtTokenProvider, private val adminService: AdminService ) { @@ -32,43 +26,11 @@ class BandQueryController( summary = "밴드에 속한 학생 명단 조회" ) fun getBandStudents(@PathVariable bandId: Long): ResponseEntity { - val response = Response.ok(data = BandStudentsData(bandService.getBandStudents(bandId = bandId))) + val response = Response.ok(data = BandStudentsData(bandQueryService.getBandStudents(bandId = bandId))) return ResponseEntity(response, HttpStatus.OK) } - @PostMapping("create") - @Operation( - summary = "밴드 생성" - ) - fun createBand( - @RequestHeader("Authorization") token: String, - @RequestBody request: BandCreateRequest - ): ResponseEntity { - val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) - val adminId = adminService.getAdminId(adminName = adminName) - bandService.createBand(adminId = adminId, bandCreateRequest = request) - - val response = Response.ok(message = "success create band") - return ResponseEntity(response, HttpStatus.OK) - } - - @DeleteMapping("{bandId}") - @Operation( - summary = "밴드 삭제" - ) - fun deleteBand( - @RequestHeader("Authorization") token: String, - @PathVariable bandId: Long - ): ResponseEntity { - val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) - val adminId = adminService.getAdminId(adminName = adminName) - bandService.deleteBand(adminId = adminId, bandId = bandId) - - val response = Response.ok(message = "success delete band") - return ResponseEntity(response, HttpStatus.OK) - } - @GetMapping("list") @Operation( summary = "해당 계정의 밴드 목록을 불러옵니다." @@ -76,24 +38,7 @@ class BandQueryController( fun getBands(@RequestHeader("Authorization") token: String): ResponseEntity { val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) val adminId = adminService.getAdminId(adminName = adminName) - val response = Response.ok(data = bandService.getBands(adminId = adminId)) - return ResponseEntity(response, HttpStatus.OK) - } - - @PutMapping("") - @Operation( - summary = "밴드 수정" - ) - fun updateBand( - @RequestHeader("Authorization") token: String, - @RequestBody request: BandUpdateRequest - ): ResponseEntity { - val adminName = jwtTokenProvider.getAdminNameByToken(token.removePrefix("Bearer ")) - val adminId = adminService.getAdminId(adminName = adminName) - bandService.updateBand(adminId = adminId, bandUpdateRequest = request) - - val response = Response.ok(message = "success update band") - + val response = Response.ok(data = bandQueryService.getBands(adminId = adminId)) return ResponseEntity(response, HttpStatus.OK) } @@ -103,7 +48,7 @@ class BandQueryController( ) fun getBandDetail(@PathVariable bandId: Long): ResponseEntity { - val response = Response.ok(data = bandService.getBandDetail(bandId = bandId)) + val response = Response.ok(data = bandQueryService.getBandDetail(bandId = bandId)) return ResponseEntity(response, HttpStatus.OK) } diff --git a/wabi/src/main/kotlin/com/wap/wabi/band/service/BandCommandService.kt b/wabi/src/main/kotlin/com/wap/wabi/band/service/BandCommandService.kt index 525b7be..8104fd5 100644 --- a/wabi/src/main/kotlin/com/wap/wabi/band/service/BandCommandService.kt +++ b/wabi/src/main/kotlin/com/wap/wabi/band/service/BandCommandService.kt @@ -1,9 +1,12 @@ package com.wap.wabi.band.service +import com.wap.wabi.auth.admin.repository.AdminRepository import com.wap.wabi.band.entity.Band import com.wap.wabi.band.entity.BandStudent import com.wap.wabi.band.payload.BandStudentDto +import com.wap.wabi.band.payload.request.BandCreateRequest import com.wap.wabi.band.payload.request.BandStudentEnrollRequest +import com.wap.wabi.band.payload.request.BandUpdateRequest import com.wap.wabi.band.repository.BandRepository import com.wap.wabi.band.repository.BandStudentRepository import com.wap.wabi.exception.ErrorCode @@ -19,8 +22,55 @@ class BandCommandService( private val bandRepository: BandRepository, private val bandStudentRepository: BandStudentRepository, private val studentRepository: StudentRepository, - private val fileToBandStudentTranslator: FileToBandStudentTranslator + private val fileToBandStudentTranslator: FileToBandStudentTranslator, + private val adminRepository: AdminRepository ) { + @Transactional + fun createBand(adminId: Long, bandCreateRequest: BandCreateRequest) { + val admin = adminRepository.findById(adminId) + + if (admin.isEmpty) { + throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) + } + + val createBand = bandCreateRequest.toBand(admin.get().id) + + bandRepository.save(createBand) + } + + @Transactional + fun updateBand(adminId: Long, bandUpdateRequest: BandUpdateRequest) { + val band = bandRepository.findById(bandUpdateRequest.bandId) + .orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } + val admin = adminRepository.findById(adminId) + + if (admin.isEmpty) { + throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) + } + + if (band.adminId != admin.get().id) { + throw RestApiException(ErrorCode.UNAUTHORIZED_BAND) + } + + band.update(bandUpdateRequest) + } + + @Transactional + fun deleteBand(adminId: Long, bandId: Long) { + val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } + val admin = adminRepository.findById(adminId) + + if (admin.isEmpty) { + throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) + } + + if (band.adminId != admin.get().id) { + throw RestApiException(ErrorCode.UNAUTHORIZED_BAND) + } + + band.delete() + } + fun enrollByFile(bandId: Long, file: MultipartFile): Long { val bandStudentDtos = fileToBandStudentTranslator.translateFileToDto(file) return enrollByDto(bandId, bandStudentDtos) diff --git a/wabi/src/main/kotlin/com/wap/wabi/band/service/BandQueryService.kt b/wabi/src/main/kotlin/com/wap/wabi/band/service/BandQueryService.kt new file mode 100644 index 0000000..14ffec4 --- /dev/null +++ b/wabi/src/main/kotlin/com/wap/wabi/band/service/BandQueryService.kt @@ -0,0 +1,56 @@ +package com.wap.wabi.band.service + +import com.wap.wabi.auth.admin.repository.AdminRepository +import com.wap.wabi.band.entity.Band +import com.wap.wabi.band.payload.response.BandDetailData +import com.wap.wabi.band.payload.response.BandStudentData +import com.wap.wabi.band.payload.response.BandsData +import com.wap.wabi.band.repository.BandRepository +import com.wap.wabi.band.repository.BandStudentRepository +import com.wap.wabi.exception.ErrorCode +import com.wap.wabi.exception.RestApiException +import jakarta.transaction.Transactional +import org.springframework.stereotype.Service + +@Service +class BandQueryService( + private val bandRepository: BandRepository, + private val bandStudentRepository: BandStudentRepository, + private val adminRepository: AdminRepository, +) { + @Transactional + fun getBandStudents(bandId: Long): List { + val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } + + val bandStudents = bandStudentRepository.findAllByBand(band) + + return BandStudentData.of(bandStudents) + } + + @Transactional + fun getBands(adminId: Long): List { + val admin = adminRepository.findById(adminId) + + if (admin.isEmpty) { + throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) + } + + val bands: List = bandRepository.findAllByAdminId(admin.get().id) + val bandsDatas: MutableList = mutableListOf() + bands.forEach { band -> + if (band.isAvailable()) { + bandsDatas.add(BandsData(bandId = band.id, bandName = band.bandName)) + } + } + + return bandsDatas + } + + @Transactional + fun getBandDetail(bandId: Long): BandDetailData { + val band = bandRepository.findById(bandId) + .orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } + + return BandDetailData.of(band = band) + } +} diff --git a/wabi/src/main/kotlin/com/wap/wabi/band/service/BandService.kt b/wabi/src/main/kotlin/com/wap/wabi/band/service/BandService.kt deleted file mode 100644 index 3f958ec..0000000 --- a/wabi/src/main/kotlin/com/wap/wabi/band/service/BandService.kt +++ /dev/null @@ -1,128 +0,0 @@ -package com.wap.wabi.band.service - -import com.wap.wabi.auth.admin.repository.AdminRepository -import com.wap.wabi.band.entity.Band -import com.wap.wabi.band.payload.request.BandCreateRequest -import com.wap.wabi.band.payload.request.BandUpdateRequest -import com.wap.wabi.band.payload.response.BandDetailData -import com.wap.wabi.band.payload.response.BandStudentData -import com.wap.wabi.band.payload.response.BandsData -import com.wap.wabi.band.repository.BandRepository -import com.wap.wabi.band.repository.BandStudentRepository -import com.wap.wabi.event.entity.Event -import com.wap.wabi.event.repository.EventBandRepository -import com.wap.wabi.event.repository.EventRepository -import com.wap.wabi.event.repository.EventStudentBandNameRepository -import com.wap.wabi.event.repository.EventStudentRepository -import com.wap.wabi.exception.ErrorCode -import com.wap.wabi.exception.RestApiException -import com.wap.wabi.student.repository.StudentRepository -import jakarta.transaction.Transactional -import org.springframework.stereotype.Service - -@Service -class BandService( - private val bandRepository: BandRepository, - private val bandStudentRepository: BandStudentRepository, - private val adminRepository: AdminRepository, - private val eventStudentBandNameRepository: EventStudentBandNameRepository, - private val eventBandRepository: EventBandRepository, - private val eventRepository: EventRepository, - private val eventStudentRepository: EventStudentRepository, - private val studentRepository: StudentRepository -) { - @Transactional - fun getBandStudents(bandId: Long): List { - val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } - - val bandStudents = bandStudentRepository.findAllByBand(band) - - return BandStudentData.of(bandStudents) - } - - @Transactional - fun createBand(adminId: Long, bandCreateRequest: BandCreateRequest) { - val admin = adminRepository.findById(adminId) - - if (admin.isEmpty) { - throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) - } - - val createBand = bandCreateRequest.toBand(admin.get().id) - - bandRepository.save(createBand) - } - - @Transactional - fun deleteBand(adminId: Long, bandId: Long) { - val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } - val admin = adminRepository.findById(adminId) - - if (admin.isEmpty) { - throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) - } - - if (band.adminId != admin.get().id) { - throw RestApiException(ErrorCode.UNAUTHORIZED_BAND) - } - - band.delete() - } - - private fun deleteEventByBand(event: Event) { - val eventStudens = eventStudentRepository.findAllByEvent(event) - - eventStudens.forEach { eventStudent -> - eventStudentBandNameRepository.deleteByEventStudent(eventStudent) - } - - eventStudentRepository.deleteByEvent(event) - eventBandRepository.deleteByEvent(event) - eventRepository.delete(event) - } - - @Transactional - fun getBands(adminId: Long): List { - val admin = adminRepository.findById(adminId) - - if (admin.isEmpty) { - throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) - } - - val bands: List = bandRepository.findAllByAdminId(admin.get().id) - val bandsDatas: MutableList = mutableListOf() - bands.forEach { band -> - if (band.isAvailable()) { - bandsDatas.add(BandsData(bandId = band.id, bandName = band.bandName)) - } - } - - return bandsDatas - } - - @Transactional - fun updateBand(adminId: Long, bandUpdateRequest: BandUpdateRequest) { - val band = bandRepository.findById(bandUpdateRequest.bandId) - .orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } - val admin = adminRepository.findById(adminId) - - if (admin.isEmpty) { - throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST) - } - - if (band.adminId != admin.get().id) { - throw RestApiException(ErrorCode.UNAUTHORIZED_BAND) - } - - band.update(bandUpdateRequest) - } - - @Transactional - fun getBandDetail(bandId: Long): BandDetailData { - val band = bandRepository.findById(bandId) - .orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) } - - return BandDetailData.of(band = band) - } - -}