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

refactor: 경매 구매 요청 시 경매 재고가 부족하다면 200응답을 주도록 변경한다. #321

Merged
merged 3 commits into from
Aug 27, 2024

Conversation

HiiWee
Copy link
Member

@HiiWee HiiWee commented Aug 27, 2024

📄 Summary

  • 기존 검증 코드는 아래와 같습니다.
    image
  • Auction.canPurchase(...)에서 2개의 재고 검증 로직을 분리하여 처리하도록 변경했습니다.
    // AS IS
    private boolean canPurchase(long quantity) {
        if (quantity <= 0) {
            return false;
        }
        if (quantity > maximumPurchaseLimitCount) {
            return false;
        }
    
        return currentStock >= quantity;
    }
    
    // TO BE
    private boolean isOutOfBoundQuantity(long quantity) {
        return quantity > maximumPurchaseLimitCount || quantity <= 0;
    }
    
    private boolean hasEnoughStock(long quantity) {
        return currentStock >= quantity;
    }
  • 따라서 다음과 같이 예외를 분기할 수 있습니다.
    private void verifyPurchaseQuantity(long quantity) {
        if (isOutOfBoundQuantity(quantity)) {
            String message = String.format("구매 가능 갯수를 초과하거나 0이하의 갯수만큼 구매할 수 없습니다. 요청: %d, 인당구매제한: %d", quantity,
                    maximumPurchaseLimitCount);
            throw new BadRequestException(message, ErrorCode.A030);
        }
        if (!hasEnoughStock(quantity)) {
            String message = String.format("재고가 부족합니다. 현재 재고: %d, 요청 구매 수량: %d", currentStock, quantity);
            throw new SuccessfulOperationException(message, ErrorCode.A012);
        }
    }
  • SuccessfulOperationException은 다음과 같이 200응답을 반환하도록 예외를 처리하고 있습니다.
    @ExceptionHandler(SuccessfulOperationException.class)
    public ResponseEntity<ErrorResponse> handleSuccessfulOperationException(final SuccessfulOperationException e) {
        log.warn("SUCCESS RESULT CODE {} : {}", e.getErrorCode(), e.getMessage());
        return ResponseEntity.ok()
                .body(ErrorResponse.of(e.getMessage(), e.getErrorCode().name()));
    }

🙋🏻 More

  • 이 작업을 통해 A012 에러 코드는 200응답을 반환하게 됩니다! @chhs2131
    image

@HiiWee HiiWee requested a review from a team August 27, 2024 05:59
@HiiWee HiiWee self-assigned this Aug 27, 2024
@HiiWee HiiWee added the REFACTOR 코드 개선 label Aug 27, 2024
Copy link
Collaborator

@chhs2131 chhs2131 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^_^b 좋습니다 ㅎㅎ

@HiiWee HiiWee merged commit 63eebfb into dev Aug 27, 2024
3 checks passed
@chhs2131 chhs2131 deleted the refactor/317 branch August 31, 2024 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REFACTOR 코드 개선
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REFACTOR] 경매 구매 요청 시 경매 재고가 부족하다면 200응답을 주도록 변경한다.
2 participants