admin
This commit is contained in:
@@ -20,54 +20,49 @@ else if (product == null)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
<div class="detail-container">
|
||||||
|
<a href="/katalog" class="back-link">← Zpět do katalogu</a>
|
||||||
|
|
||||||
{
|
<div class="detail-card">
|
||||||
<div class="detail-container">
|
<div class="detail-image">
|
||||||
<a href="/katalog" class="back-link">← Zpět do katalogu</a>
|
<img src="@product.ImageUrl" alt="@product.Name" />
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="detail-card">
|
<div class="detail-info">
|
||||||
<div class="detail-image">
|
<span class="detail-category">
|
||||||
<img src="@product.ImageUrl" alt="@product.Name" />
|
@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>
|
||||||
|
|
||||||
<div class="detail-info">
|
<div class="detail-divider"></div>
|
||||||
<span class="detail-category">
|
|
||||||
@product.Category?.Name
|
|
||||||
</span>
|
|
||||||
|
|
||||||
<h1>@product.Name</h1>
|
<div class="detail-bottom">
|
||||||
|
<div>
|
||||||
<p class="detail-description">
|
<span class="price-label">Cena produktu</span>
|
||||||
@product.Description
|
<div class="detail-price">
|
||||||
</p>
|
@product.Price.ToString("N0") Kč
|
||||||
|
|
||||||
<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>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<button class="detail-cart-button"
|
|
||||||
disabled="@(product.StockQuantity <= 0)"
|
|
||||||
@onclick="() => HandleAddToCart(product)">
|
|
||||||
🛒 Přidat do košíku
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<button class="product-cart-button"
|
||||||
|
disabled="@(product.StockQuantity <= 0)"
|
||||||
|
@onclick="() => HandleAddToCart(product)">
|
||||||
|
🛒 Přidat do košíku
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
@@ -76,6 +71,20 @@ else
|
|||||||
|
|
||||||
private Product? product;
|
private Product? product;
|
||||||
private bool loading = true;
|
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()
|
protected override async Task OnParametersSetAsync()
|
||||||
{
|
{
|
||||||
@@ -87,65 +96,51 @@ else
|
|||||||
|
|
||||||
loading = false;
|
loading = false;
|
||||||
}
|
}
|
||||||
private string? cartToken;
|
|
||||||
|
|
||||||
protected override async Task OnInitializedAsync()
|
private async Task HandleAddToCart(Product product)
|
||||||
{
|
|
||||||
if (!ApplicationState.TryTakeFromJson<string>(
|
|
||||||
"CartToken", out cartToken)
|
|
||||||
|| string.IsNullOrWhiteSpace(cartToken))
|
|
||||||
{
|
{
|
||||||
var context = HttpContextAccessor.HttpContext;
|
if (string.IsNullOrWhiteSpace(cartToken))
|
||||||
|
return;
|
||||||
|
|
||||||
cartToken = context?.Items["CartToken"] as string
|
var cart = await DbContext.Carts
|
||||||
?? context?.Request.Cookies["CartToken"];
|
.FirstOrDefaultAsync(c => c.Token == cartToken);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task HandleAddToCart(Product product)
|
if (cart == null)
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(cartToken))
|
|
||||||
return;
|
|
||||||
|
|
||||||
var cart = await DbContext.Carts
|
|
||||||
.FirstOrDefaultAsync(c => c.Token == cartToken);
|
|
||||||
|
|
||||||
if (cart == null)
|
|
||||||
{
|
|
||||||
cart = new Cart
|
|
||||||
{
|
{
|
||||||
Token = cartToken,
|
cart = new Cart
|
||||||
CreatedAt = DateTime.UtcNow,
|
{
|
||||||
UpdatedAt = DateTime.UtcNow
|
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();
|
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