diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/DatailVyrobku.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/DatailVyrobku.razor index 090b84d..91574f6 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/DatailVyrobku.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/DatailVyrobku.razor @@ -20,54 +20,49 @@ else if (product == null) } else { - - { -
- ← Zpět do katalogu +
+ ← Zpět do katalogu -
-
- @product.Name +
+
+ @product.Name +
+ +
+ + @product.Category?.Name + + +

@product.Name

+ +

+ @product.Description +

+ +
+ + Skladem: @product.StockQuantity ks
-
- - @product.Category?.Name - +
-

@product.Name

- -

- @product.Description -

- -
- - Skladem: @product.StockQuantity ks -
- -
- -
-
- Cena produktu -
- @product.Price.ToString("N0") Kč -
+
+
+ Cena produktu +
+ @product.Price.ToString("N0") Kč
- - - -
+ +
- } +
} @code { @@ -76,6 +71,20 @@ else private Product? product; private bool loading = true; + private string? cartToken; + + protected override async Task OnInitializedAsync() + { + if (!ApplicationState.TryTakeFromJson( + "CartToken", out cartToken) + || string.IsNullOrWhiteSpace(cartToken)) + { + var context = HttpContextAccessor.HttpContext; + + cartToken = context?.Items["CartToken"] as string + ?? context?.Request.Cookies["CartToken"]; + } + } protected override async Task OnParametersSetAsync() { @@ -87,65 +96,51 @@ else loading = false; } - private string? cartToken; -protected override async Task OnInitializedAsync() -{ - if (!ApplicationState.TryTakeFromJson( - "CartToken", out cartToken) - || string.IsNullOrWhiteSpace(cartToken)) + private async Task HandleAddToCart(Product product) { - var context = HttpContextAccessor.HttpContext; + if (string.IsNullOrWhiteSpace(cartToken)) + return; - cartToken = context?.Items["CartToken"] as string - ?? context?.Request.Cookies["CartToken"]; - } -} + var cart = await DbContext.Carts + .FirstOrDefaultAsync(c => c.Token == cartToken); -private async Task HandleAddToCart(Product product) -{ - if (string.IsNullOrWhiteSpace(cartToken)) - return; - - var cart = await DbContext.Carts - .FirstOrDefaultAsync(c => c.Token == cartToken); - - if (cart == null) - { - cart = new Cart + if (cart == null) { - Token = cartToken, - CreatedAt = DateTime.UtcNow, - UpdatedAt = DateTime.UtcNow - }; + cart = new Cart + { + Token = cartToken, + CreatedAt = DateTime.UtcNow, + UpdatedAt = DateTime.UtcNow + }; + + DbContext.Carts.Add(cart); + await DbContext.SaveChangesAsync(); + } + + var cartItem = await DbContext.CartItems + .FirstOrDefaultAsync(ci => + ci.CartId == cart.Id && + ci.ProductId == product.Id); + + if (cartItem != null) + { + cartItem.Quantity++; + } + else + { + cartItem = new CartItem + { + CartId = cart.Id, + ProductId = product.Id, + Quantity = 1 + }; + + DbContext.CartItems.Add(cartItem); + } + + cart.UpdatedAt = DateTime.UtcNow; - DbContext.Carts.Add(cart); await DbContext.SaveChangesAsync(); } - - var cartItem = await DbContext.CartItems - .FirstOrDefaultAsync(ci => - ci.CartId == cart.Id && - ci.ProductId == product.Id); - - if (cartItem != null) - { - cartItem.Quantity++; - } - else - { - cartItem = new CartItem - { - CartId = cart.Id, - ProductId = product.Id, - Quantity = 1 - }; - - DbContext.CartItems.Add(cartItem); - } - - cart.UpdatedAt = DateTime.UtcNow; - - await DbContext.SaveChangesAsync(); -} }