diff --git a/Core.Inventory.DAL.API/Controllers/ProductController.cs b/Core.Inventory.DAL.API/Controllers/ProductController.cs new file mode 100644 index 0000000..8f17be9 --- /dev/null +++ b/Core.Inventory.DAL.API/Controllers/ProductController.cs @@ -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 +{ + /// + /// Handles all requests for Product operations. + /// + [ApiVersion(MimeTypes.ApplicationVersion)] + [Route("api/v{api-version:apiVersion}/[controller]")] + [Produces(MimeTypes.ApplicationJson)] + [Consumes(MimeTypes.ApplicationJson)] + [ApiController] + [AllowAnonymous] + public class ProductController(IProductProvider service) : ControllerBase + { + /// + /// Gets all the Products. + /// + /// The found entities. + /// The products found. + /// The products not found error. + /// The service internal error. + [HttpGet] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAllProductsAsync(CancellationToken cancellationToken) + { + var result = await service.GetAllProducts(cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Gets all the Products by Product identifiers. + /// + /// The list of Product identifiers. + /// The found entities. + /// The Products found. + /// The Products not found error. + /// The service internal error. + [HttpPost] + [Route("GetProductList")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task 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); + } + + /// + /// Gets the Product by identifier. + /// + /// The Product identifier. + /// The found entity. + /// The Product found. + /// The Product not found error. + /// The service internal error. + [HttpGet] + [Route("{id}")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] + public async Task 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); + } + + /// + /// Creates a new Product. + /// + /// The Product to be added. + /// The created entity. + /// The Product created. + /// The Product could not be created. + /// The service internal error. + [HttpPost] + [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status201Created)] + public async Task CreateProductAsync([FromBody] ProductRequest newProduct, CancellationToken cancellationToken) + { + var result = await service.CreateProduct(newProduct, cancellationToken).ConfigureAwait(false); + return Created("CreatedWithIdAsync", result); + } + + /// + /// Updates a full Product by identifier. + /// + /// The Product to update. + /// The Product identifier. + /// The updated entity. + /// The Product updated. + /// The Product not found. + /// The Product could not be updated. + /// The service internal error. + [HttpPut] + [Route("{id}")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] + public async Task 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); + } + + /// + /// Changes the status of the Product. + /// + /// The Product identifier. + /// The new status of the Product. + /// The updated entity. + /// The Product updates. + /// The Product not found. + /// The Product could not be deleted. + /// The service internal error. + [HttpPatch] + [Route("{id}/{newStatus}/ChangeStatus")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] + public async Task ChangeProductStatus([FromRoute] string id, [FromRoute] ProductStatus newStatus, CancellationToken cancellationToken) + { + var result = await service.ChangeProductStatus(id, newStatus, cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Adds a tag to the product. + /// + /// The Product identifier. + /// The Tag identifier to add. + /// The updated entity. + /// The tag added to product. + /// The Product not found. + /// The tag could not be added. + /// The service internal error. + [HttpPost] + [Route("{productId}/tags/{tagId}")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] + public async Task 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); + } + + /// + /// Removes a tag from the product. + /// + /// The Product identifier. + /// The Tag identifier to remove. + /// The updated entity. + /// The tag removed from product. + /// The Product not found. + /// The tag could not be removed. + /// The service internal error. + [HttpDelete] + [Route("{productId}/tags/{tagId}")] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(ProductAdapter), StatusCodes.Status200OK)] + public async Task 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); + } + } +} \ No newline at end of file diff --git a/Core.Inventory.Domain/Contexts/Inventory/Request/ProductRequest.cs b/Core.Inventory.Domain/Contexts/Inventory/Request/ProductRequest.cs new file mode 100644 index 0000000..dbadbfb --- /dev/null +++ b/Core.Inventory.Domain/Contexts/Inventory/Request/ProductRequest.cs @@ -0,0 +1,52 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using System.Text.Json.Serialization; + +namespace Core.Inventory.Domain.Contexts.Inventory.Request +{ + /// + /// Data transfer object (DTO) for adding product. + /// + public class ProductRequest + { + /// + /// Gets or sets the tenantId of the product. + /// + [BsonElement("tenantId")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("tenantId")] + public string TenantId { get; set; } = null!; + + /// + /// Gets or sets the name of the product. + /// + [BsonElement("productName")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("productName")] + public string ProductName { get; set; } = null!; + + /// + /// Gets or sets the description of the product. + /// + [BsonElement("description")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("description")] + public string Description { get; set; } = null!; + + /// + /// Gets or sets the status of the product. + /// + [BsonElement("status")] + [BsonRepresentation(BsonType.String)] + [JsonPropertyName("status")] + public string Status { get; set; } = null!; + + /// + /// Gets or sets the list of Tag Ids associated with this product. + /// + [BsonElement("tagIds")] + [BsonRepresentation(BsonType.ObjectId)] + [JsonPropertyName("tagIds")] + public List TagIds { get; set; } = new List(); + } +} \ No newline at end of file diff --git a/Core.Inventory.Provider/Contracts/IProductProvider.cs b/Core.Inventory.Provider/Contracts/IProductProvider.cs new file mode 100644 index 0000000..323bec3 --- /dev/null +++ b/Core.Inventory.Provider/Contracts/IProductProvider.cs @@ -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 + { + /// + /// Creates a new Product. + /// + /// The Product to be created. + /// A representing + /// the asynchronous execution of the service. + ValueTask CreateProduct(ProductRequest newProduct, CancellationToken cancellationToken); + + /// + /// Gets a Product by identifier. + /// + /// The Product identifier. + /// A representing + /// the asynchronous execution of the service. + ValueTask GetProductById(string _id, CancellationToken cancellationToken); + + /// + /// Gets all the products. + /// + /// A representing + /// the asynchronous execution of the service. + ValueTask> GetAllProducts(CancellationToken cancellationToken); + + /// + /// Gets all the products by products identifier list. + /// + /// The list of products identifiers. + /// A representing + /// the asynchronous execution of the service. + ValueTask> GetAllProductsByList(string[] products, CancellationToken cancellationToken); + + /// + /// Changes the status of the product. + /// + /// The product identifier. + /// The new status of the product. + /// The updated entity. + /// A representing + /// the asynchronous execution of the service. + ValueTask ChangeProductStatus(string id, ProductStatus newStatus, CancellationToken cancellationToken); + + /// + /// Updates a Product by id. + /// + /// The Product to be updated. + /// The Product identifier. + /// A representing + /// the asynchronous execution of the service. + ValueTask UpdateProduct(ProductAdapter entity, CancellationToken cancellationToken); + + /// + /// Adds a tag to the product. + /// + /// The ID of the product. + /// The ID of the tag to add. + /// A representing + /// the asynchronous execution of the service. + ValueTask AddTagToProduct(string productId, string tagId, CancellationToken cancellationToken); + + /// + /// Removes a tag from the product. + /// + /// The ID of the product. + /// The ID of the tag to remove. + /// A representing + /// the asynchronous execution of the service. + ValueTask RemoveTagFromProduct(string productId, string tagId, CancellationToken cancellationToken); + } +} \ No newline at end of file diff --git a/Core.Inventory.Provider/Core.Inventory.Provider.csproj b/Core.Inventory.Provider/Core.Inventory.Provider.csproj index 2e45719..bbcdffe 100644 --- a/Core.Inventory.Provider/Core.Inventory.Provider.csproj +++ b/Core.Inventory.Provider/Core.Inventory.Provider.csproj @@ -7,7 +7,7 @@ - + diff --git a/Core.Inventory.Provider/Providers/Inventory/ProductProvider.cs b/Core.Inventory.Provider/Providers/Inventory/ProductProvider.cs new file mode 100644 index 0000000..e3c2b7e --- /dev/null +++ b/Core.Inventory.Provider/Providers/Inventory/ProductProvider.cs @@ -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 +{ + /// + /// Handles all services and business rules related to . + /// + public class ProductProvider : IProductProvider + { + private readonly CollectionRepository repository; + private readonly CacheSettings cacheSettings; + private readonly IRedisCacheProvider cacheProvider; + + public ProductProvider(CollectionRepository repository, + IRedisCacheProvider cacheProvider, + IOptions cacheSettings) + { + this.repository = repository; + this.repository.CollectionInitialization(); + this.cacheSettings = cacheSettings.Value; + this.cacheProvider = cacheProvider; + } + + /// + /// Creates a new Product. + /// + /// The Product to be created. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask CreateProduct(ProductRequest newProduct, CancellationToken cancellationToken) + { + var productCollection = newProduct.Adapt(); + + await repository.InsertOneAsync(productCollection); + + return productCollection; + } + + /// + /// Gets a Product by identifier. + /// + /// The Product identifier. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask GetProductById(string _id, CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetProductById", _id); + var cachedData = await cacheProvider.GetAsync(cacheKey); + + if (cachedData is not null) { return cachedData; } + + var product = await repository.FindByIdAsync(_id); + + await cacheProvider.SetAsync(cacheKey, product); + + return product; + } + + /// + /// Gets all the Products. + /// + /// A representing + /// the asynchronous execution of the service. + public async ValueTask> GetAllProducts(CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetProducts"); + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var products = await repository.AsQueryable(); + + await cacheProvider.SetAsync(cacheKey, products); + + return products; + } + + /// + /// Gets all the Products by Products identifier list. + /// + /// The list of Products identifiers. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask> GetAllProductsByList(string[] products, CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllProductsByList", products); + + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var builder = Builders.Filter; + var filters = new List>(); + + 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; + } + + /// + /// Changes the status of the Product. + /// + /// The Product identifier. + /// The new status of the Product. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask ChangeProductStatus(string id, ProductStatus newStatus, CancellationToken cancellationToken) + { + var entity = await repository.FindByIdAsync(id); + entity.Status = newStatus; + + await repository.ReplaceOneAsync(entity); + + return entity; + } + + /// + /// Updates a Product by id. + /// + /// The Product to be updated. + /// The Product identifier. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask UpdateProduct(ProductAdapter entity, CancellationToken cancellationToken) + { + await repository.ReplaceOneAsync(entity); + + return entity; + } + + /// + /// Adds a tag to the product. + /// + /// The ID of the product. + /// The ID of the tag to add. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask 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; + } + + /// + /// Removes a tag from the product. + /// + /// The ID of the product. + /// The ID of the tag to remove. + /// A representing + /// the asynchronous execution of the service. + public async ValueTask 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; + } + } +} \ No newline at end of file diff --git a/Core.Inventory.Provider/ServiceCollectionExtensions.cs b/Core.Inventory.Provider/ServiceCollectionExtensions.cs index 30faf2d..60d28c9 100644 --- a/Core.Inventory.Provider/ServiceCollectionExtensions.cs +++ b/Core.Inventory.Provider/ServiceCollectionExtensions.cs @@ -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(); services.AddScoped>(); + services.AddScoped(); + services.AddScoped>(); + return services; }