Moved Change Basket Quantities to PurchaseBFF. MVC client updated
This commit is contained in:
parent
d8382f8d52
commit
0256c6a7ad
@ -26,7 +26,7 @@
|
|||||||
"DownstreamHost": "purchasebff",
|
"DownstreamHost": "purchasebff",
|
||||||
"DownstreamPort": 80,
|
"DownstreamPort": 80,
|
||||||
"UpstreamPathTemplate": "/purchase-bff/{everything}",
|
"UpstreamPathTemplate": "/purchase-bff/{everything}",
|
||||||
"UpstreamHttpMethod": ["POST"],
|
"UpstreamHttpMethod": [ "POST", "PUT" ],
|
||||||
"AuthenticationOptions": {
|
"AuthenticationOptions": {
|
||||||
"AuthenticationProviderKey": "IdentityApiKey",
|
"AuthenticationProviderKey": "IdentityApiKey",
|
||||||
"AllowedScopes": []
|
"AllowedScopes": []
|
||||||
|
@ -21,7 +21,40 @@ namespace PurchaseBff.Controllers
|
|||||||
_basket = basketService;
|
_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)
|
public async Task<IActionResult> AddBasketItem([FromBody] AddBasketItemRequest data)
|
||||||
{
|
{
|
||||||
if (data == null || data.Quantity == 0)
|
if (data == null || data.Quantity == 0)
|
||||||
|
@ -49,7 +49,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
|
|||||||
{
|
{
|
||||||
var user = _appUserParser.Parse(HttpContext.User);
|
var user = _appUserParser.Parse(HttpContext.User);
|
||||||
var basket = await _basketSvc.SetQuantities(user, quantities);
|
var basket = await _basketSvc.SetQuantities(user, quantities);
|
||||||
var vm = await _basketSvc.UpdateBasket(basket);
|
|
||||||
|
|
||||||
if (action == "[ Checkout ]")
|
if (action == "[ Checkout ]")
|
||||||
{
|
{
|
||||||
|
@ -8,6 +8,7 @@ namespace WebMVC.Infrastructure
|
|||||||
public static class Purchase
|
public static class Purchase
|
||||||
{
|
{
|
||||||
public static string AddItemToBasket(string baseUri) => $"{baseUri}/basket/items";
|
public static string AddItemToBasket(string baseUri) => $"{baseUri}/basket/items";
|
||||||
|
public static string UpdateBasketItem(string baseUri) => $"{baseUri}/basket/items";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Basket
|
public static class Basket
|
||||||
|
@ -5,6 +5,7 @@ using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
|||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using WebMVC.Infrastructure;
|
using WebMVC.Infrastructure;
|
||||||
using WebMVC.Models;
|
using WebMVC.Models;
|
||||||
@ -72,19 +73,24 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
|
|||||||
|
|
||||||
public async Task<Basket> SetQuantities(ApplicationUser user, Dictionary<string, int> quantities)
|
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
|
BasketId = userId,
|
||||||
// new out variable initializer.
|
Updates = quantities.Select(kvp => new
|
||||||
if (quantities.TryGetValue(x.Id, out var quantity))
|
|
||||||
{
|
{
|
||||||
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)
|
public Order MapBasketToOrder(Basket basket)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user