using Microsoft.AspNetCore.Mvc; using Microsoft.eShopOnContainers.WebMVC.ViewModels; using Microsoft.eShopOnContainers.WebMVC.ViewModels.CartViewModels; using Microsoft.eShopOnContainers.WebMVC.Services; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Polly.CircuitBreaker; namespace Microsoft.eShopOnContainers.WebMVC.ViewComponents { public class Cart : ViewComponent { private readonly IBasketService _cartSvc; public Cart(IBasketService cartSvc) => _cartSvc = cartSvc; public async Task InvokeAsync(ApplicationUser user) { var vm = new CartComponentViewModel(); try { var itemsInCart = await ItemsInCartAsync(user); vm.ItemsCount = itemsInCart; return View(vm); } catch (BrokenCircuitException) { // Catch error when Basket.api is in circuit-opened mode ViewBag.IsBasketInoperative = true; vm.ItemsCount = 0; } return View(vm); } private async Task ItemsInCartAsync(ApplicationUser user) { var basket = await _cartSvc.GetBasket(user); return basket.Items.Count; } } }