manage basket checkout after login (#371)

This commit is contained in:
Cédric Michel
2020-05-06 21:53:52 +02:00
committed by GitHub
parent fdb4869c0b
commit 516d87aaa1
5 changed files with 25 additions and 7 deletions

View File

@@ -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)

View File

@@ -5,5 +5,6 @@
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";
}
}

View File

@@ -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)

View File

@@ -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>