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

예외 로직 구현 #46

Merged
merged 15 commits into from
Mar 31, 2024
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 @@ -31,4 +31,12 @@ private LibrarySeat(int seatNumber, String availability) {
public static LibrarySeat of(int seatNumber, String availability) {
return new LibrarySeat(seatNumber, availability);
}

public void updateUnavailable() {
this.availability = "사용중";
}

public void updateAvailable() {
this.availability = "사용가능";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.librarySeat.exception;

import com.example.pnuunivmiryangcampus.support.exception.NotFoundException;

public class LibrarySeatNotFoundException extends NotFoundException {

private static final String MESSAGE = "해당 좌석은 존재하지 않습니다.";

public LibrarySeatNotFoundException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.reservation.exception;

import com.example.pnuunivmiryangcampus.support.exception.BadRequestException;

public class DuplicateReservationException extends BadRequestException {

private static final String MESSAGE = "이미 예약된 좌석이 존재합니다.";

public DuplicateReservationException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.reservation.exception;

import com.example.pnuunivmiryangcampus.support.exception.BadRequestException;

public class ExpiredRenewalTimeException extends BadRequestException {

private static final String MESSAGE = "연장 가능 시간은 종료 시간의 30분 전입니다.";

public ExpiredRenewalTimeException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.pnuunivmiryangcampus.reservation.exception;

public class ReservationLimitExceededException extends IllegalArgumentException{
import com.example.pnuunivmiryangcampus.support.exception.BadRequestException;

public class ReservationLimitExceededException extends BadRequestException {

private static final String MESSAGE = "연장 가능 횟수를 초과했습니다.";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.reservation.exception;

import com.example.pnuunivmiryangcampus.support.exception.NotFoundException;

public class ReservationNotFoundException extends NotFoundException {

private static final String MESSAGE = "예약된 좌석을 찾을 수 없습니다.";

public ReservationNotFoundException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.example.pnuunivmiryangcampus.reservation.service;

import com.example.pnuunivmiryangcampus.librarySeat.LibrarySeat;
import com.example.pnuunivmiryangcampus.librarySeat.exception.LibrarySeatNotFoundException;
import com.example.pnuunivmiryangcampus.librarySeat.repository.LibrarySeatRepository;
import com.example.pnuunivmiryangcampus.reservation.Reservation;
import com.example.pnuunivmiryangcampus.reservation.dto.ReservationDto;
import com.example.pnuunivmiryangcampus.reservation.dto.response.ReservationRenewalResponse;
import com.example.pnuunivmiryangcampus.reservation.dto.response.ReservationResponse;
import com.example.pnuunivmiryangcampus.reservation.exception.DuplicateReservationException;
import com.example.pnuunivmiryangcampus.reservation.exception.ExpiredRenewalTimeException;
import com.example.pnuunivmiryangcampus.reservation.exception.ReservationLimitExceededException;
import com.example.pnuunivmiryangcampus.reservation.exception.ReservationNotFoundException;
import com.example.pnuunivmiryangcampus.reservation.repository.ReservationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@RequiredArgsConstructor
Expand All @@ -23,15 +29,27 @@ public class ReservationService {

@Transactional
public void saveReservation(ReservationDto dto) {
checkDuplicateReservation(dto.userAccountId());

reservationRepository.save(dto.toEntity());
LibrarySeat librarySeat = librarySeatRepository.findById(dto.librarySeatId()).orElseThrow(LibrarySeatNotFoundException::new);
librarySeat.updateUnavailable();
}

private void checkDuplicateReservation(Long userId) {
ReservationResponse reservationResponse = this.getReservationByUserId(userId);

if (reservationResponse != null) {
throw new DuplicateReservationException();
}
}

@Transactional(readOnly = true)
public ReservationResponse getReservationByUserId(Long userId) {
Optional<Reservation> reservation = reservationRepository.findByUserAccountId(userId);

if (reservation.isPresent()) {
Long reservedSeatId = librarySeatRepository.findById(reservation.get().getLibrarySeatId()).orElseThrow().getId();
Long reservedSeatId = librarySeatRepository.findById(reservation.get().getLibrarySeatId()).orElseThrow(LibrarySeatNotFoundException::new).getId();

return ReservationResponse.from(reservation.get(), reservedSeatId);
}
Expand All @@ -41,14 +59,22 @@ public ReservationResponse getReservationByUserId(Long userId) {

@Transactional
public ReservationRenewalResponse updateReservationRenewalCount(Long reservationId) {
Reservation reservation = reservationRepository.findById(reservationId).orElseThrow();
Reservation reservation = reservationRepository.findById(reservationId).orElseThrow(ReservationNotFoundException::new);

checkRenewalTime(reservation);

reservation.update(reservation.getEndAt().plusHours(3), reservation.getRenewalCount() + 1);
checkRenewalCount(reservation);

return ReservationRenewalResponse.from(reservation);
}

private static void checkRenewalTime(Reservation reservation) {
if (LocalDateTime.now().isBefore(reservation.getEndAt().minusMinutes(30))) {
throw new ExpiredRenewalTimeException();
}
}

private void checkRenewalCount(Reservation reservation) {
if (reservation.getRenewalCount() > 4) {
throw new ReservationLimitExceededException();
Expand All @@ -57,7 +83,15 @@ private void checkRenewalCount(Reservation reservation) {

@Transactional
public void deleteReservation(Long reservationId) {
reservationRepository.deleteById(reservationId);
reservationRepository.findById(reservationId)
.ifPresentOrElse(
reservation -> {
reservationRepository.deleteById(reservationId);
LibrarySeat librarySeat = librarySeatRepository.findById(reservation.getLibrarySeatId()).orElseThrow(LibrarySeatNotFoundException::new);
librarySeat.updateAvailable();
},
() -> { throw new ReservationNotFoundException(); }
);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.pnuunivmiryangcampus.support.exception;

public class BadRequestException extends BusinessException {

public BadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.pnuunivmiryangcampus.support.exception;

public class BusinessException extends RuntimeException {

public BusinessException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.pnuunivmiryangcampus.support.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ControllerAdvice {

@ExceptionHandler(NotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFoundException(NotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(BadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(BadRequestException e) {
return ResponseEntity.badRequest().body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(ForbiddenException.class)
public ResponseEntity<ErrorResponse> handleForbiddenException(ForbiddenException e) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(UnauthorizedException.class)
public ResponseEntity<ErrorResponse> handleUnauthorizedException(UnauthorizedException e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(RuntimeException.class)
public ResponseEntity<ErrorResponse> handleRuntimeException(RuntimeException e) {
return ResponseEntity.internalServerError().body(new ErrorResponse("서버에 알 수 없는 문제가 발생했습니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.pnuunivmiryangcampus.support.exception;

import lombok.Getter;

@Getter
public class ErrorResponse {

private final String message;

public ErrorResponse(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.pnuunivmiryangcampus.support.exception;

public class ForbiddenException extends BusinessException {

public ForbiddenException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.pnuunivmiryangcampus.support.exception;

public class InternalException extends BusinessException {

public InternalException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.pnuunivmiryangcampus.support.exception;

public class NotFoundException extends BusinessException {

public NotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.pnuunivmiryangcampus.support.exception;

public class UnauthorizedException extends BusinessException {

public UnauthorizedException(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.example.pnuunivmiryangcampus.auth.OIDCDecodePayload;
import com.example.pnuunivmiryangcampus.auth.OIDCPublicKeyDto;
import com.example.pnuunivmiryangcampus.support.token.exception.ExpiredTokenException;
import com.example.pnuunivmiryangcampus.support.token.exception.InvalidTokenException;
import com.example.pnuunivmiryangcampus.support.token.exception.JsonParsingException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jws;
Expand Down Expand Up @@ -31,7 +34,7 @@ public String getKidFromTokenHeader(String token) {
JSONObject jsonObject = new JSONObject(decodedHeader);
return jsonObject.get(KID).toString();
} catch (JSONException e) {
return e.toString();
throw new JsonParsingException(e.getMessage());
}
}

Expand All @@ -44,10 +47,10 @@ public Jws<Claims> getOIDCTokenJws(String token, OIDCPublicKeyDto oidcPublicKeyD
.build()
.parseSignedClaims(token);
} catch (ExpiredJwtException e) {
throw new Exception500(e.getMessage());
throw new ExpiredTokenException();
} catch (Exception e) {
log.error(e.toString());
throw new Exception500(e.getMessage());
throw new InvalidTokenException();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.support.token.exception;

import com.example.pnuunivmiryangcampus.support.exception.UnauthorizedException;

public class ExpiredTokenException extends UnauthorizedException {

private static final String MESSAGE = "Token의 유효기간이 만료되었습니다.";

public ExpiredTokenException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.support.token.exception;

import com.example.pnuunivmiryangcampus.support.exception.UnauthorizedException;

public class InvalidTokenException extends UnauthorizedException {

private static final String MESSAGE = "유효하지 않는 Token 입니다.";

public InvalidTokenException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.pnuunivmiryangcampus.support.token.exception;

import com.example.pnuunivmiryangcampus.support.exception.InternalException;

public class JsonParsingException extends InternalException {

private static final String MESSAGE = "JSON Parsing 과정에서 오류가 발생했습니다.";

public JsonParsingException(String message) {
super(MESSAGE + ": " + message);
}
}
Loading