Skip to content

Commit

Permalink
Merge pull request #20 from comento-backend-camp/feature/18
Browse files Browse the repository at this point in the history
Feature/18
  • Loading branch information
hyejungg authored Oct 18, 2021
2 parents a1cbe50 + c0c7665 commit 2f8767a
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum ErrorCode {

INVALID_VALUE(400, HttpStatus.BAD_REQUEST, "BAD REQUEST", "잘못된 요청입니다. 다시 요청해주세요."),
NO_USER(401, HttpStatus.UNAUTHORIZED,"UNAUTHORIZED", "등록되지 않은 사용자입니다"),
NO_DATA(404, HttpStatus.NOT_FOUND, "NOT FOUND ERROR", "요청할 수 없는 리소스입니다."),
NOT_FOUND(404, HttpStatus.NOT_FOUND, "NOT FOUND ERROR", "요청할 수 없는 리소스입니다."),
INVALID_USER(409, HttpStatus.CONFLICT, "CONFLICT", "이미 존재하는 사용자입니다."),
INVALID_SEAT(409, HttpStatus.CONFLICT, "CONFLICT", "이미 예약된 좌석입니다."),
SERVER_ERROR(500, HttpStatus.INTERNAL_SERVER_ERROR, "INTERVAL_SERVER ERROR", "서버 에러입니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class ErrorResponse<T>{
private String message;
private String reason;

//공통된 Error Message를 전송할 때 사용
public static ErrorResponse res(final Integer status, final String message, final String reason){
return ErrorResponse.builder()
.status(status)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package comento.backend.ticket.config;

import com.fasterxml.jackson.core.JsonParseException;
import comento.backend.ticket.config.customException.DuplicatedException;
import comento.backend.ticket.config.customException.NoAuthException;
import comento.backend.ticket.config.customException.NotFoundDataException;
import javassist.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
Expand All @@ -22,57 +18,36 @@
public class GlobalExceptionHandler {
private static ErrorCode errorCode;

/**
* 특정 Exception을 지정하여 별도로 처리
*/

//ConstraintViolationException.class 는 유효성 검사 실패시 (@Validated)
@ExceptionHandler({JsonParseException.class,
MethodArgumentNotValidException.class,
ConstraintViolationException.class,
MethodArgumentTypeMismatchException.class,
MissingServletRequestParameterException.class})
public static ResponseEntity missMatchExceptionHandler(Throwable t){
public static ResponseEntity<ErrorResponse> missMatchExceptionHandler(){
errorCode = ErrorCode.INVALID_VALUE;
log.error(errorCode.getStatus() + " " + errorCode.getMessage(), t);
return new ResponseEntity<>(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()),
errorCode.getHttpStatus());
}

@ExceptionHandler(NoAuthException.class)
public static ResponseEntity noAuthExceptionHandler(NoAuthException e){
errorCode = ErrorCode.NO_USER;
log.error(errorCode.getStatus() + errorCode.getMessage(), e);
return new ResponseEntity<>(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()),
errorCode.getHttpStatus());
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage());
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}

@ExceptionHandler({NoSuchElementException.class, NotFoundException.class, NotFoundDataException.class})
public static ResponseEntity notFoundExceptionHandler(Throwable t){
errorCode = ErrorCode.NO_DATA;
log.error(errorCode.getStatus() + " " + errorCode.getMessage(), t);
return new ResponseEntity<>(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()),
errorCode.getHttpStatus());
@ExceptionHandler(CustomException.class)
public static ResponseEntity customExceptionHandler(CustomException e){
errorCode = e.getErrorCode();
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage());
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}

@ExceptionHandler(DuplicatedException.class)
public static ResponseEntity duplicateExceptionHandler(DuplicatedException e){
if (e.getMessage() == "UserService") { //호출된 곳이 UserService
errorCode = ErrorCode.INVALID_USER;
log.error(errorCode.getStatus() + " " + errorCode.getMessage(), e);
}else{ //좌석 예약 시
errorCode = ErrorCode.INVALID_SEAT;
log.error(errorCode.getStatus() + " " + errorCode.getMessage(), e);
}
return new ResponseEntity<>(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()),
errorCode.getHttpStatus());
@ExceptionHandler({NoSuchElementException.class, NotFoundException.class})
public static ResponseEntity notFoundExceptionHandler(){
errorCode = ErrorCode.NOT_FOUND;
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage());
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}

