First Commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using JobTrackerApi.Models;
|
||||
|
||||
namespace JobTrackerApi.Data
|
||||
{
|
||||
public class JobTrackerContext : IdentityDbContext<ApplicationUser>
|
||||
{
|
||||
public string? CurrentUserId { get; }
|
||||
|
||||
public JobTrackerContext(DbContextOptions<JobTrackerContext> options, JobTrackerApi.Services.ICurrentUserService currentUser) : base(options)
|
||||
{
|
||||
CurrentUserId = currentUser.UserId;
|
||||
}
|
||||
|
||||
public DbSet<Company> Companies => Set<Company>();
|
||||
public DbSet<JobApplication> JobApplications => Set<JobApplication>();
|
||||
public DbSet<Correspondence> Correspondences => Set<Correspondence>();
|
||||
public DbSet<Attachment> Attachments => Set<Attachment>();
|
||||
public DbSet<RuleSettings> RuleSettings => Set<RuleSettings>();
|
||||
public DbSet<UserRuleSettings> UserRuleSettings => Set<UserRuleSettings>();
|
||||
public DbSet<JobEvent> JobEvents => Set<JobEvent>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.Entity<Company>()
|
||||
.HasQueryFilter(c => CurrentUserId == null || c.OwnerUserId == CurrentUserId);
|
||||
|
||||
modelBuilder.Entity<JobApplication>()
|
||||
.HasQueryFilter(j => CurrentUserId == null || j.OwnerUserId == CurrentUserId);
|
||||
|
||||
modelBuilder.Entity<UserRuleSettings>()
|
||||
.HasKey(x => x.OwnerUserId);
|
||||
|
||||
modelBuilder.Entity<UserRuleSettings>()
|
||||
.HasQueryFilter(x => CurrentUserId == null || x.OwnerUserId == CurrentUserId);
|
||||
|
||||
modelBuilder.Entity<RuleSettings>()
|
||||
.HasData(new RuleSettings { Id = 1 });
|
||||
|
||||
modelBuilder.Entity<JobApplication>()
|
||||
.HasOne(j => j.Company)
|
||||
.WithMany(c => c.Jobs)
|
||||
.HasForeignKey(j => j.CompanyId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<JobApplication>()
|
||||
.HasIndex(j => j.OwnerUserId);
|
||||
|
||||
modelBuilder.Entity<Company>()
|
||||
.HasIndex(c => c.OwnerUserId);
|
||||
|
||||
modelBuilder.Entity<Correspondence>()
|
||||
.HasOne(c => c.JobApplication)
|
||||
.WithMany(j => j.Messages)
|
||||
.HasForeignKey(c => c.JobApplicationId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<Attachment>()
|
||||
.HasOne(a => a.JobApplication)
|
||||
.WithMany(j => j.Attachments)
|
||||
.HasForeignKey(a => a.JobApplicationId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<JobEvent>()
|
||||
.HasOne(e => e.JobApplication)
|
||||
.WithMany(j => j.Events)
|
||||
.HasForeignKey(e => e.JobApplicationId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user