diff --git a/ShopOnline.Api/Controllers/ShoppingCartController.cs b/ShopOnline.Api/Controllers/ShoppingCartController.cs index 7fd9377..8dd5b48 100644 --- a/ShopOnline.Api/Controllers/ShoppingCartController.cs +++ b/ShopOnline.Api/Controllers/ShoppingCartController.cs @@ -128,5 +128,29 @@ namespace ShopOnline.Api.Controllers } } + + [HttpPatch("{id:int}")] + public async Task> UpdateQuantity(int id, CartItemQuantityUpdateDto cartItemQuantityUpdateDto) + { + try + { + var cartItem = await this.shoppingCartRepository.UpdateQuantity(id, cartItemQuantityUpdateDto); + if(cartItem == null) + { + return NotFound(); + } + + var product = await productRepository.GetItem(cartItem.ProductId); + + var cartItemDto = cartItem.ConvertToDto(product); + + return Ok(cartItemDto); + } + catch (Exception ex) + { + return StatusCode(StatusCodes.Status500InternalServerError, ex.Message); + + } + } } } diff --git a/ShopOnline.Api/Repositories/ShoppingCartRepository.cs b/ShopOnline.Api/Repositories/ShoppingCartRepository.cs index 76b2154..d2d7c3f 100644 --- a/ShopOnline.Api/Repositories/ShoppingCartRepository.cs +++ b/ShopOnline.Api/Repositories/ShoppingCartRepository.cs @@ -87,9 +87,18 @@ namespace ShopOnline.Api.Repositories }).ToListAsync(); } - public Task UpdateQuantity(int id, CartItemQuantityUpdateDto cartItemQuantityUpdateDto) + public async Task UpdateQuantity(int id, CartItemQuantityUpdateDto cartItemQuantityUpdateDto) { - throw new NotImplementedException(); + var item = await this.shopOnlineDbContext.CartItems.FindAsync(id); + + if(item != null) + { + item.Quantity = cartItemQuantityUpdateDto.Quantity; + await this.shopOnlineDbContext.SaveChangesAsync(); + return item; + } + + return null; } } } diff --git a/ShopOnline.Web/Pages/ShoppingCart.razor b/ShopOnline.Web/Pages/ShoppingCart.razor index 208d6f9..86c70ac 100644 --- a/ShopOnline.Web/Pages/ShoppingCart.razor +++ b/ShopOnline.Web/Pages/ShoppingCart.razor @@ -19,7 +19,12 @@ else {
@item.ProductName
@item.ProductDescription
- Price: @item.Price.ToString("C") + Price: @item.Price.ToString("C") + + +
@@ -31,7 +36,7 @@ else {
Cart Summary
-
Total -
+
Total - (@TotalQuantity items)  @TotalPrice
  Proceed to Checkout diff --git a/ShopOnline.Web/Pages/ShoppingCartBase.cs b/ShopOnline.Web/Pages/ShoppingCartBase.cs index 011d148..e6cd164 100644 --- a/ShopOnline.Web/Pages/ShoppingCartBase.cs +++ b/ShopOnline.Web/Pages/ShoppingCartBase.cs @@ -13,11 +13,15 @@ namespace ShopOnline.Web.Pages public string ErrorMessage { get; set; } + protected string TotalPrice { get; set; } + protected int TotalQuantity { get; set; } + protected override async Task OnInitializedAsync() { try { ShoppingCartItems = await ShoppingCartService.GetItems(HardCoded.UserId); + CalculateCartSummaryTotals(); } catch (Exception ex) { @@ -30,9 +34,71 @@ namespace ShopOnline.Web.Pages var cartItemDto = await ShoppingCartService.DeleteItem(id); RemoveCartItem(id); + CalculateCartSummaryTotals(); } + protected async Task UpdateQuantityCartItem_Click(int id, int quantity) + { + try + { + if(quantity > 0) + { + var updateItemDto = new CartItemQuantityUpdateDto + { + CartItemId = id, + Quantity = quantity + }; + + var returnedUpdateItemDto = await this.ShoppingCartService.UpdateQuantity(updateItemDto); + + UpdateItemTotalPrice(returnedUpdateItemDto); + CalculateCartSummaryTotals(); + } + else + { + var item = this.ShoppingCartItems.FirstOrDefault(i => i.Id == id); + + if(item != null) + { + item.Quantity = 1; + item.TotalPrice = item.Price; + } + } + } + catch (Exception) + { + + throw; + } + } + + private void UpdateItemTotalPrice(CartItemDto cartItemDto) + { + var item = GetCartItem(cartItemDto.Id); + + if(item != null) + { + item.TotalPrice = cartItemDto.Price * cartItemDto.Quantity; + } + } + + private void CalculateCartSummaryTotals() + { + SetTotalPrice(); + SetTotalQuantity(); + } + + private void SetTotalPrice() + { + TotalPrice = this.ShoppingCartItems.Sum(p => p.TotalPrice).ToString("C"); + } + + private void SetTotalQuantity() + { + TotalQuantity = this.ShoppingCartItems.Sum(p => p.Quantity); + } + private CartItemDto GetCartItem(int id) { return ShoppingCartItems.FirstOrDefault(i => i.Id == id); @@ -44,5 +110,7 @@ namespace ShopOnline.Web.Pages ShoppingCartItems.Remove(CartItemDto); } + + } } diff --git a/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs b/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs index 4d974a5..0a9fbaa 100644 --- a/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs +++ b/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs @@ -7,5 +7,6 @@ namespace ShopOnline.Web.Services.Contracts Task> GetItems(int userId); Task AddItem(CartItemToAddDto cartItemToAddDto); Task DeleteItem(int id); + Task UpdateQuantity(CartItemQuantityUpdateDto cartItemQuantityUpdateDto); } } diff --git a/ShopOnline.Web/Services/ShoppingCartService.cs b/ShopOnline.Web/Services/ShoppingCartService.cs index 98b80b1..542c95b 100644 --- a/ShopOnline.Web/Services/ShoppingCartService.cs +++ b/ShopOnline.Web/Services/ShoppingCartService.cs @@ -1,6 +1,9 @@ -using ShopOnline.Models.Dtos; +using Newtonsoft.Json; +using ShopOnline.Models.Dtos; using ShopOnline.Web.Services.Contracts; using System.Net.Http.Json; +using System.Text; +using System.Text.Json.Serialization; namespace ShopOnline.Web.Services { @@ -82,6 +85,26 @@ namespace ShopOnline.Web.Services } } + public async Task UpdateQuantity(CartItemQuantityUpdateDto cartItemQuantityUpdateDto) + { + try + { + var jsonRequest = JsonConvert.SerializeObject(cartItemQuantityUpdateDto); + var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json-patch+json"); + var response = await httpClient.PatchAsync($"api/ShoppingCart/{cartItemQuantityUpdateDto.CartItemId}", content); + + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadFromJsonAsync(); + } + return null; + } + catch (Exception) + { + + throw; + } + } } } diff --git a/ShopOnline.Web/ShopOnline.Web.csproj b/ShopOnline.Web/ShopOnline.Web.csproj index 41404a9..9db1809 100644 --- a/ShopOnline.Web/ShopOnline.Web.csproj +++ b/ShopOnline.Web/ShopOnline.Web.csproj @@ -9,6 +9,7 @@ +