Repository navigation
Expand file tree
/
Copy pathConfigurations.cs
More file actions
214 lines (205 loc) · 12.2 KB
/
Copy pathConfigurations.cs
File metadata and controls
214 lines (205 loc) · 12.2 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using LearnStack.Infrastructure.DataProtection;
using LearnStack.Infrastructure.Persistence;
using LearnStack.Modules.Education.Domain;
using LearnStack.SharedKernel.DataProtection;
using LearnStack.SharedKernel.Identifiers;
using LearnStack.SharedKernel.Localization;
using LearnStack.SharedKernel.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace LearnStack.Modules.Education.Infrastructure.Persistence;
internal static class EducationMapping
{
internal static void MapScope<TEntity>(this EntityTypeBuilder<TEntity> builder, string table)
where TEntity : class, IOrganizationScoped
{
builder.Property(x => x.TenantId)
.HasConversion<TenantId.EfCoreValueConverter, TenantId.EfCoreValueComparer>()
.IsRequired();
builder.Property(x => x.OrganizationId)
.HasConversion<OrganizationId.EfCoreValueConverter, OrganizationId.EfCoreValueComparer>();
builder.HasIndex(x => new { x.TenantId, x.OrganizationId })
.HasDatabaseName($"ix_{table}_tenant_id_organization_id");
}
// ADR-0048 fixes lowercase storage independently of CLR enum spelling.
internal static void MapStatus(this PropertyBuilder<PublicationStatus> builder)
=> builder.HasConversion(
value => value.ToString().ToLowerInvariant(),
value => Enum.Parse<PublicationStatus>(value, true))
.HasColumnType("text")
.IsRequired();
internal static string SlugCheck(string column)
=> $"{column} ~ '^[a-z0-9]+(-[a-z0-9]+)*$' "
+ $"AND {column} !~ '^[0-9a-f]{{32}}$' "
+ $"AND {column} !~ '^[0-9a-f]{{8}}-[0-9a-f]{{4}}-[0-9a-f]{{4}}-[0-9a-f]{{4}}-[0-9a-f]{{12}}$'";
}
internal sealed class CourseConfiguration : IEntityTypeConfiguration<Course>
{
public void Configure(EntityTypeBuilder<Course> builder)
{
builder.ToTable("courses", table =>
{
table.HasCheckConstraint("ck_courses_slug_key_format", EducationMapping.SlugCheck("slug_key"));
table.HasCheckConstraint("ck_courses_status", "status IN ('draft', 'published')");
table.HasCheckConstraint("ck_courses_content_access", "content_access IN ('public', 'enrollment_required')");
table.HasCheckConstraint("ck_courses_level_reference", """
(level_taxonomy_key IS NULL AND level_taxonomy_schema_version IS NULL AND level_band_key IS NULL)
OR (level_taxonomy_key IS NOT NULL AND level_taxonomy_schema_version IS NOT NULL
AND level_taxonomy_schema_version > 0 AND level_band_key IS NOT NULL)
""");
});
builder.HasKey(x => x.Id).HasName("pk_courses");
builder.Property(x => x.Id)
.HasConversion<CourseId.EfCoreValueConverter, CourseId.EfCoreValueComparer>()
.ValueGeneratedNever();
builder.MapScope("courses");
builder.HasAlternateKey(x => new { x.TenantId, x.Id }).HasName("ux_courses_tenant_id_id");
builder.Property(x => x.SlugKey).HasMaxLength(EducationSlug.MaxLength).IsRequired();
builder.Property(x => x.Status).MapStatus();
builder.Property(x => x.ContentAccess)
.HasConversion(
value => value == CourseContentAccess.Public ? "public" : "enrollment_required",
value => value == "public" ? CourseContentAccess.Public : CourseContentAccess.EnrollmentRequired)
.HasColumnType("text")
.HasDefaultValue(CourseContentAccess.EnrollmentRequired)
.IsRequired();
builder.Property(x => x.LevelTaxonomyKey).HasMaxLength(EducationPinKey.MaxLength);
builder.Property(x => x.LevelTaxonomySchemaVersion);
builder.Property(x => x.LevelBandKey).HasMaxLength(EducationPinKey.MaxLength);
builder.MapAuditColumns<Course, CourseId>();
builder.DeclarePersonalData(PersonalDataCategory.None,
"Curriculum record identity and institution scope, not a person identifier.",
nameof(Course.Id), nameof(Course.TenantId), nameof(Course.OrganizationId));
builder.DeclarePersonalData(PersonalDataCategory.None,
"Course catalogue handle, publication state and content-access configuration.",
nameof(Course.SlugKey), nameof(Course.Status), nameof(Course.ContentAccess));
builder.DeclarePersonalData(PersonalDataCategory.None,
"Pinned curriculum taxonomy definition and band references.",
nameof(Course.LevelTaxonomyKey), nameof(Course.LevelTaxonomySchemaVersion), nameof(Course.LevelBandKey));
builder.HasIndex(x => new { x.TenantId, x.SlugKey })
.IsUnique().HasFilter("deleted_at IS NULL")
.HasDatabaseName("ux_courses_tenant_id_slug_key");
builder.HasIndex(x => new { x.TenantId, x.OrganizationId, x.CreatedAt, x.Id })
.HasFilter("deleted_at IS NULL")
.HasDatabaseName("ix_courses_tenant_id_organization_id_created_at_id");
// Containment with an ordinary entity preserves the satellite's own filter.
builder.HasMany(x => x.Translations).WithOne()
.HasForeignKey(x => new { x.TenantId, x.CourseId })
.HasPrincipalKey(x => new { x.TenantId, x.Id })
.HasConstraintName("fk_course_translations_course")
.OnDelete(DeleteBehavior.Cascade);
builder.Navigation(x => x.Translations).HasField("_translations")
.UsePropertyAccessMode(PropertyAccessMode.Field);
}
}
internal sealed class LessonConfiguration : IEntityTypeConfiguration<Lesson>
{
public void Configure(EntityTypeBuilder<Lesson> builder)
{
builder.ToTable("lessons", table =>
{
table.HasCheckConstraint("ck_lessons_status", "status IN ('draft', 'published')");
table.HasCheckConstraint("ck_lessons_sort", "sort >= 0");
table.HasCheckConstraint("ck_lessons_content_type_schema_version", "content_type_schema_version > 0");
});
builder.HasKey(x => x.Id).HasName("pk_lessons");
builder.Property(x => x.Id)
.HasConversion<LessonId.EfCoreValueConverter, LessonId.EfCoreValueComparer>()
.ValueGeneratedNever();
builder.MapScope("lessons");
builder.HasAlternateKey(x => new { x.TenantId, x.Id }).HasName("ux_lessons_tenant_id_id");
builder.Property(x => x.CourseId)
.HasConversion<CourseId.EfCoreValueConverter, CourseId.EfCoreValueComparer>()
.IsRequired();
builder.Property(x => x.Sort).IsRequired();
builder.Property(x => x.Status).MapStatus();
builder.Property(x => x.ContentTypeKey).HasMaxLength(EducationPinKey.MaxLength).IsRequired();
builder.Property(x => x.ContentTypeSchemaVersion).IsRequired();
builder.MapAuditColumns<Lesson, LessonId>();
builder.DeclarePersonalData(PersonalDataCategory.None,
"Curriculum record identity, parent course reference and institution scope.",
nameof(Lesson.Id), nameof(Lesson.TenantId), nameof(Lesson.OrganizationId), nameof(Lesson.CourseId));
builder.DeclarePersonalData(PersonalDataCategory.None,
"Lesson curriculum ordering and publication state.",
nameof(Lesson.Sort), nameof(Lesson.Status));
builder.DeclarePersonalData(PersonalDataCategory.None,
"Pinned lesson content-type definition reference.",
nameof(Lesson.ContentTypeKey), nameof(Lesson.ContentTypeSchemaVersion));
builder.HasOne<Course>().WithMany()
.HasForeignKey(x => new { x.TenantId, x.CourseId })
.HasPrincipalKey(x => new { x.TenantId, x.Id })
.HasConstraintName("fk_lessons_course")
.OnDelete(DeleteBehavior.Restrict);
builder.HasIndex(x => new { x.TenantId, x.CourseId })
.HasDatabaseName("ix_lessons_tenant_id_course_id");
builder.HasIndex(x => new { x.TenantId, x.CourseId, x.Sort, x.Id })
.HasFilter("deleted_at IS NULL")
.HasDatabaseName("ix_lessons_tenant_id_course_id_sort_id");
builder.HasMany(x => x.Translations).WithOne()
.HasForeignKey(x => new { x.TenantId, x.LessonId })
.HasPrincipalKey(x => new { x.TenantId, x.Id })
.HasConstraintName("fk_lesson_translations_lesson")
.OnDelete(DeleteBehavior.Cascade);
builder.Navigation(x => x.Translations).HasField("_translations")
.UsePropertyAccessMode(PropertyAccessMode.Field);
}
}
internal sealed class CourseTranslationConfiguration : IEntityTypeConfiguration<CourseTranslation>
{
public void Configure(EntityTypeBuilder<CourseTranslation> builder)
{
builder.ToTable("course_translations", table => table.HasCheckConstraint(
"ck_course_translations_slug_format", EducationMapping.SlugCheck("slug")));
builder.HasKey(x => new { x.CourseId, x.Locale }).HasName("pk_course_translations");
builder.Property(x => x.CourseId)
.HasConversion<CourseId.EfCoreValueConverter, CourseId.EfCoreValueComparer>()
.ValueGeneratedNever();
builder.MapScope("course_translations");
builder.Property(x => x.Locale).HasMaxLength(LocaleTag.MaxLength).IsRequired();
builder.Property(x => x.Title).HasColumnType("text").IsRequired();
builder.Property(x => x.Summary).HasColumnType("text");
builder.Property(x => x.Slug).HasMaxLength(EducationSlug.MaxLength).IsRequired();
builder.DeclarePersonalData(PersonalDataCategory.None,
"Translated curriculum parent reference, institution scope and content locale.",
nameof(CourseTranslation.CourseId), nameof(CourseTranslation.TenantId),
nameof(CourseTranslation.OrganizationId), nameof(CourseTranslation.Locale));
builder.DeclarePersonalData(PersonalDataCategory.None,
"Course catalogue content and route slug, not a learner or profile record.",
nameof(CourseTranslation.Title), nameof(CourseTranslation.Summary), nameof(CourseTranslation.Slug));
builder.HasAlternateKey(x => new { x.TenantId, x.Locale, x.Slug })
.HasName("ux_course_translations_tenant_id_locale_slug");
builder.HasIndex(x => new { x.TenantId, x.CourseId })
.HasDatabaseName("ix_course_translations_tenant_id_course_id");
}
}
internal sealed class LessonTranslationConfiguration : IEntityTypeConfiguration<LessonTranslation>
{
public void Configure(EntityTypeBuilder<LessonTranslation> builder)
{
builder.ToTable("lesson_translations", table =>
{
table.HasCheckConstraint("ck_lesson_translations_slug_format", EducationMapping.SlugCheck("slug"));
table.HasCheckConstraint("ck_lesson_translations_body_object", "jsonb_typeof(body) = 'object'");
});
builder.HasKey(x => new { x.LessonId, x.Locale }).HasName("pk_lesson_translations");
builder.Property(x => x.LessonId)
.HasConversion<LessonId.EfCoreValueConverter, LessonId.EfCoreValueComparer>()
.ValueGeneratedNever();
builder.MapScope("lesson_translations");
builder.Property(x => x.Locale).HasMaxLength(LocaleTag.MaxLength).IsRequired();
builder.Property(x => x.Title).HasColumnType("text").IsRequired();
builder.Property(x => x.Slug).HasMaxLength(EducationSlug.MaxLength).IsRequired();
builder.Property(x => x.Body).HasColumnType("jsonb").IsRequired();
builder.DeclarePersonalData(PersonalDataCategory.None,
"Translated curriculum parent reference, institution scope and content locale.",
nameof(LessonTranslation.LessonId), nameof(LessonTranslation.TenantId),
nameof(LessonTranslation.OrganizationId), nameof(LessonTranslation.Locale));
builder.DeclarePersonalData(PersonalDataCategory.None,
"Lesson curriculum content and route slug, not a learner or profile record.",
nameof(LessonTranslation.Title), nameof(LessonTranslation.Slug), nameof(LessonTranslation.Body));
builder.HasAlternateKey(x => new { x.TenantId, x.Locale, x.Slug })
.HasName("ux_lesson_translations_tenant_id_locale_slug");
builder.HasIndex(x => new { x.TenantId, x.LessonId })
.HasDatabaseName("ix_lesson_translations_tenant_id_lesson_id");
}
}