Exemplo n.º 1
0
        public async Task <(decimal summaryPrice, decimal discount)> CalculateTotalPriceAsync(Bucket bucket, Guid userId, bool force = false)
        {
            string priceCacheKey     = "bucketPrice" + bucket.Id;
            string discountCacheKey  = "bucketDiscount" + bucket.Id;
            var    precalculatePrice = await _distributedCache.GetAsync(priceCacheKey);

            var precalculateDiscount = await _distributedCache.GetAsync(discountCacheKey);

            if (!force && precalculatePrice != null && precalculateDiscount != null)
            {
                return(BitconverterExt.ToDecimal(precalculatePrice), BitconverterExt.ToDecimal(precalculateDiscount));
            }
            else
            {
                var response = await _pricingClient.PriceAsync(userId, new PriceRequestDTO
                {
                    Products = bucket.Items.Select(g => new PProductDTO
                    {
                        ProductId = g.ProductId.ToString(),
                        Quantity  = g.Quantity
                    }).ToArray()
                });

                _distributedCache.Set(priceCacheKey, BitconverterExt.GetBytes(response.SummaryPrice), new DistributedCacheEntryOptions {
                    AbsoluteExpirationRelativeToNow = new TimeSpan(0, 5, 0)
                });
                _distributedCache.Set(discountCacheKey, BitconverterExt.GetBytes(response.Discount), new DistributedCacheEntryOptions {
                    AbsoluteExpirationRelativeToNow = new TimeSpan(0, 5, 0)
                });

                return(response.SummaryPrice, response.Discount);
            }
        }