private static void AddErrorResponse(ErrorResponse errorResponse, string key, ModelErrorCollection errorss)
 {
     if (errorss != null && errorss.Count > 0)
     {
         foreach (var error in errorss)
         {
             var errorMessage = new ErrorResponse.ErrorMessage()
             {
                 Key     = key,
                 Message = error.ErrorMessage
             };
             errorResponse.ErrorMessages.Add(errorMessage);
         }
     }
 }
        public async Task <ResponseState> LoginValidator(string password, AppUser loggedinUser)
        {
            var response = new ResponseState();

            if (loggedinUser is null || !(await IsValidPassword(password, loggedinUser)))
            {
                var errorMessage = new ErrorResponse.ErrorMessage()
                {
                    Key     = AccountErrorConst.UserKey,
                    Message = AccountErrorConst.EmailOrPassNotCorrect
                };
                response.ErrorResponse.ErrorMessages.Add(errorMessage);
                response.ResponseCode = StatusCodes.Status401Unauthorized;
            }
            return(response);
        }
        private ResponseState ValidatePasswordMatching(string password, string confirmPassword)
        {
            var response = new ResponseState();

            if (!password.Trim().Equals(confirmPassword.Trim()))
            {
                var errorMessage = new ErrorResponse.ErrorMessage()
                {
                    Key     = AccountErrorConst.PasswordKey,
                    Message = AccountErrorConst.PasswordMatching
                };
                response.ErrorResponse.ErrorMessages.Add(errorMessage);
                response.ResponseCode = StatusCodes.Status422UnprocessableEntity;
            }
            return(response);
        }
        private ResponseState ValidateRole(string role)
        {
            var response = new ResponseState();

            if (!RolesConst.IsExist(role))
            {
                var errorMessage = new ErrorResponse.ErrorMessage()
                {
                    Key     = AccountErrorConst.RoleKey,
                    Message = AccountErrorConst.RoleIsNotFound
                };
                response.ErrorResponse.ErrorMessages.Add(errorMessage);
                response.ResponseCode = StatusCodes.Status422UnprocessableEntity;
            }

            return(response);
        }
        private async Task <ResponseState> CreateUser(AppUser model, string password)
        {
            var creatdUser = await _userManager.CreateAsync(model, password);

            var response = new ResponseState();

            if (!creatdUser.Succeeded)
            {
                var errorMessage = new ErrorResponse.ErrorMessage()
                {
                    Key     = AccountErrorConst.UserKey,
                    Message = AccountErrorConst.UserFaildToCreate
                };
                response.ErrorResponse.ErrorMessages.Add(errorMessage);
                response.ResponseCode = StatusCodes.Status422UnprocessableEntity;
            }
            return(response);
        }
        private async Task <ResponseState> ValidateExistingUser(string email)
        {
            var response = new ResponseState();

            var existingUser = await _userManager.FindByEmailAsync(email);

            if (existingUser is not null)
            {
                var errorMessage = new ErrorResponse.ErrorMessage()
                {
                    Key     = AccountErrorConst.UserKey,
                    Message = AccountErrorConst.UserAlreadyExist
                };
                response.ErrorResponse.ErrorMessages.Add(errorMessage);
                response.ResponseCode = StatusCodes.Status422UnprocessableEntity;
            }

            return(response);
        }
        private async Task <ResponseState> ValidatePasswordSyntax(string password)
        {
            var response = new ResponseState();

            var passwordValidator = new PasswordValidator <AppUser>();
            var result            = await passwordValidator.ValidateAsync(_userManager, null, password);

            if (!result.Succeeded)
            {
                var errorMessage = new ErrorResponse.ErrorMessage()
                {
                    Key     = AccountErrorConst.PasswordKey,
                    Message = AccountErrorConst.PasswordNotValid
                };
                response.ErrorResponse.ErrorMessages.Add(errorMessage);
                response.ResponseCode = StatusCodes.Status422UnprocessableEntity;
            }

            return(response);
        }