public async System.Threading.Tasks.Task UpdateEntryCommandHandlerTest_Success()
        {
            //Given
            var entryName        = "TestUser";
            var entryPhoneNumber = "0817810008";
            var entryName2       = "TestUser2";

            var phoneBook = new PhoneBookAggregateRoot {
                Name = _phoneBookName
            };

            phoneBook.InsertEntry(entryName, entryPhoneNumber);
            var entryId = phoneBook.Entries.FirstOrDefault().Id;

            await _phoneBookRepositoryMock.AddAsync(phoneBook);

            var updateEntryCommandHandler = new UpdateEntryCommandHandler(_phoneBookRepositoryMock);
            var updateEntryCommand        = new UpdateEntryCommand {
                Name = entryName2, PhoneNumber = entryPhoneNumber, PhoneBookId = phoneBook.Id, EntryId = entryId
            };

            //When
            await updateEntryCommandHandler.HandleAsync(updateEntryCommand);

            //Then
            var entry = ((PhoneBookRepositoryMock)_phoneBookRepositoryMock).phoneBookAggregates.FirstOrDefault().Entries.FirstOrDefault();

            Assert.NotNull(entry);
            Assert.Equal(entryName2, entry.Name);
            Assert.Equal(entryPhoneNumber, entry.PhoneNumber);
        }
        public async Task HandleAsync(CreatePhoneBookCommand command)
        {
            var phoneBook = await _phoneBookRepository.GetPhoneBookByNameAsync(command.Name);

            if (phoneBook != null)
            {
                throw new ValidateException("Phone book already exists");
            }
            await _phoneBookRepository.AddAsync(new Entities.PhoneBookAggregateRoot(command.Name));

            await _phoneBookRepository.SaveAsync();
        }
Ejemplo n.º 3
0
        public async System.Threading.Tasks.Task DeleteEntryCommandHandler_Success()
        {
            //Given
            var phoneBook = new PhoneBookAggregateRoot {
                Name = _phoneBookName
            };

            phoneBook.InsertEntry("TestUser", "0817810008");
            var entryId = phoneBook.Entries.FirstOrDefault().Id;

            await _phoneBookRepositoryMock.AddAsync(phoneBook);

            var deleteEntryCommandHandler = new DeleteEntryCommandHandler(_phoneBookRepositoryMock);
            var deleteEntryCommand        = new DeleteEntryCommand {
                PhoneBookId = phoneBook.Id, EntryId = entryId
            };

            //When
            await deleteEntryCommandHandler.HandleAsync(deleteEntryCommand);

            //Then
            Assert.Empty(((PhoneBookRepositoryMock)_phoneBookRepositoryMock).phoneBookAggregates.FirstOrDefault().Entries);
        }
Ejemplo n.º 4
0
        public async System.Threading.Tasks.Task CreatePhoneBookCommandHandler_Fail()
        {
            //Given
            var createPhoneBookCommandHandler = new CreatePhoneBookCommandHandler(_phoneBookRepositoryMock);
            await _phoneBookRepositoryMock.AddAsync(new PhoneBookAggregateRoot { Name = _phoneBookName });

            var createPhoneBookCommand = new CreatePhoneBookCommand
            {
                Name = _phoneBookName
            };

            //When
            await Assert.ThrowsAsync <ValidateException>(() => createPhoneBookCommandHandler.HandleAsync(createPhoneBookCommand));
        }
Ejemplo n.º 5
0
        public async Task <PhoneBookResponse> SaveAsync(PhoneBook phonebook)
        {
            try
            {
                await _phonebookRepository.AddAsync(phonebook);

                await _unitOfWork.CompleteAsync();

                return(new PhoneBookResponse(phonebook));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new PhoneBookResponse($"An error occurred when saving the phonebook: {ex.Message}"));
            }
        }