Implement product categories CRUD operations with CMS integration
This commit is contained in:
+53
-6
@@ -1,20 +1,67 @@
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using CMSCategory = CMSMicroservice.Protobuf.Protos.Category;
|
||||
using CMSPruductCategory = CMSMicroservice.Protobuf.Protos.PruductCategory;
|
||||
|
||||
namespace BackOffice.BFF.Application.ProductsCQ.Queries.GetCategories;
|
||||
|
||||
public class GetCategoriesQueryHandler : IRequestHandler<GetCategoriesQuery, GetCategoriesResponseDto>
|
||||
{
|
||||
private readonly IApplicationContractContext _context;
|
||||
|
||||
public GetCategoriesQueryHandler(IApplicationContractContext context)
|
||||
{
|
||||
// context is currently unused; reserved for future CMS integration
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetCategoriesResponseDto> Handle(GetCategoriesQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Stub implementation: returns empty list.
|
||||
// When CMS protobuf package with Category support is available,
|
||||
// this handler can be updated to call Category and PruductCategory services.
|
||||
await Task.CompletedTask;
|
||||
return new GetCategoriesResponseDto();
|
||||
// Load all categories
|
||||
var categoryRequest = new CMSCategory.GetAllCategoryByFilterRequest
|
||||
{
|
||||
Filter = new CMSCategory.GetAllCategoryByFilterFilter(),
|
||||
PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
|
||||
{
|
||||
PageNumber = 1,
|
||||
PageSize = 1000
|
||||
}
|
||||
};
|
||||
|
||||
var categoriesResponse = await _context.Categories.GetAllCategoryByFilterAsync(categoryRequest, cancellationToken: cancellationToken);
|
||||
var categories = categoriesResponse.Models ?? new();
|
||||
|
||||
// Load existing product-category relations for this product
|
||||
var productCategoryRequest = new CMSPruductCategory.GetAllPruductCategoryByFilterRequest
|
||||
{
|
||||
Filter = new CMSPruductCategory.GetAllPruductCategoryByFilterFilter
|
||||
{
|
||||
ProductId = request.ProductId
|
||||
},
|
||||
PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
|
||||
{
|
||||
PageNumber = 1,
|
||||
PageSize = 1000
|
||||
}
|
||||
};
|
||||
|
||||
var productCategoriesResponse = await _context.ProductCategories.GetAllPruductCategoryByFilterAsync(productCategoryRequest, cancellationToken: cancellationToken);
|
||||
var productCategories = productCategoriesResponse.Models ?? new();
|
||||
|
||||
var selectedIds = productCategories
|
||||
.Select(pc => pc.CategoryId)
|
||||
.ToHashSet();
|
||||
|
||||
var result = new GetCategoriesResponseDto
|
||||
{
|
||||
Items = categories
|
||||
.Select(c => new CategoryItemDto
|
||||
{
|
||||
Id = c.Id,
|
||||
Title = c.Title,
|
||||
Selected = selectedIds.Contains(c.Id)
|
||||
})
|
||||
.ToList()
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user