H2: Hero-module (tabellen en migratie, beheer-API, publieke GET /hero, afbeeldingsupload met focus, opschoonservice, teksten) en OpenAPI-snapshot

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-16 15:46:55 +01:00
co-authored by Claude Sonnet 5.5
parent 43925cbf37
commit ae04120d8c
31 changed files with 4221 additions and 3 deletions
@@ -0,0 +1,87 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using SlpModularCms.Modules.Hero.Data.Entities;
namespace SlpModularCms.Modules.Hero.Data;
public class HeroDbContext(DbContextOptions<HeroDbContext> options) : DbContext(options)
{
public DbSet<HeroContent> Heroes => Set<HeroContent>();
public DbSet<HeroSlide> HeroSlides => Set<HeroSlide>();
public DbSet<HeroImage> HeroImages => Set<HeroImage>();
public DbSet<HeroRetiredFile> HeroRetiredFiles => Set<HeroRetiredFile>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HeroContent>(entity =>
{
entity.ToTable("Heroes");
entity.HasKey(e => e.Id);
entity.Property(e => e.Language).IsRequired().HasMaxLength(10);
entity.HasIndex(e => e.Language).IsUnique();
entity.Property(e => e.Title).IsRequired().HasMaxLength(1000);
entity.Property(e => e.Tagline).HasMaxLength(200);
entity.Property(e => e.Lead).HasMaxLength(1000);
entity.Property(e => e.Display).IsRequired().HasMaxLength(20);
entity.Property(e => e.Button1Label).HasMaxLength(100);
entity.Property(e => e.Button1Target).HasMaxLength(500);
entity.Property(e => e.Button2Label).HasMaxLength(100);
entity.Property(e => e.Button2Target).HasMaxLength(500);
entity.Property(e => e.IsActive).IsRequired();
entity.Property(e => e.UpdatedAt).IsRequired();
entity.Property(e => e.LastModifiedByUserId).IsRequired();
entity.HasMany(e => e.Slides).WithOne().HasForeignKey(s => s.HeroId).OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<HeroSlide>(entity =>
{
entity.ToTable("HeroSlides");
entity.HasKey(e => e.Id);
entity.Property(e => e.Alt).IsRequired().HasMaxLength(600);
entity.Property(e => e.SortOrder).IsRequired();
// Not unique: re-ordering updates several rows in one save and must not trip over itself.
entity.HasIndex(e => new { e.HeroId, e.SortOrder });
entity.HasIndex(e => e.ImageId);
// Restrict: an image that a slide still uses cannot be deleted (the service answers 409 first).
entity.HasOne(e => e.Image).WithMany().HasForeignKey(e => e.ImageId).OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity<HeroImage>(entity =>
{
entity.ToTable("HeroImages");
entity.HasKey(e => e.Id);
entity.Property(e => e.OriginalSha256).IsRequired().HasMaxLength(64);
entity.HasIndex(e => e.OriginalSha256).IsUnique();
entity.Property(e => e.OriginalKey).IsRequired().HasMaxLength(200);
entity.Property(e => e.SourceFormat).IsRequired().HasMaxLength(10);
entity.Property(e => e.SetHash).IsRequired().HasMaxLength(16);
entity.Property(e => e.CreatedAt).IsRequired();
entity.Property(e => e.CreatedByUserId).IsRequired();
entity.Ignore(e => e.General);
// The variants of the current set: a small list that is always replaced as a whole.
entity.Property(e => e.Variants)
.HasConversion(
variants => JsonSerializer.Serialize(variants, (JsonSerializerOptions?)null),
json => JsonSerializer.Deserialize<List<HeroVariant>>(json, (JsonSerializerOptions?)null) ?? new List<HeroVariant>())
.HasColumnType("longtext")
.Metadata.SetValueComparer(new ValueComparer<List<HeroVariant>>(
(a, b) => (a ?? new List<HeroVariant>()).SequenceEqual(b ?? new List<HeroVariant>()),
v => v.Aggregate(0, (hash, item) => HashCode.Combine(hash, item.GetHashCode())),
v => v.ToList()));
});
modelBuilder.Entity<HeroRetiredFile>(entity =>
{
entity.ToTable("HeroRetiredFiles");
entity.HasKey(e => e.Id);
entity.Property(e => e.Kind).IsRequired().HasMaxLength(10);
entity.Property(e => e.Key).IsRequired().HasMaxLength(200);
entity.Property(e => e.RetiredAt).IsRequired();
});
}
}