diff --git a/src/Web/Controllers/BasketController.cs b/src/Web/Controllers/BasketController.cs index 3aea615fd..55e58fa64 100644 --- a/src/Web/Controllers/BasketController.cs +++ b/src/Web/Controllers/BasketController.cs @@ -7,6 +7,7 @@ using Infrastructure.Identity; using System; using Web; +using System.Collections.Generic; namespace Microsoft.eShopWeb.Controllers { @@ -17,14 +18,17 @@ public class BasketController : Controller private const string _basketSessionKey = "basketId"; private readonly IUriComposer _uriComposer; private readonly SignInManager _signInManager; + private readonly IAppLogger _logger; public BasketController(IBasketService basketService, IUriComposer uriComposer, - SignInManager signInManager) + SignInManager signInManager, + IAppLogger logger) { _basketService = basketService; _uriComposer = uriComposer; _signInManager = signInManager; + _logger = logger; } [HttpGet] @@ -35,6 +39,16 @@ public async Task Index() return View(basketModel); } + [HttpPost] + public async Task Index(Dictionary items) + { + var basketViewModel = await GetBasketViewModelAsync(); + await _basketService.SetQuantities(basketViewModel.Id, items); + + return View(await GetBasketViewModelAsync()); + } + + // POST: /Basket/AddToBasket [HttpPost] public async Task AddToBasket(CatalogItemViewModel productDetails) @@ -51,9 +65,17 @@ public async Task AddToBasket(CatalogItemViewModel productDetails } [HttpPost] - public async Task Checkout() + public async Task Checkout(List model) { + // TODO: Get model binding working with collection of items var basket = await GetBasketViewModelAsync(); + //await _basketService.SetQuantities(basket.Id, quantities); + + foreach (var item in basket.Items) + { + _logger.LogWarning($"Id: {item.Id}; Qty: {item.Quantity}"); + } + // redirect to OrdersController await _basketService.Checkout(basket.Id); diff --git a/src/Web/Interfaces/IBasketService.cs b/src/Web/Interfaces/IBasketService.cs index 1cdbd2c94..e04e55106 100644 --- a/src/Web/Interfaces/IBasketService.cs +++ b/src/Web/Interfaces/IBasketService.cs @@ -1,4 +1,6 @@ -using Microsoft.eShopWeb.ViewModels; +using Microsoft.eShopWeb.ApplicationCore.Entities; +using Microsoft.eShopWeb.ViewModels; +using System.Collections.Generic; using System.Threading.Tasks; namespace ApplicationCore.Interfaces @@ -8,6 +10,7 @@ public interface IBasketService Task GetOrCreateBasketForUser(string userName); Task TransferBasketAsync(string anonymousId, string userName); Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity); + Task SetQuantities(int basketId, Dictionary quantities); Task Checkout(int basketId); } } diff --git a/src/Web/Services/BasketService.cs b/src/Web/Services/BasketService.cs index 3e0e859a6..1900be622 100644 --- a/src/Web/Services/BasketService.cs +++ b/src/Web/Services/BasketService.cs @@ -12,14 +12,17 @@ public class BasketService : IBasketService { private readonly IAsyncRepository _basketRepository; private readonly IUriComposer _uriComposer; + private readonly IAppLogger _logger; private readonly IRepository _itemRepository; public BasketService(IAsyncRepository basketRepository, IRepository itemRepository, - IUriComposer uriComposer) + IUriComposer uriComposer, + IAppLogger logger) { _basketRepository = basketRepository; _uriComposer = uriComposer; + this._logger = logger; _itemRepository = itemRepository; } @@ -81,6 +84,20 @@ public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price await _basketRepository.UpdateAsync(basket); } + public async Task SetQuantities(int basketId, Dictionary quantities) + { + var basket = await _basketRepository.GetByIdAsync(basketId); + foreach (var item in basket.Items) + { + if (quantities.TryGetValue(item.Id.ToString(), out var quantity)) + { + _logger.LogWarning($"Updating quantity of item ID:{item.Id} to {quantity}."); + item.Quantity = quantity; + } + } + await _basketRepository.UpdateAsync(basket); + } + public async Task Checkout(int basketId) { var basket = await _basketRepository.GetByIdAsync(basketId); diff --git a/src/Web/ViewComponents/Basket.cs b/src/Web/ViewComponents/Basket.cs index 990e91792..47effdfca 100644 --- a/src/Web/ViewComponents/Basket.cs +++ b/src/Web/ViewComponents/Basket.cs @@ -1,27 +1,52 @@ using ApplicationCore.Interfaces; +using Infrastructure.Identity; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.eShopWeb.ViewModels; +using System; +using System.Linq; using System.Threading.Tasks; namespace Web.ViewComponents { public class Basket : ViewComponent { - private readonly IBasketService _cartSvc; + private readonly IBasketService _basketService; + private readonly SignInManager _signInManager; - public Basket(IBasketService cartSvc) => _cartSvc = cartSvc; + public Basket(IBasketService basketService, + SignInManager signInManager) + { + _basketService = basketService; + _signInManager = signInManager; + } public async Task InvokeAsync(string userName) { var vm = new BasketComponentViewModel(); - var itemsInCart = await ItemsInBasketAsync(userName); - vm.ItemsCount = itemsInCart; + vm.ItemsCount = (await GetBasketViewModelAsync()).Items.Sum(i => i.Quantity); return View(vm); } - private async Task ItemsInBasketAsync(string userName) + + private async Task GetBasketViewModelAsync() + { + if (_signInManager.IsSignedIn(HttpContext.User)) + { + return await _basketService.GetOrCreateBasketForUser(User.Identity.Name); + } + string anonymousId = GetBasketIdFromCookie(); + if (anonymousId == null) return new BasketViewModel(); + return await _basketService.GetOrCreateBasketForUser(anonymousId); + } + + private string GetBasketIdFromCookie() { - var basket = await _cartSvc.GetOrCreateBasketForUser(userName); - return basket.Items.Count; + if (Request.Cookies.ContainsKey(Constants.BASKET_COOKIENAME)) + { + return Request.Cookies[Constants.BASKET_COOKIENAME]; + } + return null; } } } diff --git a/src/Web/Views/Basket/Index.cshtml b/src/Web/Views/Basket/Index.cshtml index a68b37e1b..4915da915 100644 --- a/src/Web/Views/Basket/Index.cshtml +++ b/src/Web/Views/Basket/Index.cshtml @@ -1,7 +1,7 @@ @using Microsoft.eShopWeb.ViewModels +@model BasketViewModel @{ ViewData["Title"] = "Basket"; - @model BasketViewModel }
@@ -23,9 +23,10 @@
Cost
- @foreach (var item in Model.Items) + @for (int i=0; i< Model.Items.Count; i++) { -
+ var item = Model.Items[i]; +
@@ -33,8 +34,8 @@
@item.ProductName
$ @item.UnitPrice.ToString("N2")
- - + +
$ @Math.Round(item.Quantity * item.UnitPrice, 2).ToString("N2")
@@ -65,7 +66,9 @@
} -
+
+ diff --git a/src/Web/Views/Catalog/_pagination.cshtml b/src/Web/Views/Catalog/_pagination.cshtml index ca94c726f..6ba9f4f13 100644 --- a/src/Web/Views/Catalog/_pagination.cshtml +++ b/src/Web/Views/Catalog/_pagination.cshtml @@ -1,32 +1,38 @@ @model PaginationInfoViewModel
-
-
- -
-
+
+
+ +
+
diff --git a/src/Web/Views/Shared/_Layout.cshtml b/src/Web/Views/Shared/_Layout.cshtml index cb0b16bfe..03a2be64b 100644 --- a/src/Web/Views/Shared/_Layout.cshtml +++ b/src/Web/Views/Shared/_Layout.cshtml @@ -1,89 +1,79 @@  - - - @ViewData["Title"] - Microsoft.eShopOnWeb + + + @ViewData["Title"] - Microsoft.eShopOnWeb - - + + - - @* - - - *@ - - - - @* - - - *@ - - - - + + + + + + - + + - - @RenderBody() + @RenderBody() -
-
-
+
+
+
-
- -
+
+ +
-
- -
+
+ +
-
-
-
+
+
+
- - - - - - - - - - + + + + + + + + + + - @RenderSection("scripts", required: false) + @RenderSection("scripts", required: false) diff --git a/src/Web/Views/Shared/_LoginPartial.cshtml b/src/Web/Views/Shared/_LoginPartial.cshtml index 77ed3ef7b..3d97d0ec4 100644 --- a/src/Web/Views/Shared/_LoginPartial.cshtml +++ b/src/Web/Views/Shared/_LoginPartial.cshtml @@ -39,7 +39,7 @@ } else { -
+
@@ -52,6 +52,7 @@ else
-
@await Component.InvokeAsync("Basket") -
+
+ @await Component.InvokeAsync("Basket") +
} diff --git a/src/Web/wwwroot/css/catalog/pager.css b/src/Web/wwwroot/css/catalog/pager.css index eec6e3940..b98b6dec5 100644 --- a/src/Web/wwwroot/css/catalog/pager.css +++ b/src/Web/wwwroot/css/catalog/pager.css @@ -3,8 +3,12 @@ text-align: center; } -.esh-pager-item { - margin: 0 5vw; +.esh-pager-item-left { + float: left; +} + +.esh-pager-item-right { + float: right; } .esh-pager-item--navigable {