Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 예약 조회 권한 수정 #157

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -35,7 +39,6 @@ class ReservationController(
return ResponseEntity.ok(reservationService.getRoomReservationsBetween(roomId, start, end))
}

// @AuthenticatedForReservation
@GetMapping("/week")
fun getWeeklyReservations(
@RequestParam roomId: Long,
Expand All @@ -48,10 +51,17 @@ class ReservationController(
return ResponseEntity.ok(reservationService.getRoomReservationsBetween(roomId, start, end))
}

// @AuthenticatedForReservation
@GetMapping("/{reservationId}")
fun getReservation(@PathVariable reservationId: Long): ResponseEntity<ReservationDto> {
return ResponseEntity.ok(reservationService.getReservation(reservationId))
fun getReservation(
@PathVariable reservationId: Long,
@AuthenticationPrincipal oidcUser: OidcUser?
): ResponseEntity<ReservationDto> {
val isStaff = oidcUser?.let {
val username = it.idToken.getClaim<String>("username")
val user = userRepository.findByUsername(username)
user?.role == Role.ROLE_STAFF
} ?: false
return ResponseEntity.ok(reservationService.getReservation(reservationId, isStaff))
}

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ReservationEntity(
val professor: String,
val recurringWeeks: Int = 1,

val recurrenceId: UUID? = null
val recurrenceId: UUID

) : BaseTimeEntity() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.*
interface ReservationService {
fun reserveRoom(reserveRequest: ReserveRequest): List<ReservationDto>
fun getRoomReservationsBetween(roomId: Long, start: LocalDateTime, end: LocalDateTime): List<SimpleReservationDto>
fun getReservation(reservationId: Long): ReservationDto
fun getReservation(reservationId: Long, isStaff: Boolean): ReservationDto
fun cancelSpecific(reservationId: Long)
fun cancelRecurring(recurrenceId: UUID)
}
Expand Down Expand Up @@ -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) {
Expand Down
Loading