Skip to content

Commit

Permalink
Adding Order Features (dotnet-architecture#47)
Browse files Browse the repository at this point in the history
* Working on order model binding from checkout page - WIP

* Small layout tweaks (dotnet-architecture#43)

* Updating quantities implemented.

* Fixed basket widget count
  • Loading branch information
ardalis authored Sep 13, 2017
1 parent ed5d176 commit db6ad75
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 117 deletions.
26 changes: 24 additions & 2 deletions src/Web/Controllers/BasketController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Infrastructure.Identity;
using System;
using Web;
using System.Collections.Generic;

namespace Microsoft.eShopWeb.Controllers
{
Expand All @@ -17,14 +18,17 @@ public class BasketController : Controller
private const string _basketSessionKey = "basketId";
private readonly IUriComposer _uriComposer;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IAppLogger<BasketController> _logger;

public BasketController(IBasketService basketService,
IUriComposer uriComposer,
SignInManager<ApplicationUser> signInManager)
SignInManager<ApplicationUser> signInManager,
IAppLogger<BasketController> logger)
{
_basketService = basketService;
_uriComposer = uriComposer;
_signInManager = signInManager;
_logger = logger;
}

[HttpGet]
Expand All @@ -35,6 +39,16 @@ public async Task<IActionResult> Index()
return View(basketModel);
}

[HttpPost]
public async Task<IActionResult> Index(Dictionary<string, int> items)
{
var basketViewModel = await GetBasketViewModelAsync();
await _basketService.SetQuantities(basketViewModel.Id, items);

return View(await GetBasketViewModelAsync());
}


// POST: /Basket/AddToBasket
[HttpPost]
public async Task<IActionResult> AddToBasket(CatalogItemViewModel productDetails)
Expand All @@ -51,9 +65,17 @@ public async Task<IActionResult> AddToBasket(CatalogItemViewModel productDetails
}

[HttpPost]
public async Task<IActionResult> Checkout()
public async Task<IActionResult> Checkout(List<BasketItemViewModel> 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);

Expand Down
5 changes: 4 additions & 1 deletion src/Web/Interfaces/IBasketService.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,6 +10,7 @@ public interface IBasketService
Task<BasketViewModel> GetOrCreateBasketForUser(string userName);
Task TransferBasketAsync(string anonymousId, string userName);
Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity);
Task SetQuantities(int basketId, Dictionary<string, int> quantities);
Task Checkout(int basketId);
}
}
19 changes: 18 additions & 1 deletion src/Web/Services/BasketService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ public class BasketService : IBasketService
{
private readonly IAsyncRepository<Basket> _basketRepository;
private readonly IUriComposer _uriComposer;
private readonly IAppLogger<BasketService> _logger;
private readonly IRepository<CatalogItem> _itemRepository;

public BasketService(IAsyncRepository<Basket> basketRepository,
IRepository<CatalogItem> itemRepository,
IUriComposer uriComposer)
IUriComposer uriComposer,
IAppLogger<BasketService> logger)
{
_basketRepository = basketRepository;
_uriComposer = uriComposer;
this._logger = logger;
_itemRepository = itemRepository;
}

Expand Down Expand Up @@ -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<string,int> 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);
Expand Down
39 changes: 32 additions & 7 deletions src/Web/ViewComponents/Basket.cs
Original file line number Diff line number Diff line change
@@ -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<ApplicationUser> _signInManager;

public Basket(IBasketService cartSvc) => _cartSvc = cartSvc;
public Basket(IBasketService basketService,
SignInManager<ApplicationUser> signInManager)
{
_basketService = basketService;
_signInManager = signInManager;
}

public async Task<IViewComponentResult> 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<int> ItemsInBasketAsync(string userName)

private async Task<BasketViewModel> 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;
}
}
}
15 changes: 9 additions & 6 deletions src/Web/Views/Basket/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@using Microsoft.eShopWeb.ViewModels
@model BasketViewModel
@{
ViewData["Title"] = "Basket";
@model BasketViewModel
}
<section class="esh-catalog-hero">
<div class="container">
Expand All @@ -23,18 +23,19 @@
<section class="esh-basket-title col-xs-2">Cost</section>
</article>
<div class="esh-catalog-items row">
@foreach (var item in Model.Items)
@for (int i=0; i< Model.Items.Count; i++)
{
<article class="esh-basket-items row">
var item = Model.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="@("quantities[" + item.Id +"].Key")" value="@item.Id" />
<input type="number" class="esh-basket-input" min="1" name="@("quantities[" + item.Id +"].Value")" value="@item.Quantity" />
<input type="hidden" name="@("Items[" + i + "].Key")" value="@item.Id" />
<input type="number" class="esh-basket-input" min="1" name="@("Items[" + i + "].Value")" 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>
Expand Down Expand Up @@ -65,7 +66,9 @@
</article>
</div>
}
<section class="esh-basket-item col-xs-push-9 col-xs-3">
<section class="esh-basket-item col-xs-push-8 col-xs-4">
<button class="btn esh-basket-checkout" name="updatebutton" value="" type="submit"
asp-action="Update">[ Update ]</button>
<input type="submit" asp-action="Checkout"
class="btn esh-basket-checkout"
value="[ Checkout ]" name="action" />
Expand Down
60 changes: 33 additions & 27 deletions src/Web/Views/Catalog/_pagination.cshtml
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
@model PaginationInfoViewModel

<div class="esh-pager">
<div class="container">
<article class="esh-pager-wrapper row">
<nav>
<a class="esh-pager-item esh-pager-item--navigable @Model.Previous"
id="Previous"
asp-controller="Catalog"
asp-action="Index"
asp-route-page="@(Model.ActualPage -1)"
aria-label="Previous">
Previous
</a>

<span class="esh-pager-item">
Showing @Model.ItemsPerPage of @Model.TotalItems products - Page @(Model.ActualPage + 1) - @Model.TotalPages
</span>

<a class="esh-pager-item esh-pager-item--navigable @Model.Next"
id="Next"
asp-controller="Catalog"
asp-action="Index"
asp-route-page="@(Model.ActualPage + 1)"
aria-label="Next">
Next
</a>
</nav>
</article>
</div>
<div class="container-fluid">
<article class="esh-pager-wrapper row">
<nav>
<div class="col-md-2 col-xs-12">
<a class="esh-pager-item-left esh-pager-item--navigable @Model.Previous"
id="Previous"
asp-controller="Catalog"
asp-action="Index"
asp-route-page="@(Model.ActualPage - 1)"
aria-label="Previous">
Previous
</a>
</div>

<div class="col-md-8 col-xs-12">
<span class="esh-pager-item">
Showing @Model.ItemsPerPage of @Model.TotalItems products - Page @(Model.ActualPage + 1) - @Model.TotalPages
</span>
</div>

<div class="col-md-2 col-xs-12">
<a class="esh-pager-item-right esh-pager-item--navigable @Model.Next"
id="Next"
asp-controller="Catalog"
asp-action="Index"
asp-route-page="@(Model.ActualPage + 1)"
aria-label="Next">
Next
</a>
</div>
</nav>
</article>
</div>
</div>

Loading

0 comments on commit db6ad75

Please sign in to comment.