Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* transfer basket on login

* review page

* unit tests for TransferBasketAsync
  • Loading branch information
yigith authored Jun 25, 2020
1 parent a4d02d9 commit 3b13397
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 51 deletions.
21 changes: 16 additions & 5 deletions src/ApplicationCore/Services/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,22 @@ public async Task TransferBasketAsync(string anonymousId, string userName)
{
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
Guard.Against.NullOrEmpty(userName, nameof(userName));
var basketSpec = new BasketWithItemsSpecification(anonymousId);
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
if (basket == null) return;
basket.SetNewBuyerId(userName);
await _basketRepository.UpdateAsync(basket);
var anonymousBasketSpec = new BasketWithItemsSpecification(anonymousId);
var anonymousBasket = await _basketRepository.FirstOrDefaultAsync(anonymousBasketSpec);
if (anonymousBasket == null) return;
var userBasketSpec = new BasketWithItemsSpecification(userName);
var userBasket = await _basketRepository.FirstOrDefaultAsync(userBasketSpec);
if (userBasket == null)
{
userBasket = new Basket(userName);
await _basketRepository.AddAsync(userBasket);
}
foreach (var item in anonymousBasket.Items)
{
userBasket.AddItem(item.CatalogItemId, item.UnitPrice, item.Quantity);
}
await _basketRepository.UpdateAsync(userBasket);
await _basketRepository.DeleteAsync(anonymousBasket);
}
}
}
16 changes: 15 additions & 1 deletion src/Web/Areas/Identity/Pages/Account/Login.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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
{
Expand All @@ -18,11 +19,13 @@ public class LoginModel : PageModel
{
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]
Expand Down Expand Up @@ -78,6 +81,7 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
await TransferAnonymousBasketToUserAsync(Input.Email);
return LocalRedirect(returnUrl);
}
if (result.RequiresTwoFactor)
Expand All @@ -99,5 +103,15 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
// 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);
}
}
}
}
80 changes: 75 additions & 5 deletions src/Web/Pages/Basket/Checkout.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@page
@model CheckoutModel
@model CheckoutModel
@{
ViewData["Title"] = "Checkout Complete";
ViewData["Title"] = "Checkout";
}
<section class="esh-catalog-hero">
<div class="container">
Expand All @@ -10,7 +10,77 @@
</section>

<div class="container">
<h1>Thanks for your Order!</h1>
<h1>Review</h1>
@if (Model.BasketModel.Items.Any())
{
<form asp-page="Checkout" method="post">
<article class="esh-basket-titles row">
<br />
<section class="esh-basket-title col-xs-3">Product</section>
<section class="esh-basket-title col-xs-3 hidden-lg-down"></section>
<section class="esh-basket-title col-xs-2">Price</section>
<section class="esh-basket-title col-xs-2">Quantity</section>
<section class="esh-basket-title col-xs-2">Cost</section>
</article>
<div class="esh-catalog-items row">
<div asp-validation-summary="All" class="text-danger"></div>
@for (int i = 0; i < Model.BasketModel.Items.Count; i++)
{
var item = Model.BasketModel.Items[i];
<article class="esh-basket-items row">
<div>
<section class="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down">
<img class="esh-basket-image" src="@item.PictureUrl" />
</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-3">@item.ProductName</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">
<input type="hidden" name="@("Items[" + i + "].Id")" value="@item.Id" />
<input type="hidden" name="@("Items[" + i + "].Quantity")" value="@item.Quantity" />
@item.Quantity
</section>
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
</div>
<div class="row">

<a asp-page="/Index">Continue Shopping...</a>
</div>
</div>
</article>
}

<div class="container">
<article class="esh-basket-titles esh-basket-titles--clean row">
<section class="esh-basket-title col-xs-10"></section>
<section class="esh-basket-title col-xs-2">Total</section>
</article>

<article class="esh-basket-items row">
<section class="esh-basket-item col-xs-10"></section>
<section class="esh-basket-item esh-basket-item--mark col-xs-2">$ @Model.BasketModel.Total().ToString("N2")</section>
</article>

<article class="esh-basket-items row">
<section class="esh-basket-item col-xs-7"></section>
</article>
</div>
<div class="row">
<section class="esh-basket-item col-xs-1">
<a asp-page="Index" class="btn esh-basket-checkout text-white">[ Back ]</a>
</section>
<section class="esh-basket-item col-xs-push-7 col-xs-4 text-right">
<input type="submit" class="btn esh-basket-checkout" value="[ Pay Now ]" />
</section>
</div>
</div>
</form>
}
else
{
<h3 class="esh-catalog-items row">
Basket is empty.
</h3>

<section class="esh-basket-item">
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping..]</a>
</section>
}
</div>
14 changes: 5 additions & 9 deletions src/Web/Pages/Basket/Checkout.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
Expand All @@ -14,6 +15,7 @@

