Created
May 15, 2021 09:30
-
-
Save dev-aritra/11f7b9b2d43ea603bd92d8422cb80cc9 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.findByIdWithWriteLock(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