예제 #1
0
 public void The_project_parameters_request(String clientId, string name, string code, decimal budget, DateTime date)
 {
     _projectName = ProjectName.From(name);
     _projectCode = ProjectCode.From(code);
     _startDate   = DateAndTime.From(date);
     _budget      = Money.From(budget);
     _clientId    = EntityId.From(Guid.Parse(clientId));
 }
예제 #2
0
 public static Project ToProject(this ProjectState state)
 => Project.From(
     EntityId.From(state.Id),
     ProjectName.From(state.Name),
     ProjectCode.From(state.Code),
     DateAndTime.From(state.StartDate),
     Money.From(state.Budget),
     EntityId.From(state.ClientId),
     Email.From(state.Owner),
     ProjectStatus.From(state.Status),
     ServiceOrderNumber.From(state.OrderNumber),
     Version.From(BitConverter.ToInt32(state.RowVersion)));
        public void project_add_command_succed()
        {
            var fixture = new Fixture().Customize(new AutoNSubstituteCustomization {
                ConfigureMembers = true
            });

            fixture.Register <EntityId>(() => EntityId.From(fixture.Create <Guid>()));
            fixture.Register <ProjectName>(() => ProjectName.From(fixture.Create <string>()));
            fixture.Register <ProjectCode>(() => ProjectCode.From(fixture.Create <string>()));
            fixture.Register <DateAndTime>(() => DateAndTime.From(fixture.Create <DateTime>()));
            fixture.Register <ProjectStatus>(() => ProjectStatus.Default());
            fixture.Register <ServiceOrderNumber>(() => ServiceOrderNumber.Empty());
            fixture.Register <Email>(() => Email.From(string.Format($"{fixture.Create<string>()}@teste.com")));
            fixture.Register <Project>(() => Project.NewRequest(fixture.Create <EntityId>(),
                                                                fixture.Create <ProjectName>(), fixture.Create <ProjectCode>(),
                                                                fixture.Create <DateAndTime>(), fixture.Create <Money>(), fixture.Create <EntityId>()));
            fixture.Register <User>(() => User.From(fixture.Create <EntityId>(),
                                                    fixture.Create <Name>(), fixture.Create <SocialSecurityId>(),
                                                    fixture.Create <Email>(), fixture.Create <Version>()));

            var finalProject = fixture.Create <Project>();
            var finalClient  = fixture.Create <User>();

            var command = fixture.Build <AddProjectCommand>()
                          .With(project => project.Name, finalProject.Name.Value)
                          .With(project => project.Code, finalProject.Code.Value)
                          .With(project => project.Budget, finalProject.Budget.Value)
                          .With(project => project.ClientId, finalProject.ClientId.Value)
                          .With(project => project.StartDate, finalProject.StartDate.Value)
                          .Create();

            var mediator  = fixture.Create <IMediator>();
            var logger    = fixture.Create <ILogger <AddProjectCommandHandler> >();
            var projectDb = fixture.Create <IDbSession <IProjectRepository> >();
            var userDb    = fixture.Create <IDbSession <IUserRepository> >();
            var handler   = new AddProjectCommandHandler(logger, mediator, projectDb, userDb);

            var result = handler.Execute(command);

            userDb.Received().Repository.Get(finalProject.ClientId).Returns(finalClient);
            projectDb.Received().Repository.Add(finalProject);
            projectDb.Received().SaveChanges();
            mediator.Received(1).Publish(Arg.Any <ProjectAddedEvent>());

            Assert.True(result.IsSucceed);
        }
        protected override CommandResult <Guid> ExecuteCommand(AddProjectCommand command)
        {
            var isSucceed     = false;
            var aggregationId = Guid.Empty;

            _logger.LogDebug("Criada agregação a partir do comando {CommandName} com valores {Valores}",
                             nameof(command), command);

            var client = _dbUserSession.Repository.Get(EntityId.From(command.ClientId));

            var agg = ProjectAggregationRoot.CreateFrom(
                ProjectName.From(command.Name),
                ProjectCode.From(command.Code),
                Money.From(command.Budget),
                DateAndTime.From(command.StartDate),
                client.Id);

            if (agg.ValidationResults.IsValid)
            {
                // _logger.LogInformation($"Agregação Project valida id gerado", agg.GetChange().Id);

                using (_logger.BeginScope("Persistencia"))
                {
                    _dbSession.Repository.Add(agg.GetChange());
                    _dbSession.SaveChanges();
                }

                // _logger.LogInformation($"Project persistido ID: {agg.GetChange().Id}");
                using (_logger.BeginScope("Publicacão de Eventos"))
                {
                    agg.GetEvents().ToImmutableList().ForEach(ev => Publisher.Publish(ev));
                }

                isSucceed     = true;
                aggregationId = agg.GetChange().Id.Value;
            }

            return(new CommandResult <Guid>(isSucceed, aggregationId, agg.ValidationResults.Errors.ToImmutableList()));
        }