diff --git a/Data/AppDbContext.cs b/Data/AppDbContext.cs new file mode 100644 index 0000000..f94cd31 --- /dev/null +++ b/Data/AppDbContext.cs @@ -0,0 +1,69 @@ +using Microsoft.EntityFrameworkCore; +using NoticeBoard.Models; + +namespace NoticeBoard.Data; + +public class AppDbContext : DbContext +{ + public AppDbContext(DbContextOptions options) : base(options) { } + + public DbSet Slides => Set(); + public DbSet Devices => Set(); + public DbSet DeviceSlides => Set(); + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .HasIndex(d => d.Slug) + .IsUnique(); + + modelBuilder.Entity() + .HasOne(ds => ds.Device) + .WithMany(d => d.DeviceSlides) + .HasForeignKey(ds => ds.DeviceId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasOne(ds => ds.Slide) + .WithMany(s => s.DeviceSlides) + .HasForeignKey(ds => ds.SlideId) + .OnDelete(DeleteBehavior.Cascade); + + modelBuilder.Entity() + .HasIndex(ds => new { ds.DeviceId, ds.DisplayOrder }); + + // Seed demo data + modelBuilder.Entity().HasData( + new Device { Id = 1, Name = "Front of House", Slug = "frontofhouse", ResolutionWidth = 1920, ResolutionHeight = 1080 }, + new Device { Id = 2, Name = "Staff Room", Slug = "staffroom", ResolutionWidth = 1920, ResolutionHeight = 1080 } + ); + + modelBuilder.Entity().HasData( + new Slide + { + Id = 1, + Name = "Welcome Screen", + SlideType = SlideType.Content, + Content = "

Welcome

Notice Board

", + BackgroundColor = "#1a1a2e" + }, + new Slide + { + Id = 2, + Name = "Welcome to Country", + SlideType = SlideType.Content, + Content = "

Acknowledgement of Country

We acknowledge the Traditional Owners of the land on which we meet and pay our respects to Elders past, present and emerging.

", + BackgroundColor = "#2d1b00" + } + ); + + modelBuilder.Entity().HasData( + new DeviceSlide { Id = 1, DeviceId = 1, SlideId = 1, DisplayOrder = 1, DurationSeconds = 30, Enabled = true }, + new DeviceSlide { Id = 2, DeviceId = 1, SlideId = 2, DisplayOrder = 2, DurationSeconds = 60, Enabled = true }, + new DeviceSlide { Id = 3, DeviceId = 2, SlideId = 1, DisplayOrder = 1, DurationSeconds = 30, Enabled = true }, + new DeviceSlide { Id = 4, DeviceId = 2, SlideId = 2, DisplayOrder = 2, DurationSeconds = 60, Enabled = true } + ); + } +}