diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor
index c6d2531..8803fba 100644
--- a/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor
+++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor
@@ -4,6 +4,9 @@
@using Web_Jarmark.Data.Models
@using Web_Jarmark.Components.Pages.Shared
@inject ApplicationDbContext DbContext
+@inject IHttpContextAccessor HttpContextAccessor
+@inject PersistentComponentState ApplicationState
+@implements IDisposable
Školní jarmark | Katalog
@@ -49,6 +52,9 @@
@code {
private List products = new();
private bool isLoading = true;
+ private string? cartToken;
+
+ private PersistingComponentStateSubscription? subscription;
protected override async Task OnInitializedAsync()
{
@@ -65,9 +71,76 @@
{
isLoading = false;
}
+ if (!ApplicationState.TryTakeFromJson(
+ "CartToken", out cartToken))
+ {
+ cartToken = HttpContextAccessor.HttpContext?
+ .Items["CartToken"] as string;
+ }
+
+ subscription = ApplicationState.RegisterOnPersisting(() =>
+ {
+ ApplicationState.PersistAsJson("CartToken", cartToken);
+ return Task.CompletedTask;
+ });
+
}
- private void HandleAddToCart(Product product)
+
+ private async Task HandleAddToCart(Product product)
{
- Console.WriteLine($"Přidán produkt: {product.Name}");
+ if (string.IsNullOrWhiteSpace(cartToken))
+ return;
+
+ // Najdeme košík podle cookie
+ var cart = await DbContext.Carts
+ .FirstOrDefaultAsync(c => c.Token == cartToken);
+
+ // Pokud košík neexistuje, vytvoříme ho
+ if (cart == null)
+ {
+ cart = new Cart
+ {
+ Token = cartToken,
+ CreatedAt = DateTime.UtcNow,
+ UpdatedAt = DateTime.UtcNow
+ };
+
+ DbContext.Carts.Add(cart);
+ await DbContext.SaveChangesAsync();
+ }
+
+ // Zjistíme, jestli už produkt v košíku je
+ var cartItem = await DbContext.CartItems
+ .FirstOrDefaultAsync(ci =>
+ ci.CartId == cart.Id &&
+ ci.ProductId == product.Id);
+
+ if (cartItem != null)
+ {
+ // Produkt už existuje – zvýšíme množství
+ cartItem.Quantity++;
+ }
+ else
+ {
+ // Produkt ještě v košíku není
+ cartItem = new CartItem
+ {
+ CartId = cart.Id,
+ ProductId = product.Id,
+ Quantity = 1
+ };
+
+ DbContext.CartItems.Add(cartItem);
+ }
+
+ cart.UpdatedAt = DateTime.UtcNow;
+
+ await DbContext.SaveChangesAsync();
}
+
+ public void Dispose()
+ {
+ subscription?.Dispose();
+ }
+
}
diff --git a/Web-Jarmark/Web-Jarmark/Program.cs b/Web-Jarmark/Web-Jarmark/Program.cs
index f219248..89ace9b 100644
--- a/Web-Jarmark/Web-Jarmark/Program.cs
+++ b/Web-Jarmark/Web-Jarmark/Program.cs
@@ -37,7 +37,7 @@ builder.Services.AddIdentityCore(options =>
.AddDefaultTokenProviders();
builder.Services.AddSingleton, IdentityNoOpEmailSender>();
-
+builder.Services.AddHttpContextAccessor();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -54,6 +54,28 @@ else
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseHttpsRedirection();
+app.Use(async (context, next) =>
+{
+ const string cookieName = "CartToken";
+
+ if (!context.Request.Cookies.TryGetValue(cookieName, out var token)
+ || !Guid.TryParse(token, out _))
+ {
+ token = Guid.NewGuid().ToString();
+
+ context.Response.Cookies.Append(cookieName, token, new CookieOptions
+ {
+ HttpOnly = true,
+ Secure = true,
+ SameSite = SameSiteMode.Lax,
+ Expires = DateTimeOffset.UtcNow.AddDays(30),
+ IsEssential = true
+ });
+ }
+
+ await next();
+});
+
app.UseAntiforgery();
app.MapStaticAssets();