From 44f3aa456cd8638f27ed95d24678497e1153c494 Mon Sep 17 00:00:00 2001 From: leeeryboy Date: Fri, 22 Sep 2023 14:37:37 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=98=88=EC=95=BD=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EA=B6=8C=ED=95=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reservation/api/ReservationController.kt | 22 ++++++++++++++----- .../reservation/database/ReservationEntity.kt | 2 +- .../core/reservation/dto/ReservationDto.kt | 3 ++- .../reservation/service/ReservationService.kt | 19 ++++++---------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/api/ReservationController.kt b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/api/ReservationController.kt index 7bc843c1..c48efc8e 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/api/ReservationController.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/api/ReservationController.kt @@ -5,7 +5,11 @@ import com.wafflestudio.csereal.core.reservation.dto.ReservationDto import com.wafflestudio.csereal.core.reservation.dto.ReserveRequest import com.wafflestudio.csereal.core.reservation.dto.SimpleReservationDto import com.wafflestudio.csereal.core.reservation.service.ReservationService +import com.wafflestudio.csereal.core.user.database.Role +import com.wafflestudio.csereal.core.user.database.UserRepository import org.springframework.http.ResponseEntity +import org.springframework.security.core.annotation.AuthenticationPrincipal +import org.springframework.security.oauth2.core.oidc.user.OidcUser import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable @@ -20,10 +24,10 @@ import java.util.UUID @RequestMapping("/api/v1/reservation") @RestController class ReservationController( - private val reservationService: ReservationService + private val reservationService: ReservationService, + private val userRepository: UserRepository ) { - // @AuthenticatedForReservation TODO: CBT 끝나면 주석 제거 @GetMapping("/month") fun getMonthlyReservations( @RequestParam roomId: Long, @@ -35,7 +39,6 @@ class ReservationController( return ResponseEntity.ok(reservationService.getRoomReservationsBetween(roomId, start, end)) } - // @AuthenticatedForReservation @GetMapping("/week") fun getWeeklyReservations( @RequestParam roomId: Long, @@ -48,10 +51,17 @@ class ReservationController( return ResponseEntity.ok(reservationService.getRoomReservationsBetween(roomId, start, end)) } - // @AuthenticatedForReservation @GetMapping("/{reservationId}") - fun getReservation(@PathVariable reservationId: Long): ResponseEntity { - return ResponseEntity.ok(reservationService.getReservation(reservationId)) + fun getReservation( + @PathVariable reservationId: Long, + @AuthenticationPrincipal oidcUser: OidcUser? + ): ResponseEntity { + val isStaff = oidcUser?.let { + val username = it.idToken.getClaim("username") + val user = userRepository.findByUsername(username) + user?.role == Role.ROLE_STAFF + } ?: false + return ResponseEntity.ok(reservationService.getReservation(reservationId, isStaff)) } @PostMapping diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/database/ReservationEntity.kt b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/database/ReservationEntity.kt index 2b08ae95..95a7e22f 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/database/ReservationEntity.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/database/ReservationEntity.kt @@ -33,7 +33,7 @@ class ReservationEntity( val professor: String, val recurringWeeks: Int = 1, - val recurrenceId: UUID? = null + val recurrenceId: UUID ) : BaseTimeEntity() { diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/dto/ReservationDto.kt b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/dto/ReservationDto.kt index 39d22019..f6820e63 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/dto/ReservationDto.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/dto/ReservationDto.kt @@ -6,7 +6,7 @@ import java.util.UUID data class ReservationDto( val id: Long, - val recurrenceId: UUID? = null, + val recurrenceId: UUID, val title: String, val purpose: String, val startTime: LocalDateTime, @@ -41,6 +41,7 @@ data class ReservationDto( fun forNormalUser(reservationEntity: ReservationEntity): ReservationDto { return ReservationDto( id = reservationEntity.id, + recurrenceId = reservationEntity.recurrenceId, title = reservationEntity.title, purpose = reservationEntity.purpose, startTime = reservationEntity.startTime, diff --git a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/service/ReservationService.kt b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/service/ReservationService.kt index 59b4ce35..30fda4fe 100644 --- a/src/main/kotlin/com/wafflestudio/csereal/core/reservation/service/ReservationService.kt +++ b/src/main/kotlin/com/wafflestudio/csereal/core/reservation/service/ReservationService.kt @@ -17,7 +17,7 @@ import java.util.* interface ReservationService { fun reserveRoom(reserveRequest: ReserveRequest): List fun getRoomReservationsBetween(roomId: Long, start: LocalDateTime, end: LocalDateTime): List - fun getReservation(reservationId: Long): ReservationDto + fun getReservation(reservationId: Long, isStaff: Boolean): ReservationDto fun cancelSpecific(reservationId: Long) fun cancelRecurring(recurrenceId: UUID) } @@ -78,20 +78,15 @@ class ReservationServiceImpl( } @Transactional(readOnly = true) - override fun getReservation(reservationId: Long): ReservationDto { + override fun getReservation(reservationId: Long, isStaff: Boolean): ReservationDto { val reservationEntity = reservationRepository.findByIdOrNull(reservationId) ?: throw CserealException.Csereal404("예약을 찾을 수 없습니다.") -// val user = RequestContextHolder.getRequestAttributes()?.getAttribute( -// "loggedInUser", -// RequestAttributes.SCOPE_REQUEST -// ) as UserEntity -// -// if (user.role == Role.ROLE_STAFF) { -// return ReservationDto.of(reservationEntity) -// } else { - return ReservationDto.forNormalUser(reservationEntity) -// } + return if (isStaff) { + ReservationDto.of(reservationEntity) + } else { + ReservationDto.forNormalUser(reservationEntity) + } } override fun cancelSpecific(reservationId: Long) {