public async Task <IActionResult> Put(string id, [FromBody] AccountDTO accountDto, CancellationToken ct) { var updateExistingAccountCommand = new UpdateExistingAccountCommand( Guid.Parse(id), 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(updateExistingAccountCommand, false, ct); var account = await _queryProcessor.ExecuteAsync(new GetAccountById(updateExistingAccountCommand.AccountId), ct); return(Ok(AccountDTO.FromQueryResult(account))); }
public async Task When_trying_to_update_a_locked_Account() { var commandProcessor = new FakeCommandProcessor(); using (var uow = new AccountContext(_options)) { //Add new account directly via repository not handler (needs both entries so no unit of work) var id = Guid.NewGuid(); var account = new Account() { AccountId = id, 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" }, }; var repository = new AccountRepositoryAsync(new EFUnitOfWork(uow)); await repository.AddAsync(account); //create update command var updateCommand = new UpdateExistingAccountCommand ( id, new Name { FirstName = "Here's", LastName = "Johnny!!!" }, new List <Address> { new Address("Overlook Hotel", AddressType.Billing, "CO", "80517") }, new ContactDetails { Email = "*****@*****.**", TelephoneNumber = "666-6666" }, new CardDetails { CardNumber = "4104231121998973", CardSecurityCode = "517" } ); updateCommand.LockBy = "GRADY"; //Lock the existing account var aggregateLock = await repository.LockAsync(id.ToString(), "SYS"); //now try to update whilst locked var handler = new UpdateExistingAccountCommandHandlerAsync(_options, commandProcessor); Assert.ThrowsAsync <CannotGetLockException>(async() => await handler.HandleAsync(updateCommand)); //release the lock await aggregateLock.ReleaseAsync(); //now we should be able to get the lock and update await handler.HandleAsync(updateCommand); var amendedAccount = await repository.GetAsync(id); Assert.That(amendedAccount.Name.FirstName, Is.EqualTo(updateCommand.Name.FirstName)); } }
public async Task When_updating_an_account() { var commandProcessor = new FakeCommandProcessor(); using (var uow = new AccountContext(_options)) { var id = Guid.NewGuid(); var account = new Account() { AccountId = id, 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" } }; var repository = new AccountRepositoryAsync(new EFUnitOfWork(uow)); await repository.AddAsync(account); //create update command var updateCommand = new UpdateExistingAccountCommand ( id, new Name { FirstName = "Here's", LastName = "Johnny!!!" }, new List <Address> { new Address("Overlook Hotel", AddressType.Billing, "CO", "80517") }, new ContactDetails { Email = "*****@*****.**", TelephoneNumber = "666-6666" }, new CardDetails { CardNumber = "4104231121998973", CardSecurityCode = "517" } ); var handler = new UpdateExistingAccountCommandHandlerAsync(_options, commandProcessor); //act //issue update command await handler.HandleAsync(updateCommand); //assert //versions and change to current state var updateAccount = await repository.GetAsync(id); Assert.That(updateAccount.AccountId, Is.EqualTo(id)); Assert.That(updateAccount.Name.FirstName, Is.EqualTo(updateCommand.Name.FirstName)); Assert.That(updateAccount.Name.LastName, Is.EqualTo(updateCommand.Name.LastName)); } }