detail vyrobku

This commit is contained in:
maja
2026-11-04 18:02:36 +01:00
parent 3a60f670ba
commit 8a787d5582
5 changed files with 300 additions and 3 deletions

View File

@@ -1,6 +1,151 @@
@page "/Vyrobek{Id:int}" @page "/produkt/{Id:int}"
<h3>DatailVyrobku</h3>
@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="detail-cart-button"
disabled="@(product.StockQuantity <= 0)"
@onclick="() => HandleAddToCart(product)">
🛒 Přidat do košíku
</button>
</div>
</div>
</div>
</div>
}
}
@code { @code {
[Parameter]
public int Id { get; set; }
private Product? product;
private bool loading = true;
protected override async Task OnParametersSetAsync()
{
loading = true;
product = await DbContext.Products
.Include(p => p.Category)
.FirstOrDefaultAsync(p => p.Id == Id);
loading = false;
}
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"];
}
}
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();
}
} }

View File

@@ -23,7 +23,9 @@
</div> </div>
<div class="market-card-content"> <div class="market-card-content">
<a href="@($"/produkt/{Product.Id}")" class="product-detail-link">
<h3>@Product.Name</h3> <h3>@Product.Name</h3>
</a>
@if (!string.IsNullOrWhiteSpace(Product.Description)) @if (!string.IsNullOrWhiteSpace(Product.Description))
{ {

View File

@@ -2653,5 +2653,155 @@ textarea.wj-input {
font-weight: 700; font-weight: 700;
transition: background 0.2s ease; transition: background 0.2s ease;
} }
.product-detail-link {
color: inherit;
text-decoration: none;
}
.product-detail-link:hover {
text-decoration: underline;
}
.detail-container {
max-width: 1150px;
margin: 45px auto;
padding: 0 20px;
}
.back-link {
display: inline-block;
margin-bottom: 22px;
color: #777;
text-decoration: none;
font-weight: 600;
}
.back-link:hover {
color: #222;
}
.detail-card {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 45px;
padding: 35px;
background: white;
border-radius: 20px;
box-shadow: 0 8px 35px rgba(0,0,0,0.07);
}
.detail-image {
display: flex;
align-items: center;
justify-content: center;
min-height: 400px;
background: #f5f5f5;
border-radius: 16px;
overflow: hidden;
}
.detail-image img {
width: 100%;
height: 400px;
object-fit: contain;
}
.detail-info {
display: flex;
flex-direction: column;
justify-content: center;
}
.detail-category {
color: #777;
font-size: 14px;
font-weight: 600;
text-transform: uppercase;
}
.detail-info h1 {
margin: 12px 0;
font-size: 34px;
color: #222;
}
.detail-description {
color: #666;
line-height: 1.7;
font-size: 16px;
}
.detail-stock {
margin-top: 20px;
display: flex;
align-items: center;
gap: 9px;
color: #333;
font-weight: 600;
}
.stock-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: #22a06b;
}
.detail-divider {
height: 1px;
background: #eee;
margin: 25px 0;
}
.detail-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
}
.price-label {
color: #888;
font-size: 13px;
}
.detail-price {
font-size: 30px;
font-weight: 800;
color: #222;
}
.detail-cart-button {
padding: 15px 22px;
border: none;
border-radius: 10px;
background: #222;
color: white;
font-size: 15px;
font-weight: 700;
cursor: not-allowed;
opacity: 0.6;
}
@media (max-width: 750px) {
.detail-card {
grid-template-columns: 1fr;
padding: 18px;
gap: 25px;
}
.detail-image,
.detail-image img {
min-height: 250px;
height: 250px;
}
.detail-info h1 {
font-size: 26px;
}
.detail-bottom {
flex-direction: column;
align-items: stretch;
}
}