Initial commit
This commit is contained in:
commit
63ee71a19b
123 files changed
+11268
No files matched your search
@@ -0,0 +1,91 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using OnlyPrompt.Backend.Database.Core;
|
||||
using OnlyPrompt.Backend.Database.Models;
|
||||
|
||||
namespace OnlyPrompt.Backend.Database
|
||||
{
|
||||
public class OnlyPromptContext : DbContext
|
||||
{
|
||||
public DbSet<UserModel> Users { get; set; }
|
||||
public DbSet<UserProfileModel> UserProfiles { get; set; }
|
||||
public DbSet<CategoryModel> Categories { get; set; }
|
||||
public DbSet<PromptModel> Prompts { get; set; }
|
||||
public DbSet<SubscriptionTierModel> SubscriptionTiers { get; set; }
|
||||
public DbSet<SubscriptionModel> Subscriptions { get; set; }
|
||||
public DbSet<ReviewModel> Reviews { get; set; }
|
||||
|
||||
public OnlyPromptContext(DbContextOptions<OnlyPromptContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
private void HandleEntityTimestamps()
|
||||
{
|
||||
var entries = ChangeTracker.Entries<ITrackableEntity>();
|
||||
var now = DateTime.UtcNow;
|
||||
foreach (var entry in entries)
|
||||
{
|
||||
if (entry.State == EntityState.Added)
|
||||
{
|
||||
entry.Entity.CreatedAt = now;
|
||||
entry.Entity.UpdatedAt = now;
|
||||
}
|
||||
else if (entry.State == EntityState.Modified)
|
||||
{
|
||||
entry.Entity.UpdatedAt = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
|
||||
{
|
||||
HandleEntityTimestamps();
|
||||
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
}
|
||||
|
||||
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
||||
{
|
||||
HandleEntityTimestamps();
|
||||
return base.SaveChanges(acceptAllChangesOnSuccess);
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
if(optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseNpgsql();
|
||||
optionsBuilder.UseLazyLoadingProxies();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.Entity<UserModel>(entity =>
|
||||
{
|
||||
entity.HasOne(e => e.Profile)
|
||||
.WithOne(p => p.User)
|
||||
.HasForeignKey<UserProfileModel>(p => p.Id);
|
||||
});
|
||||
|
||||
modelBuilder.Entity<SubscriptionModel>(entity =>
|
||||
{
|
||||
entity.HasOne(e => e.Subscriber)
|
||||
.WithMany(s => s.Subscriptions)
|
||||
.HasForeignKey(e => e.SubscriberId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
entity.HasOne(e => e.SubscribedTo)
|
||||
.WithMany(c => c.Subscribers)
|
||||
.HasForeignKey(e => e.SubscribedToId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
entity.HasOne(e => e.SubscriptionTier)
|
||||
.WithMany(t => t.Subscriptions)
|
||||
.HasForeignKey(e => e.SubscriptionTierId)
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user