Compare commits
2 Commits
52d75a0af8
...
2c3d52299c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2c3d52299c | ||
|
|
1a66b745da |
@@ -72,30 +72,40 @@
|
||||
isLoading = false;
|
||||
}
|
||||
if (!ApplicationState.TryTakeFromJson<string>(
|
||||
"CartToken", out cartToken))
|
||||
"CartToken", out cartToken)
|
||||
|| string.IsNullOrWhiteSpace(cartToken))
|
||||
{
|
||||
cartToken = HttpContextAccessor.HttpContext?
|
||||
.Items["CartToken"] as string;
|
||||
}
|
||||
var context = HttpContextAccessor.HttpContext;
|
||||
|
||||
subscription = ApplicationState.RegisterOnPersisting(() =>
|
||||
{
|
||||
ApplicationState.PersistAsJson("CartToken", cartToken);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
cartToken = context?.Items["CartToken"] as string
|
||||
?? context?.Request.Cookies["CartToken"];
|
||||
}
|
||||
// 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 async Task HandleAddToCart(Product product)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(cartToken))
|
||||
return;
|
||||
|
||||
// Najdeme košík podle cookie
|
||||
// 1. Najdeme košík podle tokenu
|
||||
var cart = await DbContext.Carts
|
||||
.FirstOrDefaultAsync(c => c.Token == cartToken);
|
||||
|
||||
// Pokud košík neexistuje, vytvoříme ho
|
||||
// 2. Pokud košík neexistuje, vytvoříme ho
|
||||
if (cart == null)
|
||||
{
|
||||
cart = new Cart
|
||||
@@ -106,10 +116,12 @@
|
||||
};
|
||||
|
||||
DbContext.Carts.Add(cart);
|
||||
|
||||
// Uložíme košík, abychom získali jeho ID
|
||||
await DbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
// Zjistíme, jestli už produkt v košíku je
|
||||
// 3. Zjistíme, jestli produkt už v košíku je
|
||||
var cartItem = await DbContext.CartItems
|
||||
.FirstOrDefaultAsync(ci =>
|
||||
ci.CartId == cart.Id &&
|
||||
@@ -133,11 +145,14 @@
|
||||
DbContext.CartItems.Add(cartItem);
|
||||
}
|
||||
|
||||
// 4. Aktualizujeme datum změny košíku
|
||||
cart.UpdatedAt = DateTime.UtcNow;
|
||||
|
||||
// 5. Uložíme položku do databáze
|
||||
await DbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
subscription?.Dispose();
|
||||
|
||||
@@ -1,8 +1,108 @@
|
||||
@page "/kosik"
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using Web_Jarmark.Data.Models
|
||||
@using Web_Jarmark.Data
|
||||
@inject ApplicationDbContext DbContext
|
||||
@inject IHttpContextAccessor HttpContextAccessor
|
||||
@inject PersistentComponentState ApplicationState
|
||||
@implements IDisposable
|
||||
|
||||
<PageTitle>Můj košík | Školní jarmark</PageTitle>
|
||||
|
||||
<h3>Kosik</h3>
|
||||
<main class="market-shell">
|
||||
<h1>Můj košík</h1>
|
||||
|
||||
@if (isLoading)
|
||||
{
|
||||
<p>Načítám košík...</p>
|
||||
}
|
||||
else if (cartItems.Count == 0)
|
||||
{
|
||||
<div class="empty-cart">
|
||||
<h2>Tvůj košík je prázdný</h2>
|
||||
<p>Vyber si něco z našeho katalogu.</p>
|
||||
<a href="/">Zpět do katalogu</a>
|
||||
</div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="cart-items">
|
||||
@foreach (var item in cartItems)
|
||||
{
|
||||
<article class="cart-item" @key="item.Id">
|
||||
<div>
|
||||
<h3>@item.Product.Name</h3>
|
||||
<p>@item.Product.Price.ToString("N0") Kč / ks</p>
|
||||
<p>Množství: @item.Quantity ks</p>
|
||||
</div>
|
||||
|
||||
<strong>
|
||||
@((item.Product.Price * item.Quantity).ToString("N0")) Kč
|
||||
</strong>
|
||||
</article>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="cart-total">
|
||||
<h2>
|
||||
Celkem: @totalPrice.ToString("N0") Kč
|
||||
</h2>
|
||||
</div>
|
||||
}
|
||||
</main>
|
||||
|
||||
@code {
|
||||
private string? cartToken;
|
||||
private List<CartItem> cartItems = new();
|
||||
private bool isLoading = true;
|
||||
|
||||
private PersistingComponentStateSubscription? subscription;
|
||||
|
||||
private decimal totalPrice =>
|
||||
cartItems.Sum(item => item.Product.Price * item.Quantity);
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!ApplicationState.TryTakeFromJson<string>(
|
||||
"CartToken", out cartToken)
|
||||
|| string.IsNullOrWhiteSpace(cartToken))
|
||||
{
|
||||
var context = HttpContextAccessor.HttpContext;
|
||||
|
||||
cartToken = context?.Items["CartToken"] as string
|
||||
?? context?.Request.Cookies["CartToken"];
|
||||
}
|
||||
|
||||
subscription = ApplicationState.RegisterOnPersisting(() =>
|
||||
{
|
||||
ApplicationState.PersistAsJson("CartToken", cartToken);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
|
||||
if (string.IsNullOrWhiteSpace(cartToken))
|
||||
return;
|
||||
|
||||
var cart = await DbContext.Carts
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CartItems)
|
||||
.ThenInclude(ci => ci.Product)
|
||||
.FirstOrDefaultAsync(c => c.Token == cartToken);
|
||||
|
||||
if (cart != null)
|
||||
{
|
||||
cartItems = cart.CartItems.ToList();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
subscription?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user