manage basket checkout after login (#371)
This commit is contained in:
@@ -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<OrderItem>();
|
||||
foreach (var item in basket.Items)
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<IActionResult> OnPost(Dictionary<string, int> items)
|
||||
|
||||
@@ -77,8 +77,15 @@
|
||||
asp-page-handler="Update">
|
||||
[ Update ]
|
||||
</button>
|
||||
@{
|
||||
var data = new Dictionary<string, string>
|
||||
{
|
||||
{ Constants.BASKET_ID, Model.BasketModel.Id.ToString() },
|
||||
};
|
||||
}
|
||||
<input type="submit" asp-page="Checkout"
|
||||
class="btn esh-basket-checkout"
|
||||
asp-all-route-data=data
|
||||
value="[ Checkout ]" name="action" />
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user