Add TagType CRUD

This commit is contained in:
Oscar Morales
2025-07-31 19:07:40 -06:00
parent 542df8a203
commit e6d68f4fd3
18 changed files with 646 additions and 1 deletions

View File

@@ -0,0 +1,187 @@
using Asp.Versioning;
using Core.Adapters.Lib;
using Core.Inventory.Application.UseCases.TagType.Input;
using Core.Inventory.Application.UseCases.TagType.Ports;
using Lib.Architecture.BuildingBlocks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Core.Inventory.Service.API.Controllers
{
/// <summary>
/// Handles all services and business rules related to <see cref="TagTypeController"/>.
/// </summary>
[ApiVersion("1.0")]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces("application/json")]
[ApiController]
[AllowAnonymous]
public class TagTypeController : ControllerBase
{
private readonly IComponentHandler<GetTagTypeRequest> getTagTypeHandler;
private readonly IComponentHandler<GetAllTagTypesRequest> getAllTagTypesHandler;
private readonly IComponentHandler<GetAllTagTypesByListRequest> getAllTagTypesByListHandler;
private readonly IComponentHandler<CreateTagTypeRequest> createTagTypeHandler;
private readonly IComponentHandler<UpdateTagTypeRequest> updateTagTypeHandler;
private readonly IComponentHandler<ChangeTagTypeStatusRequest> changeTagTypeStatusHandler;
private readonly ITagTypePort port;
/// <summary>
/// Handles all services and business rules related to <see cref="TagTypeController"/>.
/// </summary>
public TagTypeController(
IComponentHandler<GetTagTypeRequest> getTagTypeHandler,
IComponentHandler<GetAllTagTypesRequest> getAllTagTypesHandler,
IComponentHandler<GetAllTagTypesByListRequest> getAllTagTypesByListHandler,
IComponentHandler<CreateTagTypeRequest> createTagTypeHandler,
IComponentHandler<UpdateTagTypeRequest> updateTagTypeHandler,
IComponentHandler<ChangeTagTypeStatusRequest> changeTagTypeStatusHandler,
ITagTypePort port
)
{
this.createTagTypeHandler = createTagTypeHandler;
this.updateTagTypeHandler = updateTagTypeHandler;
this.changeTagTypeStatusHandler = changeTagTypeStatusHandler;
this.getAllTagTypesHandler = getAllTagTypesHandler;
this.getTagTypeHandler = getTagTypeHandler;
this.getAllTagTypesByListHandler = getAllTagTypesByListHandler;
this.port = port;
}
/// <summary>
/// Gets all the TagTypes.
/// </summary>
[HttpGet("GetAll")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetAllTagTypesAsync(CancellationToken cancellationToken)
{
await getAllTagTypesHandler.ExecuteAsync(new GetAllTagTypesRequest { }, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Gets all the TagTypes by TagType identifiers.
/// </summary>
/// <param name="request">The request containing the list of TagType identifiers.</param>
/// <param name="cancellationToken">Cancellation token for the asynchronous operation.</param>
/// <returns>The <see cref="IActionResult"/> representing the result of the service call.</returns>
/// <response code="200">The TagTypes found.</response>
/// <response code="204">No content if no TagTypes are found.</response>
/// <response code="400">Bad request if the TagType identifiers are missing or invalid.</response>
/// <response code="401">Unauthorized if the user is not authenticated.</response>
/// <response code="412">Precondition failed if the request does not meet expected conditions.</response>
/// <response code="422">Unprocessable entity if the request cannot be processed.</response>
/// <response code="500">Internal server error if an unexpected error occurs.</response>
[HttpPost]
[Route("GetTagTypeList")]
[ProducesResponseType(typeof(IEnumerable<TagTypeAdapter>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetAllTagTypesByListAsync([FromBody] GetAllTagTypesByListRequest request, CancellationToken cancellationToken)
{
if (request == null || request.TagTypes == null || !request.TagTypes.Any())
{
return BadRequest("TagType identifiers are required.");
}
await getAllTagTypesByListHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Gets the TagType by identifier.
/// </summary>
[HttpPost]
[Route("GetById")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetTagTypeById([FromBody] GetTagTypeRequest request, CancellationToken cancellationToken)
{
if (request.Id == null || !request.Id.Any())
{
return BadRequest("Invalid TagType Id");
}
await getTagTypeHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Creates a new TagType.
/// </summary>
[HttpPost("Create")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateTagTypeAsync([FromBody] CreateTagTypeRequest newTagType, CancellationToken cancellationToken = default)
{
await createTagTypeHandler.ExecuteAsync(newTagType, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Updates a full TagType by identifier.
/// </summary>
[HttpPut("Update")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdateTagTypeAsync([FromBody] UpdateTagTypeRequest request, CancellationToken cancellationToken = default)
{
await updateTagTypeHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
/// <summary>
/// Changes the status of the TagType.
/// </summary>
[HttpPatch]
[Route("ChangeStatus")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> ChangeTagTypeStatusAsync([FromBody] ChangeTagTypeStatusRequest request,
CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid TagType identifier"); }
await changeTagTypeStatusHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
return port.ViewModel;
}
}
}

View File

@@ -5,6 +5,11 @@ using Core.Inventory.Application.UseCases.Inventory.Input.Variant;
using Core.Inventory.Application.UseCases.Inventory.Ports;
using Core.Inventory.Application.UseCases.Inventory.Validator.Base;
using Core.Inventory.Application.UseCases.Inventory.Validator.Variant;
using Core.Inventory.Application.UseCases.TagType;
using Core.Inventory.Application.UseCases.TagType.Adapter;
using Core.Inventory.Application.UseCases.TagType.Input;
using Core.Inventory.Application.UseCases.TagType.Ports;
using Core.Inventory.Application.UseCases.TagType.Validator;
using FluentValidation;
using Lib.Architecture.BuildingBlocks;
@@ -66,6 +71,30 @@ namespace Core.Inventory.Service.API.Extensions
services.AddScoped<IValidator<GetFurnitureVariantsByIdsRequest>, GetFurnitureVariantsByIdsValidator>();
#endregion
#region TagType Services
services.AddScoped<ITagTypePort, TagTypePort>();
services.AddScoped<IComponentHandler<GetAllTagTypesRequest>, TagTypeHandler>();
services.AddScoped<IComponentHandler<GetTagTypeRequest>, TagTypeHandler>();
services.AddValidatorsFromAssemblyContaining<GetAllTagTypesByListValidator>();
services.AddScoped<IValidator<GetAllTagTypesByListRequest>, GetAllTagTypesByListValidator>();
services.AddScoped<IComponentHandler<GetAllTagTypesByListRequest>, TagTypeHandler>();
services.AddValidatorsFromAssemblyContaining<CreateTagTypeValidator>();
services.AddScoped<IValidator<CreateTagTypeRequest>, CreateTagTypeValidator>();
services.AddScoped<IComponentHandler<CreateTagTypeRequest>, TagTypeHandler>();
services.AddValidatorsFromAssemblyContaining<UpdateTagTypeValidator>();
services.AddScoped<IValidator<UpdateTagTypeRequest>, UpdateTagTypeValidator>();
services.AddScoped<IComponentHandler<UpdateTagTypeRequest>, TagTypeHandler>();
services.AddValidatorsFromAssemblyContaining<ChangeTagTypeStatusValidator>();
services.AddScoped<IValidator<ChangeTagTypeStatusRequest>, ChangeTagTypeStatusValidator>();
services.AddScoped<IComponentHandler<ChangeTagTypeStatusRequest>, TagTypeHandler>();
#endregion
return services;
}
}