public async Task <ProjectUserMessage> Handle(AddUserProjectCommand request, CancellationToken cancellationToken)
        {
            var userDomain    = _UserRepository.GetById(request.UserId).Result.ToDomain <User>(_Mapper);
            var projectDomain = _ProjectRepository.GetById(request.ProjectId).Result.ToDomain <Project>(_Mapper);

            projectDomain.Validate();
            //projectDomain.AddUser(userDomain);

            #region Persistence

            await _ProjectRepository.AddUser(projectDomain.ToModel <Command.UserProject>(_Mapper));

            await _UnitOfWork.Commit();

            #endregion

            #region Bus

            var addUserProjectMessage = new Message();
            addUserProjectMessage.MessageType = "UpdateUserProject";
            var response = new ProjectUserMessage
            {
                Project = projectDomain.ToQueryModel <Query.Project>(_Mapper),
                User    = userDomain.ToQueryModel <UserQuery.User>(_Mapper)
            };
            addUserProjectMessage.SetData(response);

            await _SendEndpointProvider.Send(addUserProjectMessage, cancellationToken);

            #endregion

            return(response);
        }
Exemple #2
0
        public async Task <AddUserProjectPayload> AddUserProject(
            UserProjectInput input,
            [Service] IServiceProvider serviceProvider,
            CancellationToken cancellationToken)
        {
            var addUserProjectCommand = new AddUserProjectCommand()
            {
                ProjectId = input.ProjectId,
                UserId    = input.UserId
            };

            using (var scope = serviceProvider.CreateScope())
            {
                var mediator = scope.ServiceProvider.GetRequiredService <IMediator>();
                try
                {
                    var response = await mediator.Send(addUserProjectCommand, cancellationToken);

                    return(new AddUserProjectPayload(response.Project, response.User));
                }
                catch (ValidationException e)
                {
                    var userErrors = new List <UserError>();
                    e.Message.Split(";").ForAll(item =>
                    {
                        userErrors.Add(new UserError(item, item));
                    });
                    return(new AddUserProjectPayload(userErrors));
                }
            }
        }
        public void ProjectCommandHandler_Handle_AddUserProjectCommand_AddInvalidUserValidProject()
        {
            IRequestHandler <AddUserProjectCommand, bool> handler = GetCommandHandlerInstance();
            var commandData = new AddUserProjectCommand()
            {
                ProjectId = FacebookId, UserId = Guid.NewGuid()
            };

            Action handle = () => handler.Handle(commandData, default).Wait();

            handle.Should().Throw <ElementNotFoundException>();
        }
        public void ProjectCommandHandler_Handle_AddUserProjectCommand_AddInvalidUserRepeatedProject()
        {
            IRequestHandler <AddUserProjectCommand, bool> handler = GetCommandHandlerInstance();
            var commandData = new AddUserProjectCommand()
            {
                ProjectId = FacebookId, UserId = MarkId
            };

            Action handle = () => handler.Handle(commandData, default).Wait();

            handle.Should().Throw <ValidationException>();
        }
        public void ProjectCommandHandler_Handle_AddUserProjectCommand_AddValidUserValidProject()
        {
            IRequestHandler <AddUserProjectCommand, bool> handler = GetCommandHandlerInstance();
            var commandData = new AddUserProjectCommand()
            {
                ProjectId = FacebookId, UserId = GatesId
            };

            var handle = handler.Handle(commandData, default).Result;

            handle.Should().BeTrue();
        }