public async Task <IActionResult> Post([FromBody] AccountDTO accountDto, CancellationToken ct)
        {
            var addNewAccountCommand = new AddNewAccountCommand(
                new Name {
                FirstName = accountDto.Name.FirstName, LastName = accountDto.Name.LastName
            },
                accountDto.Addresses.Select(addr =>
                                            new Address(
                                                addr.FistLineOfAddress,
                                                Enum.Parse <AddressType>(addr.AddressType),
                                                addr.State,
                                                addr.ZipCode))
                .ToList(),
                new ContactDetails {
                Email = accountDto.ContactDetails.Email, TelephoneNumber = accountDto.ContactDetails.TelephoneNumber
            },
                new CardDetails {
                CardNumber = accountDto.CardDetails.CardNumber, CardSecurityCode = accountDto.CardDetails.CardSecurityCode
            }
                );

            await _commandProcessor.SendAsync(addNewAccountCommand, false, ct);

            var account = await _queryProcessor.ExecuteAsync(new GetAccountById(addNewAccountCommand.Id), ct);

            return(Ok(AccountDTO.FromQueryResult(account)));
        }
        public async Task <IActionResult> AddNewAccount(AddNewAccountCommand command)
        {
            try
            {
                await _mediator.Send(command);

                TempData["successMessage"] = "New account successfully created and added";
                return(RedirectToAction("Index", "Customer", new { customerId = command.CustomerId }));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                TempData["errorMessage"] = ex.Message;
                return(View("ManageAccounts", await _mediator.Send(new GetAccountTransferDataQuery {
                    CustomerId = command.CustomerId
                })));
            }
        }
        public async Task When_adding_an_account()
        {
            //arrange
            var commandProcessor = new FakeCommandProcessor();
            var handler          = new AddNewAccountHandlerAsync(_options, commandProcessor);
            var command          = new AddNewAccountCommand()
            {
                Id   = Guid.NewGuid(),
                Name = new Name {
                    FirstName = "Jack", LastName = "Torrance"
                },
                Addresses = new List <Address>
                {
                    new Address("Overlook Hotel", AddressType.Billing, "CO", "80517")
                },
                ContactDetails = new ContactDetails {
                    Email = "*****@*****.**", TelephoneNumber = "666-6666"
                },
                CardDetails = new CardDetails {
                    CardNumber = "4104231121998973", CardSecurityCode = "517"
                }
            };

            //act
            await handler.HandleAsync(command);

            var savedAccount = await new EFUnitOfWork(new AccountContext(_options)).GetAsync(command.Id);

            //assert
            Assert.That(savedAccount.AccountId, Is.EqualTo(command.Id));
            Assert.That(savedAccount.Name.FirstName, Is.EqualTo(command.Name.FirstName));
            Assert.That(savedAccount.Name.LastName, Is.EqualTo(command.Name.LastName));
            var expectedAddress = command.Addresses.First();
            var savedAddress    = savedAccount.Addresses.FirstOrDefault();

            Assert.That(savedAddress, Is.Not.Null);
            Assert.That(savedAddress.AddressType, Is.EqualTo(expectedAddress.AddressType));
            Assert.That(savedAddress.FistLineOfAddress, Is.EqualTo(expectedAddress.FistLineOfAddress));
            Assert.That(savedAddress.State, Is.EqualTo(expectedAddress.State));
            Assert.That(savedAddress.ZipCode, Is.EqualTo(expectedAddress.ZipCode));

            Assert.IsTrue(commandProcessor.RaiseAccountEvent);
            Assert.IsTrue(commandProcessor.AllSent);
        }