Wired up CartDrawel and ProductView to cart service and local storage
continuous-integration/drone/pr Build is passing

This commit is contained in:
Khwezi Mngoma
2026-06-10 23:01:21 +02:00
parent 3bce80c963
commit 64e0fcba27
5 changed files with 85 additions and 61 deletions
@@ -1,5 +1,6 @@
using LiteCharms.Features.Browser;
using LiteCharms.Features.Hasher;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products.Models;
using MidrandBookshop.Services.ShoppingCart.Models;
@@ -30,7 +31,7 @@ public sealed class CartService(LocalStorageService localStorage)
public async Task SaveCartToStorageAsync() => await localStorage.SaveAsync(CartStorageKey, ShoppingCart);
public void AddItem(ProductPrice productPrice)
public void AddItem(ProductPrice productPrice, Product product, Author author)
{
var itemExists = false;
@@ -50,6 +51,8 @@ public sealed class CartService(LocalStorageService localStorage)
if (!itemExists)
ShoppingCart.Items.Add(new CartItem
{
Product = product,
Author = author,
Price = productPrice,
Amount = productPrice.Amount,
Quantity = 1,
@@ -59,16 +62,8 @@ public sealed class CartService(LocalStorageService localStorage)
NotifyStateChanged();
}
public void UpdateQuantity(long productPriceId, int newQuantity)
public void UpdateQuantity(long productPriceId, int delta)
{
if (newQuantity <= 0)
{
RemoveAllSameItem(productPriceId);
NotifyStateChanged();
return;
}
for (var i = 0; i < ShoppingCart.Items.Count; i++)
{
if (ShoppingCart.Items[i].Price!.Id == productPriceId)
@@ -76,8 +71,8 @@ public sealed class CartService(LocalStorageService localStorage)
var oldQuantity = ShoppingCart.Items[i].Quantity;
var pricePerUnit = ShoppingCart.Items[i].Price!.Amount;
ShoppingCart.Items[i].Quantity = newQuantity;
ShoppingCart.Items[i].Amount = pricePerUnit * newQuantity;
ShoppingCart.Items[i].Quantity += delta;
ShoppingCart.Items[i].Amount = pricePerUnit * ShoppingCart.Items[i].Quantity;
break;
}
}
@@ -94,7 +89,9 @@ public sealed class CartService(LocalStorageService localStorage)
{
if (ShoppingCart.Items[i].Quantity <= 1)
{
ShoppingCart.Items.RemoveAt(i);
ShoppingCart.Items.Remove(ShoppingCart.Items[i]);
break;
}
else
{
@@ -1,12 +1,17 @@
using LiteCharms.Features.MidrandBooks.Products.Models;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products.Models;
namespace MidrandBookshop.Services.ShoppingCart.Models;
public sealed class CartItem
{
public Author? Author { get; set; }
public Product? Product { get; set; }
public ProductPrice? Price { get; set; }
public long Quantity { get; set; }
public int Quantity { get; set; }
public decimal Amount { get; set; }
}