namespace Microsoft.eShopWeb.Web.Pages.Basket
{
[Authorize]
public class CheckoutModel : PageModel
{
private readonly IBasketService _basketService;
Expand All @@ -40,13 +42,7 @@ public CheckoutModel(IBasketService basketService,

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);
}
await SetBasketModelAsync();
}

public async Task<IActionResult> OnPost(IEnumerable<BasketItemViewModel> items)
Expand All @@ -72,7 +68,7 @@ public async Task<IActionResult> OnPost(IEnumerable<BasketItemViewModel> items)
return RedirectToPage("/Basket/Index");
}

return RedirectToPage();
return RedirectToPage("Success");
}

private async Task SetBasketModelAsync()
Expand Down
45 changes: 18 additions & 27 deletions src/Web/Pages/Basket/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@
@for (int i = 0; i < Model.BasketModel.Items.Count; i++)
{
var item = Model.BasketModel.Items[i];
<article class="esh-basket-items row">
<div>
<section class="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down">
<img class="esh-basket-image" src="@item.PictureUrl" />
</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-3">@item.ProductName</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">
<input type="hidden" name="@("Items[" + i + "].Id")" value="@item.Id" />
<input type="number" class="esh-basket-input" min="0" name="@("Items[" + i + "].Quantity")" value="@item.Quantity" />
</section>
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
</div>
<div class="row">
<article class="esh-basket-items row">
<div>
<section class="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down">
<img class="esh-basket-image" src="@item.PictureUrl" />
</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-3">@item.ProductName</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">$ @item.UnitPrice.ToString("N2")</section>
<section class="esh-basket-item esh-basket-item--middle col-xs-2">
<input type="hidden" name="@("Items[" + i + "].Id")" value="@item.Id" />
<input type="number" class="esh-basket-input" min="0" name="@("Items[" + i + "].Quantity")" value="@item.Quantity" />
</section>
<section class="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2">$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")</section>
</div>
<div class="row">

</div>
</article>
</div>
</article>
}

<div class="container">
Expand Down Expand Up @@ -71,16 +71,7 @@
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" />
<a asp-page="./Checkout" class="btn esh-basket-checkout">[ Checkout ]</a>
</section>
</div>
</div>
Expand All @@ -91,7 +82,7 @@
<h3 class="esh-catalog-items row">
Basket is empty.
</h3>

<section class="esh-basket-item">
<a asp-page="/Index" class="btn esh-basket-checkout text-white">[ Continue Shopping..]</a>
</section>
Expand Down
17 changes: 17 additions & 0 deletions src/Web/Pages/Basket/Success.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@page
@model SuccessModel
@{
ViewData["Title"] = "Checkout Complete";
}

<section class="esh-catalog-hero">
<div class="container">
<img class="esh-catalog-title" src="~/images/main_banner_text.png" />
</div>
</section>

<div class="container">
<h1>Thanks for your Order!</h1>

<a asp-page="/Index">Continue Shopping...</a>
</div>
19 changes: 19 additions & 0 deletions src/Web/Pages/Basket/Success.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Microsoft.eShopWeb.Web.Pages.Basket
{
[Authorize]
public class SuccessModel : PageModel
{
public void OnGet()
{

}
}
}
2 changes: 1 addition & 1 deletion src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.4" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.76" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.4.1" />
Expand Down
2 changes: 2 additions & 0 deletions src/Web/wwwroot/css/basket/basket.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@
.esh-basket-checkout:hover {
background-color: #4a760f;
transition: all 0.35s; }
.esh-basket-checkout:visited {
color: #FFFFFF; }
2 changes: 1 addition & 1 deletion src/Web/wwwroot/css/basket/basket.component.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/Web/wwwroot/css/basket/basket.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
background-color: $color-secondary-darker;
transition: all $animation-speed-default;
}

&:visited {
color: $color-foreground-brighter;
}
}
}

Expand Down
Loading

0 comments on commit 3b13397

Please sign in to comment.