Files
PIbd-21_Permyakov_R.G._Blac…/TheBlacksmithVakulaContract/TheBlacksmithVakulaDatabase/TheBlacksmithVakulaDbContext.cs

97 lines
3.6 KiB
C#

using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using TheBlacksmithVakulaContract.Infrastructure;
using TheBlacksmithVakulaContract.Infrastructure.RankConfigurations;
using TheBlacksmithVakulaDatabase.Models;
namespace TheBlacksmithVakulaDatabase
{
internal class TheBlacksmithVakulaDbContext : DbContext
{
private readonly IConfigurationDatabase? _configurationDatabase;
public TheBlacksmithVakulaDbContext(IConfigurationDatabase configurationDatabase)
{
_configurationDatabase = configurationDatabase;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_configurationDatabase?.ConnectionString, o => o.SetPostgresVersion(16, 2));
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Buyer>().HasIndex(x => x.PhoneNumber).IsUnique();
modelBuilder.Entity<Order>()
.HasOne(e => e.Buyer)
.WithMany(e => e.Orders)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Billet>().HasIndex(x => x.BilletName).IsUnique();
modelBuilder.Entity<Rank>()
.HasIndex(e => new { e.RankName, e.IsActual })
.IsUnique()
.HasFilter($"\"{nameof(Rank.IsActual)}\" = TRUE");
modelBuilder.Entity<Rank>()
.HasIndex(e => new { e.RankId, e.IsActual })
.IsUnique()
.HasFilter($"\"{nameof(Rank.IsActual)}\" = TRUE");
modelBuilder.Entity<Product>()
.HasIndex(x => new { x.ProductName, x.IsDeleted })
.IsUnique()
.HasFilter($"\"{nameof(Product.IsDeleted)}\" = FALSE");
modelBuilder.Entity<Product>()
.HasOne(e => e.Billet)
.WithMany(e => e.Products)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<OrderProduct>()
.HasKey(x => new { x.OrderId, x.ProductId });
modelBuilder.Entity<Rank>()
.Property(x => x.Configuration)
.HasColumnType("jsonb")
.HasConversion(
x => SerializeRankConfiguration(x),
x => DeserialzeRankConfiguration(x)
);
}
public DbSet<Buyer> Buyers { get; set; }
public DbSet<Billet> Billets { get; set; }
public DbSet<Rank> Ranks { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductHistory> ProductHistories { get; set; }
public DbSet<Salary> Salaries { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderProduct> OrderProducts { get; set; }
public DbSet<Blacksmith> Blacksmiths { get; set; }
private static string SerializeRankConfiguration(RankConfiguration rankConfiguration) => JsonConvert.SerializeObject(rankConfiguration);
private static RankConfiguration DeserialzeRankConfiguration(string jsonString) => JToken.Parse(jsonString).Value<string>("Type") switch
{
nameof(ExpertRankConfiguration) => JsonConvert.DeserializeObject<ExpertRankConfiguration>(jsonString)!,
nameof(StudentRankConfiguration) => JsonConvert.DeserializeObject<StudentRankConfiguration>(jsonString)!,
_ => JsonConvert.DeserializeObject<RankConfiguration>(jsonString)!,
};
}
}