feat: update Protobuf definitions and add customer-facing APIs for various services
This commit is contained in:
+69
@@ -0,0 +1,69 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CMSMicroservice.Application.Common.Interfaces;
|
||||
using Mapster;
|
||||
using MediatR;
|
||||
|
||||
namespace CMSMicroservice.Application.ProductsCQ.Queries.GetAllProductsByFilter;
|
||||
|
||||
public class
|
||||
GetAllProductsByFilterQueryHandler : IRequestHandler<GetAllProductsByFilterQuery, GetAllProductsByFilterResponseDto>
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
|
||||
public GetAllProductsByFilterQueryHandler(IApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<GetAllProductsByFilterResponseDto> Handle(GetAllProductsByFilterQuery request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var grpcRequest = new CmsProductsProtos.GetAllProductsByFilterRequest
|
||||
{
|
||||
PaginationState = request.PaginationState is { } pagination
|
||||
? new CmsPaginationState
|
||||
{
|
||||
PageNumber = pagination.PageNumber,
|
||||
PageSize = pagination.PageSize
|
||||
}
|
||||
: null,
|
||||
SortBy = request.SortBy,
|
||||
Filter = BuildFilter(request.Filter)
|
||||
};
|
||||
|
||||
var result = await _context.Product.GetAllProductsByFilterAsync(grpcRequest,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
if (request.Filter?.CategoryId is { } categoryId)
|
||||
{
|
||||
var matchingModels = result.Models
|
||||
.Where(model => model.CategoryIds.Contains(categoryId))
|
||||
.ToList();
|
||||
result.Models.Clear();
|
||||
result.Models.AddRange(matchingModels);
|
||||
}
|
||||
return result.Adapt<GetAllProductsByFilterResponseDto>();
|
||||
}
|
||||
|
||||
private static CmsProductsProtos.GetAllProductsByFilterFilter? BuildFilter(GetAllProductsByFilterFilter? filter)
|
||||
{
|
||||
if (filter is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CmsProductsProtos.GetAllProductsByFilterFilter
|
||||
{
|
||||
Id = filter.Id,
|
||||
Title = filter.Title,
|
||||
Description = filter.Description,
|
||||
ShortInfomation = filter.ShortInfomation,
|
||||
FullInformation = filter.FullInformation,
|
||||
Price = filter.Price,
|
||||
Discount = filter.Discount,
|
||||
Rate = filter.Rate
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user