Skip to content

Commit

Permalink
refactor(Band~) : 객체 기능 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
Due-IT committed Nov 30, 2024
1 parent 661e935 commit d1e5cff
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 189 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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<Response> {
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<Response> {
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<Response> {
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)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@ 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

@RestController
@RequestMapping("/api/bands/")
class BandQueryController(
private val bandService: BandService,
private val bandQueryService: BandQueryService,
private val jwtTokenProvider: JwtTokenProvider,
private val adminService: AdminService
) {
Expand All @@ -32,68 +26,19 @@ class BandQueryController(
summary = "밴드에 속한 학생 명단 조회"
)
fun getBandStudents(@PathVariable bandId: Long): ResponseEntity<Response> {
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<Response> {
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<Response> {
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 = "해당 계정의 밴드 목록을 불러옵니다."
)
fun getBands(@RequestHeader("Authorization") token: String): ResponseEntity<Response> {
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<Response> {
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)
}

Expand All @@ -103,7 +48,7 @@ class BandQueryController(
)
fun getBandDetail(@PathVariable bandId: Long): ResponseEntity<Response> {

val response = Response.ok(data = bandService.getBandDetail(bandId = bandId))
val response = Response.ok(data = bandQueryService.getBandDetail(bandId = bandId))

return ResponseEntity(response, HttpStatus.OK)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions wabi/src/main/kotlin/com/wap/wabi/band/service/BandQueryService.kt
Original file line number Diff line number Diff line change
@@ -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<BandStudentData> {
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<BandsData> {
val admin = adminRepository.findById(adminId)

if (admin.isEmpty) {
throw RestApiException(ErrorCode.UNAUTHORIZED_REQUEST)
}

val bands: List<Band> = bandRepository.findAllByAdminId(admin.get().id)
val bandsDatas: MutableList<BandsData> = 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)
}
}
Loading

0 comments on commit d1e5cff

Please sign in to comment.