From 4ef0de81a801a74d83e0943e894f2c538c49cdd3 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Fri, 23 Aug 2024 19:47:27 +0300 Subject: [PATCH] Adds base class for all API controllers. --- .../Controllers/Common/ApiController.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/Lumina.Presentation.Api/Controllers/Common/ApiController.cs diff --git a/src/Lumina.Presentation.Api/Controllers/Common/ApiController.cs b/src/Lumina.Presentation.Api/Controllers/Common/ApiController.cs new file mode 100644 index 00000000..730fd53a --- /dev/null +++ b/src/Lumina.Presentation.Api/Controllers/Common/ApiController.cs @@ -0,0 +1,68 @@ +#region ========================================================================= USING ===================================================================================== +using ErrorOr; +using Lumina.Presentation.Api.Common.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ModelBinding; +#endregion + +namespace Lumina.Presentation.Api.Controllers.Common; + +/// +/// Base class for API controllers. +/// +[ApiController] +[ApiVersion("1.0")] +public abstract class ApiController : ControllerBase +{ + #region ===================================================================== METHODS =================================================================================== + /// + /// Handles controllers errors. + /// + /// The error to handle. + protected internal IActionResult Problem(List errors) + { + if (errors.Count is 0) // if there are no custom errors given, return a generic problem + return Problem(); + // try to make the problem type more specific + if (errors.All(error => error.Type == ErrorType.Validation)) + return ValidationProblem(errors); + // here we could handle custom error types, for instance: if (errors.All(error => error.NumericType == 23)), where 23 could be "domain errors" + + // share the list of errors within the scope of this HTTP request + HttpContext.Items[HttpContextItemKeys.ERRORS] = errors; + // get a HTTP error status code for the selected domain error type + return Problem(errors.First()); + } + + /// + /// Converts an to a standard problem response. + /// + /// The error to convert. + protected internal IActionResult Problem(Error error) + { + var statusCode = error.Type switch + { + ErrorType.Conflict => StatusCodes.Status409Conflict, + ErrorType.NotFound => StatusCodes.Status404NotFound, + ErrorType.Validation => StatusCodes.Status400BadRequest, + ErrorType.Failure => StatusCodes.Status403Forbidden, + _ => StatusCodes.Status500InternalServerError, + + }; + // return the HTTP error status and the domain error message + return Problem(statusCode: statusCode, title: error.Type.ToString(), detail: error.Description); + } + + /// + /// Converts a list of into a standard validation problem response. + /// + /// The list of errors to convert. + protected internal IActionResult ValidationProblem(List errors) + { + var modelStateDictionary = new ModelStateDictionary(); + foreach (var error in errors) + modelStateDictionary.AddModelError(error.Code, error.Description); + return ValidationProblem(modelStateDictionary: modelStateDictionary, title: "One or more validation errors occurred."); + } + #endregion +} \ No newline at end of file