admin
This commit is contained in:
@@ -20,54 +20,49 @@ else if (product == null)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
{
|
||||
<div class="detail-container">
|
||||
<a href="/katalog" class="back-link">← Zpět do katalogu</a>
|
||||
<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 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-info">
|
||||
<span class="detail-category">
|
||||
@product.Category?.Name
|
||||
</span>
|
||||
<div class="detail-divider"></div>
|
||||
|
||||
<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 class="detail-bottom">
|
||||
<div>
|
||||
<span class="price-label">Cena produktu</span>
|
||||
<div class="detail-price">
|
||||
@product.Price.ToString("N0") Kč
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<button class="detail-cart-button"
|
||||
disabled="@(product.StockQuantity <= 0)"
|
||||
@onclick="() => HandleAddToCart(product)">
|
||||
🛒 Přidat do košíku
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<button class="product-cart-button"
|
||||
disabled="@(product.StockQuantity <= 0)"
|
||||
@onclick="() => HandleAddToCart(product)">
|
||||
🛒 Přidat do košíku
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
@@ -76,6 +71,20 @@ else
|
||||
|
||||
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()
|
||||
{
|
||||
@@ -87,65 +96,51 @@ else
|
||||
|
||||
loading = false;
|
||||
}
|
||||
private string? cartToken;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (!ApplicationState.TryTakeFromJson<string>(
|
||||
"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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user