cookie pro kosik
This commit is contained in:
@@ -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
|
||||
|
||||
<PageTitle>Školní jarmark | Katalog</PageTitle>
|
||||
|
||||
@@ -49,6 +52,9 @@
|
||||
@code {
|
||||
private List<Product> 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<string>(
|
||||
"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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ builder.Services.AddIdentityCore<ApplicationUser>(options =>
|
||||
.AddDefaultTokenProviders();
|
||||
|
||||
builder.Services.AddSingleton<IEmailSender<ApplicationUser>, 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();
|
||||
|
||||
Reference in New Issue
Block a user