"Remove from Cart" functionality added; need to go back to debug why the Cart is no longer coming up.
This commit is contained in:
parent
3bf434124c
commit
675444795c
|
@ -100,5 +100,33 @@ namespace ShopOnline.Api.Controllers
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{id:int}")]
|
||||||
|
public async Task<ActionResult<CartItemDto>> 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,9 +45,16 @@ namespace ShopOnline.Api.Repositories
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Task<CartItem> DeleteItem(int id)
|
public async Task<CartItem> 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<CartItem> GetItem(int id)
|
public async Task<CartItem> GetItem(int id)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using Microsoft.AspNetCore.Components;
|
using Microsoft.AspNetCore.Components;
|
||||||
using ShopOnline.Models.Dtos;
|
using ShopOnline.Models.Dtos;
|
||||||
|
using ShopOnline.Web.Services;
|
||||||
using ShopOnline.Web.Services.Contracts;
|
using ShopOnline.Web.Services.Contracts;
|
||||||
|
|
||||||
namespace ShopOnline.Web.Pages
|
namespace ShopOnline.Web.Pages
|
||||||
|
@ -28,5 +29,6 @@ namespace ShopOnline.Web.Pages
|
||||||
{
|
{
|
||||||
return groupedProductDtos.FirstOrDefault(pg => pg.CategoryId == groupedProductDtos.Key).CategoryName;
|
return groupedProductDtos.FirstOrDefault(pg => pg.CategoryId == groupedProductDtos.Key).CategoryName;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,10 @@ else {
|
||||||
<h5>@item.ProductName</h5>
|
<h5>@item.ProductName</h5>
|
||||||
<div class="mb-4">@item.ProductDescription</div>
|
<div class="mb-4">@item.ProductDescription</div>
|
||||||
<span>Price: <b>@item.Price.ToString("C")</b></span>
|
<span>Price: <b>@item.Price.ToString("C")</b></span>
|
||||||
|
<div>
|
||||||
|
<button @onclick = "(() => DeleteCartItem_Click(item.Id))"
|
||||||
|
class="btn btn-danger sm">Remove</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace ShopOnline.Web.Pages
|
||||||
[Inject]
|
[Inject]
|
||||||
public IShoppingCartService ShoppingCartService { get; set; }
|
public IShoppingCartService ShoppingCartService { get; set; }
|
||||||
|
|
||||||
public IEnumerable<CartItemDto> ShoppingCartItems { get; set; }
|
public List<CartItemDto> ShoppingCartItems { get; set; }
|
||||||
|
|
||||||
public string ErrorMessage { 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,8 @@ namespace ShopOnline.Web.Services.Contracts
|
||||||
{
|
{
|
||||||
public interface IShoppingCartService
|
public interface IShoppingCartService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<CartItemDto>> GetItems(int userId);
|
Task<List<CartItemDto>> GetItems(int userId);
|
||||||
Task<CartItemDto> AddItem(CartItemToAddDto cartItemToAddDto);
|
Task<CartItemDto> AddItem(CartItemToAddDto cartItemToAddDto);
|
||||||
|
Task<CartItemDto> DeleteItem(int id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,25 @@ namespace ShopOnline.Web.Services
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<CartItemDto>> GetItems(int userId)
|
public async Task<CartItemDto> DeleteItem(int id)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await httpClient.DeleteAsync($"api/ShoppingCart/{id}");
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
return await response.Content.ReadFromJsonAsync<CartItemDto>();
|
||||||
|
}
|
||||||
|
return default(CartItemDto);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<CartItemDto>> GetItems(int userId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -47,9 +65,9 @@ namespace ShopOnline.Web.Services
|
||||||
{
|
{
|
||||||
if(response.StatusCode == System.Net.HttpStatusCode.NoContent)
|
if(response.StatusCode == System.Net.HttpStatusCode.NoContent)
|
||||||
{
|
{
|
||||||
return Enumerable.Empty<CartItemDto>();
|
return Enumerable.Empty<CartItemDto>().ToList();
|
||||||
}
|
}
|
||||||
return await response.Content.ReadFromJsonAsync<IEnumerable<CartItemDto>>();
|
return await response.Content.ReadFromJsonAsync<List<CartItemDto>>();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -63,5 +81,10 @@ namespace ShopOnline.Web.Services
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Task<List<CartItemDto>> IShoppingCartService.GetItems(int userId)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue