feat: Added Product controller and endpoints

- updated Core.Adapters.Lib package version
This commit is contained in:
2025-08-03 17:50:20 -06:00
parent 1f9bae385c
commit a706f96bd8
6 changed files with 535 additions and 2 deletions

View 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);
}
}
}