Ejemplo n.º 1
0
        public async Task <IActionResult> ChasngeUserPassword([FromBody, CustomizeValidator] UserPasswordToChange userInfo, CancellationToken cancellationToken = default)
        {
            if (userInfo is null || !ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var result = await _userService.UpdatePasswordAsync(userInfo, cancellationToken);

                return(result.IsError ? throw new InvalidOperationException(result.Message)
                     : result.IsSuccess ? (IActionResult)Ok(result.IsSuccess)
                     : StatusCode(StatusCodes.Status206PartialContent, result.Message.CollectProblemDetailsPartialContent(HttpContext)));
            }
            catch (InvalidOperationException ex)
            {
                Log.Error(ex, ex.Message);
                return(StatusCode(StatusCodes.Status500InternalServerError, new CustumResult()
                {
                    Status = StatusCodes.Status500InternalServerError, Message = ex.Message
                }));
            }
        }
        public async Task <Result <UserView> > UpdatePasswordAsync(UserPasswordToChange userInfo, CancellationToken cancellationToken = default)
        {
            AppUser usersIdentity = await _userManager.FindByIdAsync(userInfo.IdFromIdentity);

            if (usersIdentity is null)
            {
                return(Result <UserView> .Fail <UserView>(ExceptionConstants.USER_WAS_NOT_FOUND));
            }
            try
            {
                await _userManager.ChangePasswordAsync(usersIdentity, userInfo.CurrentPassword, userInfo.NewPassword);

                UserView view = GetUserByIdFromIdentityAsync(userInfo.IdFromIdentity).Result.Data;
                return(Result <UserView> .Ok(view));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Result <UserView> .Fail <UserView>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message));
            }
            catch (DbUpdateException ex)
            {
                return(Result <UserView> .Fail <UserView>(ExceptionConstants.CANNOT_UPDATE_MODEL + ex.Message));
            }
        }