diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor index 406c0de..6951f00 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/Kosik.razor @@ -1,127 +1,164 @@ @page "/kosik" + @using Microsoft.EntityFrameworkCore @using Web_Jarmark.Data.Models @using Web_Jarmark.Data + @inject ApplicationDbContext DbContext @inject IHttpContextAccessor HttpContextAccessor @inject PersistentComponentState ApplicationState + @implements IDisposable Můj košík | Školní jarmark -
+
-
-
-
- ŠKOLNÍ JARMARK -

Můj košík

-

Vše, co sis vybral, na jednom místě.

-
- - ← Pokračovat v nákupu +
+
+ ŠKOLNÍ JARMARK +

Můj košík

+

Vše, co sis vybral, na jednom místě.

- @if (isLoading) - { -

Načítám košík...

- } - else if (cartItems.Count == 0) - { -
-
🛒
-

Košík je zatím prázdný

-

V katalogu na tebe čeká spousta zajímavých produktů.

- Prohlédnout katalog -
- } - else - { -
-
-
-

Vybrané produkty

- @cartItems.Count položek -
+ ← Pokračovat v nákupu +
+ + @if (isLoading) + { +

Načítám košík...

+ } + else if (cartItems.Count == 0) + { +
+
🛒
+

Košík je zatím prázdný

+

V katalogu na tebe čeká spousta zajímavých produktů.

+ Prohlédnout katalog +
+ } + else + { +
+ +
+ +
+

Vybrané produkty

+ @cartItems.Count položek +
+ + @foreach (var item in cartItems) + { +
+ +
+ @if (!string.IsNullOrWhiteSpace(item.Product.ImageUrl)) + { + @item.Product.Name + } + else + { + 🎁 + } +
+ +
+

@item.Product.Name

+ + @if (item.Product.Category != null) + { + + @item.Product.Category.Name + + } + +

@item.Product.Price.ToString("N0") Kč / ks

+
+ +
+ Množství + +
+ + + + @item.Quantity + + - @foreach (var item in cartItems) - { -
-
- @if (!string.IsNullOrWhiteSpace(item.Product.ImageUrl)) - { - @item.Product.Name - } - else - { - 🎁 - }
+
-
-

@item.Product.Name

- @if (item.Product.Category != null) - { - - @item.Product.Category.Name - - } -

@item.Product.Price.ToString("N0") Kč / ks

-
+
+ + @((item.Product.Price * item.Quantity).ToString("N0")) Kč + -
- Množství - @item.Quantity ks -
+ +
-
- - @((item.Product.Price * item.Quantity).ToString("N0")) Kč - -
-
- } -
+ + } -
- } -
+
+ Celkem + @totalPrice.ToString("N0") Kč +
+ + + +

+ Objednávku dokončíš v dalším kroku. +

+ + + + + } -
@code { private string? cartToken; + private List cartItems = new(); + private bool isLoading = true; private PersistingComponentStateSubscription? subscription; @@ -156,6 +193,7 @@ .AsNoTracking() .Include(c => c.CartItems) .ThenInclude(ci => ci.Product) + .ThenInclude(p => p.Category) .FirstOrDefaultAsync(c => c.Token == cartToken); if (cart != null) @@ -169,6 +207,68 @@ } } + private async Task ChangeQuantity(CartItem item, int change) + { + if (string.IsNullOrWhiteSpace(cartToken)) + return; + + var cart = await DbContext.Carts + .FirstOrDefaultAsync(c => c.Token == cartToken); + + if (cart == null) + return; + + var cartItem = await DbContext.CartItems + .Include(ci => ci.Product) + .FirstOrDefaultAsync(ci => + ci.Id == item.Id && + ci.CartId == cart.Id); + + if (cartItem == null) + return; + + int newQuantity = cartItem.Quantity + change; + + if (newQuantity < 1 || + newQuantity > cartItem.Product.StockQuantity) + return; + + cartItem.Quantity = newQuantity; + cart.UpdatedAt = DateTime.UtcNow; + + await DbContext.SaveChangesAsync(); + + item.Quantity = newQuantity; + } + + private async Task RemoveFromCart(CartItem item) + { + if (string.IsNullOrWhiteSpace(cartToken)) + return; + + var cart = await DbContext.Carts + .FirstOrDefaultAsync(c => c.Token == cartToken); + + if (cart == null) + return; + + var cartItem = await DbContext.CartItems + .FirstOrDefaultAsync(ci => + ci.Id == item.Id && + ci.CartId == cart.Id); + + if (cartItem == null) + return; + + DbContext.CartItems.Remove(cartItem); + + cart.UpdatedAt = DateTime.UtcNow; + + await DbContext.SaveChangesAsync(); + + cartItems.Remove(item); + } + public void Dispose() { subscription?.Dispose(); diff --git a/Web-Jarmark/Web-Jarmark/Components/Pages/NovaObjednavka.razor b/Web-Jarmark/Web-Jarmark/Components/Pages/NovaObjednavka.razor index ea7546e..b66366f 100644 --- a/Web-Jarmark/Web-Jarmark/Components/Pages/NovaObjednavka.razor +++ b/Web-Jarmark/Web-Jarmark/Components/Pages/NovaObjednavka.razor @@ -1,5 +1,207 @@ -

NovaObjednavka

+@page "/nova-objednavka" +@using Microsoft.EntityFrameworkCore +@using Web_Jarmark.Data +@using Web_Jarmark.Data.Models +@inject ApplicationDbContext DbContext +@inject IHttpContextAccessor HttpContextAccessor +@inject PersistentComponentState ApplicationState +@inject NavigationManager Navigation +@implements IDisposable + +Dokončení objednávky | Školní jarmark + +
+

Dokončení objednávky

+ + @if (isLoading) + { +

Načítám objednávku...

+ } + else if (!string.IsNullOrWhiteSpace(errorMessage)) + { +

@errorMessage

+ Zpět do košíku + } + else if (cartItems.Count == 0) + { +

Košík je prázdný.

+ Zpět do katalogu + } + else + { +
+

Vybrané produkty

+ + @foreach (var item in cartItems) + { +

+ @item.Product.Name – @item.Quantity ks – + @((item.Product.Price * item.Quantity).ToString("N0")) Kč +

+ } + +

Celkem: @totalPrice.ToString("N0") Kč

+
+ +
+

Způsob platby

+ + + +
+ + +
+ + + +

@saveError

+

Zpět do košíku

+ } +
@code { + private string? cartToken; + private List cartItems = new(); + private bool isLoading = true; + private bool isSaving; + private string? errorMessage; + private string? saveError; + + private PaymentMethod selectedPayment = PaymentMethod.BankTransfer; + + private PersistingComponentStateSubscription? subscription; + + private decimal totalPrice => + cartItems.Sum(item => item.Product.Price * item.Quantity); + + protected override async Task OnInitializedAsync() + { + try + { + if (!ApplicationState.TryTakeFromJson( + "CartToken", out cartToken) + || string.IsNullOrWhiteSpace(cartToken)) + { + var context = HttpContextAccessor.HttpContext; + + cartToken = context?.Items["CartToken"] as string + ?? context?.Request.Cookies["CartToken"]; + } + + subscription = ApplicationState.RegisterOnPersisting(() => + { + ApplicationState.PersistAsJson("CartToken", cartToken); + return Task.CompletedTask; + }); + + if (string.IsNullOrWhiteSpace(cartToken)) + { + errorMessage = "Nepodařilo se najít tvůj košík."; + return; + } + + var cart = await DbContext.Carts + .AsNoTracking() + .Include(c => c.CartItems) + .ThenInclude(ci => ci.Product) + .FirstOrDefaultAsync(c => c.Token == cartToken); + + if (cart != null) + { + cartItems = cart.CartItems.ToList(); + } + } + catch + { + errorMessage = "Při načítání košíku nastala chyba."; + } + finally + { + isLoading = false; + } + } + + private async Task CreateOrder() + { + if (isSaving || string.IsNullOrWhiteSpace(cartToken)) + return; + + isSaving = true; + saveError = null; + + try + { + var cart = await DbContext.Carts + .Include(c => c.CartItems) + .ThenInclude(ci => ci.Product) + .FirstOrDefaultAsync(c => c.Token == cartToken); + + if (cart == null || cart.CartItems.Count == 0) + { + saveError = "Košík je prázdný."; + return; + } + + var order = new Order + { + PreferredPayment = selectedPayment, + Status = OrderStatus.Pending, + CreatedAt = DateTime.UtcNow, + TotalAmount = cart.CartItems.Sum( + item => item.Product.Price * item.Quantity) + }; + + foreach (var item in cart.CartItems) + { + order.OrderItems.Add(new OrderItem + { + ProductId = item.ProductId, + ProductName = item.Product.Name, + UnitPrice = item.Product.Price, + Quantity = item.Quantity, + Order = order, + Product = item.Product + }); + } + + DbContext.Orders.Add(order); + + DbContext.CartItems.RemoveRange(cart.CartItems); + + await DbContext.SaveChangesAsync(); + + Navigation.NavigateTo($"/objednavka/{order.Id}"); + } + catch + { + saveError = "Objednávku se nepodařilo uložit. Zkus to znovu."; + } + finally + { + isSaving = false; + } + } + + public void Dispose() + { + subscription?.Dispose(); + } } diff --git a/Web-Jarmark/Web-Jarmark/Data/ApplicationDbContext.cs b/Web-Jarmark/Web-Jarmark/Data/ApplicationDbContext.cs index 5218ec8..d4e756b 100644 --- a/Web-Jarmark/Web-Jarmark/Data/ApplicationDbContext.cs +++ b/Web-Jarmark/Web-Jarmark/Data/ApplicationDbContext.cs @@ -1,3 +1,4 @@ + using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Web_Jarmark.Data; @@ -5,35 +6,41 @@ using Web_Jarmark.Data.Models; namespace Web_Jarmark.Data { - public class ApplicationDbContext(DbContextOptions options) : IdentityDbContext(options) + public class ApplicationDbContext(DbContextOptions options) + : IdentityDbContext(options) { public DbSet Categories { get; set; } public DbSet Products { get; set; } public DbSet Carts { get; set; } public DbSet CartItems { get; set; } + + // Objednávky + public DbSet Orders { get; set; } + public DbSet OrderItems { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - // Nastavení vazby 1:N mezi Category a Product + // Kategorie a produkty modelBuilder.Entity() - .HasMany(c => c.Products) // Jedna kategorie obsahuje mnoho produktů - .WithOne(p => p.Category) // Každý produkt patří ke jedné kategorii - .HasForeignKey(p => p.CategoryId); // Cizí klíč v tabulce Product + .HasMany(c => c.Products) + .WithOne(p => p.Category) + .HasForeignKey(p => p.CategoryId); - // Jeden košík obsahuje více položek + // Košík a položky košíku modelBuilder.Entity() .HasMany(c => c.CartItems) .WithOne(ci => ci.Cart) .HasForeignKey(ci => ci.CartId); - // Každá položka odkazuje na jeden produkt + // Položka košíku odkazuje na produkt modelBuilder.Entity() .HasOne(ci => ci.Product) .WithMany() .HasForeignKey(ci => ci.ProductId); - // Jeden produkt může být v košíku pouze jednou + // Produkt může být v košíku pouze jednou modelBuilder.Entity() .HasIndex(ci => new { ci.CartId, ci.ProductId }) .IsUnique(); @@ -42,7 +49,19 @@ namespace Web_Jarmark.Data modelBuilder.Entity() .HasIndex(c => c.Token) .IsUnique(); + + // Objednávka a její položky + modelBuilder.Entity() + .HasMany(o => o.OrderItems) + .WithOne(oi => oi.Order) + .HasForeignKey(oi => oi.OrderId); + + // Položka objednávky odkazuje na produkt + modelBuilder.Entity() + .HasOne(oi => oi.Product) + .WithMany() + .HasForeignKey(oi => oi.ProductId); } } - } + diff --git a/Web-Jarmark/Web-Jarmark/Data/Models/Order.cs b/Web-Jarmark/Web-Jarmark/Data/Models/Order.cs index 0a284c5..0e40b4f 100644 --- a/Web-Jarmark/Web-Jarmark/Data/Models/Order.cs +++ b/Web-Jarmark/Web-Jarmark/Data/Models/Order.cs @@ -8,6 +8,7 @@ Delivered, Cancelled } + public enum PaymentMethod { CreditCard, @@ -19,23 +20,21 @@ public class Order { public int Id { get; set; } - public string Code { get; set; } - public OrderStatus Status { get; set; } - public string CustomerName { get; set; } - public string Email { get; set; } - public string Phone { get; set; } - public string Note { get; set; } - public PaymentMethod PreferredPayment { get; set; } - public string PaymentReference { get; set; } - public decimal TotalAmount { get; set; } - public DateTime CreatedAt { get; set; } - public DateTime? ExpiresAt { get; set; } - public DateTime? PaidAt { get; set; } - public DateTime? IssuedAt { get; set; } - public string IssuedByUserId { get; set; } - public DateTime? CancelledAt { get; set; } - // Navigační vlastnost pro objednávací položky + public string Code { get; set; } = Guid.NewGuid().ToString("N")[..8].ToUpper(); + + public OrderStatus Status { get; set; } = OrderStatus.Pending; + + public PaymentMethod PreferredPayment { get; set; } + + public string? PaymentReference { get; set; } + + public decimal TotalAmount { get; set; } + + public DateTime CreatedAt { get; set; } = DateTime.UtcNow; + + public DateTime? PaidAt { get; set; } + public ICollection OrderItems { get; set; } = new List(); } -} +} \ No newline at end of file diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db b/Web-Jarmark/Web-Jarmark/jarmark.db index 470db60..abb38fe 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db and b/Web-Jarmark/Web-Jarmark/jarmark.db differ diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-shm b/Web-Jarmark/Web-Jarmark/jarmark.db-shm index 1944f3c..8544857 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-shm and b/Web-Jarmark/Web-Jarmark/jarmark.db-shm differ diff --git a/Web-Jarmark/Web-Jarmark/jarmark.db-wal b/Web-Jarmark/Web-Jarmark/jarmark.db-wal index 3d60585..9fdcd07 100644 Binary files a/Web-Jarmark/Web-Jarmark/jarmark.db-wal and b/Web-Jarmark/Web-Jarmark/jarmark.db-wal differ diff --git a/Web-Jarmark/Web-Jarmark/wwwroot/app.css b/Web-Jarmark/Web-Jarmark/wwwroot/app.css index b502b6a..09bd3ec 100644 --- a/Web-Jarmark/Web-Jarmark/wwwroot/app.css +++ b/Web-Jarmark/Web-Jarmark/wwwroot/app.css @@ -3061,3 +3061,58 @@ textarea.wj-input { background: #f0f0f2; color: #6e6e73; } + +/* Ovládání množství v košíku */ +.cart-quantity-controls { + display: flex; + align-items: center; + gap: 12px; + margin-top: 8px; +} + + .cart-quantity-controls strong { + min-width: 22px; + text-align: center; + font-size: 16px; + } + + .cart-quantity-controls button { + width: 34px; + height: 34px; + border: none; + border-radius: 8px; + background: #222; + color: white; + font-size: 20px; + font-weight: 700; + cursor: pointer; + transition: background 0.2s ease; + } + + .cart-quantity-controls button:hover:not(:disabled) { + background: #444; + } + + .cart-quantity-controls button:disabled { + opacity: 0.35; + cursor: not-allowed; + } + +/* Odstranìní produktu */ +.cart-remove-button { + display: block; + margin-top: 12px; + padding: 8px 12px; + border: none; + border-radius: 8px; + background: #dc3545; + color: white; + font-size: 13px; + font-weight: 600; + cursor: pointer; + transition: background 0.2s ease; +} + + .cart-remove-button:hover { + background: #b02a37; + }