コード例 #1
0
        public Result <Guid> CreateUser([FromBody] UserDto userDto)
        {
            try
            {
                if (CanSaveUserValidation(userDto, out var failResult))
                {
                    return(failResult);
                }

                var user = userDto.CreateUser();

                var roleName = "";
                if (userDto.Role != null)
                {
                    var roleNameResult = Executor.GetQuery <GetRoleByIdQuery>().Process(q => q.Execute(userDto.Role.Id));
                    if (roleNameResult.IsFailure)
                    {
                        return(Result <Guid> .Fail(Guid.Empty, "Выбранная роль не найдена"));
                    }

                    roleName = roleNameResult.Value.Name;
                }

                Executor.CommandChain()
                .AddCommand <CreateUserCommand>(c => c.Execute(user))
                .AddCommand <CreateUserClaimCommand>(c => c.Execute(UserClaimDto.UserNameClaim(userDto, user.Id)))
                .AddCommand <CreateUserClaimCommand>(c => c.Execute(UserClaimDto.UserRoleClaim(roleName, user.Id)))
                .AddCommand <CreateUserClaimCommand>(c => c.Execute(UserClaimDto.UserGenderClaim(userDto, user.Id)))
                .ExecuteAllWithTransaction();

                return(Result <Guid> .Ok(user.Id));
            }
            catch (Exception exception)
            {
                return(Result <Guid> .Fail(Guid.Empty, $"{exception.Message}, {exception.StackTrace}"));
            }
        }
コード例 #2
0
        public void CreateUserCommand_UserCreated_Success()
        {
            var executor = GetExecutor();

            var roleResult = executor.GetQuery <GetRoleByNameQuery>().Process(q => q.Execute(RoleDtoTestData.AdminisrationRoleDto.Name));

            roleResult.Should().NotBeNull();
            var role = roleResult.Value;

            var userDto = new UserDto
            {
                FirstName = "CreatedUser",
                Role      = role,
                IsActive  = true
            };

            var createdUserIdResult = executor.GetCommand <CreateUserCommand>().Process(c => c.Execute(userDto.CreateUser()));

            createdUserIdResult.Should().NotBeNull($"{nameof(createdUserIdResult)} is null");

            var createdUserDtoResult = executor.GetQuery <GetUserByIdQuery>().Process(q => q.Execute(createdUserIdResult.Value));

            createdUserDtoResult.Should().NotBeNull();
            createdUserDtoResult.IsFailure.Should().BeFalse(createdUserDtoResult.Error);
            createdUserDtoResult.Value.Should().NotBeNull();
            createdUserDtoResult.Value.FirstName.Should().Be("CreatedUser");
        }