Exemplo n.º 1
0
        public async Task <IActionResult> CreateUserAsync([FromBody] CreateUserRequest user)
        {
            try
            {
                _logger.LogInformation("Trying to create a new user.");

                var currentUserId         = GetCurrentUserId();
                var isRequestingUserAdmin = await _userBusiness
                                            .CheckAdminPrivilegesAsync(currentUserId)
                                            .ConfigureAwait(false);

                if (!isRequestingUserAdmin)
                {
                    _logger.LogInformation($"User with id {currentUserId} tried to manipulate user data.");
                    return(Unauthorized());
                }

                var newUserModel = await _userBusiness
                                   .CreateUserAsync(user.Map())
                                   .ConfigureAwait(false);

                return(Ok(newUserModel.Map()));
            }
            catch (InvalidUserDataException ex)
            {
                _logger.LogWarning(ex, "The specified object contained errors", user);
                return(BadRequest());
            }
            catch (UserAlreadyExistsException ex)
            {
                _logger.LogWarning("The user that was tried to create already exists", ex);
                return(BadRequest());
            }
            catch (Exception ex)
            {
                _logger.LogError("An unexpected error occured while creating a new user.", ex);
                return(StatusCode(500));
            }
        }