From 0256c6a7ad75403c8a5d4cf1587e41550aaf58a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Tom=C3=A0s?= Date: Wed, 31 Jan 2018 16:29:06 +0000 Subject: [PATCH] Moved Change Basket Quantities to PurchaseBFF. MVC client updated --- .../configuration/configuration.json | 2 +- .../Controllers/BasketController.cs | 35 ++++++++++++++++++- src/Web/WebMVC/Controllers/CartController.cs | 1 - src/Web/WebMVC/Infrastructure/API.cs | 1 + src/Web/WebMVC/Services/BasketService.cs | 24 ++++++++----- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/src/Apigw/OcelotApiGw/configuration/configuration.json b/src/Apigw/OcelotApiGw/configuration/configuration.json index 2e95c8843..5216c9540 100644 --- a/src/Apigw/OcelotApiGw/configuration/configuration.json +++ b/src/Apigw/OcelotApiGw/configuration/configuration.json @@ -26,7 +26,7 @@ "DownstreamHost": "purchasebff", "DownstreamPort": 80, "UpstreamPathTemplate": "/purchase-bff/{everything}", - "UpstreamHttpMethod": ["POST"], + "UpstreamHttpMethod": [ "POST", "PUT" ], "AuthenticationOptions": { "AuthenticationProviderKey": "IdentityApiKey", "AllowedScopes": [] diff --git a/src/BFFs/PurchaseBff/Controllers/BasketController.cs b/src/BFFs/PurchaseBff/Controllers/BasketController.cs index 6030dfadb..12290a127 100644 --- a/src/BFFs/PurchaseBff/Controllers/BasketController.cs +++ b/src/BFFs/PurchaseBff/Controllers/BasketController.cs @@ -21,7 +21,40 @@ namespace PurchaseBff.Controllers _basket = basketService; } - [HttpPost("items")] + [HttpPut] + [Route("items")] + public async Task UpdateQuantities([FromBody] UpdateBasketItemsRequest data) + { + if (!data.Updates.Any()) + { + return BadRequest("No updates sent"); + } + + // Retrieve the current basket + var currentBasket = await _basket.GetById(data.BasketId); + if (currentBasket == null) + { + return BadRequest($"Basket with id {data.BasketId} not found."); + } + + // Update with new quantities + foreach (var update in data.Updates) + { + var basketItem = currentBasket.Items.SingleOrDefault(bitem => bitem.Id == update.BasketItemId); + if (basketItem == null) + { + return BadRequest($"Basket item with id {update.BasketItemId} not found"); + } + basketItem.Quantity = update.NewQty; + } + + // Save the updated basket + await _basket.Update(currentBasket); + return Ok(currentBasket); + } + + [HttpPost] + [Route("items")] public async Task AddBasketItem([FromBody] AddBasketItemRequest data) { if (data == null || data.Quantity == 0) diff --git a/src/Web/WebMVC/Controllers/CartController.cs b/src/Web/WebMVC/Controllers/CartController.cs index 94f50a2de..41484d79c 100644 --- a/src/Web/WebMVC/Controllers/CartController.cs +++ b/src/Web/WebMVC/Controllers/CartController.cs @@ -49,7 +49,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers { var user = _appUserParser.Parse(HttpContext.User); var basket = await _basketSvc.SetQuantities(user, quantities); - var vm = await _basketSvc.UpdateBasket(basket); if (action == "[ Checkout ]") { diff --git a/src/Web/WebMVC/Infrastructure/API.cs b/src/Web/WebMVC/Infrastructure/API.cs index b59183512..765ee3e9b 100644 --- a/src/Web/WebMVC/Infrastructure/API.cs +++ b/src/Web/WebMVC/Infrastructure/API.cs @@ -8,6 +8,7 @@ namespace WebMVC.Infrastructure public static class Purchase { public static string AddItemToBasket(string baseUri) => $"{baseUri}/basket/items"; + public static string UpdateBasketItem(string baseUri) => $"{baseUri}/basket/items"; } public static class Basket diff --git a/src/Web/WebMVC/Services/BasketService.cs b/src/Web/WebMVC/Services/BasketService.cs index e167f73cc..1c4b2aa87 100644 --- a/src/Web/WebMVC/Services/BasketService.cs +++ b/src/Web/WebMVC/Services/BasketService.cs @@ -5,6 +5,7 @@ using Microsoft.eShopOnContainers.WebMVC.ViewModels; using Microsoft.Extensions.Options; using Newtonsoft.Json; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using WebMVC.Infrastructure; using WebMVC.Models; @@ -72,19 +73,24 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services public async Task SetQuantities(ApplicationUser user, Dictionary quantities) { - var basket = await GetBasket(user); - basket.Items.ForEach(x => + var token = await GetUserTokenAsync(); + var updateBasketUri = API.Purchase.UpdateBasketItem(_purchaseUrl); + var userId = user.Id; + + var response = await _apiClient.PutAsync(updateBasketUri, new { - // Simplify this logic by using the - // new out variable initializer. - if (quantities.TryGetValue(x.Id, out var quantity)) + BasketId = userId, + Updates = quantities.Select(kvp => new { - x.Quantity = quantity; - } - }); + BasketItemId = kvp.Key, + NewQty = kvp.Value + }).ToArray() + }, token); - return basket; + response.EnsureSuccessStatusCode(); + var jsonResponse = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(jsonResponse); } public Order MapBasketToOrder(Basket basket)