mirror of
https://github.com/valentincanonical/eShopOnWeb-chisel-demo.git
synced 2026-05-20 15:22:54 +00:00
* Adding tests for GetById endpoint * Updating tests and messages * Adding paged endpoint and also AutoMapper * Authenticate endpoint works as bool with tests * Got JWT token security working with Create and Delete endpoints and Swashbuckle. * Working on getting cookie and jwt token auth working in the same app All tests are passing * Creating new project and moving APIs Build succeeds; tests need updated. * all tests passing after moving services to PublicApi project * Fix authorize attributes * Uncomment and update ApiCatalogControllerLists tests Co-authored-by: Eric Fleming <eric-fleming18@hotmail.com>
103 lines
3.7 KiB
C#
103 lines
3.7 KiB
C#
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
|
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
|
using Microsoft.eShopWeb.Infrastructure.Identity;
|
|
using Microsoft.eShopWeb.Web.Interfaces;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Microsoft.eShopWeb.Web.Pages.Basket
|
|
{
|
|
[Authorize]
|
|
public class CheckoutModel : PageModel
|
|
{
|
|
private readonly IBasketService _basketService;
|
|
private readonly SignInManager<ApplicationUser> _signInManager;
|
|
private readonly IOrderService _orderService;
|
|
private string _username = null;
|
|
private readonly IBasketViewModelService _basketViewModelService;
|
|
private readonly IAppLogger<CheckoutModel> _logger;
|
|
|
|
public CheckoutModel(IBasketService basketService,
|
|
IBasketViewModelService basketViewModelService,
|
|
SignInManager<ApplicationUser> signInManager,
|
|
IOrderService orderService,
|
|
IAppLogger<CheckoutModel> logger)
|
|
{
|
|
_basketService = basketService;
|
|
_signInManager = signInManager;
|
|
_orderService = orderService;
|
|
_basketViewModelService = basketViewModelService;
|
|
_logger = logger;
|
|
}
|
|
|
|
public BasketViewModel BasketModel { get; set; } = new BasketViewModel();
|
|
|
|
public async Task OnGet()
|
|
{
|
|
await SetBasketModelAsync();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPost(IEnumerable<BasketItemViewModel> items)
|
|
{
|
|
try
|
|
{
|
|
await SetBasketModelAsync();
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
var updateModel = items.ToDictionary(b => b.Id.ToString(), b => b.Quantity);
|
|
await _basketService.SetQuantities(BasketModel.Id, updateModel);
|
|
await _orderService.CreateOrderAsync(BasketModel.Id, new Address("123 Main St.", "Kent", "OH", "United States", "44240"));
|
|
await _basketService.DeleteBasketAsync(BasketModel.Id);
|
|
}
|
|
catch (EmptyBasketOnCheckoutException emptyBasketOnCheckoutException)
|
|
{
|
|
//Redirect to Empty Basket page
|
|
_logger.LogWarning(emptyBasketOnCheckoutException.Message);
|
|
return RedirectToPage("/Basket/Index");
|
|
}
|
|
|
|
return RedirectToPage("Success");
|
|
}
|
|
|
|
private async Task SetBasketModelAsync()
|
|
{
|
|
if (_signInManager.IsSignedIn(HttpContext.User))
|
|
{
|
|
BasketModel = await _basketViewModelService.GetOrCreateBasketForUser(User.Identity.Name);
|
|
}
|
|
else
|
|
{
|
|
GetOrSetBasketCookieAndUserName();
|
|
BasketModel = await _basketViewModelService.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);
|
|
}
|
|
}
|
|
}
|