Exemplo n.º 1
0
        public void Changing_email_from_corporate_to_non_corporate()
        {
            // Arrange
            User user;

            using (var transaction = new Transaction(ConnectionString))
            {
                var userRepository    = new UserRepository(transaction);
                var companyRepository = new CompanyRepository(transaction);
                user = CreateUser(
                    "*****@*****.**", UserType.Employee, userRepository);
                CreateCompany("mycorp.com", 1, companyRepository);

                transaction.Commit();
            }

            var busSpy     = new BusSpy();
            var messageBus = new MessageBus(busSpy);
            var loggerMock = new Mock <IDomainLogger>();

            string result;

            using (var transaction = new Transaction(ConnectionString))
            {
                var sut = new UserController(transaction, messageBus, loggerMock.Object);

                // Act
                result = sut.ChangeEmail(user.UserId, "*****@*****.**");
            }

            // Assert
            Assert.Equal("OK", result);

            using (var transaction = new Transaction(ConnectionString))
            {
                var userRepository    = new UserRepository(transaction);
                var companyRepository = new CompanyRepository(transaction);

                object[] userData   = userRepository.GetUserById(user.UserId);
                User     userFromDb = UserFactory.Create(userData);
                Assert.Equal("*****@*****.**", userFromDb.Email);
                Assert.Equal(UserType.Customer, userFromDb.Type);

                object[] companyData   = companyRepository.GetCompany();
                Company  companyFromDb = CompanyFactory.Create(companyData);
                Assert.Equal(0, companyFromDb.NumberOfEmployees);

                busSpy.ShouldSendNumberOfMessages(1)
                .WithEmailChangedMessage(user.UserId, "*****@*****.**");
                loggerMock.Verify(
                    x => x.UserTypeHasChanged(
                        user.UserId, UserType.Employee, UserType.Customer),
                    Times.Once);
            }
        }
Exemplo n.º 2
0
        public string ChangeEmail(int userId, string newEmail)
        {
            object[] userData = _userRepository.GetUserById(userId);
            User     user     = UserFactory.Create(userData);

            string error = user.CanChangeEmail();

            if (error != null)
            {
                return(error);
            }

            object[] companyData = _companyRepository.GetCompany();
            Company  company     = CompanyFactory.Create(companyData);

            user.ChangeEmail(newEmail, company);

            _companyRepository.SaveCompany(company);
            _userRepository.SaveUser(user);
            _eventDispatcher.Dispatch(user.DomainEvents);

            _transaction.Commit();
            return("OK");
        }