Add Tag CRUD
This commit is contained in:
		
							
								
								
									
										75
									
								
								Core.Inventory.Provider/Contracts/ITagProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								Core.Inventory.Provider/Contracts/ITagProvider.cs
									
									
									
									
									
										Normal 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); | ||||
|     } | ||||
| } | ||||
| @@ -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" /> | ||||
|   | ||||
							
								
								
									
										192
									
								
								Core.Inventory.Provider/Providers/Inventory/TagProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										192
									
								
								Core.Inventory.Provider/Providers/Inventory/TagProvider.cs
									
									
									
									
									
										Normal 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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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; | ||||
|  | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales