92 lines
3.0 KiB
C#
92 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.eShopOnContainers.WebMVC.Services;
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
|
using Polly.CircuitBreaker;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
|
{
|
|
[Authorize(AuthenticationSchemes = "OpenIdConnect")]
|
|
public class CartController : Controller
|
|
{
|
|
private readonly IBasketService _basketSvc;
|
|
private readonly ICatalogService _catalogSvc;
|
|
private readonly IIdentityParser<ApplicationUser> _appUserParser;
|
|
|
|
public CartController(IBasketService basketSvc, ICatalogService catalogSvc, IIdentityParser<ApplicationUser> appUserParser)
|
|
{
|
|
_basketSvc = basketSvc;
|
|
_catalogSvc = catalogSvc;
|
|
_appUserParser = appUserParser;
|
|
}
|
|
|
|
public async Task<IActionResult> Index()
|
|
{
|
|
try
|
|
{
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
|
var vm = await _basketSvc.GetBasket(user);
|
|
|
|
return View(vm);
|
|
}
|
|
catch (BrokenCircuitException)
|
|
{
|
|
// Catch error when Basket.api is in circuit-opened mode
|
|
HandleBrokenCircuitException();
|
|
}
|
|
|
|
return View();
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Index(Dictionary<string, int> quantities, string action)
|
|
{
|
|
try
|
|
{
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
|
var basket = await _basketSvc.SetQuantities(user, quantities);
|
|
if (action == "[ Checkout ]")
|
|
{
|
|
return RedirectToAction("Create", "Order");
|
|
}
|
|
}
|
|
catch (BrokenCircuitException)
|
|
{
|
|
// Catch error when Basket.api is in circuit-opened mode
|
|
HandleBrokenCircuitException();
|
|
}
|
|
|
|
return View();
|
|
}
|
|
|
|
public async Task<IActionResult> AddToCart(CatalogItem productDetails)
|
|
{
|
|
try
|
|
{
|
|
if (productDetails?.Id != null)
|
|
{
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
|
await _basketSvc.AddItemToBasket(user, productDetails.Id);
|
|
}
|
|
return RedirectToAction("Index", "Catalog");
|
|
}
|
|
catch (BrokenCircuitException)
|
|
{
|
|
// Catch error when Basket.api is in circuit-opened mode
|
|
HandleBrokenCircuitException();
|
|
}
|
|
|
|
return RedirectToAction("Index", "Catalog", new { errorMsg = ViewBag.BasketInoperativeMsg });
|
|
}
|
|
|
|
private void HandleBrokenCircuitException()
|
|
{
|
|
ViewBag.BasketInoperativeMsg = "Basket Service is inoperative, please try later on. (Business Msg Due to Circuit-Breaker)";
|
|
}
|
|
}
|
|
}
|