Implemented cart service with state tracker and linked to main layout
continuous-integration/drone/pr Build is passing

This commit is contained in:
Khwezi Mngoma
2026-06-09 23:39:49 +02:00
parent d3e9b30be5
commit 3bce80c963
10 changed files with 326 additions and 92 deletions
@@ -3,6 +3,7 @@ using LiteCharms.Features.MidrandBooks.Authors;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products;
using LiteCharms.Features.MidrandBooks.Products.Models;
using MidrandBookshop.Services.ShoppingCart;
namespace MidrandBookshop.Components.Pages;
@@ -12,10 +13,12 @@ public partial class ProductView : ComponentBase
[Inject] private ProductService ProductService { get; set; } = default!;
[Inject] private AuthorService AuthorService { get; set; } = default!;
[Inject] private CartService CartService { get; set; } = default!;
[Inject] private NavigationManager Navigation { get; set; } = default!;
protected bool IsLoading { get; private set; } = true;
protected Product? CurrentProduct { get; private set; }
protected ProductPrice? CurrentPrice { get; private set; }
protected decimal LivePrice { get; private set; } = 0.00m;
protected string AuthorName { get; private set; } = "Unknown Author";
protected string PrimaryCategory { get; private set; } = "General";
@@ -44,6 +47,7 @@ public partial class ProductView : ComponentBase
var priceResult = await ProductService.GetProductPriceAsync(BookId);
LivePrice = priceResult.IsSuccess ? priceResult.Value.Amount : 0m;
CurrentPrice = priceResult.IsSuccess ? priceResult.Value : null;
var categoryResult = await ProductService.GetProductCategoriesAsync(BookId);
if (categoryResult.IsSuccess && categoryResult.Value.Length > 0)
@@ -73,6 +77,7 @@ public partial class ProductView : ComponentBase
catch (Exception)
{
CurrentProduct = null;
CurrentPrice = null;
}
finally
{
@@ -80,14 +85,34 @@ public partial class ProductView : ComponentBase
}
}
protected void IncreaseQty() => Quantity++;
protected void DecreaseQty() { if (Quantity > 1) Quantity--; }
protected void IncreaseQty()
{
Quantity++;
protected void HandleAddToCart()
if (CurrentPrice is not null)
CartService.UpdateQuantity(CurrentPrice!.Id, Quantity);
}
protected void DecreaseQty()
{
if (Quantity > 1)
{
Quantity--;
CartService.UpdateQuantity(CurrentPrice!.Id, Quantity);
}
}
protected async void HandleAddToCart()
{
if (CurrentProduct == null) return;
if (CurrentPrice is not null)
{
CartService.AddItem(CurrentPrice);
Quantity++;
}
}
protected void ViewAllAuthorBooks()
{
if (CurrentAuthor is not null)