147 lines
3.8 KiB
Plaintext
147 lines
3.8 KiB
Plaintext
@page "/produkt/{Id:int}"
|
|
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using Web_Jarmark.Data
|
|
@using Web_Jarmark.Data.Models
|
|
|
|
@inject ApplicationDbContext DbContext
|
|
@inject IHttpContextAccessor HttpContextAccessor
|
|
@inject PersistentComponentState ApplicationState
|
|
|
|
<PageTitle>Detail produktu</PageTitle>
|
|
|
|
@if (loading)
|
|
{
|
|
<p>Načítám produkt...</p>
|
|
}
|
|
else if (product == null)
|
|
{
|
|
<h3>Produkt nebyl nalezen.</h3>
|
|
}
|
|
else
|
|
{
|
|
<div class="detail-container">
|
|
<a href="/katalog" class="back-link">← Zpět do katalogu</a>
|
|
|
|
<div class="detail-card">
|
|
<div class="detail-image">
|
|
<img src="@product.ImageUrl" alt="@product.Name" />
|
|
</div>
|
|
|
|
<div class="detail-info">
|
|
<span class="detail-category">
|
|
@product.Category?.Name
|
|
</span>
|
|
|
|
<h1>@product.Name</h1>
|
|
|
|
<p class="detail-description">
|
|
@product.Description
|
|
</p>
|
|
|
|
<div class="detail-stock">
|
|
<span class="stock-dot"></span>
|
|
Skladem: @product.StockQuantity ks
|
|
</div>
|
|
|
|
<div class="detail-divider"></div>
|
|
|
|
<div class="detail-bottom">
|
|
<div>
|
|
<span class="price-label">Cena produktu</span>
|
|
<div class="detail-price">
|
|
@product.Price.ToString("N0") Kč
|
|
</div>
|
|
</div>
|
|
|
|
<button class="product-cart-button"
|
|
disabled="@(product.StockQuantity <= 0)"
|
|
@onclick="() => HandleAddToCart(product)">
|
|
🛒 Přidat do košíku
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
[Parameter]
|
|
public int Id { get; set; }
|
|
|
|
private Product? product;
|
|
private bool loading = true;
|
|
private string? cartToken;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
if (!ApplicationState.TryTakeFromJson<string>(
|
|
"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()
|
|
{
|
|
loading = true;
|
|
|
|
product = await DbContext.Products
|
|
.Include(p => p.Category)
|
|
.FirstOrDefaultAsync(p => p.Id == Id);
|
|
|
|
loading = false;
|
|
}
|
|
|
|
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
|
|
{
|
|
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;
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
}
|
|
}
|