-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathApplicationDbContext.cs
184 lines (169 loc) · 6.77 KB
/
ApplicationDbContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Models.DataModels;
using Models.DataModels.RoleSystem;
using Models.Helpers;
using Newtonsoft.Json;
namespace Models;
public class ApplicationDbContext : DbContext
{
public static string LocalDatabaseName { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Permission> Permissions { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<RolePermission> RolePermissions { get; set; }
public DbSet<ActionRole> ActionRoles { get; set; }
public DbSet<ActionPermission> ActionPermissions { get; set; }
public DbSet<TenantEntry> Tenants { get; set; }
public int TenantId { get; private init; } = 1;
private string _connectionString;
public ApplicationDbContext()
{
}
public ApplicationDbContext(int tenantId = 1, string connectionString = null)
{
TenantId = tenantId;
_connectionString = connectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseLazyLoadingProxies();
string file;
try
{
file = File.ReadAllText(Path.Combine("..", "WebAPI", "appsettings.json"));
}
catch (Exception)
{
file = File.ReadAllText("appsettings.json");
}
var appSettings = JsonConvert.DeserializeAnonymousType(file,
new { AppSettings = new AppSettings() })!.AppSettings;
if (appSettings.Postgres.Use)
{
if (_connectionString != null)
{
options.UseNpgsql(_connectionString);
}
else
{
var url = Environment.GetEnvironmentVariable("DATABASE_URL");
if (string.IsNullOrWhiteSpace(url))
{
options.UseNpgsql(
$"Host={appSettings.Postgres.Host};" +
$"Port={appSettings.Postgres.Port};" +
$"Database={appSettings.Postgres.DatabaseName};" +
$"Username={appSettings.Postgres.Username};" +
$"Password={appSettings.Postgres.Password}");
}
else
{
url = url[(url.IndexOf("//", StringComparison.Ordinal) + 2)..];
var userName = url[..url.IndexOf(':')];
url = url[(url.IndexOf(':') + 1)..];
var password = url[..url.IndexOf('@')];
url = url[(url.IndexOf('@') + 1)..];
var host = url[..url.IndexOf(':')];
url = url[(url.IndexOf(':') + 1)..];
var port = url[..url.IndexOf('/')];
var database = url[(url.IndexOf('/') + 1)..];
options.UseNpgsql(
$"Host={host};Port={port};Database={database};Username={userName};" +
$"Password={password};SSLMode=Require;TrustServerCertificate=true");
}
}
}
else if (appSettings.Mssql.Use)
{
if (_connectionString == null)
{
options.UseSqlServer(appSettings.Mssql.ConnectionString);
}
else
{
options.UseSqlServer(_connectionString);
}
}
}
private static LambdaExpression MakeEqualityComparison<T>(IReadOnlyTypeBase entityType, string property,
Expression value)
{
var parameter = Expression.Parameter(entityType.ClrType, "e");
var methodInfo = typeof(EF).GetMethod(nameof(EF.Property))!.MakeGenericMethod(typeof(T))!;
var efPropertyCall = Expression.Call(null, methodInfo, parameter, Expression.Constant(property));
var body = Expression.MakeBinary(ExpressionType.Equal, efPropertyCall, value);
return Expression.Lambda(body, parameter);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// add is deleted query filter for entities having an existing IsDeleted property
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var isDeletedProperty = entityType.FindProperty(nameof(BaseModel.IsDeleted));
if (isDeletedProperty != null && isDeletedProperty.ClrType == typeof(bool))
{
var expression = MakeEqualityComparison<bool>(entityType, nameof(BaseModel.IsDeleted),
Expression.Constant(false));
modelBuilder.Entity(entityType.ClrType).HasQueryFilter(expression);
}
/*
var tenantIdProperty = entityType.FindProperty(nameof(BaseModel.TenantId));
if (tenantIdProperty != null && tenantIdProperty.ClrType == typeof(int))
{
var expression = MakeEqualityComparison<int>(entityType, nameof(BaseModel.TenantId),
Expression.Property(Expression.Constant(this), nameof(TenantId)));
modelBuilder.Entity(entityType.ClrType).HasQueryFilter(expression);
}
*/
}
}
private void SaveHelper()
{
foreach (var entry in ChangeTracker.Entries<BaseModel>().ToList())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.AddedDate = DateTime.UtcNow;
entry.Entity.TenantId = TenantId;
break;
case EntityState.Modified when entry.Entity.IsDeleted &&
!entry.OriginalValues.GetValue<bool>(nameof(entry.Entity.IsDeleted)):
entry.Entity.DeletedDate = DateTime.UtcNow;
break;
case EntityState.Modified:
entry.Entity.ModifiedDate = DateTime.UtcNow;
break;
}
}
}
public override int SaveChanges()
{
SaveHelper();
return base.SaveChanges();
}
public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
SaveHelper();
return base.SaveChanges(acceptAllChangesOnSuccess);
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
CancellationToken cancellationToken = new CancellationToken())
{
SaveHelper();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
SaveHelper();
return base.SaveChangesAsync(cancellationToken);
}
}