Merge pull request 'feat: Added Product controller and endpoints (Service Layer)' (#3) from feature/create-Product-and-ProductTag-CRUD into development
Reviewed-on: https://gitea.white-enciso.pro/AgileWebs/Core.Inventory.Service/pulls/3 Reviewed-by: Sergio Matías <sergio.matias@agilewebs.com> Reviewed-by: OscarMmtz <oscar.morales@agilewebs.com>
This commit is contained in:
commit
a63a351b84
@ -0,0 +1,19 @@
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Core.Inventory.Application.UseCases.Product.Ports;
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Adapter
|
||||
{
|
||||
public class ProductPort : BasePresenter, IProductPort
|
||||
{
|
||||
public void Success(ProductAdapter output)
|
||||
{
|
||||
ViewModel = new OkObjectResult(output);
|
||||
}
|
||||
public void Success(List<ProductAdapter> output)
|
||||
{
|
||||
ViewModel = new OkObjectResult(output);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Input
|
||||
{
|
||||
public class ChangeProductStatusRequest : Notificator, ICommand
|
||||
{
|
||||
public string Id { get; set; } = null!;
|
||||
public ProductStatus NewStatus { get; set; }
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
return Id != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Input
|
||||
{
|
||||
public class CreateProductRequest : Notificator, ICommand
|
||||
{
|
||||
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>();
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
return ProductName != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Input
|
||||
{
|
||||
public class GetAllProductsByListRequest : Notificator, ICommand
|
||||
{
|
||||
public string[] Products { get; set; } = null!;
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
return Products != null && Products.Any();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Input
|
||||
{
|
||||
public class GetAllProductsRequest : Notificator, ICommand
|
||||
{
|
||||
public bool Validate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Input
|
||||
{
|
||||
public class GetProductRequest : Notificator, ICommand
|
||||
{
|
||||
public string Id { get; set; } = null!;
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
return Id != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Input
|
||||
{
|
||||
public class UpdateProductRequest : Notificator, ICommand
|
||||
{
|
||||
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>();
|
||||
|
||||
public bool Validate()
|
||||
{
|
||||
return Id != null && ProductName != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Ports
|
||||
{
|
||||
public interface IProductPort : IBasePort,
|
||||
ICommandSuccessPort<ProductAdapter>,
|
||||
ICommandSuccessPort<List<ProductAdapter>>,
|
||||
INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort,
|
||||
INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort,
|
||||
IBadRequestPort
|
||||
{
|
||||
}
|
||||
}
|
||||
214
Core.Inventory.Application/UseCases/Product/ProductHandler.cs
Normal file
214
Core.Inventory.Application/UseCases/Product/ProductHandler.cs
Normal file
@ -0,0 +1,214 @@
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using Core.Inventory.Application.UseCases.Product.Ports;
|
||||
using Core.Inventory.External.Clients;
|
||||
using Core.Inventory.External.Clients.Requests;
|
||||
using FluentValidation;
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
using Lib.Architecture.BuildingBlocks.Helpers;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product
|
||||
{
|
||||
public class ProductHandler :
|
||||
IComponentHandler<ChangeProductStatusRequest>,
|
||||
IComponentHandler<GetAllProductsRequest>,
|
||||
IComponentHandler<GetAllProductsByListRequest>,
|
||||
IComponentHandler<UpdateProductRequest>,
|
||||
IComponentHandler<GetProductRequest>,
|
||||
IComponentHandler<CreateProductRequest>
|
||||
{
|
||||
private readonly IProductPort _port;
|
||||
private readonly IValidator<ChangeProductStatusRequest> _changeProductStatusValidator;
|
||||
private readonly IValidator<CreateProductRequest> _registerProductValidator;
|
||||
private readonly IValidator<UpdateProductRequest> _updateProductValidator;
|
||||
private readonly IValidator<GetAllProductsByListRequest> _productsByListValidator;
|
||||
private readonly IInventoryServiceClient _inventoryServiceClient;
|
||||
|
||||
public ProductHandler(
|
||||
IProductPort port,
|
||||
IValidator<ChangeProductStatusRequest> changeProductStatusValidator,
|
||||
IValidator<CreateProductRequest> registerProductValidator,
|
||||
IValidator<UpdateProductRequest> updateProductValidator,
|
||||
IValidator<GetAllProductsByListRequest> productsByListValidator,
|
||||
IInventoryServiceClient inventoryDALService)
|
||||
{
|
||||
_port = port ?? throw new ArgumentNullException(nameof(port));
|
||||
_changeProductStatusValidator = changeProductStatusValidator ?? throw new ArgumentNullException(nameof(changeProductStatusValidator));
|
||||
_registerProductValidator = registerProductValidator ?? throw new ArgumentNullException(nameof(registerProductValidator));
|
||||
_updateProductValidator = updateProductValidator ?? throw new ArgumentNullException(nameof(updateProductValidator));
|
||||
_inventoryServiceClient = inventoryDALService ?? throw new ArgumentNullException(nameof(inventoryDALService));
|
||||
_productsByListValidator = productsByListValidator ?? throw new ArgumentNullException(nameof(productsByListValidator));
|
||||
}
|
||||
|
||||
public async ValueTask ExecuteAsync(GetProductRequest command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
var result = await _inventoryServiceClient.GetProductByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_port.NoContentSuccess();
|
||||
return;
|
||||
}
|
||||
|
||||
_port.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiResponseHelper.EvaluatePort(ex, _port);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask ExecuteAsync(GetAllProductsRequest command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
var _result = await _inventoryServiceClient.GetAllProductsAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (!_result.Any())
|
||||
{
|
||||
_port.NoContentSuccess();
|
||||
return;
|
||||
}
|
||||
_port.Success(_result.ToList());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiResponseHelper.EvaluatePort(ex, _port);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask ExecuteAsync(GetAllProductsByListRequest command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
if (!command.IsValid(_productsByListValidator))
|
||||
{
|
||||
_port.ValidationErrors(command.Notifications);
|
||||
return;
|
||||
}
|
||||
|
||||
var _result = await _inventoryServiceClient.GetAllProductsByListAsync(command.Products, cancellationToken).ConfigureAwait(false);
|
||||
if (!_result.Any())
|
||||
{
|
||||
_port.NoContentSuccess();
|
||||
return;
|
||||
}
|
||||
_port.Success(_result.ToList());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiResponseHelper.EvaluatePort(ex, _port);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask ExecuteAsync(ChangeProductStatusRequest command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
if (!command.IsValid(_changeProductStatusValidator))
|
||||
{
|
||||
_port.ValidationErrors(command.Notifications);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await _inventoryServiceClient.ChangeProductStatusAsync(command.Id, command.NewStatus, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_port.NoContentSuccess();
|
||||
return;
|
||||
}
|
||||
|
||||
_port.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiResponseHelper.EvaluatePort(ex, _port);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask ExecuteAsync(CreateProductRequest command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
if (!command.IsValid(_registerProductValidator))
|
||||
{
|
||||
_port.ValidationErrors(command.Notifications);
|
||||
return;
|
||||
}
|
||||
|
||||
var productRequest = new ProductRequest
|
||||
{
|
||||
TenantId = command.TenantId,
|
||||
ProductName = command.ProductName,
|
||||
Description = command.Description,
|
||||
Status = command.Status,
|
||||
TagIds = command.TagIds
|
||||
};
|
||||
|
||||
var result = await _inventoryServiceClient.CreateProductAsync(productRequest, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_port.NoContentSuccess();
|
||||
return;
|
||||
}
|
||||
|
||||
_port.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiResponseHelper.EvaluatePort(ex, _port);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask ExecuteAsync(UpdateProductRequest command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(command);
|
||||
|
||||
if (!command.IsValid(_updateProductValidator))
|
||||
{
|
||||
_port.ValidationErrors(command.Notifications);
|
||||
return;
|
||||
}
|
||||
|
||||
var productAdapter = new ProductAdapter
|
||||
{
|
||||
Id = command.Id,
|
||||
TenantId = command.TenantId,
|
||||
ProductName = command.ProductName,
|
||||
Description = command.Description,
|
||||
Status = Enum.Parse<ProductStatus>(command.Status),
|
||||
TagIds = command.TagIds.Select(id => MongoDB.Bson.ObjectId.Parse(id)).ToList()
|
||||
};
|
||||
|
||||
var result = await _inventoryServiceClient.UpdateProductAsync(productAdapter, command.Id, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
_port.NoContentSuccess();
|
||||
return;
|
||||
}
|
||||
|
||||
_port.Success(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiResponseHelper.EvaluatePort(ex, _port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Validator
|
||||
{
|
||||
public class ChangeProductStatusValidator : AbstractValidator<ChangeProductStatusRequest>
|
||||
{
|
||||
public ChangeProductStatusValidator()
|
||||
{
|
||||
RuleFor(i => i.Id).NotEmpty().NotNull().OverridePropertyName(x => x.Id).WithName("Product Id").WithMessage("Product Id is Obligatory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Validator
|
||||
{
|
||||
public class CreateProductValidator : AbstractValidator<CreateProductRequest>
|
||||
{
|
||||
public CreateProductValidator()
|
||||
{
|
||||
RuleFor(i => i.ProductName).NotEmpty().NotNull().OverridePropertyName(x => x.ProductName).WithName("Product Name").WithMessage("Product Name is Obligatory.");
|
||||
RuleFor(i => i.Description).NotEmpty().NotNull().OverridePropertyName(x => x.Description).WithName("Product Description").WithMessage("Product Description is Obligatory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Validator
|
||||
{
|
||||
public class GetAllProductsByListValidator : AbstractValidator<GetAllProductsByListRequest>
|
||||
{
|
||||
public GetAllProductsByListValidator()
|
||||
{
|
||||
RuleFor(i => i.Products).NotEmpty().NotNull().OverridePropertyName(x => x.Products).WithName("Products").WithMessage("Products are Obligatory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Core.Inventory.Application.UseCases.Product.Validator
|
||||
{
|
||||
public class UpdateProductValidator : AbstractValidator<UpdateProductRequest>
|
||||
{
|
||||
public UpdateProductValidator()
|
||||
{
|
||||
RuleFor(i => i.ProductName).NotEmpty().NotNull().OverridePropertyName(x => x.ProductName).WithName("Product Name").WithMessage("Product Name is Obligatory.");
|
||||
RuleFor(i => i.Description).NotEmpty().NotNull().OverridePropertyName(x => x.Description).WithName("Product Description").WithMessage("Product Description is Obligatory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using Core.Adapters.Lib;
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Core.Blueprint.Mongo;
|
||||
using Core.Inventory.External.Clients.Adapters;
|
||||
using Core.Inventory.External.Clients.Requests;
|
||||
@ -101,5 +102,33 @@ namespace Core.Inventory.External.Clients
|
||||
Task<TagAdapter> RemoveParentTagAsync([FromRoute] string tagId, [FromRoute] string parentTagId, CancellationToken cancellationToken = default);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Product
|
||||
|
||||
[Get("/api/v1/Product")]
|
||||
Task<IEnumerable<ProductAdapter>> GetAllProductsAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
[Post("/api/v1/Product/GetProductList")]
|
||||
Task<IEnumerable<ProductAdapter>> GetAllProductsByListAsync([FromBody] string[] request, CancellationToken cancellationToken = default);
|
||||
|
||||
[Get("/api/v1/Product/{id}")]
|
||||
Task<ProductAdapter> GetProductByIdAsync([FromRoute] string id, CancellationToken cancellationToken = default);
|
||||
|
||||
[Post("/api/v1/Product")]
|
||||
Task<ProductAdapter> CreateProductAsync([FromBody] ProductRequest newProduct, CancellationToken cancellationToken = default);
|
||||
|
||||
[Put("/api/v1/Product/{id}")]
|
||||
Task<ProductAdapter> UpdateProductAsync([FromBody] ProductAdapter entity, [FromRoute] string id, CancellationToken cancellationToken = default);
|
||||
|
||||
[Patch("/api/v1/Product/{id}/{newStatus}/ChangeStatus")]
|
||||
Task<ProductAdapter> ChangeProductStatusAsync([FromRoute] string id, [FromRoute] ProductStatus newStatus, CancellationToken cancellationToken = default);
|
||||
|
||||
[Post("/api/v1/Product/{productId}/tags/{tagId}")]
|
||||
Task<ProductAdapter> AddTagToProductAsync([FromRoute] string productId, [FromRoute] string tagId, CancellationToken cancellationToken = default);
|
||||
|
||||
[Delete("/api/v1/Product/{productId}/tags/{tagId}")]
|
||||
Task<ProductAdapter> RemoveTagFromProductAsync([FromRoute] string productId, [FromRoute] string tagId, CancellationToken cancellationToken = default);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
11
Core.Inventory.External/Clients/Requests/ProductRequest.cs
Normal file
11
Core.Inventory.External/Clients/Requests/ProductRequest.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace Core.Inventory.External.Clients.Requests
|
||||
{
|
||||
public class ProductRequest
|
||||
{
|
||||
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>();
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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="Refit" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
187
Core.Inventory.Service.API/Controllers/ProductController.cs
Normal file
187
Core.Inventory.Service.API/Controllers/ProductController.cs
Normal file
@ -0,0 +1,187 @@
|
||||
using Asp.Versioning;
|
||||
using Core.Adapters.Lib.Inventory;
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using Core.Inventory.Application.UseCases.Product.Ports;
|
||||
using Lib.Architecture.BuildingBlocks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Core.Inventory.Service.API.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles all services and business rules related to <see cref="ProductController"/>.
|
||||
/// </summary>
|
||||
[ApiVersion("1.0")]
|
||||
[Route("api/v{api-version:apiVersion}/[controller]")]
|
||||
[Produces("application/json")]
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class ProductController : ControllerBase
|
||||
{
|
||||
private readonly IComponentHandler<GetProductRequest> getProductHandler;
|
||||
private readonly IComponentHandler<GetAllProductsRequest> getAllProductsHandler;
|
||||
private readonly IComponentHandler<GetAllProductsByListRequest> getAllProductsByListHandler;
|
||||
private readonly IComponentHandler<CreateProductRequest> createProductHandler;
|
||||
private readonly IComponentHandler<UpdateProductRequest> updateProductHandler;
|
||||
private readonly IComponentHandler<ChangeProductStatusRequest> changeProductStatusHandler;
|
||||
private readonly IProductPort port;
|
||||
|
||||
/// <summary>
|
||||
/// Handles all services and business rules related to <see cref="ProductController"/>.
|
||||
/// </summary>
|
||||
public ProductController(
|
||||
IComponentHandler<GetProductRequest> getProductHandler,
|
||||
IComponentHandler<GetAllProductsRequest> getAllProductsHandler,
|
||||
IComponentHandler<GetAllProductsByListRequest> getAllProductsByListHandler,
|
||||
IComponentHandler<CreateProductRequest> createProductHandler,
|
||||
IComponentHandler<UpdateProductRequest> updateProductHandler,
|
||||
IComponentHandler<ChangeProductStatusRequest> changeProductStatusHandler,
|
||||
IProductPort port
|
||||
)
|
||||
{
|
||||
this.createProductHandler = createProductHandler;
|
||||
this.updateProductHandler = updateProductHandler;
|
||||
this.changeProductStatusHandler = changeProductStatusHandler;
|
||||
this.getAllProductsHandler = getAllProductsHandler;
|
||||
this.getProductHandler = getProductHandler;
|
||||
this.getAllProductsByListHandler = getAllProductsByListHandler;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all the Products.
|
||||
/// </summary>
|
||||
[HttpGet("GetAll")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
||||
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> GetAllProductsAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await getAllProductsHandler.ExecuteAsync(new GetAllProductsRequest { }, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return port.ViewModel;
|
||||
}
|
||||
|
||||
/// <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="412">Precondition failed if the request does not meet expected conditions.</response>
|
||||
/// <response code="422">Unprocessable entity if the request cannot be processed.</response>
|
||||
/// <response code="500">Internal server error if an unexpected error occurs.</response>
|
||||
[HttpPost]
|
||||
[Route("GetProductList")]
|
||||
[ProducesResponseType(typeof(IEnumerable<ProductAdapter>), 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> GetAllProductsByListAsync([FromBody] GetAllProductsByListRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
if (request == null || request.Products == null || !request.Products.Any())
|
||||
{
|
||||
return BadRequest("Product identifiers are required.");
|
||||
}
|
||||
|
||||
await getAllProductsByListHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return port.ViewModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Product by identifier.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[Route("GetById")]
|
||||
[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> GetProductById([FromBody] GetProductRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
if (request.Id == null || !request.Id.Any())
|
||||
{
|
||||
return BadRequest("Invalid Product Id");
|
||||
}
|
||||
|
||||
await getProductHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return port.ViewModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Product.
|
||||
/// </summary>
|
||||
[HttpPost("Create")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
||||
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> CreateProductAsync([FromBody] CreateProductRequest newProduct, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await createProductHandler.ExecuteAsync(newProduct, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return port.ViewModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a full Product by identifier.
|
||||
/// </summary>
|
||||
[HttpPut("Update")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)]
|
||||
[ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public async Task<IActionResult> UpdateProductAsync([FromBody] UpdateProductRequest request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await updateProductHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return port.ViewModel;
|
||||
}
|
||||
|
||||
/// <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> ChangeProductStatusAsync([FromBody] ChangeProductStatusRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid Product identifier"); }
|
||||
|
||||
await changeProductStatusHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return port.ViewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,11 @@ using Core.Inventory.Application.UseCases.Inventory.Input.Variant;
|
||||
using Core.Inventory.Application.UseCases.Inventory.Ports;
|
||||
using Core.Inventory.Application.UseCases.Inventory.Validator.Base;
|
||||
using Core.Inventory.Application.UseCases.Inventory.Validator.Variant;
|
||||
using Core.Inventory.Application.UseCases.Product;
|
||||
using Core.Inventory.Application.UseCases.Product.Adapter;
|
||||
using Core.Inventory.Application.UseCases.Product.Input;
|
||||
using Core.Inventory.Application.UseCases.Product.Ports;
|
||||
using Core.Inventory.Application.UseCases.Product.Validator;
|
||||
using Core.Inventory.Application.UseCases.Tag;
|
||||
using Core.Inventory.Application.UseCases.Tag.Adapter;
|
||||
using Core.Inventory.Application.UseCases.Tag.Input;
|
||||
@ -127,6 +132,30 @@ namespace Core.Inventory.Service.API.Extensions
|
||||
|
||||
#endregion
|
||||
|
||||
#region Product Services
|
||||
|
||||
services.AddScoped<IProductPort, ProductPort>();
|
||||
services.AddScoped<IComponentHandler<GetAllProductsRequest>, ProductHandler>();
|
||||
services.AddScoped<IComponentHandler<GetProductRequest>, ProductHandler>();
|
||||
|
||||
services.AddValidatorsFromAssemblyContaining<GetAllProductsByListValidator>();
|
||||
services.AddScoped<IValidator<GetAllProductsByListRequest>, GetAllProductsByListValidator>();
|
||||
services.AddScoped<IComponentHandler<GetAllProductsByListRequest>, ProductHandler>();
|
||||
|
||||
services.AddValidatorsFromAssemblyContaining<CreateProductValidator>();
|
||||
services.AddScoped<IValidator<CreateProductRequest>, CreateProductValidator>();
|
||||
services.AddScoped<IComponentHandler<CreateProductRequest>, ProductHandler>();
|
||||
|
||||
services.AddValidatorsFromAssemblyContaining<UpdateProductValidator>();
|
||||
services.AddScoped<IValidator<UpdateProductRequest>, UpdateProductValidator>();
|
||||
services.AddScoped<IComponentHandler<UpdateProductRequest>, ProductHandler>();
|
||||
|
||||
services.AddValidatorsFromAssemblyContaining<ChangeProductStatusValidator>();
|
||||
services.AddScoped<IValidator<ChangeProductStatusRequest>, ChangeProductStatusValidator>();
|
||||
services.AddScoped<IComponentHandler<ChangeProductStatusRequest>, ProductHandler>();
|
||||
|
||||
#endregion
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user