Compare commits

..

4 Commits

Author SHA1 Message Date
khwezi 787507bed9 Merge pull request 'Added CartService and LocalStorageService (browser)' (#93) from payments into master
Reviewed-on: #93
2026-06-09 09:10:32 +02:00
Khwezi Mngoma 59af9a5406 Added CartService and LocalStorageService (browser)
continuous-integration/drone/pr Build is passing
2026-06-09 09:08:46 +02:00
khwezi 5140da2c6c Merge pull request 'Passing token hint during signout' (#92) from payments into master
Reviewed-on: #92
2026-06-07 14:09:31 +02:00
Khwezi Mngoma 02ff14ccc8 Passing token hint during signout
continuous-integration/drone/pr Build is passing
2026-06-07 14:09:02 +02:00
9 changed files with 258 additions and 16 deletions
@@ -11,7 +11,7 @@
<!-- Quartz Scheduler--> <!-- Quartz Scheduler-->
<ItemGroup> <ItemGroup>
<PackageReference Include="Bogus" Version="35.6.5" /> <PackageReference Include="Bogus" Version="35.6.5" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.101"> <PackageReference Include="Meziantou.Analyzer" Version="3.0.102">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
@@ -116,8 +116,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.5" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24.1" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.2" />
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" /> <ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
<!-- global Usings --> <!-- global Usings -->
@@ -32,7 +32,7 @@
<!-- Quartz Scheduler--> <!-- Quartz Scheduler-->
<ItemGroup> <ItemGroup>
<PackageReference Include="Humanizer" Version="3.0.10" /> <PackageReference Include="Humanizer" Version="3.0.10" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.101"> <PackageReference Include="Meziantou.Analyzer" Version="3.0.102">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
@@ -136,8 +136,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.5" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24.1" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.2" />
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" /> <ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
<!-- global Usings --> <!-- global Usings -->
@@ -0,0 +1,128 @@
using LiteCharms.Features.Abstractions;
using LiteCharms.Features.MidrandBooks.Orders.Models;
using LiteCharms.Features.MidrandBooks.Products.Models;
namespace LiteCharms.Features.MidrandBooks.Orders;
public sealed class CartService : IService
{
private Cart cart = new();
public Cart GetCart() => cart;
public void LoadCart(Cart savedCart) => cart = savedCart;
public void AddItem(ProductPrice productPrice)
{
var itemExists = false;
for (var i = 0; i < cart.Items.Count; i++)
{
if (cart.Items[i].Price!.Id == productPrice.Id)
{
cart.Items[i].Quantity++;
cart.Items[i].Amount += productPrice.Amount;
itemExists = true;
break;
}
}
if (!itemExists)
cart.Items.Add(new CartItem
{
Price = productPrice,
Amount = productPrice.Amount,
Quantity = 1,
});
CalculateTotalPrice();
}
public void UpdateQuantity(long productPriceId, int newQuantity)
{
if (newQuantity <= 0)
{
RemoveAllSameItem(productPriceId);
return;
}
for (var i = 0; i < cart.Items.Count; i++)
{
if (cart.Items[i].Price!.Id == productPriceId)
{
var oldQuantity = cart.Items[i].Quantity;
var pricePerUnit = cart.Items[i].Price!.Amount;
cart.Items[i].Quantity = newQuantity;
cart.Items[i].Amount = pricePerUnit * newQuantity;
break;
}
}
CalculateTotalPrice();
}
public void RemoveOneItem(long productPriceId)
{
for (var i = 0; i < cart.Items.Count; i++)
{
if (cart.Items[i].Price!.Id == productPriceId)
{
if (cart.Items[i].Quantity <= 1)
{
cart.Items.RemoveAt(i);
}
else
{
cart.Items[i].Quantity--;
cart.Items[i].Amount -= cart.Items[i].Price!.Amount;
}
break;
}
}
CalculateTotalPrice();
}
public void RemoveAllSameItem(long productPriceId)
{
if (cart.Items.Count == 0) return;
var item = cart.Items.FirstOrDefault(i => i.Price?.Id == productPriceId);
if (item is not null) cart.Items.Remove(item);
CalculateTotalPrice();
}
public void Clear()
{
if(cart.CustomerId is not null || cart.OrderId is not null)
{
cart.TotalPrice = 0;
cart.TotalVat = 0;
cart.Items.Clear();
return;
}
cart = new Cart();
}
public decimal CalculateTotalPrice()
{
if (cart.Items.Count == 0) return 0;
var gross = cart.Items.Sum(i => i.Amount);
if (!cart.IsVatInclusive) cart.TotalVat = gross * cart.VatRate;
cart.TotalPrice = gross + cart.TotalVat;
return cart.TotalPrice;
}
}
@@ -0,0 +1,18 @@
namespace LiteCharms.Features.MidrandBooks.Orders.Models;
public sealed class Cart
{
public long? CustomerId { get; set; }
public long? OrderId { get; set; }
public decimal TotalPrice { 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; } = [];
}
@@ -0,0 +1,12 @@
using LiteCharms.Features.MidrandBooks.Products.Models;
namespace LiteCharms.Features.MidrandBooks.Orders.Models;
public sealed class CartItem
{
public ProductPrice? Price { get; set; }
public long Quantity { get; set; }
public decimal Amount { get; set; }
}
@@ -136,8 +136,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.5" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24.1" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.2" />
<ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" /> <ProjectReference Include="..\LiteCharms.Features\LiteCharms.Features.csproj" />
<!-- global Usings --> <!-- global Usings -->
@@ -0,0 +1,80 @@
using LiteCharms.Features.Abstractions;
namespace LiteCharms.Features.Browser;
public sealed class LocalStorageService(ProtectedLocalStorage storage) : IService
{
public async ValueTask<Result> DeleteAsync(string key)
{
try
{
await storage.DeleteAsync(key);
return Result.Ok();
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> SaveAsync(string key, string value)
{
try
{
await storage.SetAsync(key, value);
return Result.Ok();
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> SaveAsync<TValue>(string key, TValue value) where TValue : class
{
try
{
await storage.SetAsync(key, value);
return Result.Ok();
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<string>> GetAsync(string key)
{
try
{
var retrieval = await storage.GetAsync<string>(key);
return retrieval.Success && !string.IsNullOrWhiteSpace(retrieval.Value)
? Result.Ok(retrieval.Value)
: Result.Fail($"Could not find object by key {key}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<TValue>> GetAsync<TValue>(string key) where TValue : class
{
try
{
var retrieval = await storage.GetAsync<TValue>(key);
return retrieval.Success && retrieval.Value is not null
? Result.Ok(retrieval.Value)
: Result.Fail($"Could not find object by key {key}");
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
}
+8 -5
View File
@@ -104,12 +104,15 @@ public static class Api
app.MapGet("/logout", async (HttpContext context) => app.MapGet("/logout", async (HttpContext context) =>
{ {
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); var idToken = await context.GetTokenAsync("id_token");
await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties var authProperties = new AuthenticationProperties { RedirectUri = "/", };
{
RedirectUri = "/", if (!string.IsNullOrEmpty(idToken))
}); authProperties.Parameters.Add("id_token_hint", idToken);
await context.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, authProperties);
await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}); });
return app; return app;
@@ -67,7 +67,7 @@
<!-- Quartz Scheduler--> <!-- Quartz Scheduler-->
<ItemGroup> <ItemGroup>
<PackageReference Include="Hashids.net" Version="1.7.0" /> <PackageReference Include="Hashids.net" Version="1.7.0" />
<PackageReference Include="Meziantou.Analyzer" Version="3.0.101"> <PackageReference Include="Meziantou.Analyzer" Version="3.0.102">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
@@ -171,8 +171,8 @@
<!-- Amazon S3 SDK --> <!-- Amazon S3 SDK -->
<ItemGroup> <ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.4" /> <PackageReference Include="AWSSDK.Extensions.NetCore.Setup" Version="4.0.4.5" />
<PackageReference Include="AWSSDK.S3" Version="4.0.24.1" /> <PackageReference Include="AWSSDK.S3" Version="4.0.24.2" />
<!-- global Usings --> <!-- global Usings -->
<Using Include="Amazon.S3" /> <Using Include="Amazon.S3" />
@@ -182,6 +182,7 @@
<!-- Shared Usings --> <!-- Shared Usings -->
<ItemGroup> <ItemGroup>
<Using Include="Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage" />
<Using Include="System.Reflection" /> <Using Include="System.Reflection" />
<Using Include="Microsoft.Extensions.DependencyInjection.Extensions" /> <Using Include="Microsoft.Extensions.DependencyInjection.Extensions" />
<Using Include="Microsoft.AspNetCore.Routing" /> <Using Include="Microsoft.AspNetCore.Routing" />