using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.AspNetCore.Http; using Microsoft.eShopWeb.Web.ViewModels; using Microsoft.AspNetCore.Identity; using Microsoft.eShopWeb.Infrastructure.Identity; using System; using System.Collections.Generic; using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate; using Microsoft.AspNetCore.Authorization; using Microsoft.eShopWeb.Web.Interfaces; namespace Microsoft.eShopWeb.Web.Controllers { [Route("[controller]/[action]")] public class BasketController : Controller { private readonly IBasketService _basketService; private readonly IUriComposer _uriComposer; private readonly SignInManager _signInManager; private readonly IAppLogger _logger; private readonly IOrderService _orderService; private readonly IBasketViewModelService _basketViewModelService; public BasketController(IBasketService basketService, IBasketViewModelService basketViewModelService, IOrderService orderService, IUriComposer uriComposer, SignInManager signInManager, IAppLogger logger) { _basketService = basketService; _uriComposer = uriComposer; _signInManager = signInManager; _logger = logger; _orderService = orderService; _basketViewModelService = basketViewModelService; } [HttpGet] public async Task Index() { var basketModel = await GetBasketViewModelAsync(); return View(basketModel); } [HttpPost] public async Task Index(Dictionary items) { var basketViewModel = await GetBasketViewModelAsync(); await _basketService.SetQuantities(basketViewModel.Id, items); return View(await GetBasketViewModelAsync()); } // POST: /Basket/AddToBasket [HttpPost] public async Task AddToBasket(CatalogItemViewModel productDetails) { if (productDetails?.Id == null) { return RedirectToAction("Index", "Catalog"); } var basketViewModel = await GetBasketViewModelAsync(); await _basketService.AddItemToBasket(basketViewModel.Id, productDetails.Id, productDetails.Price, 1); return RedirectToAction("Index"); } [HttpPost] [Authorize] public async Task Checkout(Dictionary items) { var basketViewModel = await GetBasketViewModelAsync(); await _basketService.SetQuantities(basketViewModel.Id, items); await _orderService.CreateOrderAsync(basketViewModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240")); await _basketService.DeleteBasketAsync(basketViewModel.Id); return View("Checkout"); } private async Task GetBasketViewModelAsync() { if (_signInManager.IsSignedIn(HttpContext.User)) { return await _basketViewModelService.GetOrCreateBasketForUser(User.Identity.Name); } string anonymousId = GetOrSetBasketCookie(); return await _basketViewModelService.GetOrCreateBasketForUser(anonymousId); } private string GetOrSetBasketCookie() { if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME)) { return Request.Cookies[Constants.BASKET_COOKIENAME]; } string anonymousId = Guid.NewGuid().ToString(); var cookieOptions = new CookieOptions(); cookieOptions.Expires = DateTime.Today.AddYears(10); Response.Cookies.Append(Constants.BASKET_COOKIENAME, anonymousId, cookieOptions); return anonymousId; } } }