From 675444795cfae40a431b6635613d119e949bd3e5 Mon Sep 17 00:00:00 2001 From: Kira Date: Mon, 12 Sep 2022 11:27:36 -0700 Subject: [PATCH] "Remove from Cart" functionality added; need to go back to debug why the Cart is no longer coming up. --- .../Controllers/ShoppingCartController.cs | 28 ++++++++++++++++++ .../Repositories/ShoppingCartRepository.cs | 11 +++++-- ShopOnline.Web/Pages/ProductsBase.cs | 2 ++ ShopOnline.Web/Pages/ShoppingCart.razor | 4 +++ ShopOnline.Web/Pages/ShoppingCartBase.cs | 21 +++++++++++++- .../Contracts/IShoppingCartService.cs | 3 +- .../Services/ShoppingCartService.cs | 29 +++++++++++++++++-- 7 files changed, 91 insertions(+), 7 deletions(-) diff --git a/ShopOnline.Api/Controllers/ShoppingCartController.cs b/ShopOnline.Api/Controllers/ShoppingCartController.cs index bd3e79a..7fd9377 100644 --- a/ShopOnline.Api/Controllers/ShoppingCartController.cs +++ b/ShopOnline.Api/Controllers/ShoppingCartController.cs @@ -100,5 +100,33 @@ namespace ShopOnline.Api.Controllers } } + + [HttpDelete("{id:int}")] + public async Task> DeleteItem(int id) + { + try + { + var cartItem = await this.shoppingCartRepository.DeleteItem(id); + if(cartItem == null) + { + return NotFound(); + } + + var product = await this.productRepository.GetItem(cartItem.ProductId); + if(product == null) + { + return NotFound(); + } + + 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 6b3fd95..76b2154 100644 --- a/ShopOnline.Api/Repositories/ShoppingCartRepository.cs +++ b/ShopOnline.Api/Repositories/ShoppingCartRepository.cs @@ -45,9 +45,16 @@ namespace ShopOnline.Api.Repositories } - public Task DeleteItem(int id) + public async Task DeleteItem(int id) { - throw new NotImplementedException(); + var item = await this.shopOnlineDbContext.CartItems.FindAsync(id); + + if(item != null) + { + this.shopOnlineDbContext.CartItems.Remove(item); + await this.shopOnlineDbContext.SaveChangesAsync(); + } + return item; } public async Task GetItem(int id) diff --git a/ShopOnline.Web/Pages/ProductsBase.cs b/ShopOnline.Web/Pages/ProductsBase.cs index 5977abf..76556b6 100644 --- a/ShopOnline.Web/Pages/ProductsBase.cs +++ b/ShopOnline.Web/Pages/ProductsBase.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Components; using ShopOnline.Models.Dtos; +using ShopOnline.Web.Services; using ShopOnline.Web.Services.Contracts; namespace ShopOnline.Web.Pages @@ -28,5 +29,6 @@ namespace ShopOnline.Web.Pages { return groupedProductDtos.FirstOrDefault(pg => pg.CategoryId == groupedProductDtos.Key).CategoryName; } + } } diff --git a/ShopOnline.Web/Pages/ShoppingCart.razor b/ShopOnline.Web/Pages/ShoppingCart.razor index 862edfe..208d6f9 100644 --- a/ShopOnline.Web/Pages/ShoppingCart.razor +++ b/ShopOnline.Web/Pages/ShoppingCart.razor @@ -20,6 +20,10 @@ else {
@item.ProductName
@item.ProductDescription
Price: @item.Price.ToString("C") +
+ +
} diff --git a/ShopOnline.Web/Pages/ShoppingCartBase.cs b/ShopOnline.Web/Pages/ShoppingCartBase.cs index a7b9c65..011d148 100644 --- a/ShopOnline.Web/Pages/ShoppingCartBase.cs +++ b/ShopOnline.Web/Pages/ShoppingCartBase.cs @@ -9,7 +9,7 @@ namespace ShopOnline.Web.Pages [Inject] public IShoppingCartService ShoppingCartService { get; set; } - public IEnumerable ShoppingCartItems { get; set; } + public List ShoppingCartItems { get; set; } public string ErrorMessage { get; set; } @@ -25,5 +25,24 @@ namespace ShopOnline.Web.Pages } } + protected async Task DeleteCartItem_Click(int id) + { + var cartItemDto = await ShoppingCartService.DeleteItem(id); + + RemoveCartItem(id); + + } + + private CartItemDto GetCartItem(int id) + { + return ShoppingCartItems.FirstOrDefault(i => i.Id == id); + } + + private void RemoveCartItem(int id) + { + var CartItemDto = GetCartItem(id); + + ShoppingCartItems.Remove(CartItemDto); + } } } diff --git a/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs b/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs index 557612a..4d974a5 100644 --- a/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs +++ b/ShopOnline.Web/Services/Contracts/IShoppingCartService.cs @@ -4,7 +4,8 @@ namespace ShopOnline.Web.Services.Contracts { public interface IShoppingCartService { - Task> GetItems(int userId); + Task> GetItems(int userId); Task AddItem(CartItemToAddDto cartItemToAddDto); + Task DeleteItem(int id); } } diff --git a/ShopOnline.Web/Services/ShoppingCartService.cs b/ShopOnline.Web/Services/ShoppingCartService.cs index aa8014d..023ff2a 100644 --- a/ShopOnline.Web/Services/ShoppingCartService.cs +++ b/ShopOnline.Web/Services/ShoppingCartService.cs @@ -38,7 +38,25 @@ namespace ShopOnline.Web.Services } } - public async Task> GetItems(int userId) + public async Task DeleteItem(int id) + { + try + { + var response = await httpClient.DeleteAsync($"api/ShoppingCart/{id}"); + if (response.IsSuccessStatusCode) + { + return await response.Content.ReadFromJsonAsync(); + } + return default(CartItemDto); + } + catch (Exception) + { + + throw; + } + } + + public async Task> GetItems(int userId) { try { @@ -47,9 +65,9 @@ namespace ShopOnline.Web.Services { if(response.StatusCode == System.Net.HttpStatusCode.NoContent) { - return Enumerable.Empty(); + return Enumerable.Empty().ToList(); } - return await response.Content.ReadFromJsonAsync>(); + return await response.Content.ReadFromJsonAsync>(); } else { @@ -63,5 +81,10 @@ namespace ShopOnline.Web.Services throw; } } + + Task> IShoppingCartService.GetItems(int userId) + { + throw new NotImplementedException(); + } } }