This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
using MidrandBookshop.Services.ShoppingCart;
|
using LiteCharms.Features.MidrandBooks.Payments;
|
||||||
using MidrandBookshop.Services.ShoppingCart.Models;
|
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
||||||
|
|
||||||
namespace MidrandBookshop.Components.Layout;
|
namespace MidrandBookshop.Components.Layout;
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -1,11 +1,11 @@
|
|||||||
using MidrandBookshop.Services.ShoppingCart;
|
using LiteCharms.Features.MidrandBooks.Payments;
|
||||||
using MidrandBookshop.Services.ShoppingCart.Models;
|
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
||||||
|
|
||||||
namespace MidrandBookshop.Components.Pages;
|
namespace MidrandBookshop.Components.Pages;
|
||||||
|
|
||||||
public partial class Cart(CartService cartService)
|
public partial class CartReview(CartService cartService)
|
||||||
{
|
{
|
||||||
protected Services.ShoppingCart.Models.Cart ShoppingCart => cartService?.ShoppingCart!;
|
protected Cart ShoppingCart => cartService?.ShoppingCart!;
|
||||||
|
|
||||||
protected async void IncreaseQty(CartItem item)
|
protected async void IncreaseQty(CartItem item)
|
||||||
{
|
{
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
using MidrandBookshop.Services.ShoppingCart;
|
using LiteCharms.Features.MidrandBooks.Payments;
|
||||||
using MidrandBookshop.Services.ShoppingCart.Models;
|
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
||||||
|
|
||||||
namespace MidrandBookshop.Components.Pages;
|
namespace MidrandBookshop.Components.Pages;
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ public partial class Checkout(CartService cartService)
|
|||||||
[Inject]
|
[Inject]
|
||||||
private AuthenticationStateProvider AuthStateProvider { get; set; } = default!;
|
private AuthenticationStateProvider AuthStateProvider { get; set; } = default!;
|
||||||
|
|
||||||
private Services.ShoppingCart.Models.Cart ShoppingCart => cartService.ShoppingCart;
|
private LiteCharms.Features.MidrandBooks.Payments.Models.Cart ShoppingCart => cartService.ShoppingCart;
|
||||||
|
|
||||||
private AuthenticationState? AuthState { get; set; }
|
private AuthenticationState? AuthState { get; set; }
|
||||||
private System.Security.Claims.ClaimsPrincipal? User { get; set; }
|
private System.Security.Claims.ClaimsPrincipal? User { get; set; }
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
using LiteCharms.Features;
|
using LiteCharms.Features;
|
||||||
using LiteCharms.Features.MidrandBooks.Authors;
|
using LiteCharms.Features.MidrandBooks.Authors;
|
||||||
using LiteCharms.Features.MidrandBooks.Authors.Models;
|
using LiteCharms.Features.MidrandBooks.Authors.Models;
|
||||||
|
using LiteCharms.Features.MidrandBooks.Payments;
|
||||||
|
using LiteCharms.Features.MidrandBooks.Payments.Models;
|
||||||
using LiteCharms.Features.MidrandBooks.Products;
|
using LiteCharms.Features.MidrandBooks.Products;
|
||||||
using LiteCharms.Features.MidrandBooks.Products.Models;
|
using LiteCharms.Features.MidrandBooks.Products.Models;
|
||||||
using Microsoft.AspNetCore.Cors.Infrastructure;
|
|
||||||
using MidrandBookshop.Services.ShoppingCart;
|
|
||||||
|
|
||||||
namespace MidrandBookshop.Components.Pages;
|
namespace MidrandBookshop.Components.Pages;
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ public partial class ProductView : ComponentBase
|
|||||||
[Inject] private NavigationManager Navigation { get; set; } = default!;
|
[Inject] private NavigationManager Navigation { get; set; } = default!;
|
||||||
[Inject] private CartService CartService { get; set; } = default!;
|
[Inject] private CartService CartService { get; set; } = default!;
|
||||||
|
|
||||||
protected Services.ShoppingCart.Models.Cart ShoppingCart => CartService?.ShoppingCart!;
|
protected Cart ShoppingCart => CartService?.ShoppingCart!;
|
||||||
|
|
||||||
protected bool IsLoading { get; private set; } = true;
|
protected bool IsLoading { get; private set; } = true;
|
||||||
protected Product? CurrentProduct { get; private set; }
|
protected Product? CurrentProduct { get; private set; }
|
||||||
|
|||||||
@@ -18,13 +18,13 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="LiteCharms.Features" Version="1.101.0" />
|
<PackageReference Include="LiteCharms.Features" Version="1.102.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- UI -->
|
<!-- UI -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="ANM.Blazored.Toast" Version="0.1.1" />
|
<PackageReference Include="ANM.Blazored.Toast" Version="0.1.1" />
|
||||||
<PackageReference Include="LiteCharms.Features.MidrandBooks" Version="1.101.0" />
|
<PackageReference Include="LiteCharms.Features.MidrandBooks" Version="1.102.0" />
|
||||||
|
|
||||||
<!-- Global Usings -->
|
<!-- Global Usings -->
|
||||||
<Using Include="Blazored.Toast.Services" />
|
<Using Include="Blazored.Toast.Services" />
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using LiteCharms.Features.Mediator;
|
|||||||
using LiteCharms.Features.MidrandBooks.Extensions;
|
using LiteCharms.Features.MidrandBooks.Extensions;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using MidrandBookshop.Components;
|
using MidrandBookshop.Components;
|
||||||
using MidrandBookshop.Services.ShoppingCart;
|
|
||||||
using static LiteCharms.Features.Extensions.Quartz;
|
using static LiteCharms.Features.Extensions.Quartz;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
@@ -27,7 +26,6 @@ builder.Services.AddEmailServiceBus();
|
|||||||
|
|
||||||
builder.Services.AddHttpClient();
|
builder.Services.AddHttpClient();
|
||||||
builder.Services.AddShopServices();
|
builder.Services.AddShopServices();
|
||||||
builder.Services.AddScoped<CartService>();
|
|
||||||
builder.Services.AddHashServices(builder.Configuration);
|
builder.Services.AddHashServices(builder.Configuration);
|
||||||
builder.Services.AddMidrandShopDatabase(builder.Configuration);
|
builder.Services.AddMidrandShopDatabase(builder.Configuration);
|
||||||
|
|
||||||
|
|||||||
@@ -1,153 +0,0 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
namespace MidrandBookshop.Services.ShoppingCart;
|
|
||||||
|
|
||||||
public sealed class CartService(LocalStorageService localStorage)
|
|
||||||
{
|
|
||||||
private readonly string CartStorageKey = HashService.ToMd5Hash(nameof(Cart)).Value;
|
|
||||||
|
|
||||||
public Cart ShoppingCart { get; private set; } = new();
|
|
||||||
|
|
||||||
public event Action? OnCartChanged;
|
|
||||||
|
|
||||||
public static Func<Cart, long, int> GetCartItemQuantity = (shoppingCart, productPriceId) =>
|
|
||||||
shoppingCart.Items.FirstOrDefault(p => p.Price!.Id == productPriceId)?.Quantity ?? 1;
|
|
||||||
|
|
||||||
public Cart GetCart() => ShoppingCart;
|
|
||||||
|
|
||||||
public void NotifyStateChanged() => OnCartChanged?.Invoke();
|
|
||||||
|
|
||||||
public async Task LoadCartFromStorageAsync()
|
|
||||||
{
|
|
||||||
var loadResult = await localStorage.GetAsync<Cart>(CartStorageKey);
|
|
||||||
|
|
||||||
if (loadResult.IsFailed) await localStorage.SaveAsync(CartStorageKey, ShoppingCart);
|
|
||||||
|
|
||||||
if (loadResult.IsSuccess) ShoppingCart = loadResult.Value;
|
|
||||||
|
|
||||||
NotifyStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task SaveCartToStorageAsync() => await localStorage.SaveAsync(CartStorageKey, ShoppingCart);
|
|
||||||
|
|
||||||
public void AddItem(ProductPrice productPrice, Product product, Author author)
|
|
||||||
{
|
|
||||||
var itemExists = false;
|
|
||||||
|
|
||||||
for (var i = 0; i < ShoppingCart.Items.Count; i++)
|
|
||||||
{
|
|
||||||
if (ShoppingCart.Items[i].Price!.Id == productPrice.Id)
|
|
||||||
{
|
|
||||||
ShoppingCart.Items[i].Quantity++;
|
|
||||||
ShoppingCart.Items[i].Amount += productPrice.Amount;
|
|
||||||
|
|
||||||
itemExists = true;
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!itemExists)
|
|
||||||
ShoppingCart.Items.Add(new CartItem
|
|
||||||
{
|
|
||||||
Product = product,
|
|
||||||
Author = author,
|
|
||||||
Price = productPrice,
|
|
||||||
Amount = productPrice.Amount,
|
|
||||||
Quantity = 1,
|
|
||||||
});
|
|
||||||
|
|
||||||
CalculateTotalPrice();
|
|
||||||
NotifyStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateQuantity(long productPriceId, int delta)
|
|
||||||
{
|
|
||||||
for (var i = 0; i < ShoppingCart.Items.Count; i++)
|
|
||||||
{
|
|
||||||
if (ShoppingCart.Items[i].Price!.Id == productPriceId)
|
|
||||||
{
|
|
||||||
var oldQuantity = ShoppingCart.Items[i].Quantity;
|
|
||||||
var pricePerUnit = ShoppingCart.Items[i].Price!.Amount;
|
|
||||||
|
|
||||||
ShoppingCart.Items[i].Quantity += delta;
|
|
||||||
ShoppingCart.Items[i].Amount = pricePerUnit * ShoppingCart.Items[i].Quantity;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CalculateTotalPrice();
|
|
||||||
NotifyStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveOneItem(long productPriceId)
|
|
||||||
{
|
|
||||||
for (var i = 0; i < ShoppingCart.Items.Count; i++)
|
|
||||||
{
|
|
||||||
if (ShoppingCart.Items[i].Price!.Id == productPriceId)
|
|
||||||
{
|
|
||||||
if (ShoppingCart.Items[i].Quantity <= 1)
|
|
||||||
{
|
|
||||||
ShoppingCart.Items.Remove(ShoppingCart.Items[i]);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ShoppingCart.Items[i].Quantity--;
|
|
||||||
ShoppingCart.Items[i].Amount -= ShoppingCart.Items[i].Price!.Amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CalculateTotalPrice();
|
|
||||||
NotifyStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RemoveAllSameItem(long productPriceId)
|
|
||||||
{
|
|
||||||
if (ShoppingCart.Items.Count == 0) return;
|
|
||||||
|
|
||||||
var item = ShoppingCart.Items.FirstOrDefault(i => i.Price?.Id == productPriceId);
|
|
||||||
|
|
||||||
if (item is not null) ShoppingCart.Items.Remove(item);
|
|
||||||
|
|
||||||
CalculateTotalPrice();
|
|
||||||
NotifyStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Clear()
|
|
||||||
{
|
|
||||||
if(ShoppingCart.CustomerId is not null || ShoppingCart.OrderId is not null)
|
|
||||||
{
|
|
||||||
ShoppingCart.TotalAmount = 0;
|
|
||||||
ShoppingCart.TotalVat = 0;
|
|
||||||
ShoppingCart.Items.Clear();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ShoppingCart = new Cart();
|
|
||||||
|
|
||||||
NotifyStateChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
public decimal CalculateTotalPrice()
|
|
||||||
{
|
|
||||||
if (ShoppingCart.Items.Count == 0) return 0;
|
|
||||||
|
|
||||||
var gross = ShoppingCart.Items.Sum(i => i.Amount);
|
|
||||||
|
|
||||||
if (!ShoppingCart.IsVatInclusive) ShoppingCart.TotalVat = gross * ShoppingCart.VatRate;
|
|
||||||
|
|
||||||
ShoppingCart.TotalAmount = gross + ShoppingCart.TotalVat;
|
|
||||||
|
|
||||||
return ShoppingCart.TotalAmount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
namespace MidrandBookshop.Services.ShoppingCart.Models;
|
|
||||||
|
|
||||||
public sealed class Cart
|
|
||||||
{
|
|
||||||
public long? CustomerId { get; set; }
|
|
||||||
|
|
||||||
public long? OrderId { get; set; }
|
|
||||||
|
|
||||||
public decimal TotalAmount { get; set; }
|
|
||||||
|
|
||||||
public decimal TotalVat { get; set; }
|
|
||||||
|
|
||||||
public decimal VatRate { get; set; } = 0.15m;
|
|
||||||
|
|
||||||
public bool IsVatInclusive { get; set; } = true;
|
|
||||||
|
|
||||||
public IList<CartItem> Items { get; set; } = [];
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
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 int Quantity { get; set; }
|
|
||||||
|
|
||||||
public decimal Amount { get; set; }
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user