DigitalData.Core/DigitalData.Core.API/CRUDControllerBase.cs
2024-03-06 16:14:36 +01:00

127 lines
5.8 KiB
C#

using DigitalData.Core.Contracts.CleanArchitecture.Application;
using DigitalData.Core.Contracts.CleanArchitecture.Infrastructure;
using Microsoft.AspNetCore.Mvc;
namespace DigitalData.Core.API
{
/// <summary>
/// A base controller class providing generic CRUD (Create, Read, Update, Delete) operations for a specified entity type.
/// </summary>
/// <typeparam name="TOriginalController">The derived controller type implementing this base class.</typeparam>
/// <typeparam name="TCRUDService">The derived CRUD service type implementing ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>.</typeparam>
/// <typeparam name="TCreateDto">The Data Transfer Object type for create operations.</typeparam>
/// <typeparam name="TReadDto">The Data Transfer Object type for read operations.</typeparam>
/// <typeparam name="TUpdateDto">The Data Transfer Object type for update operations.</typeparam>
/// <typeparam name="TEntity">The entity type CRUD operations will be performed on.</typeparam>
/// <typeparam name="TId">The type of the entity's identifier.</typeparam>
[ApiController]
[Route("api/[controller]")]
public class CRUDControllerBase<TOriginalController, TCRUDService, TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ControllerBase
where TOriginalController : CRUDControllerBase<TOriginalController, TCRUDService, TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCRUDService : ICRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId>
where TCreateDto : class
where TReadDto : class
where TUpdateDto : class
where TEntity : class
{
protected readonly ILogger<TOriginalController> _logger;
protected readonly TCRUDService _service;
/// <summary>
/// Initializes a new instance of the CRUDControllerBase class with specified logger and CRUD service.
/// </summary>
/// <param name="logger">The logger to be used by the controller.</param>
/// <param name="service">The CRUD service handling business logic for the entity.</param>
public CRUDControllerBase(
ILogger<TOriginalController> logger,
TCRUDService service)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_service = service ?? throw new ArgumentNullException(nameof(service));
}
/// <summary>
/// Creates a new entity based on the provided DTO.
/// </summary>
/// <param name="createDto">The DTO from which to create the entity.</param>
/// <returns>A task that represents the asynchronous create operation. The task result contains the action result.</returns>
[HttpPost]
public virtual async Task<IActionResult> 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);
}
/// <summary>
/// Retrieves an entity by its identifier.
/// </summary>
/// <param name="id">The identifier of the entity to retrieve.</param>
/// <returns>A task that represents the asynchronous read operation. The task result contains the action result.</returns>
[HttpGet("{id}")]
public virtual async Task<IActionResult> GetById([FromRoute]TId id)
{
var result = await _service.ReadByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result);
}
return NotFound(result);
}
/// <summary>
/// Retrieves all entities.
/// </summary>
/// <returns>A task that represents the asynchronous read-all operation. The task result contains the action result.</returns>
[HttpGet]
public virtual async Task<IActionResult> GetAll()
{
var result = await _service.ReadAllAsync();
if (result.IsSuccess)
{
return Ok(result);
}
return NotFound(result);
}
/// <summary>
/// Updates an existing entity based on the provided DTO.
/// </summary>
/// <param name="updateDto">The DTO containing the updated data for the entity.</param>
/// <returns>A task that represents the asynchronous update operation. The task result contains the action result.</returns>
[HttpPut]
public virtual async Task<IActionResult> Update(TUpdateDto updateDto)
{
var result = await _service.UpdateAsync(updateDto);
if (result.IsSuccess)
{
return Ok(result);
}
return BadRequest(result);
}
/// <summary>
/// Deletes an entity by its identifier.
/// </summary>
/// <param name="id">The identifier of the entity to delete.</param>
/// <returns>A task that represents the asynchronous delete operation. The task result contains the action result.</returns>
[HttpDelete("{id}")]
public virtual async Task<IActionResult> Delete([FromRoute]TId id)
{
var result = await _service.DeleteAsyncById(id);
if (result.IsSuccess)
{
return Ok(result);
}
return BadRequest(result);
}
}
}