79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using Koogle.Domain.Entities;
|
||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
namespace KoogleApp.Data;
|
||
|
||
|
||
public class AppDbContext : DbContext
|
||
{
|
||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||
: base(options)
|
||
{
|
||
}
|
||
|
||
// Stammdaten / Aggregate Roots
|
||
public DbSet<Club> Clubs => Set<Club>();
|
||
public DbSet<Person> Persons => Set<Person>();
|
||
public DbSet<Day> Days => Set<Day>();
|
||
public DbSet<Game> Games => Set<Game>();
|
||
public DbSet<Expense> Expenses => Set<Expense>();
|
||
public DbSet<Trigger> Triggers => Set<Trigger>();
|
||
public DbSet<UserProfileClubRoleAssignment> UserProfileClubRoleAssignments => Set<UserProfileClubRoleAssignment>();
|
||
// Ledger / Events
|
||
public DbSet<PersonExpense> PersonExpenses => Set<PersonExpense>();
|
||
|
||
// Domain-Profile (getrennt von Identity)
|
||
public DbSet<UserProfile> UserProfiles => Set<UserProfile>();
|
||
|
||
// Join-Entities (optional als DbSet, aber praktisch f<>r Queries/Migrations)
|
||
public DbSet<DayPerson> DayPersons => Set<DayPerson>();
|
||
public DbSet<GamePerson> GamePersons => Set<GamePerson>();
|
||
public DbSet<ExpenseTrigger> ExpenseTriggers => Set<ExpenseTrigger>();
|
||
public DbSet<UserProfileClub> UserProfileClubs => Set<UserProfileClub>();
|
||
public DbSet<ClubInvitation> ClubInvitations => Set<ClubInvitation>();
|
||
|
||
// Statistics
|
||
public DbSet<PlayerGameStatistics> PlayerGameStatistics => Set<PlayerGameStatistics>();
|
||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||
{
|
||
base.OnModelCreating(modelBuilder);
|
||
|
||
// Optional: eigenes Schema f<>r Domain-Tabellen
|
||
modelBuilder.HasDefaultSchema("app");
|
||
|
||
// Deine IEntityTypeConfiguration<> Klassen automatisch anwenden
|
||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
|
||
}
|
||
|
||
|
||
|
||
public override int SaveChanges()
|
||
{
|
||
ConvertDeletesToSoftDeletes();
|
||
return base.SaveChanges();
|
||
}
|
||
|
||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||
{
|
||
ConvertDeletesToSoftDeletes();
|
||
return base.SaveChangesAsync(cancellationToken);
|
||
}
|
||
|
||
private void ConvertDeletesToSoftDeletes()
|
||
{
|
||
foreach (var entry in ChangeTracker.Entries<BaseEntity>())
|
||
{
|
||
if (entry.State == EntityState.Deleted)
|
||
{
|
||
entry.State = EntityState.Modified;
|
||
entry.Entity.IsDeleted = true;
|
||
entry.Entity.ModifiedAt = DateTime.UtcNow;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
}
|