Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/pknu-wap/WABI-BE into de…
Browse files Browse the repository at this point in the history
…velop
  • Loading branch information
Due-IT committed Nov 28, 2024
2 parents 7190d76 + 69be782 commit de35d30
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 206 deletions.
2 changes: 1 addition & 1 deletion wabi/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ dependencies {
kapt("org.springframework.boot:spring-boot-configuration-processor")

//swagger
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0")

//openCSV
implementation("com.opencsv:opencsv:5.7.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ class SecurityConfig(
}.authorizeHttpRequests { authorizeRequests ->
authorizeRequests
.requestMatchers("/auth/**").permitAll()
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**", "/api/**").permitAll()
.requestMatchers(
"/swagger-ui/**",
"/v3/api-docs/**",
"/swagger-ui.html",
"/swagger-resources/**",
"/webjars/**"
)
.permitAll()
.anyRequest().authenticated()
}.sessionManagement { session ->
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class JwtAuthenticationFilter(
response: HttpServletResponse,
filterChain: FilterChain
) {
val path = request.requestURI
if (path.startsWith("/swagger-ui/") || path.startsWith("/v3/api-docs/")) {
filterChain.doFilter(request, response)
return
}

val token = parseBearerToken(request)

if (token != null && jwtTokenProvider.validateTokenAndGetSubject(token) != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.wap.wabi.band.controller

import com.wap.wabi.band.payload.BandStudentDto
import com.wap.wabi.band.payload.request.BandStudentEnrollRequest
import com.wap.wabi.band.service.BandCommandService
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.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.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@RestController
@RequestMapping("/api/bands/")
class BandCommandController(
private val bandCommandService: BandCommandService,
) {
@PostMapping("{bandId}/members/enrollments/file")
@Operation(
summary = "밴드 학생 추가(excel, csv)"
)
fun enrollByFile(
@PathVariable bandId: Long,
@RequestParam("file") file: MultipartFile
): ResponseEntity<Response> {
val response = Response.ok(data = "bandId : " + bandCommandService.enrollByFile(bandId = bandId, file = file))
return ResponseEntity(response, HttpStatus.OK)
}

@PostMapping("{bandId}/members/enrollments/manual")
@Operation(
summary = "밴드 학생 추가",
description = "밴드에 학생들을 추가 가능합니다."
)
fun enrollByManual(
@PathVariable bandId: Long,
@RequestBody request: BandStudentEnrollRequest
): ResponseEntity<Response> {
val response =
Response.ok(data = "bandId : " + bandCommandService.enrollBandStudent(bandId = bandId, request = request))
return ResponseEntity(response, HttpStatus.OK)
}

@PutMapping("{bandId}")
@Operation(
summary = "밴드 학생 정보 수정"
)
fun deleteBandStudent(
@PathVariable bandId: Long,
@RequestBody request: BandStudentDto
): ResponseEntity<Response> {
val response =
Response.ok(data = "bandId : " + bandCommandService.updateBandStudent(bandId = bandId, request = request))
return ResponseEntity(response, HttpStatus.OK)
}

@DeleteMapping("{bandId}/{studentId}")
@Operation(
summary = "밴드 학생 삭제"
)
fun deleteBandStudent(
@PathVariable bandId: Long,
@PathVariable studentId: String
): ResponseEntity<Response> {
bandCommandService.deleteBandStudent(bandId = bandId, studentId = studentId)

val response = Response.ok(message = "success delete band student")
return ResponseEntity(response, HttpStatus.OK)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ 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.*
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 BandController(
class BandQueryController(
private val bandService: BandService,
private val jwtTokenProvider: JwtTokenProvider,
private val adminService: AdminService
Expand Down Expand Up @@ -99,4 +107,6 @@ class BandController(

return ResponseEntity(response, HttpStatus.OK)
}


}
14 changes: 13 additions & 1 deletion wabi/src/main/kotlin/com/wap/wabi/band/entity/BandStudent.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.wap.wabi.band.entity;

import com.wap.wabi.band.payload.BandStudentDto;
import com.wap.wabi.student.entity.Student;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDate;

Expand All @@ -31,6 +33,9 @@ public class BandStudent {
private String tel;
private String academicStatus;

protected BandStudent() {
}

private BandStudent(builder builder) {
this.band = builder.band;
this.student = builder.student;
Expand Down Expand Up @@ -104,7 +109,14 @@ public BandStudent build() {
}
}

protected BandStudent() {
public void update(@NotNull BandStudentDto request) {
this.club = request.getClub();
this.position = request.getPosition();
this.joinDate = request.getJoinDate();
this.college = request.getCollege();
this.major = request.getMajor();
this.tel = request.getTel();
this.academicStatus = request.getAcademicStatus();
}

public Long getId() {
Expand Down
18 changes: 9 additions & 9 deletions wabi/src/main/kotlin/com/wap/wabi/band/payload/BandStudentDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package com.wap.wabi.band.payload
import java.time.LocalDate

data class BandStudentDto(
val studentId : String,
val name : String,
val club : String?,
val position : String?,
val joinDate : LocalDate?,
val college : String?,
val major : String?,
val tel : String?,
val academicStatus : String?
val studentId: String,
val name: String,
val club: String?,
val position: String?,
val joinDate: LocalDate?,
val college: String?,
val major: String?,
val tel: String?,
val academicStatus: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package com.wap.wabi.band.payload.request

import com.wap.wabi.band.payload.BandStudentDto

data class EnrollRequest(val bandStudentDtos: List<BandStudentDto>)
data class BandStudentEnrollRequest(val bandStudentDtos: List<BandStudentDto>)
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface BandStudentRepository extends JpaRepository<BandStudent, Long>
Optional<BandStudent> findByBandAndStudent(Band band, Student student);

void deleteAllByBand(Band band);

void deleteBandStudentByBandAndStudent(Band band, Student student);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.wap.wabi.band.service
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.EnrollRequest
import com.wap.wabi.band.payload.request.BandStudentEnrollRequest
import com.wap.wabi.band.repository.BandRepository
import com.wap.wabi.band.repository.BandStudentRepository
import com.wap.wabi.exception.ErrorCode
Expand All @@ -15,7 +15,7 @@ import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

@Service
class BandEnrollService(
class BandCommandService(
private val bandRepository: BandRepository,
private val bandStudentRepository: BandStudentRepository,
private val studentRepository: StudentRepository,
Expand All @@ -28,12 +28,12 @@ class BandEnrollService(

@Transactional
fun enrollByDto(bandId: Long, bandStudentDtos: List<BandStudentDto>): Long {
val request = EnrollRequest(bandStudentDtos)
val request = BandStudentEnrollRequest(bandStudentDtos)
return enrollBandStudent(bandId, request)
}

@Transactional
fun enrollBandStudent(bandId: Long, request: EnrollRequest): Long {
fun enrollBandStudent(bandId: Long, request: BandStudentEnrollRequest): Long {
val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) }

val bandStudents: MutableList<BandStudent> = mutableListOf()
Expand All @@ -49,17 +49,7 @@ class BandEnrollService(
)
}

val bandStudent = BandStudent.builder()
.band(band)
.student(student)
.club(bandStudentDto.club)
.position(bandStudentDto.position)
.joinDate(bandStudentDto.joinDate)
.college(bandStudentDto.college)
.major(bandStudentDto.major)
.tel(bandStudentDto.tel)
.academicStatus(bandStudentDto.academicStatus)
.build()
val bandStudent = buildBandStudent(band, student, bandStudentDto)

if (!alreadyHasSameStudentInBand(student, band)) bandStudents.add(bandStudent)
}
Expand All @@ -71,4 +61,41 @@ class BandEnrollService(
private fun alreadyHasSameStudentInBand(student: Student, band: Band): Boolean {
return bandStudentRepository.findByBandAndStudent(band, student).isPresent
}

@Transactional
fun deleteBandStudent(bandId: Long, studentId: String) {
val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) }
val student =
studentRepository.findById(studentId).orElseThrow() { RestApiException(ErrorCode.NOT_FOUND_STUDENT) }
bandStudentRepository.deleteBandStudentByBandAndStudent(band, student)
}

@Transactional
fun updateBandStudent(bandId: Long, request: BandStudentDto): Long {
val band = bandRepository.findById(bandId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_BAND) }
val student =
studentRepository.findById(request.studentId).orElseThrow { RestApiException(ErrorCode.NOT_FOUND_STUDENT) }

val bandStudent = bandStudentRepository.findByBandAndStudent(band, student)
.orElseThrow { RestApiException(ErrorCode.NOT_FOUND_STUDENT) }

bandStudent.update(request)

return bandId
}

private fun buildBandStudent(band: Band, student: Student, request: BandStudentDto): BandStudent {
val bandStudent = BandStudent.builder()
.band(band)
.student(student)
.club(request.club)
.position(request.position)
.joinDate(request.joinDate)
.college(request.college)
.major(request.major)
.tel(request.tel)
.academicStatus(request.academicStatus)
.build()
return bandStudent
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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

Expand All @@ -27,7 +28,8 @@ class BandService(
private val eventStudentBandNameRepository: EventStudentBandNameRepository,
private val eventBandRepository: EventBandRepository,
private val eventRepository: EventRepository,
private val eventStudentRepository: EventStudentRepository
private val eventStudentRepository: EventStudentRepository,
private val studentRepository: StudentRepository
) {
@Transactional
fun getBandStudents(bandId: Long): List<BandStudentData> {
Expand Down Expand Up @@ -122,4 +124,5 @@ class BandService(

return BandDetailData.of(band = band)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SwaggerConfig {
.bearerFormat("JWT")
)
return OpenAPI()
.addServersItem(Server().url("/"))
.addServersItem(Server().url("https://zepelown.site"))
.info(info)
.addSecurityItem(securityRequirement)
.components(components)
Expand Down
Loading

0 comments on commit de35d30

Please sign in to comment.