Browse Source
* Working on order model binding from checkout page - WIP * Small layout tweaks (#43) * Updating quantities implemented. * Fixed basket widget countmain
committed by
GitHub
9 changed files with 188 additions and 117 deletions
@ -1,27 +1,52 @@ |
|||||
using ApplicationCore.Interfaces; |
using ApplicationCore.Interfaces; |
||||
|
using Infrastructure.Identity; |
||||
|
using Microsoft.AspNetCore.Http; |
||||
|
using Microsoft.AspNetCore.Identity; |
||||
using Microsoft.AspNetCore.Mvc; |
using Microsoft.AspNetCore.Mvc; |
||||
using Microsoft.eShopWeb.ViewModels; |
using Microsoft.eShopWeb.ViewModels; |
||||
|
using System; |
||||
|
using System.Linq; |
||||
using System.Threading.Tasks; |
using System.Threading.Tasks; |
||||
|
|
||||
namespace Web.ViewComponents |
namespace Web.ViewComponents |
||||
{ |
{ |
||||
public class Basket : ViewComponent |
public class Basket : ViewComponent |
||||
{ |
{ |
||||
private readonly IBasketService _cartSvc; |
private readonly IBasketService _basketService; |
||||
|
private readonly SignInManager<ApplicationUser> _signInManager; |
||||
|
|
||||
public Basket(IBasketService cartSvc) => _cartSvc = cartSvc; |
public Basket(IBasketService basketService, |
||||
|
SignInManager<ApplicationUser> signInManager) |
||||
|
{ |
||||
|
_basketService = basketService; |
||||
|
_signInManager = signInManager; |
||||
|
} |
||||
|
|
||||
public async Task<IViewComponentResult> InvokeAsync(string userName) |
public async Task<IViewComponentResult> InvokeAsync(string userName) |
||||
{ |
{ |
||||
var vm = new BasketComponentViewModel(); |
var vm = new BasketComponentViewModel(); |
||||
var itemsInCart = await ItemsInBasketAsync(userName); |
vm.ItemsCount = (await GetBasketViewModelAsync()).Items.Sum(i => i.Quantity); |
||||
vm.ItemsCount = itemsInCart; |
|
||||
return View(vm); |
return View(vm); |
||||
} |
} |
||||
private async Task<int> ItemsInBasketAsync(string userName) |
|
||||
|
private async Task<BasketViewModel> GetBasketViewModelAsync() |
||||
|
{ |
||||
|
if (_signInManager.IsSignedIn(HttpContext.User)) |
||||
|
{ |
||||
|
return await _basketService.GetOrCreateBasketForUser(User.Identity.Name); |
||||
|
} |
||||
|
string anonymousId = GetBasketIdFromCookie(); |
||||
|
if (anonymousId == null) return new BasketViewModel(); |
||||
|
return await _basketService.GetOrCreateBasketForUser(anonymousId); |
||||
|
} |
||||
|
|
||||
|
private string GetBasketIdFromCookie() |
||||
{ |
{ |
||||
var basket = await _cartSvc.GetOrCreateBasketForUser(userName); |
if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME)) |
||||
return basket.Items.Count; |
{ |
||||
|
return Request.Cookies[Constants.BASKET_COOKIENAME]; |
||||
|
} |
||||
|
return null; |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue