2 Commits

Author SHA1 Message Date
e1a97514af Merge pull request 'feat: Added Product controller and endpoints (BFF)' (#3) from feature/create-Product-and-ProductTag-CRUD into development
Reviewed-on: #3
Reviewed-by: Sergio Matías <sergio.matias@agilewebs.com>
Reviewed-by: OscarMmtz <oscar.morales@agilewebs.com>
2025-08-05 16:14:43 +00:00
f31ec86352 feat: Added Product controller and endpoints
- updated Core.Adapters.Lib package version
2025-08-03 20:29:16 -06:00
11 changed files with 362 additions and 1 deletions

View File

@@ -0,0 +1,260 @@
using Asp.Versioning;
using Core.Adapters.Lib.Inventory;
using Core.Inventory.External.Clients.Inventory;
using Core.Inventory.External.Clients.Inventory.Requests.Product;
using Lib.Architecture.BuildingBlocks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
namespace Core.Inventory.BFF.API.Controllers
{
/// <summary>
/// Handles all requests for Product operations.
/// </summary>
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
[Consumes("application/json")]
[Produces("application/json")]
[ApiController]
[AllowAnonymous]
public class ProductController(IInventoryServiceClient inventoryServiceClient, ILogger<ProductController> logger) : BaseController(logger)
{
/// <summary>
/// Gets all the Products.
/// </summary>
[HttpGet("GetAll")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetAllProductsService(CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetAllProductsService)} - Request received - Payload: ");
return await Handle(() => inventoryServiceClient.GetAllProductsService(new GetAllProductsRequest { }, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(GetAllProductsService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload");
throw;
}
}
/// <summary>
/// Gets all the Products by Product identifiers.
/// </summary>
/// <param name="request">The request containing the list of Product identifiers.</param>
/// <param name="cancellationToken">Cancellation token for the asynchronous operation.</param>
/// <returns>The <see cref="IActionResult"/> representing the result of the service call.</returns>
/// <response code="200">The Products found.</response>
/// <response code="204">No content if no Products are found.</response>
/// <response code="400">Bad request if the Product identifiers are missing or invalid.</response>
/// <response code="401">Unauthorized if the user is not authenticated.</response>
/// <response code="500">Internal server error if an unexpected error occurs.</response>
[HttpPost("GetAllByList")]
[ProducesResponseType(typeof(IEnumerable<ProductAdapter>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetAllProductsByListAsync([FromBody] GetAllProductsByListRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetAllProductsByListAsync)} - Request received - Payload: {request}");
if (request == null || request.Products == null || !request.Products.Any())
{
return BadRequest("Product identifiers are required.");
}
return await Handle(() => inventoryServiceClient.GetAllProductsByListService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError(ex, $"{nameof(GetAllProductsByListAsync)} - An error occurred - {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload: {request}");
return StatusCode(StatusCodes.Status500InternalServerError, "Internal server error");
}
}
/// <summary>
/// Creates a new Product.
/// </summary>
[HttpPost("Create")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> CreateProductService(CreateProductRequest newProduct, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(CreateProductService)} - Request received - Payload: {JsonSerializer.Serialize(newProduct)}");
if (newProduct == null) return BadRequest("Invalid Product object");
if (string.IsNullOrEmpty(newProduct.ProductName)) return BadRequest("Invalid Product name");
return await Handle(() => inventoryServiceClient.CreateProductService(newProduct, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(CreateProductService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newProduct)}");
throw;
}
}
/// <summary>
/// Gets the Product by identifier.
/// </summary>
[HttpPost("GetById")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetProductByIdService(GetProductRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(GetProductByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request.Id)) return BadRequest("Invalid Product identifier");
return await Handle(() => inventoryServiceClient.GetProductByIdService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(GetProductByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
/// <summary>
/// Updates a full Product by identifier.
/// </summary>
[HttpPut("Update")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> UpdateProductService(UpdateProductRequest newProduct, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(UpdateProductService)} - Request received - Payload: {JsonSerializer.Serialize(newProduct)}");
if (newProduct == null) return BadRequest("Invalid Product object");
if (string.IsNullOrEmpty(newProduct.ProductName)) return BadRequest("Invalid Product name");
return await Handle(() => inventoryServiceClient.UpdateProductService(newProduct, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(UpdateProductService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newProduct)}");
throw;
}
}
/// <summary>
/// Changes the status of the Product.
/// </summary>
[HttpPatch]
[Route("ChangeStatus")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> ChangeProductStatusService([FromBody] ChangeProductStatusRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(ChangeProductStatusService)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid Product identifier"); }
return await Handle(() => inventoryServiceClient.ChangeProductStatusService(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(ChangeProductStatusService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
/// <summary>
/// Adds a tag to the product.
/// </summary>
[HttpPost]
[Route("AddTag")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> AddTagToProductAsync([FromBody] AddTagToProductRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(AddTagToProductAsync)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request.ProductId)) { return BadRequest("Invalid product identifier"); }
if (string.IsNullOrEmpty(request.TagId)) { return BadRequest("Invalid tag identifier"); }
return await Handle(() => inventoryServiceClient.AddTagToProductAsync(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(AddTagToProductAsync)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
/// <summary>
/// Remove a tag from the product.
/// </summary>
[HttpDelete]
[Route("RemoveTag")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> RemoveTagFromProductAsync([FromBody] RemoveTagFromProductRequest request, CancellationToken cancellationToken)
{
try
{
logger.LogInformation($"{nameof(RemoveTagFromProductAsync)} - Request received - Payload: {JsonSerializer.Serialize(request)}");
if (string.IsNullOrEmpty(request.ProductId)) { return BadRequest("Invalid product identifier"); }
if (string.IsNullOrEmpty(request.TagId)) { return BadRequest("Invalid tag identifier"); }
return await Handle(() => inventoryServiceClient.RemoveTagFromProductAsync(request, cancellationToken)).ConfigureAwait(false);
}
catch (Exception ex)
{
logger.LogError($"{nameof(RemoveTagFromProductAsync)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}");
throw;
}
}
}
}

View File

@@ -1,5 +1,7 @@
using Core.Adapters.Lib; using Core.Adapters.Lib;
using Core.Adapters.Lib.Inventory;
using Core.Inventory.External.Clients.Inventory.Requests.Base; using Core.Inventory.External.Clients.Inventory.Requests.Base;
using Core.Inventory.External.Clients.Inventory.Requests.Product;
using Core.Inventory.External.Clients.Inventory.Requests.Tag; using Core.Inventory.External.Clients.Inventory.Requests.Tag;
using Core.Inventory.External.Clients.Inventory.Requests.TagType; using Core.Inventory.External.Clients.Inventory.Requests.TagType;
using Core.Inventory.External.Clients.Inventory.Requests.Variant; using Core.Inventory.External.Clients.Inventory.Requests.Variant;
@@ -102,5 +104,33 @@ namespace Core.Inventory.External.Clients.Inventory
Task<ApiResponse<TagAdapter>> RemoveParentTagAsync([Header("TrackingId")][Body] RemoveParentTagFromTag request, CancellationToken cancellationToken = default); Task<ApiResponse<TagAdapter>> RemoveParentTagAsync([Header("TrackingId")][Body] RemoveParentTagFromTag request, CancellationToken cancellationToken = default);
#endregion #endregion
#region Product
[Post("/api/v1/Product/Create")]
Task<ApiResponse<ProductAdapter>> CreateProductService([Header("TrackingId")][Body] CreateProductRequest request, CancellationToken cancellationToken = default);
[Post("/api/v1/Product/GetById")]
Task<ApiResponse<ProductAdapter>> GetProductByIdService([Header("TrackingId")][Body] GetProductRequest request, CancellationToken cancellationToken = default);
[Get("/api/v1/Product/GetAll")]
Task<ApiResponse<IEnumerable<ProductAdapter>>> GetAllProductsService([Header("TrackingId")][Body] GetAllProductsRequest request, CancellationToken cancellationToken = default);
[Post("/api/v1/Product/GetProductList")]
Task<ApiResponse<IEnumerable<ProductAdapter>>> GetAllProductsByListService([Header("TrackingId")][Body] GetAllProductsByListRequest request, CancellationToken cancellationToken = default);
[Put("/api/v1/Product/Update")]
Task<ApiResponse<ProductAdapter>> UpdateProductService([Header("TrackingId")][Body] UpdateProductRequest request, CancellationToken cancellationToken = default);
[Patch("/api/v1/Product/ChangeStatus")]
Task<ApiResponse<ProductAdapter>> ChangeProductStatusService([Header("TrackingId")][Body] ChangeProductStatusRequest request, CancellationToken cancellationToken = default);
[Post("/api/v1/Product/AddTag")]
Task<ApiResponse<ProductAdapter>> AddTagToProductAsync([Header("TrackingId")][Body] AddTagToProductRequest request, CancellationToken cancellationToken = default);
[Delete("/api/v1/Product/RemoveTag")]
Task<ApiResponse<ProductAdapter>> RemoveTagFromProductAsync([Header("TrackingId")][Body] RemoveTagFromProductRequest request, CancellationToken cancellationToken = default);
#endregion
} }
} }

View File

@@ -0,0 +1,8 @@
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class AddTagToProductRequest
{
public string ProductId { get; set; } = null!;
public string TagId { get; set; } = null!;
}
}

View File

@@ -0,0 +1,10 @@
using Core.Adapters.Lib.Inventory;
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class ChangeProductStatusRequest
{
public string Id { get; set; } = null!;
public ProductStatus NewStatus { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class CreateProductRequest
{
public string TenantId { get; set; } = null!;
public string ProductName { get; set; } = null!;
public string Description { get; set; } = null!;
public string Status { get; set; } = null!;
public List<string> TagIds { get; set; } = new List<string>();
}
}

View File

@@ -0,0 +1,7 @@
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class GetAllProductsByListRequest
{
public string[] Products { get; set; } = null!;
}
}

View File

@@ -0,0 +1,6 @@
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class GetAllProductsRequest
{
}
}

View File

@@ -0,0 +1,7 @@
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class GetProductRequest
{
public string Id { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class RemoveTagFromProductRequest
{
public string ProductId { get; set; } = null!;
public string TagId { get; set; } = null!;
}
}

View File

@@ -0,0 +1,14 @@
using Core.Adapters.Lib.Inventory;
namespace Core.Inventory.External.Clients.Inventory.Requests.Product
{
public class UpdateProductRequest
{
public string Id { get; set; } = null!;
public string TenantId { get; set; } = null!;
public string ProductName { get; set; } = null!;
public string Description { get; set; } = null!;
public string Status { get; set; } = null!;
public List<string> TagIds { get; set; } = new List<string>();
}
}

View File

@@ -7,7 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Adapters.Lib" Version="1.0.10" /> <PackageReference Include="Adapters.Lib" Version="1.0.11" />
<PackageReference Include="BuildingBlocks.Library" Version="1.0.0" /> <PackageReference Include="BuildingBlocks.Library" Version="1.0.0" />
<PackageReference Include="Refit" Version="8.0.0" /> <PackageReference Include="Refit" Version="8.0.0" />
</ItemGroup> </ItemGroup>