diff --git a/BE/o2o/src/main/java/com/one/o2o/constants/ProductStatus.java b/BE/o2o/src/main/java/com/one/o2o/constants/ProductStatus.java new file mode 100644 index 0000000..e805ad3 --- /dev/null +++ b/BE/o2o/src/main/java/com/one/o2o/constants/ProductStatus.java @@ -0,0 +1,13 @@ +package com.one.o2o.constants; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum ProductStatus { + MISSING(6), + BROKEN(7); + + private final Integer statusId; +} \ No newline at end of file diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/GlobalExceptionHandler.java b/BE/o2o/src/main/java/com/one/o2o/exception/GlobalExceptionHandler.java index 347ee2a..9a06d1b 100644 --- a/BE/o2o/src/main/java/com/one/o2o/exception/GlobalExceptionHandler.java +++ b/BE/o2o/src/main/java/com/one/o2o/exception/GlobalExceptionHandler.java @@ -5,6 +5,8 @@ import com.one.o2o.exception.products.ProductException; import com.one.o2o.exception.products.report.ProductReportException; import com.one.o2o.exception.products.request.ProductRequestException; +import com.one.o2o.exception.rent.RentException; +import com.one.o2o.exception.reserve.ReserveException; import com.one.o2o.exception.user.UserException; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -66,4 +68,22 @@ public ResponseEntity handleProductRequestExceptionException(Prod ); return new ResponseEntity<>(errorResponse, e.getErrorCode().getHttpStatus()); } + + @ExceptionHandler(RentException.class) + public ResponseEntity handleRentException(RentException e) { + ErrorResponse errorResponse = ErrorResponse.of( + e.getErrorCode(), + e.getMessage() + ); + return new ResponseEntity<>(errorResponse, e.getErrorCode().getHttpStatus()); + } + + @ExceptionHandler(ReserveException.class) + public ResponseEntity handleReserveException(ReserveException e) { + ErrorResponse errorResponse = ErrorResponse.of( + e.getErrorCode(), + e.getMessage() + ); + return new ResponseEntity<>(errorResponse, e.getErrorCode().getHttpStatus()); + } } diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/locker/LockerErrorCode.java b/BE/o2o/src/main/java/com/one/o2o/exception/locker/LockerErrorCode.java index e9dd6b9..2b08468 100644 --- a/BE/o2o/src/main/java/com/one/o2o/exception/locker/LockerErrorCode.java +++ b/BE/o2o/src/main/java/com/one/o2o/exception/locker/LockerErrorCode.java @@ -8,7 +8,9 @@ public enum LockerErrorCode implements ErrorCode { LOCKER_ID_INVALID(HttpStatus.BAD_REQUEST, "잘못된 locker ID입니다."), LOCKER_ID_NEGATIVE(HttpStatus.BAD_REQUEST, "locker ID의 값은 1이상의 수입니다."), - LOCKER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 locker ID입니다."); + LOCKER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 locker ID입니다."), + TOTAL_COUNT_UNDER_PRODUCT_COUNT(HttpStatus.BAD_REQUEST, "올바르지 않은 물품 개수 입력입니다."), + PRODUCT_NOT_USABLE(HttpStatus.BAD_REQUEST, "제품을 대여 및 예약할 수 없습니다."); private final HttpStatus httpStatus; private final String message; diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/products/report/ProductReportErrorCode.java b/BE/o2o/src/main/java/com/one/o2o/exception/products/report/ProductReportErrorCode.java index 7c680fe..b24ed8b 100644 --- a/BE/o2o/src/main/java/com/one/o2o/exception/products/report/ProductReportErrorCode.java +++ b/BE/o2o/src/main/java/com/one/o2o/exception/products/report/ProductReportErrorCode.java @@ -9,7 +9,8 @@ @Getter public enum ProductReportErrorCode implements ErrorCode { PRODUCT_REPORT_ID_NOT_FOUND(HttpStatus.NOT_FOUND, "이상 신고 내용을 찾을 수 없습니다."), - PRODUCT_REPORT_CONTENT_LENGTH(HttpStatus.BAD_REQUEST, String.format("이상 신고 내용이 허용된 최대 길이(%d자)를 초과했습니다.", MAX_PRODUCT_REPORT_CONTENT_LENGTH)); + PRODUCT_REPORT_CONTENT_LENGTH(HttpStatus.BAD_REQUEST, String.format("이상 신고 내용이 허용된 최대 길이(%d자)를 초과했습니다.", MAX_PRODUCT_REPORT_CONTENT_LENGTH)), + INVALID_PRODUCT_STATUS_ID(HttpStatus.BAD_REQUEST, "잘못된 상태 입력입니다."); private final HttpStatus httpStatus; private final String message; diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentErrorCode.java b/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentErrorCode.java new file mode 100644 index 0000000..3fcf957 --- /dev/null +++ b/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentErrorCode.java @@ -0,0 +1,20 @@ +package com.one.o2o.exception.rent; + +import com.one.o2o.exception.ErrorCode; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum RentErrorCode implements ErrorCode { + RENT_ERROR_CODE(HttpStatus.BAD_REQUEST, "얼"); + + private final HttpStatus httpStatus; + private final String message; + private final Integer status; + + RentErrorCode(final HttpStatus httpStatus, final String message) { + this.httpStatus = httpStatus; + this.status = httpStatus.value(); + this.message = message; + } +} diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentException.java b/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentException.java index 56944b9..abea902 100644 --- a/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentException.java +++ b/BE/o2o/src/main/java/com/one/o2o/exception/rent/RentException.java @@ -1,8 +1,19 @@ package com.one.o2o.exception.rent; import com.one.o2o.exception.GeneralException; +import lombok.Getter; + +@Getter +public class RentException extends RuntimeException { + + private final RentErrorCode errorCode; + + public RentException(RentErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } + -public class RentException { public static class RentNotFoundException extends GeneralException { public RentNotFoundException(){ super("대여 내역을 찾을 수 없습니다."); diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveErrorCode.java b/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveErrorCode.java new file mode 100644 index 0000000..2121fe1 --- /dev/null +++ b/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveErrorCode.java @@ -0,0 +1,22 @@ +package com.one.o2o.exception.reserve; + +import com.one.o2o.exception.ErrorCode; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +public enum ReserveErrorCode implements ErrorCode { + RESERVE_ID_NOT_FOUND(HttpStatus.BAD_REQUEST, "예약 기록을 찾을 수 없습니다."), + RESERVE_TIME_INVALID(HttpStatus.BAD_REQUEST, "예약 시간이 유효하지 않습니다."); + + + private final HttpStatus httpStatus; + private final Integer status; + private final String message; + + ReserveErrorCode(final HttpStatus httpStatus, final String message) { + this.httpStatus = httpStatus; + this.status = httpStatus.value(); + this.message = message; + } +} diff --git a/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveException.java b/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveException.java index 6ead06a..5671aa1 100644 --- a/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveException.java +++ b/BE/o2o/src/main/java/com/one/o2o/exception/reserve/ReserveException.java @@ -1,8 +1,18 @@ package com.one.o2o.exception.reserve; import com.one.o2o.exception.GeneralException; +import lombok.Getter; + +@Getter +public class ReserveException extends RuntimeException { + + private final ReserveErrorCode errorCode; + + public ReserveException(ReserveErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } -public class ReserveException { public static class ReserveNotFoundException extends GeneralException { public ReserveNotFoundException(){ super("예약 내역을 찾을 수 없습니다."); diff --git a/BE/o2o/src/main/java/com/one/o2o/validator/LockerValidator.java b/BE/o2o/src/main/java/com/one/o2o/validator/LockerValidator.java index cded88f..d5e3f46 100644 --- a/BE/o2o/src/main/java/com/one/o2o/validator/LockerValidator.java +++ b/BE/o2o/src/main/java/com/one/o2o/validator/LockerValidator.java @@ -36,7 +36,21 @@ public void validateLockerId(Integer lockerId) { log.error("존재하지 않는 lockerId: {}", lockerId); throw new LockerException(LockerErrorCode.LOCKER_NOT_FOUND); } - log.info("유효한 lockerId 확인됨: {}", lockerId); } + + /** + * locker 물품 등록 시, 물품의 개수를 검증 + * 총 개수가 보다 물품 개수가 많은 경우 오류를 반환 + * + * @param productCnt + * @param totalCnt + */ + public void validateTotalProductCnt(Integer productCnt, Integer totalCnt) { + log.info("productCnt = {}", productCnt); + log.info("totalCnt = {}", totalCnt); + if (totalCnt < productCnt) { + throw new LockerException(LockerErrorCode.TOTAL_COUNT_UNDER_PRODUCT_COUNT); + } + } } diff --git a/BE/o2o/src/main/java/com/one/o2o/validator/ProductReportValidator.java b/BE/o2o/src/main/java/com/one/o2o/validator/ProductReportValidator.java index 7c67d3c..fe9a3b3 100644 --- a/BE/o2o/src/main/java/com/one/o2o/validator/ProductReportValidator.java +++ b/BE/o2o/src/main/java/com/one/o2o/validator/ProductReportValidator.java @@ -1,5 +1,6 @@ package com.one.o2o.validator; +import com.one.o2o.constants.ProductStatus; import com.one.o2o.exception.products.report.ProductReportErrorCode; import com.one.o2o.exception.products.report.ProductReportException; import com.one.o2o.repository.ProductsReportRepository; @@ -16,6 +17,12 @@ public class ProductReportValidator { @Autowired private ProductsReportRepository productsReportRepository; + /** + * 이상 신고의 처리 아이디 검증 + * 존재하지 않는 ID인 경우 오류를 반환 + * + * @param id 검증할 내용 + */ public void validateProductReportId(Integer id) { boolean exist = productsReportRepository.existsById(id); if (!exist) { @@ -24,7 +31,19 @@ public void validateProductReportId(Integer id) { } /** - * 제품 보고서의 내용 길이 검증 + * 이상 신고의 물품 상태 아이디 검증 + * 분실(6), 파손(7)이 아닌 경우 오류를 반환 + * + * @param statusId 검증할 내용 + */ + public void validateProductStatusId(Integer statusId) { + if (ProductStatus.MISSING.getStatusId() != statusId && ProductStatus.BROKEN.getStatusId() != statusId) { + throw new ProductReportException(ProductReportErrorCode.INVALID_PRODUCT_STATUS_ID); + } + } + + /** + * 이상 신고의 내용 길이 검증 * 내용의 길이가 허용된 최대 길이를 초과하는 경우 오류를 반환 * * @param content 검증할 내용 diff --git a/BE/o2o/src/main/java/com/one/o2o/validator/RentValidator.java b/BE/o2o/src/main/java/com/one/o2o/validator/RentValidator.java new file mode 100644 index 0000000..805b839 --- /dev/null +++ b/BE/o2o/src/main/java/com/one/o2o/validator/RentValidator.java @@ -0,0 +1,5 @@ +package com.one.o2o.validator; + +public class RentValidator { + +} diff --git a/BE/o2o/src/main/java/com/one/o2o/validator/ReserveValidator.java b/BE/o2o/src/main/java/com/one/o2o/validator/ReserveValidator.java new file mode 100644 index 0000000..b489338 --- /dev/null +++ b/BE/o2o/src/main/java/com/one/o2o/validator/ReserveValidator.java @@ -0,0 +1,42 @@ +package com.one.o2o.validator; + +import com.one.o2o.exception.reserve.ReserveErrorCode; +import com.one.o2o.exception.reserve.ReserveException; +import com.one.o2o.repository.ReserveRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; + +@Slf4j +@Component +@RequiredArgsConstructor +public class ReserveValidator { + + private final ReserveRepository reserveRepository; + + /** + * 예약 기록이 존재하는지 검증 + * 예약 기록이 DB에 없는 경우 오류를 반환 + * + * @param reserveId 검증할 예약 ID + */ + public void validateReserveId(Integer reserveId) { + if (!reserveRepository.existsById(reserveId)) { + throw new ReserveException(ReserveErrorCode.RESERVE_ID_NOT_FOUND); + } + } + + /** + * 예약한 수령 시간이 유효한지 검증 + * 예약한 수령 시간이 현재 시간보다 작은 경우 오류를 반환 + * + * @param reserveTime 검증할 예약 수령 시간 + */ + public void validateReserveDate(LocalDateTime reserveTime) { + if (reserveTime.isBefore(LocalDateTime.now())) { + throw new ReserveException(ReserveErrorCode.RESERVE_TIME_INVALID); + } + } +}