Exemplo n.º 1
0
        public async Task <Result> CreateAsync(Action <UserCreateAction> action, CancellationToken cancellationToken = new CancellationToken())
        {
            cancellationToken.RequestCanceled(LogInfo);

            var impl = new UserCreateActionImpl();

            action(impl);

            DefinedUser definition = impl.Definition.Value;

            string user = impl.User.Value;

            if (string.IsNullOrWhiteSpace(user))
            {
                throw new UserCredentialsMissingException("The username and/or password is missing.");
            }

            if (string.IsNullOrWhiteSpace(definition.Password) && string.IsNullOrWhiteSpace(definition.PasswordHash))
            {
                throw new UserCredentialsMissingException("The username and/or password is missing.");
            }

            string url = $"api/users/{user}";

            HttpResponseMessage response = await HttpPut(url, definition, cancellationToken);

            Result result = response.GetResponse();

            LogInfo($"Sent request to RabbitMQ server to create user '{user}'");

            return(result);
        }
Exemplo n.º 2
0
        public async Task <Result> Create(Action <UserCreateAction> action, CancellationToken cancellationToken = default)
        {
            cancellationToken.RequestCanceled();

            var impl = new UserCreateActionImpl();

            action(impl);

            DefinedUser definition = impl.Definition.Value;

            Debug.Assert(definition != null);

            string user = impl.User.Value;

            var errors = new List <Error>();

            if (string.IsNullOrWhiteSpace(user))
            {
                errors.Add(new ErrorImpl("The username is missing."));
            }

            if (string.IsNullOrWhiteSpace(definition.Password) && string.IsNullOrWhiteSpace(definition.PasswordHash))
            {
                errors.Add(new ErrorImpl("The password/hash is missing."));
            }

            if (errors.Any())
            {
                return(new FaultedResult(errors));
            }

            string url = $"api/users/{user}";

            Result result = await Put(url, definition, cancellationToken);

            return(result);
        }