using DigitalData.Core.Contracts.Application; using DigitalData.Core.Contracts.Infrastructure; using Microsoft.AspNetCore.Mvc; namespace DigitalData.Core.API { /// /// A base controller class providing generic CRUD (Create, Read, Update, Delete) operations for a specified entity type. /// /// The derived controller type implementing this base class. /// The derived CRUD service type implementing ICRUDService. /// The Data Transfer Object type for create operations. /// The Data Transfer Object type for read operations. /// The Data Transfer Object type for update operations. /// The entity type CRUD operations will be performed on. /// The type of the entity's identifier. [ApiController] [Route("api/[controller]")] public class CRUDControllerBase : ControllerBase where TOriginalController : CRUDControllerBase where TCRUDService : ICRUDService where TCRUDRepository : ICRUDRepository 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 CRUDControllerBase( 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) { var result = await _service.CreateAsync(createDto); if (result.IsSuccess) { var createdResource = new { Id = result.Data }; var actionName = nameof(GetById); var routeValues = new { id = createdResource.Id }; return CreatedAtAction(actionName, routeValues, createdResource); } return BadRequest(result); } /// /// 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) { var result = await _service.ReadByIdAsync(id); if (result.IsSuccess) { return Ok(result); } return NotFound(result); } /// /// 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() { var result = await _service.ReadAllAsync(); if (result.IsSuccess) { return Ok(result); } return NotFound(result); } /// /// 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) { var result = await _service.UpdateAsync(updateDto); if (result.IsSuccess) { return Ok(result); } return BadRequest(result); } /// /// 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) { var result = await _service.DeleteAsyncById(id); if (result.IsSuccess) { return Ok(result); } return BadRequest(result); } } }