Browse Source

Moved Change Basket Quantities to PurchaseBFF. MVC client updated

pull/565/head^2
Eduard Tomàs 7 years ago
parent
commit
0256c6a7ad
5 changed files with 51 additions and 12 deletions
  1. +1
    -1
      src/Apigw/OcelotApiGw/configuration/configuration.json
  2. +34
    -1
      src/BFFs/PurchaseBff/Controllers/BasketController.cs
  3. +0
    -1
      src/Web/WebMVC/Controllers/CartController.cs
  4. +1
    -0
      src/Web/WebMVC/Infrastructure/API.cs
  5. +15
    -9
      src/Web/WebMVC/Services/BasketService.cs

+ 1
- 1
src/Apigw/OcelotApiGw/configuration/configuration.json View File

@ -26,7 +26,7 @@
"DownstreamHost": "purchasebff",
"DownstreamPort": 80,
"UpstreamPathTemplate": "/purchase-bff/{everything}",
"UpstreamHttpMethod": ["POST"],
"UpstreamHttpMethod": [ "POST", "PUT" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "IdentityApiKey",
"AllowedScopes": []


+ 34
- 1
src/BFFs/PurchaseBff/Controllers/BasketController.cs View File

@ -21,7 +21,40 @@ namespace PurchaseBff.Controllers
_basket = basketService;
}
[HttpPost("items")]
[HttpPut]
[Route("items")]
public async Task<IActionResult> 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<IActionResult> AddBasketItem([FromBody] AddBasketItemRequest data)
{
if (data == null || data.Quantity == 0)


+ 0
- 1
src/Web/WebMVC/Controllers/CartController.cs View File

@ -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 ]")
{


+ 1
- 0
src/Web/WebMVC/Infrastructure/API.cs View File

@ -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


+ 15
- 9
src/Web/WebMVC/Services/BasketService.cs View File

@ -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<Basket> SetQuantities(ApplicationUser user, Dictionary<string, int> 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<Basket>(jsonResponse);
}
public Order MapBasketToOrder(Basket basket)


Loading…
Cancel
Save