using DigitalData.Core.Contracts.Application;
using DigitalData.Core.Contracts.Infrastructure;
using DigitalData.Core.DTO;
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)
{
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);
});
}
///
/// 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)
{
return await _service.ReadByIdAsync(id).ThenAsync(
Success: Ok,
Fail: IActionResult (messages, notices) =>
{
_logger.LogNotice(notices);
return NotFound(messages);
});
}
///
/// 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()
{
return await _service.ReadAllAsync().ThenAsync(
Success: Ok,
Fail: IActionResult (messages, notices) =>
{
_logger.LogNotice(notices);
return NotFound(messages);
});
}
///
/// 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)
{
return await _service.UpdateAsync(updateDto).ThenAsync(
Success: Ok,
Fail: IActionResult (messages, notices) =>
{
_logger.LogNotice(notices);
return BadRequest(messages);
});
}
///
/// 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)
{
return await _service.DeleteAsyncById(id).ThenAsync(
Success: Ok,
Fail: IActionResult (messages, notices) =>
{
_logger.LogNotice(notices);
return BadRequest(messages);
});
}
}
}