Adding Razor Pages Version (#60)
* In progress copying code into new RP project Cleaning up namespaces and whitespace in original Web project * Cleaning up some more namespaces * Removing unused page. * Index page loads correctly. * Fixing up paging. * Moving views; getting ready to convert to RPs * Auto stash before merge of "master" and "origin/master" Basket and Checkout pages wired up * WIP on Account pages * Working on signin/signout * Working on auth * Getting order history working Fixing auth bug * Fixing Checkout issue * Fixing link
This commit is contained in:
51
src/WebRazorPages/ViewComponents/Basket.cs
Normal file
51
src/WebRazorPages/ViewComponents/Basket.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Infrastructure.Identity;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.eShopWeb.RazorPages.Interfaces;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.ViewComponents
|
||||
{
|
||||
public class Basket : ViewComponent
|
||||
{
|
||||
private readonly IBasketService _basketService;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
|
||||
public Basket(IBasketService basketService,
|
||||
SignInManager<ApplicationUser> signInManager)
|
||||
{
|
||||
_basketService = basketService;
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
public async Task<IViewComponentResult> InvokeAsync(string userName)
|
||||
{
|
||||
var vm = new BasketComponentViewModel();
|
||||
vm.ItemsCount = (await GetBasketViewModelAsync()).Items.Sum(i => i.Quantity);
|
||||
return View(vm);
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME))
|
||||
{
|
||||
return Request.Cookies[Constants.BASKET_COOKIENAME];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user