Created
May 9, 2021 14:46
-
-
Save dev-aritra/2b0de256a41605e8908440210ec879bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Transactional | |
public Optional<Order> placeOrder(Long productId, Long userId) { | |
Optional<Product> product = productRepository.findById(productId); | |
product.orElseThrow(() -> new RuntimeException("Invalid product id")); | |
if (product.get().getAvailableUnits() > 0) { | |
productRepository.decrementAvailableUnitsCountBy1(productId); | |
Order newOrder = new Order(userId, productId); | |
orderRepository.save(newOrder); | |
return Optional.of(newOrder); | |
} | |
return Optional.empty(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment