Last active
January 25, 2017 11:36
-
-
Save gytis/134a5324dce9a04ffc04e280a44f7230 to your computer and use it in GitHub Desktop.
Blog :: New Narayana release and Spring Boot quickstart :: Buy operation
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
@PutMapping("/portfolio/{username}/{symbol}") | |
@Transactional | |
public void buy(@PathVariable String username, @PathVariable String symbol, @RequestParam("amount") int amount) { | |
User user = getUser(username); | |
Share share = getShare(symbol); | |
PortfolioEntry portfolioEntry = getOrCreatePortfolioEntry(user, share); // Get user's portfolio entry or create a new one | |
updateBudget(user, -1 * amount * share.getPrice()); // Decrease user's budget | |
updateSharesAmount(share, -1 * amount); // Decrease shares amount available for sale | |
updatePortfolioEntry(portfolioEntry, amount); // Increase shares amount owned by the user | |
sendUpdate(String.format("'%s' just bought %d shares of '%s' for the amount of %d", username, amount, symbol, | |
amount * share.getPrice())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment