Пример #1
0
        public async Task <IActionResult> CreateUserAsync([FromBody] Client.UserCreationInfo creationInfo,
                                                          CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (creationInfo == null)
            {
                var error = ServiceErrorResponses.BodyIsMissing("UserCreationInfo");
                return(BadRequest(error));
            }

            var  hashPassword      = PasswordEncoder.Encode(creationInfo.Password);
            var  modelCreationInfo = new UserCreationInfo(creationInfo.Login, hashPassword);
            User modelUser         = null;

            try
            {
                modelUser = await repository.CreateAsync(modelCreationInfo, cancellationToken)
                            .ConfigureAwait(false);
            }
            catch (UserDuplicationException)
            {
                var error = ServiceErrorResponses.UserDuplication(modelCreationInfo.Login);
                return(BadRequest(error));
            }

            var clientUser = UserConverter.Convert(modelUser);

            return(Ok(clientUser));
        }