Compare commits
	
		
			2 Commits
		
	
	
		
			1f9bae385c
			...
			827b0d8f03
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 827b0d8f03 | |||
| a706f96bd8 | 
							
								
								
									
										206
									
								
								Core.Inventory.DAL.API/Controllers/ProductController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										206
									
								
								Core.Inventory.DAL.API/Controllers/ProductController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,206 @@ | ||||
| using Asp.Versioning; | ||||
| using Core.Adapters.Lib.Inventory; | ||||
| using Core.Blueprint.Logging; | ||||
| 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 Product operations. | ||||
|     /// </summary> | ||||
|     [ApiVersion(MimeTypes.ApplicationVersion)] | ||||
|     [Route("api/v{api-version:apiVersion}/[controller]")] | ||||
|     [Produces(MimeTypes.ApplicationJson)] | ||||
|     [Consumes(MimeTypes.ApplicationJson)] | ||||
|     [ApiController] | ||||
|     [AllowAnonymous] | ||||
|     public class ProductController(IProductProvider service) : ControllerBase | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets all the Products. | ||||
|         /// </summary> | ||||
|         /// <returns>The <see cref="IEnumerable{ProductAdapter}"/> found entities.</returns> | ||||
|         /// <response code="200">The products found.</response> | ||||
|         /// <response code="404">The products not found error.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpGet] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(IEnumerable<ProductAdapter>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetAllProductsAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.GetAllProducts(cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the Products by Product identifiers. | ||||
|         /// </summary> | ||||
|         /// <param name="products">The list of Product identifiers.</param> | ||||
|         /// <returns>The <see cref="IEnumerable{ProductAdapter}"/> found entities.</returns> | ||||
|         /// <response code="200">The Products found.</response> | ||||
|         /// <response code="404">The Products not found error.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPost] | ||||
|         [Route("GetProductList")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(IEnumerable<ProductAdapter>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetAllProductsByList([FromBody] string[] products, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (products == null || !products.Any()) | ||||
|             { | ||||
|                 return BadRequest("Product identifiers are required."); | ||||
|             } | ||||
|  | ||||
|             var result = await service.GetAllProductsByList(products, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the Product by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> found entity.</returns> | ||||
|         /// <response code="200">The Product found.</response> | ||||
|         /// <response code="404">The Product not found error.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpGet] | ||||
|         [Route("{id}")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetProductByIdAsync([FromRoute] string id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.GetProductById(id, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             if (result == null) | ||||
|             { | ||||
|                 return NotFound("Entity not found"); | ||||
|             } | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Product. | ||||
|         /// </summary> | ||||
|         /// <param name="newProduct">The Product to be added.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> created entity.</returns> | ||||
|         /// <response code="201">The Product created.</response> | ||||
|         /// <response code="422">The Product could not be created.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPost] | ||||
|         [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status201Created)] | ||||
|         public async Task<IActionResult> CreateProductAsync([FromBody] ProductRequest newProduct, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.CreateProduct(newProduct, cancellationToken).ConfigureAwait(false); | ||||
|             return Created("CreatedWithIdAsync", result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a full Product by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Product to update.</param> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> updated entity.</returns> | ||||
|         /// <response code="200">The Product updated.</response> | ||||
|         /// <response code="404">The Product not found.</response> | ||||
|         /// <response code="422">The Product could not be updated.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPut] | ||||
|         [Route("{id}")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> UpdateProductAsync([FromRoute] string id, ProductAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (id != entity.Id?.ToString()) | ||||
|             { | ||||
|                 return BadRequest("Product ID mismatch"); | ||||
|             } | ||||
|  | ||||
|             var result = await service.UpdateProduct(entity, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the Product. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the Product.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> updated entity.</returns> | ||||
|         /// <response code="200">The Product updates.</response> | ||||
|         /// <response code="404">The Product not found.</response> | ||||
|         /// <response code="422">The Product 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(ProductAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> ChangeProductStatus([FromRoute] string id, [FromRoute] ProductStatus newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.ChangeProductStatus(id, newStatus, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds a tag to the product. | ||||
|         /// </summary> | ||||
|         /// <param name="productId">The Product identifier.</param> | ||||
|         /// <param name="tagId">The Tag identifier to add.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> updated entity.</returns> | ||||
|         /// <response code="200">The tag added to product.</response> | ||||
|         /// <response code="404">The Product not found.</response> | ||||
|         /// <response code="422">The tag could not be added.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPost] | ||||
|         [Route("{productId}/tags/{tagId}")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> AddTagToProduct([FromRoute] string productId, [FromRoute] string tagId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.AddTagToProduct(productId, tagId, cancellationToken).ConfigureAwait(false); | ||||
|              | ||||
|             if (result == null) | ||||
|             { | ||||
|                 return NotFound("Product not found"); | ||||
|             } | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Removes a tag from the product. | ||||
|         /// </summary> | ||||
|         /// <param name="productId">The Product identifier.</param> | ||||
|         /// <param name="tagId">The Tag identifier to remove.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> updated entity.</returns> | ||||
|         /// <response code="200">The tag removed from product.</response> | ||||
|         /// <response code="404">The Product not found.</response> | ||||
|         /// <response code="422">The tag could not be removed.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpDelete] | ||||
|         [Route("{productId}/tags/{tagId}")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> RemoveTagFromProduct([FromRoute] string productId, [FromRoute] string tagId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.RemoveTagFromProduct(productId, tagId, cancellationToken).ConfigureAwait(false); | ||||
|              | ||||
|             if (result == null) | ||||
|             { | ||||
|                 return NotFound("Product not found"); | ||||
|             } | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|     } | ||||
| }  | ||||
| @@ -0,0 +1,52 @@ | ||||
| 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 product. | ||||
|     /// </summary> | ||||
|     public class ProductRequest | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets or sets the tenantId of the product. | ||||
|         /// </summary> | ||||
|         [BsonElement("tenantId")] | ||||
|         [BsonRepresentation(BsonType.String)] | ||||
|         [JsonPropertyName("tenantId")] | ||||
|         public string TenantId { get; set; } = null!; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets or sets the name of the product. | ||||
|         /// </summary> | ||||
|         [BsonElement("productName")] | ||||
|         [BsonRepresentation(BsonType.String)] | ||||
|         [JsonPropertyName("productName")] | ||||
|         public string ProductName { get; set; } = null!; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets or sets the description of the product. | ||||
|         /// </summary> | ||||
|         [BsonElement("description")] | ||||
|         [BsonRepresentation(BsonType.String)] | ||||
|         [JsonPropertyName("description")] | ||||
|         public string Description { get; set; } = null!; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets or sets the status of the product. | ||||
|         /// </summary> | ||||
|         [BsonElement("status")] | ||||
|         [BsonRepresentation(BsonType.String)] | ||||
|         [JsonPropertyName("status")] | ||||
|         public string Status { get; set; } = null!; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets or sets the list of Tag Ids associated with this product. | ||||
|         /// </summary> | ||||
|         [BsonElement("tagIds")] | ||||
|         [BsonRepresentation(BsonType.ObjectId)] | ||||
|         [JsonPropertyName("tagIds")] | ||||
|         public List<string> TagIds { get; set; } = new List<string>(); | ||||
|     } | ||||
| }  | ||||
							
								
								
									
										77
									
								
								Core.Inventory.Provider/Contracts/IProductProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								Core.Inventory.Provider/Contracts/IProductProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,77 @@ | ||||
| using Core.Adapters.Lib.Inventory; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.Domain.Contexts.Inventory.Request; | ||||
|  | ||||
| namespace Core.Inventory.Provider.Contracts | ||||
| { | ||||
|     public interface IProductProvider | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Creates a new Product. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Product to be created.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<ProductAdapter> CreateProduct(ProductRequest newProduct, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a Product by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<ProductAdapter> GetProductById(string _id, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the products. | ||||
|         /// </summary> | ||||
|         /// <returns>A <see cref="{Task{IEnumerable{ProductAdapter}}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<IEnumerable<ProductAdapter>> GetAllProducts(CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the products by products identifier list. | ||||
|         /// </summary> | ||||
|         /// <param name="products">The list of products identifiers.</param> | ||||
|         /// <returns>A <see cref="Task{IEnumerable{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<IEnumerable<ProductAdapter>> GetAllProductsByList(string[] products, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the product. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The product identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the product.</param> | ||||
|         /// <returns>The <see cref="ProductAdapter"/> updated entity.</returns> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<ProductAdapter> ChangeProductStatus(string id, ProductStatus newStatus, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a Product by id. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Product to be updated.</param> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<ProductAdapter> UpdateProduct(ProductAdapter entity, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds a tag to the product. | ||||
|         /// </summary> | ||||
|         /// <param name="productId">The ID of the product.</param> | ||||
|         /// <param name="tagId">The ID of the tag to add.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<ProductAdapter> AddTagToProduct(string productId, string tagId, CancellationToken cancellationToken); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Removes a tag from the product. | ||||
|         /// </summary> | ||||
|         /// <param name="productId">The ID of the product.</param> | ||||
|         /// <param name="tagId">The ID of the tag to remove.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         ValueTask<ProductAdapter> RemoveTagFromProduct(string productId, string tagId, CancellationToken cancellationToken); | ||||
|     } | ||||
| }  | ||||
| @@ -7,7 +7,7 @@ | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.10" /> | ||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.11" /> | ||||
|     <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" /> | ||||
|   | ||||
							
								
								
									
										194
									
								
								Core.Inventory.Provider/Providers/Inventory/ProductProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										194
									
								
								Core.Inventory.Provider/Providers/Inventory/ProductProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,194 @@ | ||||
| using Core.Adapters.Lib.Inventory; | ||||
| 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; | ||||
| using MongoDB.Bson; | ||||
|  | ||||
| namespace Core.Inventory.Provider.Providers.Inventory | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all services and business rules related to <see cref="ProductAdapter"/>. | ||||
|     /// </summary> | ||||
|     public class ProductProvider : IProductProvider | ||||
|     { | ||||
|         private readonly CollectionRepository<ProductAdapter> repository; | ||||
|         private readonly CacheSettings cacheSettings; | ||||
|         private readonly IRedisCacheProvider cacheProvider; | ||||
|  | ||||
|         public ProductProvider(CollectionRepository<ProductAdapter> repository, | ||||
|             IRedisCacheProvider cacheProvider, | ||||
|             IOptions<CacheSettings> cacheSettings) | ||||
|         { | ||||
|             this.repository = repository; | ||||
|             this.repository.CollectionInitialization(); | ||||
|             this.cacheSettings = cacheSettings.Value; | ||||
|             this.cacheProvider = cacheProvider; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Product. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Product to be created.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<ProductAdapter> CreateProduct(ProductRequest newProduct, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var productCollection = newProduct.Adapt<ProductAdapter>(); | ||||
|  | ||||
|             await repository.InsertOneAsync(productCollection); | ||||
|  | ||||
|             return productCollection; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets a Product by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<ProductAdapter> GetProductById(string _id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetProductById", _id); | ||||
|             var cachedData = await cacheProvider.GetAsync<ProductAdapter>(cacheKey); | ||||
|  | ||||
|             if (cachedData is not null) { return cachedData; } | ||||
|  | ||||
|             var product = await repository.FindByIdAsync(_id); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, product); | ||||
|  | ||||
|             return product; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the Products. | ||||
|         /// </summary> | ||||
|         /// <returns>A <see cref="{Task{IEnumerable{ProductAdapter}}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<IEnumerable<ProductAdapter>> GetAllProducts(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetProducts"); | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<ProductAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var products = await repository.AsQueryable(); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, products); | ||||
|  | ||||
|             return products; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the Products by Products identifier list. | ||||
|         /// </summary> | ||||
|         /// <param name="products">The list of Products identifiers.</param> | ||||
|         /// <returns>A <see cref="Task{IEnumerable{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<IEnumerable<ProductAdapter>> GetAllProductsByList(string[] products, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllProductsByList", products); | ||||
|  | ||||
|             var cachedData = await cacheProvider.GetAsync<IEnumerable<ProductAdapter>>(cacheKey) ?? []; | ||||
|  | ||||
|             if (cachedData.Any()) return cachedData; | ||||
|  | ||||
|             var builder = Builders<ProductAdapter>.Filter; | ||||
|             var filters = new List<FilterDefinition<ProductAdapter>>(); | ||||
|  | ||||
|             if (products != null || !products.Any()) | ||||
|             { | ||||
|                 filters.Add(builder.In(x => x._Id, products)); | ||||
|             } | ||||
|  | ||||
|             var finalFilter = filters.Count != 0 ? builder.And(filters) : builder.Empty; | ||||
|  | ||||
|             var productsList = await repository.FilterByMongoFilterAsync(finalFilter); | ||||
|  | ||||
|             await cacheProvider.SetAsync(cacheKey, productsList); | ||||
|  | ||||
|             return productsList; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the Product. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the Product.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<ProductAdapter> ChangeProductStatus(string id, ProductStatus newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var entity = await repository.FindByIdAsync(id); | ||||
|             entity.Status = newStatus; | ||||
|  | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|  | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a Product by id. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Product to be updated.</param> | ||||
|         /// <param name="id">The Product identifier.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<ProductAdapter> UpdateProduct(ProductAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             await repository.ReplaceOneAsync(entity); | ||||
|  | ||||
|             return entity; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds a tag to the product. | ||||
|         /// </summary> | ||||
|         /// <param name="productId">The ID of the product.</param> | ||||
|         /// <param name="tagId">The ID of the tag to add.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<ProductAdapter> AddTagToProduct(string productId, string tagId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var product = await repository.FindByIdAsync(productId); | ||||
|              | ||||
|             if (product != null) | ||||
|             { | ||||
|                 var objectId = ObjectId.Parse(tagId); | ||||
|                 if (!product.TagIds.Contains(objectId)) | ||||
|                 { | ||||
|                     product.TagIds.Add(objectId); | ||||
|                     await repository.ReplaceOneAsync(product); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             return product; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Removes a tag from the product. | ||||
|         /// </summary> | ||||
|         /// <param name="productId">The ID of the product.</param> | ||||
|         /// <param name="tagId">The ID of the tag to remove.</param> | ||||
|         /// <returns>A <see cref="{Task{ProductAdapter}}"/> representing | ||||
|         /// the asynchronous execution of the service.</returns> | ||||
|         public async ValueTask<ProductAdapter> RemoveTagFromProduct(string productId, string tagId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var product = await repository.FindByIdAsync(productId); | ||||
|              | ||||
|             if (product != null) | ||||
|             { | ||||
|                 var objectId = ObjectId.Parse(tagId); | ||||
|                 product.TagIds.Remove(objectId); | ||||
|                 await repository.ReplaceOneAsync(product); | ||||
|             } | ||||
|  | ||||
|             return product; | ||||
|         } | ||||
|     } | ||||
| }  | ||||
| @@ -1,4 +1,5 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Adapters.Lib.Inventory; | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.Provider.Contracts; | ||||
| using Core.Inventory.Provider.Providers.Inventory; | ||||
| @@ -23,6 +24,9 @@ namespace Core.Inventory.Provider | ||||
|             services.AddScoped<ITagProvider, TagProvider>(); | ||||
|             services.AddScoped<CollectionRepository<TagAdapter>>(); | ||||
|  | ||||
|             services.AddScoped<IProductProvider, ProductProvider>(); | ||||
|             services.AddScoped<CollectionRepository<ProductAdapter>>(); | ||||
|  | ||||
|             return services; | ||||
|  | ||||
|         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user