Add query to retrieve all products

This commit is contained in:
lumijiez
2025-05-06 13:30:49 +03:00
parent 9e84bbac40
commit 1488845fa4
5 changed files with 79 additions and 11 deletions

View File

@@ -0,0 +1,6 @@
namespace Printbase.Application.Products.Dtos;
public class AllProductsDto
{
public ICollection<ProductDto> Products {get;set;}
}

View File

@@ -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<ProductVariantDto>? Variants { get; set; }
public ICollection<ProductVariantDto> Variants { get; set; } = new List<ProductVariantDto>();
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public bool IsActive { get; set; }

View File

@@ -0,0 +1,9 @@
using MediatR;
using Printbase.Application.Products.Dtos;
namespace Printbase.Application.Products.Queries.GetAllProducts;
public class GetAllProductsQuery(bool includeVariants = true) : IRequest<AllProductsDto?>
{
public bool IncludeVariants { get; } = includeVariants;
}

View File

@@ -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<GetAllProductsQuery, AllProductsDto?>
{
private readonly IProductRepository _productRepository = productRepository
?? throw new ArgumentNullException(nameof(productRepository));
public async Task<AllProductsDto?> 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;
}
}