Add category products query and update endpoints

This commit is contained in:
masoodafar-web
2025-11-27 04:58:09 +03:30
parent bb8d35a1d1
commit 8878c07031
9 changed files with 242 additions and 2 deletions
@@ -0,0 +1,7 @@
namespace BackOffice.BFF.Application.ProductsCQ.Queries.GetProductsForCategory;
public record GetProductsForCategoryQuery : IRequest<GetProductsForCategoryResponseDto>
{
public long CategoryId { get; init; }
}
@@ -0,0 +1,66 @@
using BackOffice.BFF.Application.Common.Interfaces;
using CMSProducts = CMSMicroservice.Protobuf.Protos.Products;
using CMSPruductCategory = CMSMicroservice.Protobuf.Protos.PruductCategory;
namespace BackOffice.BFF.Application.ProductsCQ.Queries.GetProductsForCategory;
public class GetProductsForCategoryQueryHandler : IRequestHandler<GetProductsForCategoryQuery, GetProductsForCategoryResponseDto>
{
private readonly IApplicationContractContext _context;
public GetProductsForCategoryQueryHandler(IApplicationContractContext context)
{
_context = context;
}
public async Task<GetProductsForCategoryResponseDto> Handle(GetProductsForCategoryQuery request, CancellationToken cancellationToken)
{
// Load all products
var productsRequest = new CMSProducts.GetAllProductsByFilterRequest
{
Filter = new CMSProducts.GetAllProductsByFilterFilter(),
PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
{
PageNumber = 1,
PageSize = 1000
}
};
var productsResponse = await _context.Products.GetAllProductsByFilterAsync(productsRequest, cancellationToken: cancellationToken);
var products = productsResponse.Models ?? new();
// Load links for this category
var linksRequest = new CMSPruductCategory.GetAllPruductCategoryByFilterRequest
{
Filter = new CMSPruductCategory.GetAllPruductCategoryByFilterFilter
{
CategoryId = request.CategoryId
},
PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
{
PageNumber = 1,
PageSize = 1000
}
};
var linksResponse = await _context.ProductCategories.GetAllPruductCategoryByFilterAsync(linksRequest, cancellationToken: cancellationToken);
var links = linksResponse.Models ?? new();
var selectedProductIds = links.Select(l => l.ProductId).ToHashSet();
var result = new GetProductsForCategoryResponseDto
{
Items = products
.Select(p => new CategoryProductItemDto
{
Id = p.Id,
Title = p.Title,
Selected = selectedProductIds.Contains(p.Id)
})
.ToList()
};
return result;
}
}
@@ -0,0 +1,14 @@
namespace BackOffice.BFF.Application.ProductsCQ.Queries.GetProductsForCategory;
public class GetProductsForCategoryResponseDto
{
public List<CategoryProductItemDto> Items { get; set; } = new();
}
public class CategoryProductItemDto
{
public long Id { get; set; }
public string Title { get; set; } = string.Empty;
public bool Selected { get; set; }
}