114 lines
6.9 KiB
C#
114 lines
6.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using LingAdmin.API.Models;
|
|
|
|
namespace LingAdmin.API.Data;
|
|
|
|
public class AppDbContext : DbContext
|
|
{
|
|
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<User> Users { get; set; }
|
|
public DbSet<Requisition> Requisitions { get; set; }
|
|
public DbSet<RequisitionItem> RequisitionItems { get; set; }
|
|
public DbSet<Language> Languages { get; set; }
|
|
public DbSet<Translation> Translations { get; set; }
|
|
public DbSet<MenuItem> MenuItems { get; set; }
|
|
public DbSet<InventoryItem> InventoryItems { get; set; }
|
|
public DbSet<AuditLog> AuditLogs { get; set; }
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// Configure decimal precision
|
|
modelBuilder.Entity<Requisition>()
|
|
.Property(r => r.TotalAmount)
|
|
.HasColumnType("decimal(18,2)");
|
|
|
|
modelBuilder.Entity<RequisitionItem>()
|
|
.Property(ri => ri.Price)
|
|
.HasColumnType("decimal(18,2)");
|
|
|
|
modelBuilder.Entity<InventoryItem>()
|
|
.Property(i => i.Price)
|
|
.HasColumnType("decimal(18,2)");
|
|
|
|
// Configure relationships
|
|
modelBuilder.Entity<RequisitionItem>()
|
|
.HasOne(ri => ri.Requisition)
|
|
.WithMany(r => r.Items)
|
|
.HasForeignKey(ri => ri.RequisitionId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
// Seed initial data
|
|
SeedData(modelBuilder);
|
|
}
|
|
|
|
private void SeedData(ModelBuilder modelBuilder)
|
|
{
|
|
// Note: All users have default password "Password123!" (hashed)
|
|
// This is the hash for "Password123!" - DO NOT use in production
|
|
var defaultPasswordHash = "gKr1UuNiCaLMqJ2gVPOJEXBEjN+zMdJH96kXgR/1KEA="; // Simplified for seed data
|
|
var baseTime = new DateTime(2023, 10, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
|
|
// Seed Users
|
|
modelBuilder.Entity<User>().HasData(
|
|
new User { Id = 1, Name = "John Doe", Email = "john@example.com", Role = "Admin", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 2, Name = "Jane Smith", Email = "jane@example.com", Role = "User", Status = "Inactive", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 3, Name = "Bob Johnson", Email = "bob@example.com", Role = "Editor", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 4, Name = "Alice Williams", Email = "alice@example.com", Role = "User", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 5, Name = "Charlie Brown", Email = "charlie@example.com", Role = "User", Status = "Inactive", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 6, Name = "David Miller", Email = "david@example.com", Role = "Admin", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 7, Name = "Eva Davis", Email = "eva@example.com", Role = "Editor", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 8, Name = "Frank Wilson", Email = "frank@example.com", Role = "User", Status = "Inactive", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 9, Name = "Grace Taylor", Email = "grace@example.com", Role = "User", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null },
|
|
new User { Id = 10, Name = "Henry Anderson", Email = "henry@example.com", Role = "User", Status = "Active", PasswordHash = defaultPasswordHash, CreatedAt = baseTime, LastLoginAt = null }
|
|
);
|
|
|
|
// Seed Languages
|
|
modelBuilder.Entity<Language>().HasData(
|
|
new Language { Id = 1, Code = "en-US", Name = "English", Version = "1.2" },
|
|
new Language { Id = 2, Code = "zh-CN", Name = "简体中文", Version = "1.3" }
|
|
);
|
|
|
|
// Seed Menu Items
|
|
modelBuilder.Entity<MenuItem>().HasData(
|
|
new MenuItem { Id = 1, Name = "common.dashboard", Path = "/", Icon = "LayoutDashboard", Order = 1 },
|
|
new MenuItem { Id = 2, Name = "common.inventory", Path = "/view/inventory", Icon = "FileText", Order = 2 },
|
|
new MenuItem { Id = 3, Name = "common.audit", Path = "/view/audit", Icon = "Settings", Order = 3 },
|
|
new MenuItem { Id = 4, Name = "common.requisitions", Path = "/requisitions", Icon = "FileText", Order = 4 },
|
|
new MenuItem { Id = 5, Name = "common.users", Path = "/users", Icon = "Users", Order = 5 }
|
|
);
|
|
|
|
// Seed Requisitions
|
|
modelBuilder.Entity<Requisition>().HasData(
|
|
new Requisition { Id = 101, ReqNo = "PR-2023001", Requester = "John Doe", Department = "IT", Date = new DateTime(2023, 10, 25), Status = "Approved", TotalAmount = 5000, Description = "Purchase of new laptops for the development team." },
|
|
new Requisition { Id = 102, ReqNo = "PR-2023002", Requester = "Jane Smith", Department = "HR", Date = new DateTime(2023, 10, 26), Status = "Pending", TotalAmount = 1200, Description = "Office supplies for HR department." },
|
|
new Requisition { Id = 103, ReqNo = "PR-2023003", Requester = "Bob Johnson", Department = "Sales", Date = new DateTime(2023, 10, 27), Status = "Draft", TotalAmount = 3500, Description = "Marketing materials for Q4 campaign." }
|
|
);
|
|
|
|
// Seed Requisition Items
|
|
|
|
// Seed Inventory Items
|
|
modelBuilder.Entity<InventoryItem>().HasData(
|
|
new InventoryItem { Id = 1, Sku = "SKU001", Name = "Laptop", Stock = 50, Price = 1200 },
|
|
new InventoryItem { Id = 2, Sku = "SKU002", Name = "Mouse", Stock = 200, Price = 25 },
|
|
new InventoryItem { Id = 3, Sku = "SKU003", Name = "Keyboard", Stock = 150, Price = 45 },
|
|
new InventoryItem { Id = 4, Sku = "SKU004", Name = "Monitor", Stock = 80, Price = 300 }
|
|
);
|
|
|
|
// Seed Audit Logs
|
|
modelBuilder.Entity<AuditLog>().HasData(
|
|
new AuditLog { Id = 101, User = "Admin", Action = "Login", Timestamp = new DateTime(2023, 10, 26, 10, 0, 0) },
|
|
new AuditLog { Id = 102, User = "User1", Action = "View Report", Timestamp = new DateTime(2023, 10, 26, 10, 5, 0) },
|
|
new AuditLog { Id = 103, User = "Admin", Action = "Update Settings", Timestamp = new DateTime(2023, 10, 26, 11, 20, 0) }
|
|
);
|
|
modelBuilder.Entity<RequisitionItem>().HasData(
|
|
new RequisitionItem { Id = 1, RequisitionId = 101, ItemName = "MacBook Pro 16", Quantity = 2, Price = 2400 },
|
|
new RequisitionItem { Id = 2, RequisitionId = 101, ItemName = "Dell Monitor 27", Quantity = 5, Price = 300 }
|
|
);
|
|
}
|
|
}
|