This commit is contained in:
2023-06-17 14:16:09 +03:00
parent 62178f1f32
commit 7ae5d3bc81
180 changed files with 12932 additions and 192 deletions
+90 -2
View File
@@ -1,6 +1,8 @@
namespace MyOffice.DbContext;
using Data.Models.Account;
using Data.Models.Accounts;
using Data.Models.Currencies;
using Data.Models.Motions;
using Microsoft.EntityFrameworkCore;
using Data.Models.Users;
@@ -29,6 +31,19 @@ public class AppDbContext : DbContext
public DbSet<User> Users { get; set; } = null!;
public DbSet<UserExternal> UserClaims { get; set; } = null!;
public DbSet<CurrencyGlobal> CurrencyGlobals { get; set; } = null!;
public DbSet<Currency> Currencies { get; set; } = null!;
public DbSet<CurrencyRate> CurrencyRates { get; set; } = null!;
public DbSet<AccountCategory> AccountCategories { get; set; } = null!;
public DbSet<Account> Accounts { get; set; } = null!;
public DbSet<AccountAccountCategory> AccountAccountCategories { get; set; } = null!;
public DbSet<AccountAccess> AccountAccesses { get; set; } = null!;
public DbSet<MotionGlobal> GlobalMotions { get; set; } = null!;
public DbSet<MotionCategory> MotionCategories { get; set; } = null!;
public DbSet<MotionAccount> Motions { get; set; } = null!;
//public DbSet<AccountMotion> AccountMotions { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var noCaseCollation = _noCaseCollation[_provider];
@@ -59,12 +74,22 @@ public class AppDbContext : DbContext
.WithMany(x => x.UserClaims)
.HasForeignKey(x => x.UserId);
UserCreating(modelBuilder);
CurrencyCreating(modelBuilder);
AccountCreating(modelBuilder);
MotionsCreating(modelBuilder);
base.OnModelCreating(modelBuilder);
}
private void UserCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasOne(x => x.Currency)
.WithMany()
.HasForeignKey(x => x.CurrencyId);
}
private void CurrencyCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CurrencyGlobal>()
@@ -103,6 +128,12 @@ public class AppDbContext : DbContext
modelBuilder.Entity<AccountMotion>()
.HasKey(x => x.Id);
modelBuilder.Entity<AccountCategory>()
.HasKey(x => x.Id);
modelBuilder.Entity<AccountAccountCategory>()
.HasKey(x => x.Id);
modelBuilder.Entity<Account>()
.HasOne(x => x.CurrencyGlobal)
.WithMany(x => x.Accounts)
@@ -115,7 +146,64 @@ public class AppDbContext : DbContext
modelBuilder.Entity<AccountAccess>()
.HasOne(x => x.User)
.WithMany(x => x.AccessRights)
.WithMany(x => x.AccountAccess)
.HasForeignKey(x => x.UserId);
modelBuilder.Entity<AccountMotion>()
.HasOne(x => x.Account)
.WithMany(x => x.Motions)
.HasForeignKey(x => x.AccountId);
modelBuilder.Entity<AccountCategory>()
.HasOne(x => x.User)
.WithMany(x => x.AccountCategories)
.HasForeignKey(x => x.UserId);
modelBuilder.Entity<AccountAccountCategory>()
.HasOne(x => x.Account)
.WithMany(x => x.Categories)
.HasForeignKey(x => x.AccountId);
modelBuilder.Entity<AccountAccountCategory>()
.HasOne(x => x.Category)
.WithMany(x => x.Accounts)
.HasForeignKey(x => x.CategoryId);
}
private void MotionsCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MotionGlobal>()
.HasKey(x => x.Id);
modelBuilder.Entity<MotionCategory>()
.HasKey(x => x.Id);
modelBuilder.Entity<MotionAccount>()
.HasKey(x => x.Id);
modelBuilder.Entity<AccountMotion>()
.HasKey(x => x.Id);
modelBuilder.Entity<MotionCategory>()
.HasOne(x => x.User)
.WithMany(x => x.MotionCategories)
.HasForeignKey(x => x.UserId);
modelBuilder.Entity<MotionAccount>()
.HasOne(x => x.Category)
.WithMany(x => x.Motions)
.HasForeignKey(x => x.CategoryId);
modelBuilder.Entity<MotionAccount>()
.HasOne(x => x.MotionGlobal)
.WithMany(x => x.Motions)
.HasForeignKey(x => x.MotionGlobalId);
/*modelBuilder.Entity<AccountMotion>()
.HasOne(x => x.Motion)
.WithMany(x => x.Motions)
.HasForeignKey(x => x.MotionId);
modelBuilder.Entity<AccountMotion>()
.HasOne(x => x.Account)
.WithMany(x => x.Motions)
.HasForeignKey(x => x.AccountId);*/
}
}
+26 -1
View File
@@ -1,5 +1,6 @@
namespace MyOffice.DbContext;
using Data.Models.Currencies;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@@ -17,10 +18,34 @@ public class RepositoryInitializer
var db = AppDbContextFactory.Instance.CreateDbContext();
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
//db.Database.EnsureCreated();
db.Database.Migrate();
Prefill(db);
}
public static ConnectionConfiguration ConnectionString { get; internal set; } = null!;
public static IServiceCollection Services { get; internal set; } = null!;
private static void Prefill(AppDbContext dbContext)
{
// CurrencyGlobals
var predefinedCurrencyGlobals = new List<CurrencyGlobal>()
{
new() { Id = "UAH", DefaultQuantity = 1, Symbol = "₴", Name = "Ukrainian hryvnias" },
new() { Id = "USD", DefaultQuantity = 1, Symbol = "$", Name = "US Dollar" },
new() { Id = "EUR", DefaultQuantity = 1, Symbol = "€", Name = "Euros" },
new() { Id = "GBP", DefaultQuantity = 1, Symbol = "£", Name = "British pounds sterling" },
new() { Id = "RUB", DefaultQuantity = 10, Symbol = "₽", Name = "Russia Ruble" },
};
var currencyGlobals = dbContext.CurrencyGlobals.ToList();
foreach (var predefined in predefinedCurrencyGlobals)
{
if (currencyGlobals.FirstOrDefault(x => x.Id == predefined.Id) == null)
{
dbContext.CurrencyGlobals.Add(predefined);
}
}
dbContext.SaveChanges();
}
}