Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.3 KB
Raw Blame
using Grand.Business.Core.Queries.Catalog;
using Grand.Data;
using Grand.Domain.Customers;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Mediator;

namespace Grand.Business.Catalog.Queries.Handlers;

public class GetPriceByCustomerProductQueryHandler : IRequestHandler<GetPriceByCustomerProductQuery, double?>
{
    private readonly ICacheBase _cacheBase;
    private readonly IRepository<CustomerProductPrice> _customerProductPriceRepository;

    public GetPriceByCustomerProductQueryHandler(ICacheBase cacheBase,
        IRepository<CustomerProductPrice> customerProductPriceRepository)
    {
        _cacheBase = cacheBase;
        _customerProductPriceRepository = customerProductPriceRepository;
    }

    public async Task<double?> Handle(GetPriceByCustomerProductQuery request, CancellationToken cancellationToken)
    {
        var key = string.Format(CacheKey.CUSTOMER_PRODUCT_PRICE_KEY_ID, request.CustomerId, request.ProductId);
        var productPrice = await _cacheBase.GetAsync(key, async () => 
        {
            var pp = _customerProductPriceRepository.Table.FirstOrDefault(x => x.CustomerId == request.CustomerId && x.ProductId == request.ProductId);
            return await Task.FromResult(pp);
        });

        return productPrice?.Price;
    }
}