From 1488845fa4b7d50fef869685eb34b140bb89d052 Mon Sep 17 00:00:00 2001 From: lumijiez <59575049+lumijiez@users.noreply.github.com> Date: Tue, 6 May 2025 13:30:49 +0300 Subject: [PATCH] Add query to retrieve all products --- .../Products/Dtos/AllProductsDto.cs | 6 +++ .../Products/Dtos/ProductDto.cs | 2 +- .../GetAllProducts/GetAllProductsQuery.cs | 9 ++++ .../GetAllProductsQueryHandler.cs | 48 +++++++++++++++++++ .../Controllers/ProductsController.cs | 25 ++++++---- 5 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 src/Printbase.Application/Products/Dtos/AllProductsDto.cs create mode 100644 src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQuery.cs create mode 100644 src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQueryHandler.cs diff --git a/src/Printbase.Application/Products/Dtos/AllProductsDto.cs b/src/Printbase.Application/Products/Dtos/AllProductsDto.cs new file mode 100644 index 0000000..0363924 --- /dev/null +++ b/src/Printbase.Application/Products/Dtos/AllProductsDto.cs @@ -0,0 +1,6 @@ +namespace Printbase.Application.Products.Dtos; + +public class AllProductsDto +{ + public ICollection Products {get;set;} +} \ No newline at end of file diff --git a/src/Printbase.Application/Products/Dtos/ProductDto.cs b/src/Printbase.Application/Products/Dtos/ProductDto.cs index 6351059..d7b0acd 100644 --- a/src/Printbase.Application/Products/Dtos/ProductDto.cs +++ b/src/Printbase.Application/Products/Dtos/ProductDto.cs @@ -8,7 +8,7 @@ public class ProductDto public Guid TypeId { get; set; } public string TypeName { get; set; } = string.Empty; public string GroupName { get; set; } = string.Empty; - public ICollection? Variants { get; set; } + public ICollection Variants { get; set; } = new List(); public DateTime CreatedAt { get; set; } public DateTime? UpdatedAt { get; set; } public bool IsActive { get; set; } diff --git a/src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQuery.cs b/src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQuery.cs new file mode 100644 index 0000000..e3b29f8 --- /dev/null +++ b/src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQuery.cs @@ -0,0 +1,9 @@ +using MediatR; +using Printbase.Application.Products.Dtos; + +namespace Printbase.Application.Products.Queries.GetAllProducts; + +public class GetAllProductsQuery(bool includeVariants = true) : IRequest +{ + public bool IncludeVariants { get; } = includeVariants; +} \ No newline at end of file diff --git a/src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQueryHandler.cs b/src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQueryHandler.cs new file mode 100644 index 0000000..a856c49 --- /dev/null +++ b/src/Printbase.Application/Products/Queries/GetAllProducts/GetAllProductsQueryHandler.cs @@ -0,0 +1,48 @@ +using MediatR; +using Printbase.Application.Products.Dtos; +using Printbase.Domain.Repositories; + +namespace Printbase.Application.Products.Queries.GetAllProducts; + +public class GetAllProductsQueryHandler(IProductRepository productRepository) + : IRequestHandler +{ + private readonly IProductRepository _productRepository = productRepository + ?? throw new ArgumentNullException(nameof(productRepository)); + + public async Task Handle(GetAllProductsQuery request, CancellationToken cancellationToken) + { + var products = await _productRepository.GetAllAsync(true, cancellationToken); + + var allProducts = new AllProductsDto + { + Products = products.Select(p => new ProductDto + { + Id = p.Id, + Name = p.Name, + Description = p.Description, + TypeId = p.TypeId, + TypeName = p.Type.Name, + GroupName = p.Type.Group.Name, + CreatedAt = p.CreatedAt, + UpdatedAt = p.UpdatedAt, + IsActive = p.IsActive, + Variants = request.IncludeVariants + ? p.Variants.Select(v => new ProductVariantDto + { + Id = v.Id, + Color = v.Color, + Size = v.Size, + Price = v.Price, + Discount = v.Discount, + Stock = v.Stock, + SKU = v.SKU, + IsActive = v.IsActive + }).ToList() + : [] + }).ToList() + }; + + return allProducts; + } +} \ No newline at end of file diff --git a/src/Printbase.WebApi/Controllers/ProductsController.cs b/src/Printbase.WebApi/Controllers/ProductsController.cs index c635f65..337f398 100644 --- a/src/Printbase.WebApi/Controllers/ProductsController.cs +++ b/src/Printbase.WebApi/Controllers/ProductsController.cs @@ -1,7 +1,7 @@ using MediatR; using Microsoft.AspNetCore.Mvc; using Printbase.Application.Products.Commands.CreateProduct; -using Printbase.Application.Products.Queries; +using Printbase.Application.Products.Queries.GetAllProducts; using Printbase.Application.Products.Queries.GetProductById; namespace Printbase.WebApi.Controllers; @@ -12,6 +12,18 @@ public class ProductsController(IMediator mediator) : ControllerBase { private readonly IMediator _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + [HttpGet] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task GetAllProducts() + { + var query = new GetAllProductsQuery(); + var result = await _mediator.Send(query); + + if (result == null) return NotFound(); + return Ok(result); + } + [HttpGet("{id:guid}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] @@ -20,11 +32,7 @@ public class ProductsController(IMediator mediator) : ControllerBase var query = new GetProductByIdQuery(id, includeVariants); var result = await _mediator.Send(query); - if (result == null) - { - return NotFound(); - } - + if (result == null) return NotFound(); return Ok(result); } @@ -33,10 +41,7 @@ public class ProductsController(IMediator mediator) : ControllerBase [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task CreateProduct([FromBody] CreateProductCommand command) { - if (!ModelState.IsValid) - { - return BadRequest(ModelState); - } + if (!ModelState.IsValid) return BadRequest(ModelState); try {