Moved Change Basket Quantities to PurchaseBFF. MVC client updated
This commit is contained in:
parent
d8382f8d52
commit
0256c6a7ad
@ -26,7 +26,7 @@
|
||||
"DownstreamHost": "purchasebff",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/purchase-bff/{everything}",
|
||||
"UpstreamHttpMethod": ["POST"],
|
||||
"UpstreamHttpMethod": [ "POST", "PUT" ],
|
||||
"AuthenticationOptions": {
|
||||
"AuthenticationProviderKey": "IdentityApiKey",
|
||||
"AllowedScopes": []
|
||||
|
@ -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)
|
||||
|
@ -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 ]")
|
||||
{
|
||||
|
@ -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
|
||||
|
@ -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 =>
|
||||
{
|
||||
// Simplify this logic by using the
|
||||
// new out variable initializer.
|
||||
if (quantities.TryGetValue(x.Id, out var quantity))
|
||||
{
|
||||
x.Quantity = quantity;
|
||||
}
|
||||
});
|
||||
var token = await GetUserTokenAsync();
|
||||
var updateBasketUri = API.Purchase.UpdateBasketItem(_purchaseUrl);
|
||||
var userId = user.Id;
|
||||
|
||||
return basket;
|
||||
var response = await _apiClient.PutAsync(updateBasketUri, new
|
||||
{
|
||||
BasketId = userId,
|
||||
Updates = quantities.Select(kvp => new
|
||||
{
|
||||
BasketItemId = kvp.Key,
|
||||
NewQty = kvp.Value
|
||||
}).ToArray()
|
||||
}, token);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<Basket>(jsonResponse);
|
||||
}
|
||||
|
||||
public Order MapBasketToOrder(Basket basket)
|
||||
|
Loading…
x
Reference in New Issue
Block a user