@ExceptionHandler({IllegalStateException.class, RuntimeException.class})
public static ResponseEntity IllExceptionHandler(Throwable t){
@ExceptionHandler(Exception.class)
public static ResponseEntity IllExceptionHandler(Exception e){
errorCode = ErrorCode.SERVER_ERROR;
log.error(errorCode.getStatus() + " " + errorCode.getMessage(), t);
return new ResponseEntity<>(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()),
errorCode.getHttpStatus());
log.error(String.valueOf(errorCode.getHttpStatus()), errorCode.getMessage(), e);
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.res(errorCode.getStatus(), errorCode.getMessage(), errorCode.getReason()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
@Getter
@AllArgsConstructor
public enum SuccessCode {

OK(200, HttpStatus.OK, "OK"),
CREATED(201, HttpStatus.CREATED, "CREATED");
CREATED(201, HttpStatus.CREATED, "CREATED"),
NO_DATA(204, HttpStatus.NO_CONTENT, "데이터가 존재하지 않습니다");

private final Integer status;
private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class SuccessResponse<T> {
private String message;
private Object data;

//공통된 Success Message를 전송할 때 사용
public static SuccessResponse res(final Integer status, final String message, final Object data){
return SuccessResponse.builder()
.status(status)
Expand All @@ -21,4 +20,11 @@ public static SuccessResponse res(final Integer status, final String message, fi
.build();
}

public static SuccessResponse res(final Integer status, final String message){
return SuccessResponse.builder()
.status(status)
.message(message)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package comento.backend.ticket.config.customException;

public class DuplicatedException extends RuntimeException{
public DuplicatedException() {
}
import comento.backend.ticket.config.CustomException;
import comento.backend.ticket.config.ErrorCode;

public DuplicatedException(String message) {
super(message);
public class DuplicatedException extends CustomException {
public DuplicatedException(ErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package comento.backend.ticket.config.customException;

public class NoAuthException extends RuntimeException{
public NoAuthException() {
}
import comento.backend.ticket.config.CustomException;
import comento.backend.ticket.config.ErrorCode;

public NoAuthException(String message) {
super(message);
public class NoAuthException extends CustomException {
public NoAuthException(ErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package comento.backend.ticket.config.customException;

public class NotFoundDataException extends RuntimeException{
public NotFoundDataException() {
}
import comento.backend.ticket.config.CustomException;
import comento.backend.ticket.config.ErrorCode;

public NotFoundDataException(String message) {
super(message);
public class NotFoundDataException extends CustomException {
public NotFoundDataException(ErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package comento.backend.ticket.controller;

import comento.backend.ticket.config.ErrorCode;
import comento.backend.ticket.config.ErrorResponse;
import comento.backend.ticket.config.SuccessCode;
import comento.backend.ticket.config.SuccessResponse;
import comento.backend.ticket.config.customException.DuplicatedException;
import comento.backend.ticket.domain.Performance;
import comento.backend.ticket.domain.Seat;
import comento.backend.ticket.domain.User;
import comento.backend.ticket.dto.BookingDto;
import comento.backend.ticket.dto.BookingResponse;
import comento.backend.ticket.dto.BookingResponseCreated;
import comento.backend.ticket.service.BookingService;
import comento.backend.ticket.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,27 +24,47 @@
@RequestMapping("/api/performance/booking")
public class BookingController {
private final BookingService bookingService;
private final BookingHistoryService bookingHistoryService;
private final UserService userService;
private final PerformanceService performanceService;
private final SeatService seatService;

private SuccessCode successCode;
private BookingResponseCreated result;

@Autowired
public BookingController(BookingService bookingService) {
public BookingController(BookingService bookingService, BookingHistoryService bookingHistoryService, UserService userService, PerformanceService performanceService, SeatService seatService) {
this.bookingService = bookingService;
this.bookingHistoryService = bookingHistoryService;
this.userService = userService;
this.performanceService = performanceService;
this.seatService = seatService;
}

@PostMapping("")
public ResponseEntity addBooking(@Valid @RequestBody BookingDto reqBooking){
bookingService.saveBookging(reqBooking);
BookingResponseCreated result = new BookingResponseCreated(reqBooking.getSeatType(), reqBooking.getSeatNumber());
successCode = SuccessCode.CREATED;
return new ResponseEntity(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result),
HttpStatus.CREATED);
final User user = userService.getUser(reqBooking.getEmail());
final Performance performance = performanceService.getPerformance(reqBooking.getId(), reqBooking.getTitle());
final Seat seat = seatService.getIsBooking(performance, reqBooking.getSeatType(), reqBooking.getSeatNumber()); //false라면 예약 가능

if(seat.isBooking()){ //true면 이미 예약된 상태
bookingHistoryService.saveBookingFailLog(user, performance, seat);
throw new DuplicatedException(ErrorCode.INVALID_SEAT);
}else{
bookingService.saveBooking(user, performance, seat, reqBooking);
bookingHistoryService.saveBookingSucessLog(user, performance, seat);
result = new BookingResponseCreated(reqBooking.getSeatType(), reqBooking.getSeatNumber());
successCode = SuccessCode.CREATED;
}
return ResponseEntity.status(successCode.getHttpStatus())
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result));
}

@GetMapping("/email/{email}")
public ResponseEntity showMyBooking(@Valid @PathVariable String email){
List<BookingResponse> result = bookingService.getMyBooking(email);
successCode = SuccessCode.OK;
return new ResponseEntity(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result),
HttpStatus.OK);
return ResponseEntity.status(successCode.getHttpStatus())
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public ResponseEntity showPerformanceInfo(@Valid @RequestParam(value = "date", r
@Valid @RequestParam(value = "title", required = false) String title) {
PerformanceDto performanceDto = new PerformanceDto(title, date);
List<PerformanceResponse> result = performanceService.getListPerformance(performanceDto);

return new ResponseEntity(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result),
HttpStatus.OK);
return ResponseEntity.ok().body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), result));
}

@GetMapping("/info/seat")
Expand All @@ -54,8 +52,6 @@ public ResponseEntity showPerformanceSeatInfo(@Valid @RequestParam(value = "date
List<PerformanceResponse> performanceData = performanceService.getListPerformance(performanceDto);

List<SeatResponse> seatResult = seatService.getListPerformanceSeat(performanceData.get(0));

return new ResponseEntity(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), seatResult),
HttpStatus.OK);
return ResponseEntity.ok().body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), seatResult));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import comento.backend.ticket.dto.UserDto;
import comento.backend.ticket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
Expand All @@ -26,7 +27,7 @@ public UserController(UserService userService) {
@PostMapping("/signup")
public ResponseEntity addEmail(@Validated @RequestBody UserDto userDto){
userService.saveUser(userDto);
return new ResponseEntity<>(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), CREATED_MSG),
successCode.getHttpStatus());
return ResponseEntity.status(successCode.getHttpStatus())
.body(SuccessResponse.res(successCode.getStatus(), successCode.getMessage(), CREATED_MSG));
}
}
Loading

0 comments on commit 2f8767a

Please sign in to comment.