DB a Backend Vytvoreni entiti a registrace v dbcontextu

This commit is contained in:
maja
2026-10-21 18:31:11 +02:00
parent 57d2ae4e09
commit 08b21dd378
7 changed files with 112 additions and 3 deletions

View File

@@ -25,8 +25,8 @@
</div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="DetailVyrobku">
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Detail vyrobku
<NavLink class="nav-link" href="FF">
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> FF
</NavLink>
</div>

View File

@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Web_Jarmark.Data;
using Web_Jarmark.Data.Models;
namespace Web_Jarmark.Data
{
@@ -7,3 +9,19 @@ namespace Web_Jarmark.Data
{
}
}
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext<ApplicationUser>(options)
{
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Nastavení vazby 1:N mezi Category a Product
modelBuilder.Entity<Category>()
.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
}
}

View File

@@ -0,0 +1,15 @@
using Microsoft.CodeAnalysis;
namespace Web_Jarmark.Data.Models
{
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public bool IsActive { get; set; }
// Navigační vlastnost pro produkty
public ICollection<Product> Products { get; set; } = new List<Product>();
}
}

View File

@@ -0,0 +1,41 @@
namespace Web_Jarmark.Data.Models
{
public enum OrderStatus
{
Pending,
Paid,
Shipped,
Delivered,
Cancelled
}
public enum PaymentMethod
{
CreditCard,
PayPal,
BankTransfer,
Cash
}
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 ICollection<OrderItem> OrderItems { get; set; } = new List<OrderItem>();
}
}

View File

@@ -0,0 +1,16 @@
namespace Web_Jarmark.Data.Models
{
public class OrderItem
{
public int Id { get; set; }
public int OrderId { get; set; }
public int ProductId { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
// Navigační vlastnosti pro objednávku a produkt
public Order Order { get; set; }
public Product Product { get; set; }
}
}

View File

@@ -0,0 +1,19 @@
namespace Web_Jarmark.Data.Models
{
public class Product
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int StockQuantity { get; set; }
public string ImageUrl { get; set; }
public bool IsVisible { get; set; }
public DateTime UpdatedAt { get; set; }
// Navigační vlastnosti pro kategorii a objednávky
public Category Category { get; set; }
public ICollection<OrderItem> OrderItems { get; set; } = new List<OrderItem>();
}
}

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>