Add category products query and update endpoints
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
namespace BackOffice.BFF.Application.ProductsCQ.Commands.UpdateCategoryProducts;
|
||||
|
||||
public record UpdateCategoryProductsCommand : IRequest<Unit>
|
||||
{
|
||||
public long CategoryId { get; init; }
|
||||
public IReadOnlyCollection<long> ProductIds { get; init; } = Array.Empty<long>();
|
||||
}
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
using BackOffice.BFF.Application.Common.Interfaces;
|
||||
using CMSPruductCategory = CMSMicroservice.Protobuf.Protos.PruductCategory;
|
||||
|
||||
namespace BackOffice.BFF.Application.ProductsCQ.Commands.UpdateCategoryProducts;
|
||||
|
||||
public class UpdateCategoryProductsCommandHandler : IRequestHandler<UpdateCategoryProductsCommand, Unit>
|
||||
{
|
||||
private readonly IApplicationContractContext _context;
|
||||
|
||||
public UpdateCategoryProductsCommandHandler(IApplicationContractContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Unit> Handle(UpdateCategoryProductsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var targetIds = request.ProductIds?.ToHashSet() ?? new HashSet<long>();
|
||||
|
||||
// load existing links for this category
|
||||
var existingRequest = new CMSPruductCategory.GetAllPruductCategoryByFilterRequest
|
||||
{
|
||||
Filter = new CMSPruductCategory.GetAllPruductCategoryByFilterFilter
|
||||
{
|
||||
CategoryId = request.CategoryId
|
||||
},
|
||||
PaginationState = new CMSMicroservice.Protobuf.Protos.PaginationState
|
||||
{
|
||||
PageNumber = 1,
|
||||
PageSize = 1000
|
||||
}
|
||||
};
|
||||
|
||||
var existingResponse = await _context.ProductCategories.GetAllPruductCategoryByFilterAsync(existingRequest, cancellationToken: cancellationToken);
|
||||
var existing = existingResponse.Models ?? new();
|
||||
|
||||
var existingIds = existing.Select(x => x.ProductId).ToHashSet();
|
||||
|
||||
var toAdd = targetIds.Except(existingIds).ToList();
|
||||
var toRemove = existing.Where(x => !targetIds.Contains(x.ProductId)).ToList();
|
||||
|
||||
foreach (var productId in toAdd)
|
||||
{
|
||||
var createRequest = new CMSPruductCategory.CreateNewPruductCategoryRequest
|
||||
{
|
||||
ProductId = productId,
|
||||
CategoryId = request.CategoryId
|
||||
};
|
||||
|
||||
await _context.ProductCategories.CreateNewPruductCategoryAsync(createRequest, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
foreach (var rel in toRemove)
|
||||
{
|
||||
await _context.ProductCategories.DeletePruductCategoryAsync(
|
||||
new CMSPruductCategory.DeletePruductCategoryRequest { Id = rel.Id },
|
||||
cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
return Unit.Value;
|
||||
}
|
||||
}
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
namespace BackOffice.BFF.Application.ProductsCQ.Queries.GetProductsForCategory;
|
||||
|
||||
public record GetProductsForCategoryQuery : IRequest<GetProductsForCategoryResponseDto>
|
||||
{
|
||||
public long CategoryId { get; init; }
|
||||
}
|
||||
|
||||
+66
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -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; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user