Add Tag CRUD

This commit is contained in:
Oscar Morales
2025-08-01 11:45:22 -06:00
parent d922768c85
commit 6b0a681942
20 changed files with 791 additions and 1 deletions

View File

@@ -0,0 +1,19 @@
using Core.Adapters.Lib;
using Core.Inventory.Application.UseCases.Tag.Ports;
using Lib.Architecture.BuildingBlocks;
using Microsoft.AspNetCore.Mvc;
namespace Core.Inventory.Application.UseCases.Tag.Adapter
{
public class TagPort : BasePresenter, ITagPort
{
public void Success(TagAdapter output)
{
ViewModel = new OkObjectResult(output);
}
public void Success(List<TagAdapter> output)
{
ViewModel = new OkObjectResult(output);
}
}
}

View File

@@ -0,0 +1,14 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class AddParentTagToTagRequest : Notificator, ICommand
{
public string TagId { get; set; }
public string ParentTagId { get; set; }
public bool Validate()
{
return true;
}
}
}

View File

@@ -0,0 +1,16 @@
using Core.Blueprint.Mongo;
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class ChangeTagStatusRequest : Notificator, ICommand
{
public string Id { get; set; }
public StatusEnum Status { get; set; }
public bool Validate()
{
return Id != null;
}
}
}

View File

@@ -0,0 +1,20 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class CreateTagRequest : Notificator, ICommand
{
public string TenantId { get; set; } = null!;
public string TagName { get; set; } = null!;
public string TypeId { get; set; } = null!;
public string[] ParentTagId { get; set; } = null!;
public string Slug { get; set; } = null!;
public int DisplayOrder { get; set; }
public string Icon { get; set; } = null!;
public bool Validate()
{
return TagName != null;
}
}
}

View File

@@ -0,0 +1,14 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class GetAllTagsByListRequest : Notificator, ICommand
{
public string[] Tags { get; set; }
public bool Validate()
{
return Tags != null;
}
}
}

View File

@@ -0,0 +1,12 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class GetAllTagsRequest : ICommand
{
public bool Validate()
{
return true;
}
}
}

View File

@@ -0,0 +1,13 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class GetTagRequest : Notificator, ICommand
{
public string Id { get; set; }
public bool Validate()
{
return Id != null;
}
}
}

View File

@@ -0,0 +1,14 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class RemoveParentTagFromTagRequest : Notificator, ICommand
{
public string TagId { get; set; }
public string ParentTagId { get; set; }
public bool Validate()
{
return true;
}
}
}

View File

@@ -0,0 +1,20 @@
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Input
{
public class UpdateTagRequest : Notificator, ICommand
{
public string Id { get; set; } = null!;
public string TenantId { get; set; } = null!;
public string TagName { get; set; } = null!;
public string TypeId { get; set; } = null!;
public string[] ParentTagId { get; set; } = null!;
public string Slug { get; set; } = null!;
public int DisplayOrder { get; set; }
public string Icon { get; set; } = null!;
public bool Validate()
{
return Id != null;
}
}
}

View File

@@ -0,0 +1,14 @@
using Core.Adapters.Lib;
using Lib.Architecture.BuildingBlocks;
namespace Core.Inventory.Application.UseCases.Tag.Ports
{
public interface ITagPort : IBasePort,
ICommandSuccessPort<TagAdapter>,
ICommandSuccessPort<List<TagAdapter>>,
INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort,
INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort,
IBadRequestPort
{
}
}

View File

