Merge pull request 'Add TagType CRUD' (#1) from feature/add-tag-type-crud into development

Reviewed-on: https://gitea.white-enciso.pro/AgileWebs/Core.Inventory.DAL/pulls/1
Reviewed-by: Sergio Matías <sergio.matias@agilewebs.com>
This commit is contained in:
OscarMmtz 2025-08-01 14:46:22 +00:00
commit 406ff07f62
6 changed files with 410 additions and 1 deletions

View File

@ -0,0 +1,154 @@
using Asp.Versioning;
using Core.Adapters.Lib;
using Core.Blueprint.Logging;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
using Core.Inventory.Provider.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Core.Inventory.DAL.API.Controllers
{
/// <summary>
/// Handles all requests for TagType authentication.
/// </summary>
[ApiVersion(MimeTypes.ApplicationVersion)]
[Route("api/v{api-version:apiVersion}/[controller]")]
[Produces(MimeTypes.ApplicationJson)]
[Consumes(MimeTypes.ApplicationJson)]
[ApiController]
[AllowAnonymous]
public class TagTypeController(ITagTypeProvider service) : ControllerBase
{
/// <summary>
/// Gets all the TagTypes.
/// </summary>
/// <returns>The <see cref="IEnumerable{TagTypeAdapter}"/> found entities.</returns>
/// <response code="200">The tagTypes found.</response>
/// <response code="404">The tagTypes not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<TagTypeAdapter>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllTagTypesAsync(CancellationToken cancellationToken)
{
var result = await service.GetAllTagTypes(cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets all the TagTypes by TagType identifiers.
/// </summary>
/// <param name="TagTypes">The list of TagType identifiers.</param>
/// <returns>The <see cref="IEnumerable{TagTypeAdapter}"/> found entities.</returns>
/// <response code="200">The TagTypes found.</response>
/// <response code="404">The TagTypes not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpPost]
[Route("GetTagTypeList")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(IEnumerable<TagTypeAdapter>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllTagTypesByList([FromBody] string[] tagTypes, CancellationToken cancellationToken)
{
if (tagTypes == null || !tagTypes.Any())
{
return BadRequest("TagType identifiers are required.");
}
var result = await service.GetAllTagTypesByList(tagTypes, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Gets the TagType by identifier.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <returns>The <see cref="TagTypeAdapter"/> found entity.</returns>
/// <response code="200">The TagType found.</response>
/// <response code="404">The TagType not found error.</response>
/// <response code="500">The service internal error.</response>
[HttpGet]
[Route("{id}")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> GetTagTypeByIdAsync([FromRoute] string id, CancellationToken cancellationToken)
{
var result = await service.GetTagTypeById(id, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return NotFound("Entity not found");
}
return Ok(result);
}
/// <summary>
/// Creates a new TagType.
/// </summary>
/// <param name="newTagType">The TagType to be added.</param>
/// <returns>The <see cref="TagTypeAdapter"/> created entity.</returns>
/// <response code="201">The TagType created.</response>
/// <response code="422">The TagType could not be created.</response>
/// <response code="500">The service internal e|ror.</response>
[HttpPost]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status201Created)]
public async Task<IActionResult> CreateTagTypeAsync([FromBody] TagTypeRequest newTagType, CancellationToken cancellationToken)
{
var result = await service.CreateTagType(newTagType, cancellationToken).ConfigureAwait(false);
return Created("CreatedWithIdAsync", result);
}
/// <summary>
/// Updates a full TagType by identifier.
/// </summary>
/// <param name="entity">The TagType to update.</param>
/// <param name="id">The TagType identifier.</param>
/// <returns>The <see cref="TagTypeAdapter"/> updated entity.</returns>
/// <response code="200">The TagType updated.</response>
/// <response code="404">The TagType not found.</response>
/// <response code="422">The TagType could not be updated.</response>
/// <response code="500">The service internal error.</response>
[HttpPut]
[Route("{id}")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> UpdateTagTypeAsync([FromRoute] string id, TagTypeAdapter entity, CancellationToken cancellationToken)
{
if (id != entity.Id?.ToString())
{
return BadRequest("TagType ID mismatch");
}
var result = await service.UpdateTagType(entity, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Changes the status of the TagType.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <param name="newStatus">The new status of the TagType.</param>
/// <returns>The <see cref="TagTypeAdapter"/> updated entity.</returns>
/// <response code="200">The TagType updates.</response>
/// <response code="404">The TagType not found.</response>
/// <response code="422">The TagType could not be deleted.</response>
/// <response code="500">The service internal error.</response>
[HttpPatch]
[Route("{id}/{newStatus}/ChangeStatus")]
[Consumes(MimeTypes.ApplicationJson)]
[Produces(MimeTypes.ApplicationJson)]
[ProducesResponseType(typeof(TagTypeAdapter), StatusCodes.Status200OK)]
public async Task<IActionResult> ChangeTagTypeStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken)
{
var result = await service.ChangeTagTypeStatus(id, newStatus, cancellationToken).ConfigureAwait(false);
return Ok(result);
}
}
}

View File

@ -0,0 +1,44 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.Text.Json.Serialization;
namespace Core.Inventory.Domain.Contexts.Inventory.Request
{
/// <summary>
/// Data transfer object (DTO) for adding tagType.
/// </summary>
public class TagTypeRequest
{
/// <summary>
/// Gets or sets the tenantId of the tagType.
/// </summary>
[BsonElement("tenantId")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("tenantId")]
public string TenantId { get; set; } = null!;
/// <summary>
/// Gets or sets the typeName of the tagType.
/// </summary>
[BsonElement("typeName")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("typeName")]
public string TypeName { get; set; } = null!;
/// <summary>
/// Gets or sets the level of the tagType.
/// </summary>
[BsonElement("level")]
[BsonRepresentation(BsonType.Int32)]
[JsonPropertyName("level")]
public int Level { get; set; }
/// <summary>
/// Gets or sets the parentTypeId of the tagType.
/// </summary>
[BsonElement("parentTypeId")]
[BsonRepresentation(BsonType.String)]
[JsonPropertyName("parentTypeId")]
public string ParentTypeId { get; set; } = null!;
}
}

View File

@ -0,0 +1,59 @@
using Core.Adapters.Lib;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
namespace Core.Inventory.Provider.Contracts
{
public interface ITagTypeProvider
{
/// <summary>
/// Creates a new TagType.
/// </summary>
/// <param name="entity">The TagType to be created.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> CreateTagType(TagTypeRequest newTagType, CancellationToken cancellationToken);
/// <summary>
/// Gets an TagType by identifier.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> GetTagTypeById(string _id, CancellationToken cancellationToken);
/// <summary>
/// Gets all the tagTypes.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagTypeAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypes(CancellationToken cancellationToken);
/// <summary>
/// Gets all the tagTypes by tagTypes identifier list.
/// </summary>
/// <param name="tagTypes">The list of tagTypes identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypesByList(string[] TagTypes, CancellationToken cancellationToken);
/// <summary>
/// Changes the status of the tagType.
/// </summary>
/// <param name="id">The tagType identifier.</param>
/// <param name="newStatus">The new status of the tagType.</param>
/// <returns>The <see cref="TagTypeAdapter"/> updated entity.</returns>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> ChangeTagTypeStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken);
/// <summary>
/// Updates a TagType by id.
/// </summary>
/// <param name="entity">The TagType to be updated.</param>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagTypeAdapter> UpdateTagType(TagTypeAdapter entity, CancellationToken cancellationToken);
}
}

View File

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Adapters.Lib" Version="1.0.8" />
<PackageReference Include="Adapters.Lib" Version="1.0.9" />
<PackageReference Include="Core.Blueprint.Mongo" Version="1.0.0" />
<PackageReference Include="Core.Blueprint.Redis" Version="1.0.2" />
<PackageReference Include="Mapster" Version="7.4.0" />

View File

@ -0,0 +1,149 @@
using Core.Adapters.Lib;
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
using Core.Inventory.Domain.Contexts.Inventory.Request;
using Core.Inventory.Provider.Contracts;
using Mapster;
using Microsoft.Extensions.Options;
using MongoDB.Driver;
namespace Core.Inventory.Provider.Providers.Inventory
{
/// <summary>
/// Handles all services and business rules related to <see cref="TagTypeAdapter"/>.
/// </summary>
public class TagTypeProvider : ITagTypeProvider
{
private readonly CollectionRepository<TagTypeAdapter> repository;
private readonly CacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public TagTypeProvider(CollectionRepository<TagTypeAdapter> repository,
IRedisCacheProvider cacheProvider,
IOptions<CacheSettings> cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
}
/// <summary>
/// Creates a new TagType.
/// </summary>
/// <param name="entity">The TagType to be created.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagTypeAdapter> CreateTagType(TagTypeRequest newTagType, CancellationToken cancellationToken)
{
var tagTypeCollection = newTagType.Adapt<TagTypeAdapter>();
await repository.InsertOneAsync(tagTypeCollection);
return tagTypeCollection;
}
/// <summary>
/// Gets an TagType by identifier.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>0
public async ValueTask<TagTypeAdapter> GetTagTypeById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagTypeById", _id);
var cachedData = await cacheProvider.GetAsync<TagTypeAdapter>(cacheKey);
if (cachedData is not null) { return cachedData; }
var tagType = await repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, tagType);
return tagType;
}
/// <summary>
/// Gets all the TagTypes.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagTypeAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypes(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagTypes");
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagTypeAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var tagTypes = await repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, tagTypes);
return tagTypes;
}
/// <summary>
/// Gets all the TagTypes by TagTypes identifier list.
/// </summary>
/// <param name="TagTypes">The list of TagTypes identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagTypeAdapter>> GetAllTagTypesByList(string[] tagTypes, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllTagTypesByList", tagTypes);
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagTypeAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var builder = Builders<TagTypeAdapter>.Filter;
var filters = new List<FilterDefinition<TagTypeAdapter>>();
if (tagTypes != null || !tagTypes.Any())
{
filters.Add(builder.In(x => x._Id, tagTypes));
}
var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
var tagTypesList = await repository.FilterByMongoFilterAsync(finalFilter);
await cacheProvider.SetAsync(cacheKey, tagTypesList);
return tagTypesList;
}
/// <summary>
/// Changes the status of the TagType.
/// </summary>
/// <param name="id">The TagType identifier.</param>
/// <param name="newStatus">The new status of the TagType.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagTypeAdapter> ChangeTagTypeStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken)
{
var entity = await repository.FindByIdAsync(id);
entity.Status = newStatus;
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Updates a TagType by id.
/// </summary>
/// <param name="entity">The TagType to be updated.</param>
/// <param name="id">The TagType identifier.</param>
/// <returns>A <see cref="{Task{TagTypeAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagTypeAdapter> UpdateTagType(TagTypeAdapter entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
return entity;
}
}
}

View File

@ -17,6 +17,9 @@ namespace Core.Inventory.Provider
services.AddScoped<IFurnitureVariantProvider, FurnitureVariantProvider>();
services.AddScoped<CollectionRepository<FurnitureVariant>>();
services.AddScoped<ITagTypeProvider, TagTypeProvider>();
services.AddScoped<CollectionRepository<TagTypeAdapter>>();
return services;
}