private void returnDifferenceInBidPriceAndExecutedPrice(long bidPrice, long executedPrice, long amountOfExecutedItems) { long totalDifferenceInCents = amountOfExecutedItems * bidPrice - amountOfExecutedItems * executedPrice; if (totalDifferenceInCents > 0) { commandGateway.send(new CancelCashReservationCommand(portfolioId, transactionId, totalDifferenceInCents)); } } }
@RequestMapping("/debit") @Transactional @ResponseBody public void doDebit(@RequestParam("acc") String accountNumber, @RequestParam("amount") double amount) { DebitAccountCommand debitAccountCommandCommand = new DebitAccountCommand(accountNumber, amount); commandGateway.send(debitAccountCommandCommand); }
@RequestMapping(value = "/api/tasks/{identifier}/complete", method = RequestMethod.POST) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void createTask(@PathVariable String identifier) { commandGateway.send(new CompleteTaskCommand(identifier)); }
@RequestMapping(value = "/api/tasks/{identifier}/title", method = RequestMethod.POST) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void createTask(@PathVariable String identifier, @RequestBody @Valid ModifyTitleRequest request) { commandGateway.send(new ModifyTaskTitleCommand(identifier, request.getTitle())); }
@RequestMapping(method = RequestMethod.POST) public void create() { LOGGER.info("start"); AccountId id = new AccountId(); LOGGER.debug("Account id: {}", id.toString()); commandGateway.send(new CreateAccountCommand(id, "MyAccount",1000)); commandGateway.send(new WithdrawMoneyCommand(id, 500)); commandGateway.send(new WithdrawMoneyCommand(id, 300)); commandGateway.send(new CreateAccountCommand(id, "MyAccount", 1000)); commandGateway.send(new WithdrawMoneyCommand(id, 500)); } }
@RequestMapping(value = "/api/tasks", method = RequestMethod.POST) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void createTask(Principal principal, @RequestBody @Valid CreateTaskRequest request) { commandGateway.send(new CreateTaskCommand(identifierFactory.generateIdentifier(), principal.getName(), request.getTitle())); }
@RequestMapping("/credit") @Transactional @ResponseBody public void doCredit(@RequestParam("acc") String accountNumber, @RequestParam("amount") double amount) { CreditAccountCommand creditAccountCommandCommand = new CreditAccountCommand(accountNumber, amount); GenericCommandMessage<CreditAccountCommand> message = new GenericCommandMessage<>(creditAccountCommandCommand); commandGateway.send(creditAccountCommandCommand, new LoggingCallback(message)); }
@PostMapping("/rooms/{roomId}/participants") public Future<Void> joinChatRoom(@PathVariable String roomId, @RequestBody @Valid Participant participant) { Assert.notNull(participant.getName(), "name is mandatory for a chatroom"); return commandGateway.send(new JoinRoomCommand(participant.getName(), roomId)); }
@PostMapping("/rooms/{roomId}/messages") public Future<Void> postMessage(@PathVariable String roomId, @RequestBody @Valid Message message) { Assert.notNull(message.getName(), "'name' missing - please provide a participant name"); Assert.notNull(message.getMessage(), "'message' missing - please provide a message to post"); return commandGateway.send(new PostMessageCommand(message.getName(), roomId, message.getMessage())); }
@EventHandler public void on(CompanyCreatedEvent event) { logger.debug("About to dispatch a new command to create an OrderBook for the company {}", event.getCompanyId()); OrderBookId orderBookId = new OrderBookId(); commandGateway.send(new CreateOrderBookCommand(orderBookId)); commandGateway.send(new AddOrderBookToCompanyCommand(event.getCompanyId(), orderBookId)); } }
@MessageMapping("/bank-transfers/create") public void create(BankTransferDto bankTransferDto) { log.info("Create bank transfer with payload {}", bankTransferDto); String bankTransferId = UUID.randomUUID().toString(); CreateBankTransferCommand command = new CreateBankTransferCommand(bankTransferId, bankTransferDto.getSourceBankAccountId(), bankTransferDto.getDestinationBankAccountId(), bankTransferDto.getAmount()); commandGateway.send(command); } }
@PostMapping("/modify") public Future<?> modifyMenuItem(@PathVariable String id, @RequestBody MenuItemRequestBody body) { String description = body.getDescription(); BigDecimal price = body.getPrice(); FutureCallback<ModifyMenuItemCommand, Object> callback = new FutureCallback<>(); commandGateway.send(new ModifyMenuItemCommand(id, description, price), callback); return callback.toCompletableFuture(); }
@SagaEventHandler(associationProperty = "transactionId") public void handle(BuyTransactionCancelledEvent event) { long amountToCancel = (event.getTotalAmountOfItems() - event.getAmountOfExecutedItems()) * pricePerItem; logger.debug( "Buy Transaction {} is cancelled, amount of cash reserved to cancel is {}", transactionId, amountToCancel ); commandGateway.send(new CancelCashReservationCommand(portfolioId, transactionId, amountToCancel)); }
@SagaEventHandler(associationProperty = "buyTransactionId", keyName = "transactionId") public void handle(TradeExecutedEvent event) { long tradeCount = event.getTradeCount(); long tradePrice = event.getTradePrice(); logger.debug( "Buy Transaction {} is executed, items for transaction are {} for a price of {}", transactionId, tradeCount, tradePrice ); commandGateway.send(new ExecutedTransactionCommand(transactionId, tradeCount, tradePrice)); }
@SagaEventHandler(associationProperty = "sellTransactionId", keyName = "transactionId") public void handle(TradeExecutedEvent event) { long tradeCount = event.getTradeCount(); long tradePrice = event.getTradePrice(); logger.debug( "Sell Transaction {} is executed, items for transaction are {} for a price of {}", transactionId, tradeCount, tradePrice ); commandGateway.send(new ExecutedTransactionCommand(transactionId, tradeCount, tradePrice)); }
@SagaEventHandler(associationProperty = "sellTransactionId", keyName = "transactionId") public void handle(TradeExecutedEvent event) { long tradeCount = event.getTradeCount(); long tradePrice = event.getTradePrice(); logger.debug( "Sell Transaction {} is executed, items for transaction are {} for a price of {}", transactionId, tradeCount, tradePrice ); commandGateway.send(new ExecutedTransactionCommand(transactionId, tradeCount, tradePrice)); }
@SuppressWarnings("unused") @SagaEventHandler(associationProperty = "transactionId") public void handle(BuyTransactionConfirmedEvent event) { logger.debug("Buy Transaction {} is approved to make the buy order", transactionId); commandGateway.send(new CreateBuyOrderCommand(new OrderId(), portfolioId, orderBookId, transactionId, totalItems, pricePerItem)); }
@PostMapping("/add") public Future<?> addAddress(@PathVariable String customerId, @RequestBody AddressRequestBody body) { String addressId = randomUUID().toString(); String location = body.getLocation(); String nickName = body.getNickName(); FutureCallback<AddAddressCommand, Object> callback = new FutureCallback<>(); commandGateway.send(new AddAddressCommand(customerId, addressId, nickName, location), callback); return callback .thenApply(v -> links.linkForSingleResource(Address.class, addressId).withSelfRel().getHref()) .toCompletableFuture(); }
@SagaEventHandler(associationProperty = "transactionId") public void handle(SellTransactionConfirmedEvent event) { logger.debug("Sell Transaction {} is approved to make the sell order", event.getTransactionId()); commandGateway.send(new CreateSellOrderCommand(new OrderId(), portfolioId, orderBookId, transactionId, totalItems, pricePerItem)); }
@EventHandler public void on(UserCreatedEvent event) { logger.debug("About to dispatch a new command to create a Portfolio for the new user {}", event.getUserId()); commandGateway.send(new CreatePortfolioCommand(new PortfolioId(), event.getUserId())); } }