initial commit
This commit is contained in:
70
DigitalData.Core.API/ReadControllerBase.cs
Normal file
70
DigitalData.Core.API/ReadControllerBase.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
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 Read operation for a specified entity type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TOriginalController">The derived controller type implementing this base class.</typeparam>
|
||||
/// <typeparam name="TReadDto">The Data Transfer Object type for read 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 ReadControllerBase<TOriginalController, TBasicCRUDService, TCRUDRepository, TReadDto, TEntity, TId> : ControllerBase
|
||||
where TOriginalController : ReadControllerBase<TOriginalController, TBasicCRUDService, TCRUDRepository, TReadDto, TEntity, TId>
|
||||
where TBasicCRUDService : IBasicCRUDService<TCRUDRepository, TReadDto, TEntity, TId>
|
||||
where TCRUDRepository : ICRUDRepository<TEntity, TId>
|
||||
where TReadDto : class
|
||||
where TEntity : class
|
||||
{
|
||||
protected readonly ILogger<TOriginalController> _logger;
|
||||
protected readonly TBasicCRUDService _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 ReadControllerBase(
|
||||
ILogger<TOriginalController> logger,
|
||||
TBasicCRUDService service)
|
||||
{
|
||||
_logger = logger;
|
||||
_service = service;
|
||||
}
|
||||
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user