using DigitalData.Core.Contracts.CleanArchitecture.Application;
using DigitalData.Core.Contracts.CleanArchitecture.Infrastructure;
using Microsoft.AspNetCore.Mvc;
namespace DigitalData.Core.API
{
///
/// A base controller class providing Read operation for a specified entity type.
///
/// The derived controller type implementing this base class.
/// 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]")]
public class ReadControllerBase : ControllerBase
where TOriginalController : ReadControllerBase
where TBasicCRUDService : IBasicCRUDService
where TCRUDRepository : ICRUDRepository
where TReadDto : class
where TEntity : class
{
protected readonly ILogger _logger;
protected readonly TBasicCRUDService _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 ReadControllerBase(
ILogger logger,
TBasicCRUDService service)
{
_logger = logger;
_service = service;
}
///
/// 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);
}
}
}