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:
109
src/WebRazorPages/Pages/Basket/Index.cshtml.cs
Normal file
109
src/WebRazorPages/Pages/Basket/Index.cshtml.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.RazorPages;
|
||||
using Microsoft.eShopWeb.RazorPages.ViewModels;
|
||||
using Microsoft.eShopWeb.RazorPages.Interfaces;
|
||||
using ApplicationCore.Interfaces;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Infrastructure.Identity;
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using ApplicationCore.Entities.OrderAggregate;
|
||||
|
||||
namespace Microsoft.eShopWeb.RazorPages.Pages.Basket
|
||||
{
|
||||
public class IndexModel : PageModel
|
||||
{
|
||||
private readonly IBasketService _basketService;
|
||||
private const string _basketSessionKey = "basketId";
|
||||
private readonly IUriComposer _uriComposer;
|
||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||
private readonly IAppLogger<IndexModel> _logger;
|
||||
private readonly IOrderService _orderService;
|
||||
private string _username = null;
|
||||
|
||||
public IndexModel(IBasketService basketService,
|
||||
IUriComposer uriComposer,
|
||||
SignInManager<ApplicationUser> signInManager,
|
||||
IAppLogger<IndexModel> logger,
|
||||
IOrderService orderService)
|
||||
{
|
||||
_basketService = basketService;
|
||||
_uriComposer = uriComposer;
|
||||
_signInManager = signInManager;
|
||||
_logger = logger;
|
||||
_orderService = orderService;
|
||||
}
|
||||
|
||||
public BasketViewModel BasketModel { get; set; } = new BasketViewModel();
|
||||
|
||||
public async Task OnGet()
|
||||
{
|
||||
await SetBasketModelAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPost(CatalogItemViewModel productDetails)
|
||||
{
|
||||
if (productDetails?.Id == null)
|
||||
{
|
||||
return RedirectToPage("/Index");
|
||||
}
|
||||
await SetBasketModelAsync();
|
||||
|
||||
await _basketService.AddItemToBasket(BasketModel.Id, productDetails.Id, productDetails.Price, 1);
|
||||
|
||||
await SetBasketModelAsync();
|
||||
|
||||
return RedirectToPage();
|
||||
}
|
||||
|
||||
public async Task OnPostUpdate(Dictionary<string,int> items)
|
||||
{
|
||||
await SetBasketModelAsync();
|
||||
await _basketService.SetQuantities(BasketModel.Id, items);
|
||||
|
||||
await SetBasketModelAsync();
|
||||
}
|
||||
|
||||
public async Task<IActionResult> OnPostCheckout(Dictionary<string,int> items)
|
||||
{
|
||||
await SetBasketModelAsync();
|
||||
|
||||
await _basketService.SetQuantities(BasketModel.Id, items);
|
||||
|
||||
await _orderService.CreateOrderAsync(BasketModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));
|
||||
|
||||
await _basketService.DeleteBasketAsync(BasketModel.Id);
|
||||
|
||||
return RedirectToPage("/Basket/CheckoutComplete");
|
||||
}
|
||||
|
||||
private async Task SetBasketModelAsync()
|
||||
{
|
||||
if (_signInManager.IsSignedIn(HttpContext.User))
|
||||
{
|
||||
BasketModel = await _basketService.GetOrCreateBasketForUser(User.Identity.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
GetOrSetBasketCookieAndUserName();
|
||||
BasketModel = await _basketService.GetOrCreateBasketForUser(_username);
|
||||
}
|
||||
}
|
||||
|
||||
private void GetOrSetBasketCookieAndUserName()
|
||||
{
|
||||
if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME))
|
||||
{
|
||||
_username = Request.Cookies[Constants.BASKET_COOKIENAME];
|
||||
}
|
||||
if (_username != null) return;
|
||||
|
||||
_username = Guid.NewGuid().ToString();
|
||||
var cookieOptions = new CookieOptions();
|
||||
cookieOptions.Expires = DateTime.Today.AddYears(10);
|
||||
Response.Cookies.Append(Constants.BASKET_COOKIENAME, _username, cookieOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user