Skip to content

Instantly share code, notes, and snippets.

@gytis
Last active January 25, 2017 11:36
Show Gist options
  • Save gytis/134a5324dce9a04ffc04e280a44f7230 to your computer and use it in GitHub Desktop.
Save gytis/134a5324dce9a04ffc04e280a44f7230 to your computer and use it in GitHub Desktop.
Blog :: New Narayana release and Spring Boot quickstart :: Buy operation
@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