-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCceDbContext.cs
More file actions
145 lines (131 loc) · 8 KB
/
CceDbContext.cs
File metadata and controls
145 lines (131 loc) · 8 KB
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
using System.Linq;
using System.Linq.Expressions;
using CCE.Application.Common.Interfaces;
using CCE.Domain.Audit;
using CCE.Domain.Common;
using CCE.Domain.Community;
using CCE.Domain.Content;
using CCE.Domain.Country;
using CCE.Domain.Identity;
using CCE.Domain.InteractiveCity;
using CCE.Domain.KnowledgeMaps;
using CCE.Domain.Notifications;
using CCE.Domain.Surveys;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace CCE.Infrastructure.Persistence;
/// <summary>
/// Application <see cref="DbContext"/>. Extends ASP.NET Identity's
/// <see cref="IdentityDbContext{User, Role, Guid}"/> so the Identity tables
/// (AspNetUsers/Roles/UserRoles/etc.) coexist with CCE entity tables in one model.
/// Snake-case naming via <c>EFCore.NamingConventions</c> applied in <c>DependencyInjection</c>.
/// </summary>
public sealed class CceDbContext
: IdentityDbContext<User, Role, System.Guid>, ICceDbContext
{
public CceDbContext(DbContextOptions<CceDbContext> options) : base(options) { }
// ─── Audit (Foundation) ───
public DbSet<AuditEvent> AuditEvents => Set<AuditEvent>();
// ─── Identity bounded context ───
public DbSet<StateRepresentativeAssignment> StateRepresentativeAssignments => Set<StateRepresentativeAssignment>();
public DbSet<ExpertProfile> ExpertProfiles => Set<ExpertProfile>();
public DbSet<ExpertRegistrationRequest> ExpertRegistrationRequests => Set<ExpertRegistrationRequest>();
// ─── Content ───
public DbSet<AssetFile> AssetFiles => Set<AssetFile>();
public DbSet<ResourceCategory> ResourceCategories => Set<ResourceCategory>();
public DbSet<Resource> Resources => Set<Resource>();
public DbSet<News> News => Set<News>();
public DbSet<Event> Events => Set<Event>();
public DbSet<Page> Pages => Set<Page>();
public DbSet<HomepageSection> HomepageSections => Set<HomepageSection>();
public DbSet<NewsletterSubscription> NewsletterSubscriptions => Set<NewsletterSubscription>();
// ─── Country ───
public DbSet<CCE.Domain.Country.Country> Countries => Set<CCE.Domain.Country.Country>();
public DbSet<CountryProfile> CountryProfiles => Set<CountryProfile>();
public DbSet<CountryResourceRequest> CountryResourceRequests => Set<CountryResourceRequest>();
public DbSet<CountryKapsarcSnapshot> CountryKapsarcSnapshots => Set<CountryKapsarcSnapshot>();
// ─── Community ───
public DbSet<Topic> Topics => Set<Topic>();
public DbSet<Post> Posts => Set<Post>();
public DbSet<PostReply> PostReplies => Set<PostReply>();
public DbSet<PostRating> PostRatings => Set<PostRating>();
public DbSet<TopicFollow> TopicFollows => Set<TopicFollow>();
public DbSet<UserFollow> UserFollows => Set<UserFollow>();
public DbSet<PostFollow> PostFollows => Set<PostFollow>();
// ─── Knowledge Maps ───
public DbSet<KnowledgeMap> KnowledgeMaps => Set<KnowledgeMap>();
public DbSet<KnowledgeMapNode> KnowledgeMapNodes => Set<KnowledgeMapNode>();
public DbSet<KnowledgeMapEdge> KnowledgeMapEdges => Set<KnowledgeMapEdge>();
public DbSet<KnowledgeMapAssociation> KnowledgeMapAssociations => Set<KnowledgeMapAssociation>();
// ─── Interactive City ───
public DbSet<CityScenario> CityScenarios => Set<CityScenario>();
public DbSet<CityTechnology> CityTechnologies => Set<CityTechnology>();
public DbSet<CityScenarioResult> CityScenarioResults => Set<CityScenarioResult>();
// ─── Notifications ───
public DbSet<NotificationTemplate> NotificationTemplates => Set<NotificationTemplate>();
public DbSet<UserNotification> UserNotifications => Set<UserNotification>();
// ─── Surveys ───
public DbSet<ServiceRating> ServiceRatings => Set<ServiceRating>();
public DbSet<SearchQueryLog> SearchQueryLogs => Set<SearchQueryLog>();
// ─── ICceDbContext explicit interface implementations ───
// DbSet<T> implements IQueryable<T>; the inherited Identity DbSets (Users/Roles/UserRoles)
// and the domain DbSet below satisfy the interface through these explicit projections.
IQueryable<User> ICceDbContext.Users => Users;
IQueryable<Role> ICceDbContext.Roles => Roles;
IQueryable<IdentityUserRole<System.Guid>> ICceDbContext.UserRoles => UserRoles;
IQueryable<StateRepresentativeAssignment> ICceDbContext.StateRepresentativeAssignments => StateRepresentativeAssignments;
IQueryable<CCE.Domain.Country.Country> ICceDbContext.Countries => Countries;
IQueryable<ExpertRegistrationRequest> ICceDbContext.ExpertRegistrationRequests => ExpertRegistrationRequests;
IQueryable<ExpertProfile> ICceDbContext.ExpertProfiles => ExpertProfiles;
IQueryable<AssetFile> ICceDbContext.AssetFiles => AssetFiles;
IQueryable<ResourceCategory> ICceDbContext.ResourceCategories => ResourceCategories;
IQueryable<CCE.Domain.Content.Resource> ICceDbContext.Resources => Resources;
IQueryable<CountryResourceRequest> ICceDbContext.CountryResourceRequests => CountryResourceRequests;
IQueryable<CountryProfile> ICceDbContext.CountryProfiles => CountryProfiles;
IQueryable<CountryKapsarcSnapshot> ICceDbContext.CountryKapsarcSnapshots => CountryKapsarcSnapshots;
IQueryable<CCE.Domain.Content.News> ICceDbContext.News => News;
IQueryable<CCE.Domain.Content.Event> ICceDbContext.Events => Events;
IQueryable<CCE.Domain.Content.Page> ICceDbContext.Pages => Pages;
IQueryable<HomepageSection> ICceDbContext.HomepageSections => HomepageSections;
IQueryable<Topic> ICceDbContext.Topics => Topics;
IQueryable<Post> ICceDbContext.Posts => Posts;
IQueryable<PostReply> ICceDbContext.PostReplies => PostReplies;
IQueryable<PostRating> ICceDbContext.PostRatings => PostRatings;
IQueryable<TopicFollow> ICceDbContext.TopicFollows => TopicFollows;
IQueryable<UserFollow> ICceDbContext.UserFollows => UserFollows;
IQueryable<PostFollow> ICceDbContext.PostFollows => PostFollows;
IQueryable<NotificationTemplate> ICceDbContext.NotificationTemplates => NotificationTemplates;
IQueryable<UserNotification> ICceDbContext.UserNotifications => UserNotifications;
IQueryable<ServiceRating> ICceDbContext.ServiceRatings => ServiceRatings;
IQueryable<AuditEvent> ICceDbContext.AuditEvents => AuditEvents;
IQueryable<KnowledgeMap> ICceDbContext.KnowledgeMaps => KnowledgeMaps;
IQueryable<KnowledgeMapNode> ICceDbContext.KnowledgeMapNodes => KnowledgeMapNodes;
IQueryable<KnowledgeMapEdge> ICceDbContext.KnowledgeMapEdges => KnowledgeMapEdges;
IQueryable<KnowledgeMapAssociation> ICceDbContext.KnowledgeMapAssociations => KnowledgeMapAssociations;
IQueryable<CityScenario> ICceDbContext.CityScenarios => CityScenarios;
IQueryable<CityTechnology> ICceDbContext.CityTechnologies => CityTechnologies;
IQueryable<CityScenarioResult> ICceDbContext.CityScenarioResults => CityScenarioResults;
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfigurationsFromAssembly(typeof(CceDbContext).Assembly);
ApplySoftDeleteFilter(builder);
}
/// <summary>
/// Spec §5.5: every <see cref="ISoftDeletable"/> entity gets a global query filter
/// <c>HasQueryFilter(e => !e.IsDeleted)</c>. To bypass, use <c>IgnoreQueryFilters()</c>.
/// </summary>
private static void ApplySoftDeleteFilter(ModelBuilder modelBuilder)
{
var isDeletedProperty = typeof(ISoftDeletable).GetProperty(nameof(ISoftDeletable.IsDeleted))!;
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (!typeof(ISoftDeletable).IsAssignableFrom(entityType.ClrType)) continue;
var parameter = Expression.Parameter(entityType.ClrType, "e");
var body = Expression.Not(Expression.Property(parameter, isDeletedProperty));
var lambda = Expression.Lambda(body, parameter);
modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
}
}
}