Merge pull request 'feat: added endpoint DeleteProduct (service Layer)' (#5) from feature/create-Product-and-ProductTag-CRUD into development
Reviewed-on: #5 Reviewed-by: Sergio Matías <sergio.matias@agilewebs.com>
This commit is contained in:
		| @@ -0,0 +1,7 @@ | |||||||
|  | namespace Core.Inventory.Application.UseCases.Product.Adapter | ||||||
|  | { | ||||||
|  |     public class DeleteProductResponseAdapter | ||||||
|  |     { | ||||||
|  |         public bool Success { get; init; } | ||||||
|  |     } | ||||||
|  | }  | ||||||
| @@ -15,5 +15,9 @@ namespace Core.Inventory.Application.UseCases.Product.Adapter | |||||||
|         { |         { | ||||||
|             ViewModel = new OkObjectResult(output); |             ViewModel = new OkObjectResult(output); | ||||||
|         } |         } | ||||||
|  |         public void Success(DeleteProductResponseAdapter output) | ||||||
|  |         { | ||||||
|  |             ViewModel = new OkObjectResult(output); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| }  | }  | ||||||
| @@ -7,7 +7,7 @@ namespace Core.Inventory.Application.UseCases.Product.Input | |||||||
|         public string TenantId { get; set; } = null!; |         public string TenantId { get; set; } = null!; | ||||||
|         public string ProductName { get; set; } = null!; |         public string ProductName { get; set; } = null!; | ||||||
|         public string Description { get; set; } = null!; |         public string Description { get; set; } = null!; | ||||||
|         public string Status { get; set; } = null!; |         public string ProductStatus { get; set; } = null!; | ||||||
|         public List<string> TagIds { get; set; } = new List<string>(); |         public List<string> TagIds { get; set; } = new List<string>(); | ||||||
|  |  | ||||||
|         public bool Validate() |         public bool Validate() | ||||||
|   | |||||||
| @@ -0,0 +1,14 @@ | |||||||
|  | using Lib.Architecture.BuildingBlocks; | ||||||
|  |  | ||||||
|  | namespace Core.Inventory.Application.UseCases.Product.Input | ||||||
|  | { | ||||||
|  |     public class DeleteProductRequest : Notificator, ICommand | ||||||
|  |     { | ||||||
|  |         public string Id { get; set; } = null!; | ||||||
|  |  | ||||||
|  |         public bool Validate() | ||||||
|  |         { | ||||||
|  |             return Id != null; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }  | ||||||
| @@ -9,7 +9,7 @@ namespace Core.Inventory.Application.UseCases.Product.Input | |||||||
|         public string TenantId { get; set; } = null!; |         public string TenantId { get; set; } = null!; | ||||||
|         public string ProductName { get; set; } = null!; |         public string ProductName { get; set; } = null!; | ||||||
|         public string Description { get; set; } = null!; |         public string Description { get; set; } = null!; | ||||||
|         public string Status { get; set; } = null!; |         public string ProductStatus { get; set; } = null!; | ||||||
|         public List<string> TagIds { get; set; } = new List<string>(); |         public List<string> TagIds { get; set; } = new List<string>(); | ||||||
|  |  | ||||||
|         public bool Validate() |         public bool Validate() | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| using Core.Adapters.Lib.Inventory; | using Core.Adapters.Lib.Inventory; | ||||||
|  | using Core.Inventory.Application.UseCases.Product.Adapter; | ||||||
| using Lib.Architecture.BuildingBlocks; | using Lib.Architecture.BuildingBlocks; | ||||||
|  |  | ||||||
| namespace Core.Inventory.Application.UseCases.Product.Ports | namespace Core.Inventory.Application.UseCases.Product.Ports | ||||||
| @@ -6,6 +7,7 @@ namespace Core.Inventory.Application.UseCases.Product.Ports | |||||||
|     public interface IProductPort : IBasePort, |     public interface IProductPort : IBasePort, | ||||||
|         ICommandSuccessPort<ProductAdapter>, |         ICommandSuccessPort<ProductAdapter>, | ||||||
|         ICommandSuccessPort<List<ProductAdapter>>, |         ICommandSuccessPort<List<ProductAdapter>>, | ||||||
|  |         ICommandSuccessPort<DeleteProductResponseAdapter>, | ||||||
|         INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort, |         INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort, | ||||||
|         INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort, |         INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort, | ||||||
|         IBadRequestPort |         IBadRequestPort | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| using Core.Adapters.Lib.Inventory; | using Core.Adapters.Lib.Inventory; | ||||||
|  | using Core.Inventory.Application.UseCases.Product.Adapter; | ||||||
| using Core.Inventory.Application.UseCases.Product.Input; | using Core.Inventory.Application.UseCases.Product.Input; | ||||||
| using Core.Inventory.Application.UseCases.Product.Ports; | using Core.Inventory.Application.UseCases.Product.Ports; | ||||||
| using Core.Inventory.External.Clients; | using Core.Inventory.External.Clients; | ||||||
| @@ -15,13 +16,15 @@ namespace Core.Inventory.Application.UseCases.Product | |||||||
|         IComponentHandler<GetAllProductsByListRequest>, |         IComponentHandler<GetAllProductsByListRequest>, | ||||||
|         IComponentHandler<UpdateProductRequest>, |         IComponentHandler<UpdateProductRequest>, | ||||||
|         IComponentHandler<GetProductRequest>, |         IComponentHandler<GetProductRequest>, | ||||||
|         IComponentHandler<CreateProductRequest> |         IComponentHandler<CreateProductRequest>, | ||||||
|  |         IComponentHandler<DeleteProductRequest> | ||||||
|     { |     { | ||||||
|         private readonly IProductPort _port; |         private readonly IProductPort _port; | ||||||
|         private readonly IValidator<ChangeProductStatusRequest> _changeProductStatusValidator; |         private readonly IValidator<ChangeProductStatusRequest> _changeProductStatusValidator; | ||||||
|         private readonly IValidator<CreateProductRequest> _registerProductValidator; |         private readonly IValidator<CreateProductRequest> _registerProductValidator; | ||||||
|         private readonly IValidator<UpdateProductRequest> _updateProductValidator; |         private readonly IValidator<UpdateProductRequest> _updateProductValidator; | ||||||
|         private readonly IValidator<GetAllProductsByListRequest> _productsByListValidator; |         private readonly IValidator<GetAllProductsByListRequest> _productsByListValidator; | ||||||
|  |         private readonly IValidator<DeleteProductRequest> _deleteProductValidator; | ||||||
|         private readonly IInventoryServiceClient _inventoryServiceClient; |         private readonly IInventoryServiceClient _inventoryServiceClient; | ||||||
|  |  | ||||||
|         public ProductHandler( |         public ProductHandler( | ||||||
| @@ -30,6 +33,7 @@ namespace Core.Inventory.Application.UseCases.Product | |||||||
|             IValidator<CreateProductRequest> registerProductValidator, |             IValidator<CreateProductRequest> registerProductValidator, | ||||||
|             IValidator<UpdateProductRequest> updateProductValidator, |             IValidator<UpdateProductRequest> updateProductValidator, | ||||||
|             IValidator<GetAllProductsByListRequest> productsByListValidator, |             IValidator<GetAllProductsByListRequest> productsByListValidator, | ||||||
|  |             IValidator<DeleteProductRequest> deleteProductValidator, | ||||||
|             IInventoryServiceClient inventoryDALService) |             IInventoryServiceClient inventoryDALService) | ||||||
|         { |         { | ||||||
|             _port = port ?? throw new ArgumentNullException(nameof(port)); |             _port = port ?? throw new ArgumentNullException(nameof(port)); | ||||||
| @@ -38,6 +42,7 @@ namespace Core.Inventory.Application.UseCases.Product | |||||||
|             _updateProductValidator = updateProductValidator ?? throw new ArgumentNullException(nameof(updateProductValidator)); |             _updateProductValidator = updateProductValidator ?? throw new ArgumentNullException(nameof(updateProductValidator)); | ||||||
|             _inventoryServiceClient = inventoryDALService ?? throw new ArgumentNullException(nameof(inventoryDALService)); |             _inventoryServiceClient = inventoryDALService ?? throw new ArgumentNullException(nameof(inventoryDALService)); | ||||||
|             _productsByListValidator = productsByListValidator ?? throw new ArgumentNullException(nameof(productsByListValidator)); |             _productsByListValidator = productsByListValidator ?? throw new ArgumentNullException(nameof(productsByListValidator)); | ||||||
|  |             _deleteProductValidator = deleteProductValidator ?? throw new ArgumentNullException(nameof(deleteProductValidator)); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public async ValueTask ExecuteAsync(GetProductRequest command, CancellationToken cancellationToken = default) |         public async ValueTask ExecuteAsync(GetProductRequest command, CancellationToken cancellationToken = default) | ||||||
| @@ -153,7 +158,7 @@ namespace Core.Inventory.Application.UseCases.Product | |||||||
|                     TenantId = command.TenantId, |                     TenantId = command.TenantId, | ||||||
|                     ProductName = command.ProductName, |                     ProductName = command.ProductName, | ||||||
|                     Description = command.Description, |                     Description = command.Description, | ||||||
|                     Status = command.Status, |                     ProductStatus = command.ProductStatus, | ||||||
|                     TagIds = command.TagIds |                     TagIds = command.TagIds | ||||||
|                 }; |                 }; | ||||||
|  |  | ||||||
| @@ -191,7 +196,7 @@ namespace Core.Inventory.Application.UseCases.Product | |||||||
|                     TenantId = command.TenantId, |                     TenantId = command.TenantId, | ||||||
|                     ProductName = command.ProductName, |                     ProductName = command.ProductName, | ||||||
|                     Description = command.Description, |                     Description = command.Description, | ||||||
|                     Status = Enum.Parse<ProductStatus>(command.Status), |                     ProductStatus = Enum.Parse<ProductStatus>(command.ProductStatus), | ||||||
|                     TagIds = command.TagIds.Select(id => MongoDB.Bson.ObjectId.Parse(id)).ToList() |                     TagIds = command.TagIds.Select(id => MongoDB.Bson.ObjectId.Parse(id)).ToList() | ||||||
|                 }; |                 }; | ||||||
|  |  | ||||||
| @@ -210,5 +215,33 @@ namespace Core.Inventory.Application.UseCases.Product | |||||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); |                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         public async ValueTask ExecuteAsync(DeleteProductRequest command, CancellationToken cancellationToken = default) | ||||||
|  |         { | ||||||
|  |             try | ||||||
|  |             { | ||||||
|  |                 ArgumentNullException.ThrowIfNull(command); | ||||||
|  |  | ||||||
|  |                 if (!command.IsValid(_deleteProductValidator)) | ||||||
|  |                 { | ||||||
|  |                     _port.ValidationErrors(command.Notifications); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 var result = await _inventoryServiceClient.DeleteProductAsync(command.Id, cancellationToken).ConfigureAwait(false); | ||||||
|  |  | ||||||
|  |                 if (!result) | ||||||
|  |                 { | ||||||
|  |                     _port.NoContentSuccess(); | ||||||
|  |                     return; | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 _port.Success(new DeleteProductResponseAdapter() { Success = true }); | ||||||
|  |             } | ||||||
|  |             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 DeleteProductValidator : AbstractValidator<DeleteProductRequest> | ||||||
|  |     { | ||||||
|  |         public DeleteProductValidator() | ||||||
|  |         { | ||||||
|  |             RuleFor(i => i.Id).NotEmpty().NotNull().OverridePropertyName(x => x.Id).WithName("Product Id").WithMessage("Product Id is Obligatory."); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }  | ||||||
| @@ -151,6 +151,9 @@ namespace Core.Inventory.External.Clients | |||||||
|         [Delete("/api/v1/Product/{productId}/tags/{tagId}")] |         [Delete("/api/v1/Product/{productId}/tags/{tagId}")] | ||||||
|         Task<ProductAdapter> RemoveTagFromProductAsync([FromRoute] string productId, [FromRoute] string tagId, CancellationToken cancellationToken = default); |         Task<ProductAdapter> RemoveTagFromProductAsync([FromRoute] string productId, [FromRoute] string tagId, CancellationToken cancellationToken = default); | ||||||
|  |  | ||||||
|  |         [Delete("/api/v1/Product/{id}")] | ||||||
|  |         Task<bool> DeleteProductAsync([FromRoute] string id, CancellationToken cancellationToken = default); | ||||||
|  |  | ||||||
|         #endregion |         #endregion | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ namespace Core.Inventory.External.Clients.Requests | |||||||
|         public string TenantId { get; set; } = null!; |         public string TenantId { get; set; } = null!; | ||||||
|         public string ProductName { get; set; } = null!; |         public string ProductName { get; set; } = null!; | ||||||
|         public string Description { get; set; } = null!; |         public string Description { get; set; } = null!; | ||||||
|         public string Status { get; set; } = null!; |         public string ProductStatus { get; set; } = null!; | ||||||
|         public List<string> TagIds { get; set; } = new List<string>(); |         public List<string> TagIds { get; set; } = new List<string>(); | ||||||
|     } |     } | ||||||
| }  | }  | ||||||
| @@ -7,7 +7,7 @@ | |||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.11" /> |     <PackageReference Include="Adapters.Lib" Version="1.0.13" /> | ||||||
|     <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> | ||||||
|   | |||||||
| @@ -24,6 +24,7 @@ namespace Core.Inventory.Service.API.Controllers | |||||||
|         private readonly IComponentHandler<CreateProductRequest> createProductHandler; |         private readonly IComponentHandler<CreateProductRequest> createProductHandler; | ||||||
|         private readonly IComponentHandler<UpdateProductRequest> updateProductHandler; |         private readonly IComponentHandler<UpdateProductRequest> updateProductHandler; | ||||||
|         private readonly IComponentHandler<ChangeProductStatusRequest> changeProductStatusHandler; |         private readonly IComponentHandler<ChangeProductStatusRequest> changeProductStatusHandler; | ||||||
|  |         private readonly IComponentHandler<DeleteProductRequest> deleteProductHandler; | ||||||
|         private readonly IProductPort port; |         private readonly IProductPort port; | ||||||
|  |  | ||||||
|         /// <summary> |         /// <summary> | ||||||
| @@ -36,6 +37,7 @@ namespace Core.Inventory.Service.API.Controllers | |||||||
|             IComponentHandler<CreateProductRequest> createProductHandler, |             IComponentHandler<CreateProductRequest> createProductHandler, | ||||||
|             IComponentHandler<UpdateProductRequest> updateProductHandler, |             IComponentHandler<UpdateProductRequest> updateProductHandler, | ||||||
|             IComponentHandler<ChangeProductStatusRequest> changeProductStatusHandler, |             IComponentHandler<ChangeProductStatusRequest> changeProductStatusHandler, | ||||||
|  |             IComponentHandler<DeleteProductRequest> deleteProductHandler, | ||||||
|             IProductPort port |             IProductPort port | ||||||
|             ) |             ) | ||||||
|         { |         { | ||||||
| @@ -45,6 +47,7 @@ namespace Core.Inventory.Service.API.Controllers | |||||||
|             this.getAllProductsHandler = getAllProductsHandler; |             this.getAllProductsHandler = getAllProductsHandler; | ||||||
|             this.getProductHandler = getProductHandler; |             this.getProductHandler = getProductHandler; | ||||||
|             this.getAllProductsByListHandler = getAllProductsByListHandler; |             this.getAllProductsByListHandler = getAllProductsByListHandler; | ||||||
|  |             this.deleteProductHandler = deleteProductHandler; | ||||||
|             this.port = port; |             this.port = port; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -165,6 +168,15 @@ namespace Core.Inventory.Service.API.Controllers | |||||||
|         /// <summary> |         /// <summary> | ||||||
|         /// Changes the status of the Product. |         /// Changes the status of the Product. | ||||||
|         /// </summary> |         /// </summary> | ||||||
|  |         /// <param name="request">The request containing the product ID and new ProductStatus.</param> | ||||||
|  |         /// <returns>The <see cref="ProductAdapter"/> updated entity.</returns> | ||||||
|  |         /// <response code="200">The Product updates.</response> | ||||||
|  |         /// <response code="204">The Product not found.</response> | ||||||
|  |         /// <response code="400">The Product could not be updated.</response> | ||||||
|  |         /// <response code="401">The Product could not be updated.</response> | ||||||
|  |         /// <response code="412">The Product could not be updated.</response> | ||||||
|  |         /// <response code="422">The Product could not be updated.</response> | ||||||
|  |         /// <response code="500">The service internal error.</response> | ||||||
|         [HttpPatch] |         [HttpPatch] | ||||||
|         [Route("ChangeStatus")] |         [Route("ChangeStatus")] | ||||||
|         [ProducesResponseType(StatusCodes.Status200OK)] |         [ProducesResponseType(StatusCodes.Status200OK)] | ||||||
| @@ -183,5 +195,38 @@ namespace Core.Inventory.Service.API.Controllers | |||||||
|  |  | ||||||
|             return port.ViewModel; |             return port.ViewModel; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         /// <summary> | ||||||
|  |         /// Deletes a Product by its MongoDB identifier. | ||||||
|  |         /// </summary> | ||||||
|  |         /// <param name="request">The request containing the product ID to delete.</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 Product deleted successfully.</response> | ||||||
|  |         /// <response code="204">No content if the Product was not found.</response> | ||||||
|  |         /// <response code="400">Bad request if the Product ID is 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> | ||||||
|  |         [HttpDelete("Delete")] | ||||||
|  |         [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> DeleteProductAsync([FromBody] DeleteProductRequest request, CancellationToken cancellationToken = default) | ||||||
|  |         { | ||||||
|  |             if (string.IsNullOrEmpty(request.Id)) | ||||||
|  |             { | ||||||
|  |                 return BadRequest("Invalid Product identifier"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             await deleteProductHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false); | ||||||
|  |  | ||||||
|  |             return port.ViewModel; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| }  | }  | ||||||
| @@ -183,6 +183,10 @@ namespace Core.Inventory.Service.API.Extensions | |||||||
|             services.AddScoped<IValidator<ChangeProductStatusRequest>, ChangeProductStatusValidator>(); |             services.AddScoped<IValidator<ChangeProductStatusRequest>, ChangeProductStatusValidator>(); | ||||||
|             services.AddScoped<IComponentHandler<ChangeProductStatusRequest>, ProductHandler>(); |             services.AddScoped<IComponentHandler<ChangeProductStatusRequest>, ProductHandler>(); | ||||||
|  |  | ||||||
|  |             services.AddValidatorsFromAssemblyContaining<DeleteProductValidator>(); | ||||||
|  |             services.AddScoped<IValidator<DeleteProductRequest>, DeleteProductValidator>(); | ||||||
|  |             services.AddScoped<IComponentHandler<DeleteProductRequest>, ProductHandler>(); | ||||||
|  |  | ||||||
|             #endregion |             #endregion | ||||||
|  |  | ||||||
|             return services; |             return services; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user