diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor index 8803fba..f3a8185 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Katalog.razor @@ -72,30 +72,40 @@ isLoading = false; } if (!ApplicationState.TryTakeFromJson( - "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( + // "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(); diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor index e5af474..b798990 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor @@ -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 +Můj košík | Školní jarmark -

Kosik

+
+

Můj košík

+ + @if (isLoading) + { +

Načítám košík...

+ } + else if (cartItems.Count == 0) + { +
+

Tvůj košík je prázdný

+

Vyber si něco z našeho katalogu.

+ Zpět do katalogu +
+ } + else + { +
+ @foreach (var item in cartItems) + { +
+
+

@item.Product.Name

+

@item.Product.Price.ToString("N0") Kč / ks

+

Množství: @item.Quantity ks

+
+ + + @((item.Product.Price * item.Quantity).ToString("N0")) Kč + +
+ } +
+ +
+

+ Celkem: @totalPrice.ToString("N0") Kč +

+
+ } +
@code { + private string? cartToken; + private List 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( + "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(); + } } diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db b/Web-Jarmark/Web-Jarmark/jarmark.db index eb0456a..ae3e73a 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db and b/Web-Jarmark/Web-Jarmark/jarmark.db differ diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-shm b/Web-Jarmark/Web-Jarmark/jarmark.db-shm index fe9ac28..5e54dab 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-shm and b/Web-Jarmark/Web-Jarmark/jarmark.db-shm differ diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-wal b/Web-Jarmark/Web-Jarmark/jarmark.db-wal index e69de29..8f503c3 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-wal and b/Web-Jarmark/Web-Jarmark/jarmark.db-wal differ