Add Tag CRUD

This commit is contained in:
Oscar Morales
2025-08-01 11:45:04 -06:00
parent 406ff07f62
commit 53a420ffd1
6 changed files with 528 additions and 1 deletions

View File

@@ -0,0 +1,75 @@
using Core.Adapters.Lib;
using Core.Blueprint.Mongo;
using Core.Inventory.Domain.Contexts.Inventory.Request;
namespace Core.Inventory.Provider.Contracts
{
public interface ITagProvider
{
/// <summary>
/// Creates a new Tag.
/// </summary>
/// <param name="entity">The Tag to be created.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> CreateTag(TagRequest newTag, CancellationToken cancellationToken);
/// <summary>
/// Gets an Tag by identifier.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> GetTagById(string _id, CancellationToken cancellationToken);
/// <summary>
/// Gets all the Tags.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagAdapter>> GetAllTags(CancellationToken cancellationToken);
/// <summary>
/// Gets all the Tags by Tags identifier list.
/// </summary>
/// <param name="Tags">The list of Tags identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<IEnumerable<TagAdapter>> GetAllTagsByList(string[] Tags, CancellationToken cancellationToken);
/// <summary>
/// Changes the status of the Tag.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <param name="newStatus">The new status of the Tag.</param>
/// <returns>The <see cref="TagAdapter"/> updated entity.</returns>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> ChangeTagStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken);
/// <summary>
/// Updates a Tag by id.
/// </summary>
/// <param name="entity">The Tag to be updated.</param>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
ValueTask<TagAdapter> UpdateTag(TagAdapter entity, CancellationToken cancellationToken);
/// <summary>
/// Adds a parentTag to the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be added.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
ValueTask<TagAdapter> AddParentTag(string tagId, string parentTagId, CancellationToken cancellationToken);
/// <summary>
/// Removes a parentTag from the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be added.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
ValueTask<TagAdapter> RemoveParentTag(string tagId, string parentTagId, CancellationToken cancellationToken);
}
}

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Adapters.Lib" Version="1.0.9" />
<PackageReference Include="Adapters.Lib" Version="1.0.10" />
<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,192 @@
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="TagAdapter"/>.
/// </summary>
public class TagProvider : ITagProvider
{
private readonly CollectionRepository<TagAdapter> repository;
private readonly CacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public TagProvider(CollectionRepository<TagAdapter> repository,
IRedisCacheProvider cacheProvider,
IOptions<CacheSettings> cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
}
/// <summary>
/// Creates a new Tag.
/// </summary>
/// <param name="entity">The Tag to be created.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagAdapter> CreateTag(TagRequest newTag, CancellationToken cancellationToken)
{
var tagCollection = newTag.Adapt<TagAdapter>();
await repository.InsertOneAsync(tagCollection);
return tagCollection;
}
/// <summary>
/// Gets an Tag by identifier.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>0
public async ValueTask<TagAdapter> GetTagById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTagById", _id);
var cachedData = await cacheProvider.GetAsync<TagAdapter>(cacheKey);
if (cachedData is not null) { return cachedData; }
var tag = await repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, tag);
return tag;
}
/// <summary>
/// Gets all the Tags.
/// </summary>
/// <returns>A <see cref="{Task{IEnumerbale{TagAdapter}}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagAdapter>> GetAllTags(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTags");
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var tags = await repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, tags);
return tags;
}
/// <summary>
/// Gets all the Tags by Tags identifier list.
/// </summary>
/// <param name="Tags">The list of Tags identifiers.</param>
/// <returns>A <see cref="Task{IEnumerable{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<IEnumerable<TagAdapter>> GetAllTagsByList(string[] tags, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllTagsByList", tags);
var cachedData = await cacheProvider.GetAsync<IEnumerable<TagAdapter>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var builder = Builders<TagAdapter>.Filter;
var filters = new List<FilterDefinition<TagAdapter>>();
if (tags != null || !tags.Any())
{
filters.Add(builder.In(x => x._Id, tags));
}
var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty;
var TagsList = await repository.FilterByMongoFilterAsync(finalFilter);
await cacheProvider.SetAsync(cacheKey, TagsList);
return TagsList;
}
/// <summary>
/// Changes the status of the Tag.
/// </summary>
/// <param name="id">The Tag identifier.</param>
/// <param name="newStatus">The new status of the Tag.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagAdapter> ChangeTagStatus(string id, StatusEnum newStatus, CancellationToken cancellationToken)
{
var entity = await repository.FindByIdAsync(id);
entity.Status = newStatus;
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Updates a Tag by id.
/// </summary>
/// <param name="entity">The Tag to be updated.</param>
/// <param name="id">The Tag identifier.</param>
/// <returns>A <see cref="{Task{TagAdapter}}"/> representing
/// the asynchronous execution of the service.</returns>
public async ValueTask<TagAdapter> UpdateTag(TagAdapter entity, CancellationToken cancellationToken)
{
await repository.ReplaceOneAsync(entity);
return entity;
}
/// <summary>
/// Adds a parentTag to the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be added.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
public async ValueTask<TagAdapter> AddParentTag(string tagId, string parentTagId, CancellationToken cancellationToken)
{
var tag = await repository.FindOneAsync(
u => u._Id == tagId &&
u.Status == StatusEnum.Active);
var updatedParentTags = tag.ParentTagId.Append(parentTagId).Distinct().ToArray();
tag.ParentTagId = updatedParentTags;
await repository.ReplaceOneAsync(tag);
return tag;
}
/// <summary>
/// Remove a parentTag to the tag.
/// </summary>
/// <param name="tagId">The identifier of the tag to whom the parentTag will be removed.</param>
/// <param name="parentTagId">The identifier of the parentTag to add.</param>
/// <returns>A <see cref="Task{TagAdapter}"/> representing the asynchronous operation, with the updated tag object.</returns>
public async ValueTask<TagAdapter> RemoveParentTag(string tagId, string parentTagId, CancellationToken cancellationToken)
{
var tag = await repository.FindOneAsync(
u => u._Id == tagId &&
u.Status == StatusEnum.Active);
var updatedParentTags = tag.ParentTagId
?.Where(c => c != parentTagId)
.ToArray();
tag.ParentTagId = updatedParentTags;
await repository.ReplaceOneAsync(tag);
return tag;
}
}
}

View File

@@ -20,6 +20,9 @@ namespace Core.Inventory.Provider
services.AddScoped<ITagTypeProvider, TagTypeProvider>();
services.AddScoped<CollectionRepository<TagTypeAdapter>>();
services.AddScoped<ITagProvider, TagProvider>();
services.AddScoped<CollectionRepository<TagAdapter>>();
return services;
}