@@ -0,0 +1,266 @@
using Core.Adapters.Lib;
using Core.Inventory.Application.UseCases.Tag.Input;
using Core.Inventory.Application.UseCases.Tag.Ports;
using Core.Inventory.External.Clients;
using Core.Inventory.External.Clients.Requests;
using FluentValidation;
using Lib.Architecture.BuildingBlocks;
using Lib.Architecture.BuildingBlocks.Helpers;
namespace Core.Inventory.Application.UseCases.Tag
{
public class TagHandler :
IComponentHandler<ChangeTagStatusRequest>,
IComponentHandler<GetAllTagsRequest>,
IComponentHandler<GetAllTagsByListRequest>,
IComponentHandler<UpdateTagRequest>,
IComponentHandler<GetTagRequest>,
IComponentHandler<CreateTagRequest>,
IComponentHandler<AddParentTagToTagRequest>,
IComponentHandler<RemoveParentTagFromTagRequest>
{
private readonly ITagPort _port;
private readonly IValidator<ChangeTagStatusRequest> _changeTagStatusValidator;
private readonly IValidator<CreateTagRequest> _registerTagValidator;
private readonly IValidator<UpdateTagRequest> _updateTagValidator;
private readonly IValidator<GetAllTagsByListRequest> _TagsByListValidator;
private readonly IInventoryServiceClient _inventoryServiceClient;
public TagHandler(
ITagPort port,
IValidator<ChangeTagStatusRequest> changeTagStatusValidator,
IValidator<CreateTagRequest> registerTagValidator,
IValidator<UpdateTagRequest> updateTagValidator,
IValidator<GetAllTagsByListRequest> TagsByListValidator,
IInventoryServiceClient inventoryDALService)
{
_port = port ?? throw new ArgumentNullException(nameof(port));
_changeTagStatusValidator = changeTagStatusValidator ?? throw new ArgumentNullException(nameof(changeTagStatusValidator));
_registerTagValidator = registerTagValidator ?? throw new ArgumentNullException(nameof(registerTagValidator));
_updateTagValidator = updateTagValidator ?? throw new ArgumentNullException(nameof(updateTagValidator));
_inventoryServiceClient = inventoryDALService ?? throw new ArgumentNullException(nameof(inventoryDALService));
_TagsByListValidator = TagsByListValidator ?? throw new ArgumentNullException(nameof(TagsByListValidator));
}
public async ValueTask ExecuteAsync(GetTagRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _inventoryServiceClient.GetTagByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllTagsRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var _result = await _inventoryServiceClient.GetAllTagsAsync().ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(GetAllTagsByListRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_TagsByListValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var _result = await _inventoryServiceClient.GetAllTagsByListAsync(command.Tags, cancellationToken).ConfigureAwait(false);
if (!_result.Any())
{
_port.NoContentSuccess();
return;
}
_port.Success(_result.ToList());
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(ChangeTagStatusRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_changeTagStatusValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var result = await _inventoryServiceClient.ChangeStatusTagAsync(command.Id, command.Status, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(CreateTagRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_registerTagValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new TagRequest
{
TenantId = command.TenantId,
TagName = command.TagName,
TypeId = command.TypeId,
ParentTagId = command.ParentTagId,
Slug = command.Slug,
DisplayOrder = command.DisplayOrder,
Icon = command.Icon,
};
var result = await _inventoryServiceClient.CreateTagAsync(request, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(UpdateTagRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
if (!command.IsValid(_updateTagValidator))
{
_port.ValidationErrors(command.Notifications);
return;
}
var request = new TagAdapter
{
Id = command.Id,
TenantId = command.TenantId,
TagName = command.TagName,
TypeId = command.TypeId,
ParentTagId = command.ParentTagId,
Slug = command.Slug,
DisplayOrder = command.DisplayOrder,
Icon = command.Icon
};
string id = command.Id;
var result = await _inventoryServiceClient.UpdateTagAsync(request, id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(AddParentTagToTagRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _inventoryServiceClient.AddParentTagAsync(command.TagId, command.ParentTagId, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
public async ValueTask ExecuteAsync(RemoveParentTagFromTagRequest command, CancellationToken cancellationToken = default)
{
try
{
ArgumentNullException.ThrowIfNull(command);
var result = await _inventoryServiceClient.RemoveParentTagAsync(command.TagId, command.ParentTagId, cancellationToken).ConfigureAwait(false);
if (result == null)
{
_port.NoContentSuccess();
return;
}
_port.Success(result);
}
catch (Exception ex)
{
ApiResponseHelper.EvaluatePort(ex, _port);
}
}
}
}

View File

@@ -0,0 +1,14 @@
using Core.Inventory.Application.UseCases.Tag.Input;
using FluentValidation;
namespace Core.Inventory.Application.UseCases.Tag.Validator
{
public class ChangeTagStatusValidator : AbstractValidator<ChangeTagStatusRequest>
{
public ChangeTagStatusValidator()
{
RuleFor(i => i.Id).NotEmpty().NotNull().OverridePropertyName(x => x.Id).WithName("Tag ID").WithMessage("Tag ID is Obligatory.");
RuleFor(i => i.Status).NotNull().OverridePropertyName(x => x.Status).WithName("Status").WithMessage("Status is Obligatory.");
}
}
}

View File

@@ -0,0 +1,14 @@
using Core.Inventory.Application.UseCases.Tag.Input;
using FluentValidation;
namespace Core.Inventory.Application.UseCases.Tag.Validator
{
public class CreateTagValidator : AbstractValidator<CreateTagRequest>
{
public CreateTagValidator()
{
RuleFor(i => i.TagName).NotEmpty().NotNull().OverridePropertyName(x => x.TagName).WithName("Tag Name").WithMessage("Tag Name is Obligatory.");
RuleFor(i => i.TypeId).NotEmpty().NotNull().OverridePropertyName(x => x.TypeId).WithName("Tag TypeId").WithMessage("Tag TypeId is Obligatory.");
}
}
}

View File

@@ -0,0 +1,14 @@
using Core.Inventory.Application.UseCases.Tag.Input;
using FluentValidation;
namespace Core.Inventory.Application.UseCases.Tag.Validator
{
public class GetAllTagsByListValidator : AbstractValidator<GetAllTagsByListRequest>
{
public GetAllTagsByListValidator()
{
RuleFor(i => i.Tags).NotEmpty().NotNull().OverridePropertyName(x => x.Tags).WithName("Tags").WithMessage("Tags are Obligatory.");
}
}
}

View File

@@ -0,0 +1,14 @@
using Core.Inventory.Application.UseCases.Tag.Input;
using FluentValidation;
namespace Core.Inventory.Application.UseCases.Tag.Validator
{
public class UpdateTagValidator : AbstractValidator<UpdateTagRequest>
{
public UpdateTagValidator()
{
RuleFor(i => i.TagName).NotEmpty().NotNull().OverridePropertyName(x => x.TagName).WithName("Tag Name").WithMessage("Tag Name is Obligatory.");
RuleFor(i => i.TypeId).NotEmpty().NotNull().OverridePropertyName(x => x.TypeId).WithName("Tag TypeId").WithMessage("Tag TypeId is Obligatory.");
}
}
}