public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = "{accountId}/deposit")] HttpRequest req, ILogger log) { try { var validatedRequest = await req.ValidateRequest <MakeDepositRequest, MakeDepositRequestValidator>(); if (!validatedRequest.IsValid) { return(validatedRequest.ToBadRequest()); } var body = validatedRequest.Value; var command = new MakeDepositCommand(new AccountId(body.AccountId), body.Amount, body.Description); var result = await commandBus.Handle(command); return(new OkObjectResult(new MakeDepositResponse(result.Value.Value))); } catch (Exception ex) { log.LogError(ex.StackTrace); throw ex; } }
/// <summary> /// Make the deposit /// </summary> /// <param name="makeDepositCommand"> </param> /// <returns></returns> public bool MakeDeposit(MakeDepositCommand makeDepositCommand) { if (_fundsValidationService.IsTierVerified(makeDepositCommand.AccountId, makeDepositCommand.IsCryptoCurrency).Item1) { Balance balance = _balanceRepository.GetBalanceByCurrencyAndAccountId(new Currency(makeDepositCommand.Currency), new AccountId(makeDepositCommand.AccountId)); if (balance == null) { balance = new Balance(new Currency(makeDepositCommand.Currency), new AccountId(makeDepositCommand.AccountId), makeDepositCommand.Amount, makeDepositCommand.Amount); } else { balance.AddAvailableBalance(makeDepositCommand.Amount); balance.AddCurrentBalance(makeDepositCommand.Amount); } _fundsPersistenceRepository.SaveOrUpdate(balance); if (makeDepositCommand.IsCryptoCurrency) { Deposit deposit = new Deposit(new Currency(makeDepositCommand.Currency, true), Guid.NewGuid().ToString(), DateTime.Now, DepositType.Default, makeDepositCommand.Amount, 0, TransactionStatus.Confirmed, new AccountId(makeDepositCommand.AccountId), null, null); _fundsPersistenceRepository.SaveOrUpdate(deposit); } return(true); } throw new InvalidOperationException("Require Tier Level is not verified."); }
public async void ShoulMakeDeposit() { var entityStore = new Mock <IEntityStore>(); var bankAccount = new BankAccount(); bankAccount.Open("John Doe"); var accountId = bankAccount.Id; var command = new MakeDepositCommand(accountId, 1.0m, "Bob's Auto Repair"); entityStore .Setup(mock => mock.Load <BankAccount>(accountId)) .ReturnsAsync(bankAccount); entityStore .Setup(mock => mock.Save(It.IsAny <BankAccount>())); var makeDepositHandler = new MakeDepositHandler(entityStore.Object); var commandResult = await makeDepositHandler.Handle(command); commandResult.Success.Should().Be(true); var depositId = commandResult.Value; depositId.Should().NotBe(default);
public async Task MakeDepositAsync(MakeDepositCommand command) { var account = BuildAccountFromDomainEvents(command.AccountNumber); var events = account.MakeDeposit(command); await this.EventStore.AddEventsAsync(events); }
public void Deposit(MakeDepositCommand makeDepositCommand) { var amountToDeposit = new Money(makeDepositCommand.AmountInPence); var accountId = new AccountId(makeDepositCommand.AccountId); var account = _accountRepository.Retrieve(accountId); _accountRepository.Save(account.Deposit(amountToDeposit, GetNow())); }
public async Task <Result <EntityId> > Handle(MakeDepositCommand command) { var entity = await entityStore.Load <BankAccount>(command.AccountId); var depositId = entity.MakeDeposit(command.Description, command.Amount); await entityStore.Save(entity); return(Result <EntityId> .Ok(depositId)); }
public IEnumerable <AggregateEvent> MakeDeposit(MakeDepositCommand command) { if (this.AccountState != AccountState.Created) { throw new InvalidOperationException($"Account {AccountNumber} is not available."); } var aggregateEvent = new BalanceIncreased(command.AccountNumber, ++SequenceNumber, command.Amount); Handle(aggregateEvent); return(new AggregateEvent[] { aggregateEvent }); }
public async void MakeDepsoitCommandIsSentOnTheCommandBus_ThenTheCorrectHandlerShouldBeCalled() { var depositId = new DepositId(); mockDepositHandler .Setup(mock => mock.Handle(It.IsAny <MakeDepositCommand>())) .ReturnsAsync(Result <EntityId> .Ok(depositId)); var command = new MakeDepositCommand(new AccountId(), 1.23m, "A Purchase"); var result = await commandBus.Handle(command); mockDepositHandler.Verify(mock => mock.Handle(command)); result.Success.Should().Be(true); result.Value.Should().Be(depositId); }
public async Task Deposit([FromBody] MakeDepositCommand command) { await this.AccountCommands.MakeDepositAsync(command); }
public async Task Deposit([FromBody] MakeDepositDto request) { var command = new MakeDepositCommand(request.AccountNumber, request.Amount); await this.AccountCommands.MakeDepositAsync(command); }