using DigitalData.Core.Application.Interfaces; using Microsoft.AspNetCore.Mvc; using DigitalData.Core.Application.DTO; namespace DigitalData.Core.API { /// /// A base controller class that provides generic CRUD (Create, Read, Update, Delete) operations for a specified entity type, /// with enhanced error handling to ensure robust and reliable API endpoints. /// /// The derived CRUD service type implementing ICRUDService. /// The Data Transfer Object type for create operations. /// The Data Transfer Object type for read operations. /// The entity type CRUD operations will be performed on. /// The type of the entity's identifier. [ApiController] [Route("api/[controller]")] [Obsolete("Use MediatR")] public class CRUDControllerBaseWithErrorHandling : ControllerBase where TCRUDService : ICRUDService where TCreateDto : class where TReadDto : class where TUpdateDto : class where TEntity : class { protected readonly ILogger _logger; protected readonly TCRUDService _service; /// /// Initializes a new instance of the CRUDControllerBase class with specified logger and CRUD service. /// /// The logger to be used by the controller. /// The CRUD service handling business logic for the entity. public CRUDControllerBaseWithErrorHandling( ILogger logger, TCRUDService service) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _service = service ?? throw new ArgumentNullException(nameof(service)); } /// /// Creates a new entity based on the provided DTO. /// /// The DTO from which to create the entity. /// A task that represents the asynchronous create operation. The task result contains the action result. [HttpPost] public virtual async Task Create(TCreateDto createDto) { try { return await _service.CreateAsync(createDto).ThenAsync( Success: id => { var createdResource = new { Id = id }; var actionName = nameof(GetById); var routeValues = new { id = createdResource.Id }; return CreatedAtAction(actionName, routeValues, createdResource); }, Fail: (messages, notices) => { _logger.LogNotice(notices); return BadRequest(messages); }); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } /// /// Retrieves an entity by its identifier. /// /// The identifier of the entity to retrieve. /// A task that represents the asynchronous read operation. The task result contains the action result. [HttpGet("{id}")] public virtual async Task GetById([FromRoute] TId id) { try { return await _service.ReadByIdAsync(id).ThenAsync( Success: Ok, Fail: IActionResult (messages, notices) => { _logger.LogNotice(notices); return NotFound(messages); }); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } /// /// Retrieves all entities. /// /// A task that represents the asynchronous read-all operation. The task result contains the action result. [HttpGet] public virtual async Task GetAll() { try { return await _service.ReadAllAsync().ThenAsync( Success: Ok, Fail: IActionResult (messages, notices) => { _logger.LogNotice(notices); return NotFound(messages); }); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } /// /// Updates an existing entity based on the provided DTO. /// /// The DTO containing the updated data for the entity. /// A task that represents the asynchronous update operation. The task result contains the action result. [HttpPut] public virtual async Task Update(TUpdateDto updateDto) { try { return await _service.UpdateAsync(updateDto).ThenAsync( Success: Ok, Fail: IActionResult (messages, notices) => { _logger.LogNotice(notices); return BadRequest(messages); }); } catch(Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } /// /// Deletes an entity by its identifier. /// /// The identifier of the entity to delete. /// A task that represents the asynchronous delete operation. The task result contains the action result. [HttpDelete("{id}")] public virtual async Task Delete([FromRoute] TId id) { try { return await _service.DeleteAsyncById(id).ThenAsync( Success: Ok, Fail: IActionResult (messages, notices) => { _logger.LogNotice(notices); return BadRequest(messages); }); } catch (Exception ex) { _logger.LogError(ex, "{Message}", ex.Message); return StatusCode(StatusCodes.Status500InternalServerError); } } } }