-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDbContext.cs
More file actions
220 lines (182 loc) · 9.59 KB
/
AppDbContext.cs
File metadata and controls
220 lines (182 loc) · 9.59 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
215
216
217
218
219
220
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using ShiftDrop.Domain;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options) { }
public DbSet<Pool> Pools => Set<Pool>();
public DbSet<Casual> Casuals => Set<Casual>();
public DbSet<Shift> Shifts => Set<Shift>();
public DbSet<ShiftClaim> ShiftClaims => Set<ShiftClaim>();
public DbSet<ShiftNotification> ShiftNotifications => Set<ShiftNotification>();
public DbSet<OutboxMessage> OutboxMessages => Set<OutboxMessage>();
public DbSet<PoolAdmin> PoolAdmins => Set<PoolAdmin>();
public DbSet<CasualAvailability> CasualAvailability => Set<CasualAvailability>();
public DbSet<PushSubscription> PushSubscriptions => Set<PushSubscription>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Pool>(entity =>
{
entity.HasKey(p => p.Id);
entity.Property(p => p.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(p => p.Name).HasMaxLength(200).IsRequired();
entity.Property(p => p.ManagerAuth0Id).HasMaxLength(200).IsRequired();
entity.HasIndex(p => p.ManagerAuth0Id);
// Use backing field for proper change tracking with IReadOnlyCollection
entity.Navigation(p => p.Casuals).UsePropertyAccessMode(PropertyAccessMode.Field);
entity.HasMany(p => p.Casuals)
.WithOne(c => c.Pool)
.HasForeignKey(c => c.PoolId)
.OnDelete(DeleteBehavior.Cascade);
entity.Navigation(p => p.Shifts).UsePropertyAccessMode(PropertyAccessMode.Field);
entity.HasMany(p => p.Shifts)
.WithOne(s => s.Pool)
.HasForeignKey(s => s.PoolId)
.OnDelete(DeleteBehavior.Cascade);
entity.Navigation(p => p.Admins).UsePropertyAccessMode(PropertyAccessMode.Field);
});
modelBuilder.Entity<Casual>(entity =>
{
entity.HasKey(c => c.Id);
entity.Property(c => c.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(c => c.Name).HasMaxLength(200).IsRequired();
entity.Property(c => c.PhoneNumber)
.HasMaxLength(50)
.IsRequired()
.HasConversion(
phone => phone.Value,
value => PhoneNumber.FromTrusted(value));
entity.HasIndex(c => new { c.PoolId, c.PhoneNumber }).IsUnique();
// Token fields for SMS-based verification
entity.Property(c => c.InviteToken).HasMaxLength(32);
entity.Property(c => c.InviteStatus).HasConversion<string>().HasMaxLength(20);
entity.Property(c => c.OptOutToken).HasMaxLength(32);
// Filtered indexes for token lookups (only index non-null tokens)
entity.HasIndex(c => c.InviteToken)
.HasFilter("[InviteToken] IS NOT NULL")
.IsUnique();
entity.HasIndex(c => c.OptOutToken)
.HasFilter("[OptOutToken] IS NOT NULL")
.IsUnique();
// Use backing field for proper change tracking with IReadOnlyCollection
entity.Navigation(c => c.Claims).UsePropertyAccessMode(PropertyAccessMode.Field);
entity.HasMany(c => c.Claims)
.WithOne(sc => sc.Casual)
.HasForeignKey(sc => sc.CasualId)
// NoAction to avoid multiple cascade paths (SQL Server restriction)
// Claims are deleted via Shift cascade when Pool is deleted
.OnDelete(DeleteBehavior.NoAction);
entity.Navigation(c => c.Availability).UsePropertyAccessMode(PropertyAccessMode.Field);
});
modelBuilder.Entity<Shift>(entity =>
{
entity.HasKey(s => s.Id);
entity.Property(s => s.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(s => s.Status).HasConversion<string>().HasMaxLength(20);
// Concurrency token to handle race conditions on claim
entity.Property(s => s.SpotsRemaining).IsConcurrencyToken();
entity.HasIndex(s => new { s.PoolId, s.Status, s.StartsAt });
// Use backing field for proper change tracking with IReadOnlyCollection
entity.Navigation(s => s.Claims).UsePropertyAccessMode(PropertyAccessMode.Field);
entity.HasMany(s => s.Claims)
.WithOne(sc => sc.Shift)
.HasForeignKey(sc => sc.ShiftId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<ShiftClaim>(entity =>
{
entity.HasKey(sc => sc.Id);
// Domain generates IDs, not the database - critical for proper INSERT vs UPDATE
entity.Property(sc => sc.Id).ValueGeneratedNever();
entity.Property(sc => sc.Status).HasConversion<string>().HasMaxLength(20);
entity.HasIndex(sc => new { sc.ShiftId, sc.CasualId, sc.Status });
});
modelBuilder.Entity<ShiftNotification>(entity =>
{
entity.HasKey(sn => sn.Id);
entity.Property(sn => sn.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(sn => sn.ClaimToken).HasMaxLength(32).IsRequired();
entity.Property(sn => sn.TokenStatus).HasConversion<string>().HasMaxLength(20);
// Unique index on claim token for fast lookups
entity.HasIndex(sn => sn.ClaimToken).IsUnique();
// Index for finding pending notifications by shift (for revocation)
entity.HasIndex(sn => new { sn.ShiftId, sn.TokenStatus });
// Relationships
entity.HasOne(sn => sn.Shift)
.WithMany()
.HasForeignKey(sn => sn.ShiftId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(sn => sn.Casual)
.WithMany()
.HasForeignKey(sn => sn.CasualId)
// NoAction to avoid multiple cascade paths (SQL Server restriction)
// Notifications are deleted via Shift cascade when Pool is deleted
.OnDelete(DeleteBehavior.NoAction);
});
modelBuilder.Entity<OutboxMessage>(entity =>
{
entity.HasKey(om => om.Id);
entity.Property(om => om.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(om => om.MessageType).HasMaxLength(100).IsRequired();
entity.Property(om => om.Payload).IsRequired(); // JSON text, no max length
entity.Property(om => om.Status).HasConversion<string>().HasMaxLength(20);
entity.Property(om => om.LastError).HasMaxLength(1000);
// Index for outbox processor to find pending messages ready for processing
entity.HasIndex(om => new { om.Status, om.NextRetryAt })
.HasFilter("[Status] = 'Pending'");
});
modelBuilder.Entity<PoolAdmin>(entity =>
{
entity.HasKey(pa => pa.Id);
entity.Property(pa => pa.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(pa => pa.PhoneNumber)
.HasMaxLength(50)
.IsRequired()
.HasConversion(
phone => phone.Value,
value => PhoneNumber.FromTrusted(value));
entity.Property(pa => pa.Name).HasMaxLength(200).IsRequired();
entity.Property(pa => pa.Auth0Id).HasMaxLength(200);
entity.Property(pa => pa.InviteToken).HasMaxLength(32);
// Unique index on pool + phone number (one admin per phone per pool)
entity.HasIndex(pa => new { pa.PoolId, pa.PhoneNumber }).IsUnique();
// Index for token lookup (for accepting invites)
entity.HasIndex(pa => pa.InviteToken)
.IsUnique()
.HasFilter("[InviteToken] IS NOT NULL");
// Index for Auth0Id lookup (to find pools user is admin of)
entity.HasIndex(pa => pa.Auth0Id)
.HasFilter("[Auth0Id] IS NOT NULL");
entity.HasOne(pa => pa.Pool)
.WithMany(p => p.Admins)
.HasForeignKey(pa => pa.PoolId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<CasualAvailability>(entity =>
{
entity.HasKey(ca => ca.Id);
entity.Property(ca => ca.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(ca => ca.DayOfWeek).HasConversion<int>();
// Unique constraint: one entry per casual per day
entity.HasIndex(ca => new { ca.CasualId, ca.DayOfWeek }).IsUnique();
entity.HasOne(ca => ca.Casual)
.WithMany(c => c.Availability)
.HasForeignKey(ca => ca.CasualId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<PushSubscription>(entity =>
{
entity.HasKey(ps => ps.Id);
entity.Property(ps => ps.Id).ValueGeneratedNever(); // Domain generates IDs
entity.Property(ps => ps.Endpoint).IsRequired().HasMaxLength(2048);
entity.Property(ps => ps.P256dh).IsRequired().HasMaxLength(512);
entity.Property(ps => ps.Auth).IsRequired().HasMaxLength(256);
entity.HasOne(ps => ps.Casual)
.WithMany()
.HasForeignKey(ps => ps.CasualId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasIndex(ps => new { ps.CasualId, ps.Endpoint }).IsUnique();
});
}
}