## Product Variations System - Add ProductVariation model with quantity-based pricing (1 for £10, 2 for £19, 3 for £25) - Complete CRUD operations for product variations - Enhanced ProductService to include variations in all queries - Updated OrderItem to support ProductVariationId for variation-based orders - Graceful error handling for duplicate quantity constraints - Admin interface with variations management (Create/Edit/Delete) - API endpoints for programmatic variation management ## Enhanced Order Workflow Management - Redesigned OrderStatus enum with clear workflow states (Accept → Packing → Dispatched → Delivered) - Added workflow tracking fields (AcceptedAt, PackingStartedAt, DispatchedAt, ExpectedDeliveryDate) - User tracking for accountability (AcceptedByUser, PackedByUser, DispatchedByUser) - Automatic delivery date calculation (dispatch date + working days, skips weekends) - On Hold workflow for problem resolution with reason tracking - Tab-based orders interface focused on workflow stages - One-click workflow actions from list view ## Mobile-Responsive Design - Responsive orders interface: tables on desktop, cards on mobile - Touch-friendly buttons and spacing for mobile users - Horizontal scrolling tabs with condensed labels on mobile - Color-coded status borders for quick visual recognition - Smart text switching based on screen size ## Product Import/Export System - CSV import with product variations support - Template download with examples - Export existing products to CSV - Detailed import results with success/error reporting - Category name resolution (no need for GUIDs) - Photo URLs import support ## Enhanced Dashboard - Product variations count and metrics - Stock alerts (low stock/out of stock warnings) - Order workflow breakdown (pending, accepted, dispatched counts) - Enhanced layout with more detailed information ## Technical Improvements - Fixed form binding issues across all admin forms - Removed external CDN dependencies for isolated deployment - Bot Wizard form with auto-personality assignment - Proper authentication scheme configuration (Cookie + JWT) - Enhanced debug logging for troubleshooting ## Self-Contained Deployment - All external CDN references replaced with local libraries - Ready for air-gapped/isolated network deployment - No external internet dependencies 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
261 lines
9.3 KiB
C#
261 lines
9.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using LittleShop.Models;
|
|
|
|
namespace LittleShop.Data;
|
|
|
|
public class LittleShopContext : DbContext
|
|
{
|
|
public LittleShopContext(DbContextOptions<LittleShopContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<User> Users { get; set; }
|
|
public DbSet<Category> Categories { get; set; }
|
|
public DbSet<Product> Products { get; set; }
|
|
public DbSet<ProductPhoto> ProductPhotos { get; set; }
|
|
public DbSet<ProductVariation> ProductVariations { get; set; }
|
|
public DbSet<Order> Orders { get; set; }
|
|
public DbSet<OrderItem> OrderItems { get; set; }
|
|
public DbSet<CryptoPayment> CryptoPayments { get; set; }
|
|
public DbSet<ShippingRate> ShippingRates { get; set; }
|
|
public DbSet<Bot> Bots { get; set; }
|
|
public DbSet<BotMetric> BotMetrics { get; set; }
|
|
public DbSet<BotSession> BotSessions { get; set; }
|
|
public DbSet<Customer> Customers { get; set; }
|
|
public DbSet<CustomerMessage> CustomerMessages { get; set; }
|
|
public DbSet<PushSubscription> PushSubscriptions { get; set; }
|
|
public DbSet<Review> Reviews { get; set; }
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// User entity
|
|
modelBuilder.Entity<User>(entity =>
|
|
{
|
|
entity.HasIndex(e => e.Username).IsUnique();
|
|
});
|
|
|
|
// Category entity
|
|
modelBuilder.Entity<Category>(entity =>
|
|
{
|
|
entity.HasMany(c => c.Products)
|
|
.WithOne(p => p.Category)
|
|
.HasForeignKey(p => p.CategoryId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
});
|
|
|
|
// Product entity
|
|
modelBuilder.Entity<Product>(entity =>
|
|
{
|
|
entity.HasMany(p => p.Photos)
|
|
.WithOne(pp => pp.Product)
|
|
.HasForeignKey(pp => pp.ProductId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(p => p.Variations)
|
|
.WithOne(pv => pv.Product)
|
|
.HasForeignKey(pv => pv.ProductId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(p => p.OrderItems)
|
|
.WithOne(oi => oi.Product)
|
|
.HasForeignKey(oi => oi.ProductId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
});
|
|
|
|
// ProductVariation entity
|
|
modelBuilder.Entity<ProductVariation>(entity =>
|
|
{
|
|
entity.HasIndex(e => new { e.ProductId, e.Quantity }).IsUnique(); // One variation per quantity per product
|
|
entity.HasIndex(e => new { e.ProductId, e.SortOrder });
|
|
entity.HasIndex(e => e.IsActive);
|
|
|
|
entity.HasMany(pv => pv.OrderItems)
|
|
.WithOne(oi => oi.ProductVariation)
|
|
.HasForeignKey(oi => oi.ProductVariationId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
});
|
|
|
|
// Order entity
|
|
modelBuilder.Entity<Order>(entity =>
|
|
{
|
|
entity.HasOne(o => o.Customer)
|
|
.WithMany(c => c.Orders)
|
|
.HasForeignKey(o => o.CustomerId)
|
|
.OnDelete(DeleteBehavior.Restrict)
|
|
.IsRequired(false); // Make customer optional for transition
|
|
|
|
entity.HasMany(o => o.Items)
|
|
.WithOne(oi => oi.Order)
|
|
.HasForeignKey(oi => oi.OrderId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(o => o.Payments)
|
|
.WithOne(cp => cp.Order)
|
|
.HasForeignKey(cp => cp.OrderId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasIndex(e => e.CustomerId);
|
|
entity.HasIndex(e => e.IdentityReference);
|
|
entity.HasIndex(e => e.CreatedAt);
|
|
});
|
|
|
|
// OrderItem entity
|
|
modelBuilder.Entity<OrderItem>(entity =>
|
|
{
|
|
entity.HasKey(oi => oi.Id);
|
|
});
|
|
|
|
// CryptoPayment entity
|
|
modelBuilder.Entity<CryptoPayment>(entity =>
|
|
{
|
|
entity.HasIndex(e => e.BTCPayInvoiceId);
|
|
entity.HasIndex(e => e.WalletAddress);
|
|
});
|
|
|
|
// Bot entity
|
|
modelBuilder.Entity<Bot>(entity =>
|
|
{
|
|
entity.HasIndex(e => e.BotKey).IsUnique();
|
|
entity.HasIndex(e => e.Name);
|
|
entity.HasIndex(e => e.Status);
|
|
|
|
entity.HasMany(b => b.Metrics)
|
|
.WithOne(m => m.Bot)
|
|
.HasForeignKey(m => m.BotId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasMany(b => b.Sessions)
|
|
.WithOne(s => s.Bot)
|
|
.HasForeignKey(s => s.BotId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
});
|
|
|
|
// BotMetric entity
|
|
modelBuilder.Entity<BotMetric>(entity =>
|
|
{
|
|
entity.HasIndex(e => new { e.BotId, e.RecordedAt });
|
|
entity.HasIndex(e => e.MetricType);
|
|
});
|
|
|
|
// BotSession entity
|
|
modelBuilder.Entity<BotSession>(entity =>
|
|
{
|
|
entity.HasIndex(e => new { e.BotId, e.SessionIdentifier });
|
|
entity.HasIndex(e => e.StartedAt);
|
|
entity.HasIndex(e => e.LastActivityAt);
|
|
});
|
|
|
|
// Customer entity
|
|
modelBuilder.Entity<Customer>(entity =>
|
|
{
|
|
entity.HasIndex(e => e.TelegramUserId).IsUnique();
|
|
entity.HasIndex(e => e.TelegramUsername);
|
|
entity.HasIndex(e => e.Email);
|
|
entity.HasIndex(e => e.CreatedAt);
|
|
entity.HasIndex(e => e.LastActiveAt);
|
|
entity.HasIndex(e => e.DataRetentionDate);
|
|
|
|
entity.HasMany(c => c.Orders)
|
|
.WithOne(o => o.Customer)
|
|
.HasForeignKey(o => o.CustomerId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
entity.HasMany(c => c.Messages)
|
|
.WithOne(m => m.Customer)
|
|
.HasForeignKey(m => m.CustomerId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
});
|
|
|
|
// CustomerMessage entity
|
|
modelBuilder.Entity<CustomerMessage>(entity =>
|
|
{
|
|
entity.HasOne(m => m.Customer)
|
|
.WithMany(c => c.Messages)
|
|
.HasForeignKey(m => m.CustomerId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasOne(m => m.Order)
|
|
.WithMany()
|
|
.HasForeignKey(m => m.OrderId)
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
entity.HasOne(m => m.AdminUser)
|
|
.WithMany()
|
|
.HasForeignKey(m => m.AdminUserId)
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
entity.HasOne(m => m.ParentMessage)
|
|
.WithMany(m => m.Replies)
|
|
.HasForeignKey(m => m.ParentMessageId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
entity.HasIndex(e => new { e.CustomerId, e.CreatedAt });
|
|
entity.HasIndex(e => e.OrderId);
|
|
entity.HasIndex(e => e.Status);
|
|
entity.HasIndex(e => e.Direction);
|
|
entity.HasIndex(e => e.Type);
|
|
entity.HasIndex(e => e.ScheduledFor);
|
|
entity.HasIndex(e => e.ThreadId);
|
|
entity.HasIndex(e => e.ParentMessageId);
|
|
});
|
|
|
|
// PushSubscription entity
|
|
modelBuilder.Entity<PushSubscription>(entity =>
|
|
{
|
|
entity.HasOne(ps => ps.User)
|
|
.WithMany()
|
|
.HasForeignKey(ps => ps.UserId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasOne(ps => ps.Customer)
|
|
.WithMany()
|
|
.HasForeignKey(ps => ps.CustomerId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasIndex(e => e.Endpoint).IsUnique();
|
|
entity.HasIndex(e => e.UserId);
|
|
entity.HasIndex(e => e.CustomerId);
|
|
entity.HasIndex(e => e.SubscribedAt);
|
|
entity.HasIndex(e => e.IsActive);
|
|
});
|
|
|
|
// Review entity
|
|
modelBuilder.Entity<Review>(entity =>
|
|
{
|
|
entity.HasOne(r => r.Product)
|
|
.WithMany(p => p.Reviews)
|
|
.HasForeignKey(r => r.ProductId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasOne(r => r.Customer)
|
|
.WithMany()
|
|
.HasForeignKey(r => r.CustomerId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
entity.HasOne(r => r.Order)
|
|
.WithMany()
|
|
.HasForeignKey(r => r.OrderId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
|
|
entity.HasOne(r => r.ApprovedByUser)
|
|
.WithMany()
|
|
.HasForeignKey(r => r.ApprovedByUserId)
|
|
.OnDelete(DeleteBehavior.SetNull);
|
|
|
|
// Indexes for performance
|
|
entity.HasIndex(e => e.ProductId);
|
|
entity.HasIndex(e => e.CustomerId);
|
|
entity.HasIndex(e => e.OrderId);
|
|
entity.HasIndex(e => e.Rating);
|
|
entity.HasIndex(e => e.IsApproved);
|
|
entity.HasIndex(e => e.IsActive);
|
|
entity.HasIndex(e => e.CreatedAt);
|
|
|
|
// Composite indexes for common queries
|
|
entity.HasIndex(e => new { e.ProductId, e.IsApproved, e.IsActive });
|
|
entity.HasIndex(e => new { e.CustomerId, e.ProductId }).IsUnique(); // One review per customer per product
|
|
});
|
|
}
|
|
} |