Esempio n. 1
0
        public void Handle(SendCreatePasswordMessageCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // get the establishment
            var establishment = _entities.Get <Establishment>()
                                .EagerLoad(_entities, new Expression <Func <Establishment, object> >[]
            {
                e => e.Type.Category,
            })
                                .ByEmail(command.EmailAddress);

            // get the person
            var person = _entities.Get <Person>()
                         .EagerLoad(_entities, new Expression <Func <Person, object> >[]
            {
                p => p.Emails,
                p => p.Affiliations,
            })
                         .ByEmail(command.EmailAddress);

            // create the person if they don't yet exist
            if (person == null)
            {
                var createPersonHandler = new CreatePersonHandler(_entities);
                var createPersonCommand = new CreatePersonCommand
                {
                    DisplayName = command.EmailAddress,
                };
                createPersonHandler.Handle(createPersonCommand);
                person = createPersonCommand.CreatedPerson;
            }

            // affiliate person with establishment
            person.AffiliateWith(establishment);

            // add email address if necessary
            if (person.GetEmail(command.EmailAddress) == null)
            {
                person.AddEmail(command.EmailAddress);
            }

            // save changes so nested command can find the correct data
            _unitOfWork.SaveChanges();

            // send the message
            var sendCommand = new SendConfirmEmailMessageCommand
            {
                EmailAddress = command.EmailAddress,
                Intent       = EmailConfirmationIntent.CreatePassword,
                SendFromUrl  = command.SendFromUrl,
            };

            _sendHandler.Handle(sendCommand);
            command.ConfirmationToken = sendCommand.ConfirmationToken;
        }
Esempio n. 2
0
        public async Task <IActionResult> Create([FromServices] CreatePersonHandler handler, CreatePersonModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            await handler.Handle(model);

            return(RedirectToAction("Home", "Index"));
        }
            public void ThrowsArgumentNullException_WhenCommandArgIsNull()
            {
                ArgumentNullException exception = null;
                var handler = new CreatePersonHandler(null);
                try
                {
                    handler.Handle(null);
                }
                catch (ArgumentNullException ex)
                {
                    exception = ex;
                }

                exception.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                exception.ParamName.ShouldEqual("command");
                // ReSharper restore PossibleNullReferenceException
            }
            public void CreatesPerson_WithFirstName()
            {
                var command = new CreatePersonCommand
                {
                    FirstName = "Adam",
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict);
                Person outPerson = null;
                entities.Setup(m => m.Create(It.IsAny<Person>()))
                    .Callback((Entity entity) => outPerson = (Person)entity);
                var handler = new CreatePersonHandler(entities.Object);

                handler.Handle(command);

                outPerson.ShouldNotBeNull();
                outPerson.ShouldEqual(command.CreatedPerson);
                outPerson.FirstName.ShouldEqual(command.FirstName);
                entities.Verify(m => m.Create(It.Is<Person>(p =>
                    p.FirstName == command.FirstName)), Times.Once());
            }
            public void CreatesPerson_AndUnregisteredUser()
            {
                var command = new CreatePersonCommand
                {
                    UserName = "******",
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict);
                Person outPerson = null;
                entities.Setup(m => m.Create(It.IsAny<Person>()))
                    .Callback((Entity entity) => outPerson = (Person)entity);
                var handler = new CreatePersonHandler(entities.Object);

                handler.Handle(command);

                outPerson.ShouldNotBeNull();
                outPerson.ShouldEqual(command.CreatedPerson);
                outPerson.User.ShouldNotBeNull();
                outPerson.User.Name.ShouldEqual(command.UserName);
                outPerson.User.IsRegistered.ShouldBeFalse();
                entities.Verify(m => m.Create(It.Is<Person>(p =>
                    p.User.Name == command.UserName)), Times.Once());
            }
 public TestCreatePersonCommand()
 {
     CreatePersonHandler = new CreatePersonHandler(PersonRepository.Object, EventBus.Object);
 }