From 516d87aaa13715dfd6de882077ddfacc37156a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Michel?= Date: Wed, 6 May 2020 21:53:52 +0200 Subject: [PATCH] manage basket checkout after login (#371) --- src/ApplicationCore/Services/OrderService.cs | 5 ++++- src/Web/Constants.cs | 3 ++- src/Web/Pages/Basket/Checkout.cshtml.cs | 9 ++++++++- src/Web/Pages/Basket/Index.cshtml | 7 +++++++ src/Web/Startup.cs | 8 ++++---- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/ApplicationCore/Services/OrderService.cs b/src/ApplicationCore/Services/OrderService.cs index 632e7ea..8c364dc 100644 --- a/src/ApplicationCore/Services/OrderService.cs +++ b/src/ApplicationCore/Services/OrderService.cs @@ -5,6 +5,7 @@ using Microsoft.eShopWeb.ApplicationCore.Entities; using System.Collections.Generic; using Ardalis.GuardClauses; using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate; +using Microsoft.eShopWeb.ApplicationCore.Specifications; namespace Microsoft.eShopWeb.ApplicationCore.Services { @@ -28,7 +29,9 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services public async Task CreateOrderAsync(int basketId, Address shippingAddress) { - var basket = await _basketRepository.GetByIdAsync(basketId); + var basketSpec = new BasketWithItemsSpecification(basketId); + var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec); + Guard.Against.NullBasket(basketId, basket); var items = new List(); foreach (var item in basket.Items) diff --git a/src/Web/Constants.cs b/src/Web/Constants.cs index 3117820..d383813 100644 --- a/src/Web/Constants.cs +++ b/src/Web/Constants.cs @@ -1,9 +1,10 @@ -namespace Microsoft.eShopWeb.Web + namespace Microsoft.eShopWeb.Web { public static class Constants { public const string BASKET_COOKIENAME = "eShop"; public const int ITEMS_PER_PAGE = 10; public const string DEFAULT_USERNAME = "Guest"; + public const string BASKET_ID = "BasketId"; } } diff --git a/src/Web/Pages/Basket/Checkout.cshtml.cs b/src/Web/Pages/Basket/Checkout.cshtml.cs index 07842a0..7193e39 100644 --- a/src/Web/Pages/Basket/Checkout.cshtml.cs +++ b/src/Web/Pages/Basket/Checkout.cshtml.cs @@ -33,8 +33,15 @@ namespace Microsoft.eShopWeb.Web.Pages.Basket public BasketViewModel BasketModel { get; set; } = new BasketViewModel(); - public void OnGet() + public async Task OnGet() { + if (HttpContext.Request.Query.ContainsKey(Constants.BASKET_ID)) + { + var basketId = int.Parse(HttpContext.Request.Query[Constants.BASKET_ID]); + await _basketService.TransferBasketAsync(Request.Cookies[Constants.BASKET_COOKIENAME], User.Identity.Name); + await _orderService.CreateOrderAsync(basketId, new Address("123 Main St.", "Kent", "OH", "United States", "44240")); + await _basketService.DeleteBasketAsync(basketId); + } } public async Task OnPost(Dictionary items) diff --git a/src/Web/Pages/Basket/Index.cshtml b/src/Web/Pages/Basket/Index.cshtml index bd4be05..8f3b5b6 100644 --- a/src/Web/Pages/Basket/Index.cshtml +++ b/src/Web/Pages/Basket/Index.cshtml @@ -77,8 +77,15 @@ asp-page-handler="Update"> [ Update ] + @{ + var data = new Dictionary + { + { Constants.BASKET_ID, Model.BasketModel.Id.ToString() }, + }; + } diff --git a/src/Web/Startup.cs b/src/Web/Startup.cs index 6224641..720f666 100644 --- a/src/Web/Startup.cs +++ b/src/Web/Startup.cs @@ -120,7 +120,7 @@ namespace Microsoft.eShopWeb.Web options.Conventions.Add(new RouteTokenTransformerConvention( new SlugifyParameterTransformer())); - }); + }); services.AddRazorPages(options => { options.Conventions.AuthorizePage("/Basket/Checkout"); @@ -128,8 +128,8 @@ namespace Microsoft.eShopWeb.Web services.AddControllersWithViews(); services.AddHttpContextAccessor(); - - services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1"})); + + services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApi.Models.OpenApiInfo { Title = "My API", Version = "v1" })); services.AddHealthChecks(); @@ -201,7 +201,7 @@ namespace Microsoft.eShopWeb.Web app.UseStaticFiles(); app.UseRouting(); - + app.UseHttpsRedirection(); app.UseCookiePolicy(); app.UseAuthentication();