Skip to content

Commit

Permalink
feat: use api layer to handle error
Browse files Browse the repository at this point in the history
  • Loading branch information
Harish-osmosys committed Mar 13, 2024
1 parent 67faf5d commit 71b712d
Show file tree
Hide file tree
Showing 26 changed files with 307 additions and 262 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Net;
using DotnetFoundation.Api.Helpers;
using DotnetFoundation.Application.Exceptions;
using DotnetFoundation.Application.Interfaces.Services;
using DotnetFoundation.Application.Interfaces.Validator;
using DotnetFoundation.Application.Models.Common;
using DotnetFoundation.Application.Models.DTOs.AuthenticationDTO;
using DotnetFoundation.Application.Models.Enums;
Expand All @@ -11,14 +11,14 @@ namespace DotnetFoundation.Api.Controllers;

[ApiController]
[Route("/api/auth")]
public class AuthenticationController : ControllerBase
public class AuthenticationController : BaseController
{
private readonly IAuthenticationService _authenticationService;
private readonly ErrorResponse _errorResponse;
public AuthenticationController(IAuthenticationService authenticationService, ErrorResponse errorResponse)
private readonly IUserValidator _userValidator;
public AuthenticationController(IAuthenticationService authenticationService, IUserValidator userValidator)
{
_authenticationService = authenticationService;
_errorResponse = errorResponse;
_userValidator = userValidator;
}

/// <summary>
Expand All @@ -34,6 +34,12 @@ public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> RegisterAs
BaseResponse<AuthenticationResponse> response = new(ResponseStatus.Fail);
try
{
bool isRegisteredEmail = await _userValidator.IsEmailRegistered(request.Email).ConfigureAwait(false);
if (isRegisteredEmail)
{
ModelState.AddModelError("email", "Email already in use");
throw new IdentityUserException(ErrorValues.GenricValidationMessage);
}
response.Data = await _authenticationService.RegisterAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

Expand All @@ -43,13 +49,13 @@ public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> RegisterAs
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
Expand Down Expand Up @@ -77,13 +83,13 @@ public async Task<ActionResult<BaseResponse<AuthenticationResponse>>> LoginAsync
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
Expand All @@ -102,6 +108,12 @@ public async Task<ActionResult<BaseResponse<int>>> ResetPasswordAsync(PasswordRe
BaseResponse<int> response = new(ResponseStatus.Fail);
try
{
bool isValidEmail = await _userValidator.ValidEmailId(request.Email).ConfigureAwait(false);
if (!isValidEmail)
{
ModelState.AddModelError("email", "Error Finding User");
throw new UserNotFoundException(ErrorValues.GenricNotFoundMessage);
}
await _authenticationService.ResetPasswordAsync(request).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

Expand All @@ -111,20 +123,20 @@ public async Task<ActionResult<BaseResponse<int>>> ResetPasswordAsync(PasswordRe
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (InvalidTokenException ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
response.Status = ResponseStatus.Error;
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
Expand All @@ -143,6 +155,12 @@ public async Task<ActionResult<BaseResponse<int>>> ForgotPasswordAsync(string em
BaseResponse<int> response = new(ResponseStatus.Fail);
try
{
bool isValidEmail = await _userValidator.ValidEmailId(email).ConfigureAwait(false);
if (!isValidEmail)
{
ModelState.AddModelError("email", "Error Finding User");
throw new UserNotFoundException(ErrorValues.GenricNotFoundMessage);
}
await _authenticationService.ForgotPasswordAsync(email).ConfigureAwait(false);
response.Status = ResponseStatus.Success;

Expand All @@ -152,14 +170,14 @@ public async Task<ActionResult<BaseResponse<int>>> ForgotPasswordAsync(string em
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
return BadRequest(response);
}
catch (Exception ex)
{
response.Message = ex.Message;
response.Status = ResponseStatus.Error;
response.Errors = _errorResponse.GetErrorResponse();
response.Errors = GetErrorResponse();
return StatusCode(StatusCodes.Status500InternalServerError, response);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace DotnetFoundation.Api.Controllers;

public class BaseController : ControllerBase
{
protected Dictionary<string, List<string>> GetErrorResponse()
{
return ModelState
.Where(modelError => modelError.Value!.Errors.Any())
.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value!.Errors.Select(error => error.ErrorMessage).ToList()
);
}
}
Loading

0 comments on commit 71b712d

Please sign in to comment.