* transfer basket on login

* review page

* unit tests for TransferBasketAsync
This commit is contained in:
yigith
2020-06-25 03:48:23 +03:00
committed by GitHub
parent a4d02d9dad
commit 3b1339787f
12 changed files with 254 additions and 51 deletions

View File

@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.eShopWeb.Infrastructure.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account
{
@@ -18,11 +19,13 @@ namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account
{
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly ILogger<LoginModel> _logger;
private readonly IBasketService _basketService;
public LoginModel(SignInManager<ApplicationUser> signInManager, ILogger<LoginModel> logger)
public LoginModel(SignInManager<ApplicationUser> signInManager, ILogger<LoginModel> logger, IBasketService basketService)
{
_signInManager = signInManager;
_logger = logger;
_basketService = basketService;
}
[BindProperty]
@@ -78,6 +81,7 @@ namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
await TransferAnonymousBasketToUserAsync(Input.Email);
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
@@ -99,5 +103,15 @@ namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account
// If we got this far, something failed, redisplay form
return Page();
}
private async Task TransferAnonymousBasketToUserAsync(string userName)
{
if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME))
{
var anonymousId = Request.Cookies[Constants.BASKET_COOKIENAME];
await _basketService.TransferBasketAsync(anonymousId, userName);
Response.Cookies.Delete(Constants.BASKET_COOKIENAME);
}
}
}
}