| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Channels; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using MUNityCore.Models.User; |
| | 9 | | using MUNityCore.Models; |
| | 10 | | using Microsoft.AspNetCore.Identity.EntityFrameworkCore; |
| | 11 | | using Microsoft.AspNetCore.Identity; |
| | 12 | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | 13 | | using MUNity.Database.General; |
| | 14 | | using MUNity.Database.Models.User; |
| | 15 | | using MUNity.Database.Models.Conference; |
| | 16 | | using MUNity.Database.Models.Organization; |
| | 17 | | using MUNity.Database.Models.Conference.Roles; |
| | 18 | | using MUNity.Database.Models.Simulation; |
| | 19 | | using MUNity.Database.Models.LoS; |
| | 20 | | using MUNity.Database.Models; |
| | 21 | | using MUNity.Database.Models.Resolution; |
| | 22 | | using MUNity.Database.Models.Session; |
| | 23 | | using MUNity.Database.Interfaces; |
| | 24 | | using MUNity.Database.Models.Website; |
| | 25 | | using MUNity.Database.Models.General; |
| | 26 | | using MUNity.Database.FluentAPI; |
| | 27 | | using System.Diagnostics; |
| | 28 | |
|
| | 29 | | namespace MUNity.Database.Context |
| | 30 | | { |
| | 31 | | public class MunityContext |
| | 32 | | : IdentityDbContext<MunityUser, |
| | 33 | | MunityRole, |
| | 34 | | string, |
| | 35 | | IdentityUserClaim<string>, |
| | 36 | | MunityUserRole, |
| | 37 | | IdentityUserLogin<string>, |
| | 38 | | IdentityRoleClaim<string>, |
| | 39 | | IdentityUserToken<string>> |
| | 40 | | { |
| | 41 | |
|
| 95 | 42 | | public FluentProvider Fluent { get; private set; } |
| | 43 | |
|
| 910 | 44 | | public DbSet<Country> Countries { get; set; } |
| | 45 | |
|
| 33 | 46 | | public DbSet<CountryNameTranslation> CountryNameTranslations { get; set; } |
| | 47 | |
|
| 94 | 48 | | public DbSet<Organization> Organizations { get; set; } |
| | 49 | |
|
| 34 | 50 | | public DbSet<OrganizationRole> OrganizationRoles { get; set; } |
| | 51 | |
|
| 36 | 52 | | public DbSet<OrganizationMember> OrganizationMembers { get; set; } |
| | 53 | |
|
| 51 | 54 | | public DbSet<Project> Projects { get; set; } |
| | 55 | |
|
| 75 | 56 | | public DbSet<Committee> Committees { get; set; } |
| | 57 | |
|
| 138 | 58 | | public DbSet<Delegation> Delegations { get; set; } |
| | 59 | |
|
| 404 | 60 | | public DbSet<ConferenceDelegateRole> Delegates { get; set; } |
| | 61 | |
|
| 40 | 62 | | public DbSet<ConferenceTeamRole> ConferenceTeamRoles { get; set; } |
| | 63 | |
|
| 28 | 64 | | public DbSet<ConferenceSecretaryGeneralRole> SecretaryGenerals { get; set; } |
| | 65 | |
|
| 115 | 66 | | public DbSet<Conference> Conferences { get; set; } |
| | 67 | |
|
| 28 | 68 | | public DbSet<ConferenceParticipationCostRule> ConferenceParticipationCostRules { get; set; } |
| | 69 | |
|
| 30 | 70 | | public DbSet<CommitteeTopic> CommitteeTopics { get; set; } |
| | 71 | |
|
| 26 | 72 | | public DbSet<AttendanceState> AttendanceStates { get; set; } |
| | 73 | |
|
| 26 | 74 | | public DbSet<AttendanceCheck> AttendanceChecks { get; set; } |
| | 75 | |
|
| 28 | 76 | | public DbSet<CommitteeSession> CommitteeSessions { get; set; } |
| | 77 | |
|
| 35 | 78 | | public DbSet<Participation> Participations { get; set; } |
| | 79 | |
|
| 50 | 80 | | public DbSet<ConferenceRoleAuth> ConferenceRoleAuthorizations { get; set; } |
| | 81 | |
|
| 28 | 82 | | public DbSet<RoleApplication> RoleApplications { get; set; } |
| | 83 | |
|
| 31 | 84 | | public DbSet<DelegationApplication> DelegationApplications { get; set; } |
| | 85 | |
|
| 27 | 86 | | public DbSet<DelegationApplicationUserEntry> DelegationApplicationUserEntries { get; set; } |
| | 87 | |
|
| 26 | 88 | | public DbSet<DelegationApplicationPickedDelegation> DelegationApplicationPickedDelegations { get; set; } |
| | 89 | |
|
| 25 | 90 | | public DbSet<ConferenceDelegationApplicationFieldInput> ConferenceDelegationApplicationFieldInputs { get; set; } |
| | 91 | |
|
| 25 | 92 | | public DbSet<FillApplication> FillApplications { get; set; } |
| | 93 | |
|
| 25 | 94 | | public DbSet<ConferenceFillApplicationFieldInput> ConferenceFillApplicationFieldInputs { get; set; } |
| | 95 | |
|
| 32 | 96 | | public DbSet<TeamRoleGroup> TeamRoleGroups { get; set; } |
| | 97 | |
|
| 28 | 98 | | public DbSet<ResolutionAuth> ResolutionAuths { get; set; } |
| | 99 | |
|
| 26 | 100 | | public DbSet<ResolutionUser> ResolutionUsers { get; set; } |
| | 101 | |
|
| | 102 | | //public DbSet<Simulation> Simulations { get; set; } |
| | 103 | |
|
| | 104 | | //public DbSet<SimulationRole> SimulationRoles { get; set; } |
| | 105 | |
|
| | 106 | | //public DbSet<SimulationUser> SimulationUser { get; set; } |
| | 107 | |
|
| | 108 | | //public DbSet<SimulationVoting> SimulationVotings { get; set; } |
| | 109 | |
|
| | 110 | | //public DbSet<SimulationVotingSlot> VotingSlots { get; set; } |
| | 111 | |
|
| | 112 | | //public DbSet<SimulationLog> SimulationLog { get; set; } |
| | 113 | |
|
| | 114 | | //public DbSet<SimulationInvite> SimulationInvites { get; set; } |
| | 115 | |
|
| | 116 | | //public DbSet<AgendaItem> AgendaItems { get; set; } |
| | 117 | |
|
| | 118 | | //public DbSet<PetitionType> PetitionTypes { get; set; } |
| | 119 | |
|
| | 120 | | //public DbSet<Petition> Petitions { get; set; } |
| | 121 | |
|
| | 122 | | //public DbSet<PetitionTypeSimulation> SimulationPetitionTypes { get; set; } |
| | 123 | |
|
| | 124 | | //public DbSet<SimulationStatus> SimulationStatuses { get; set; } |
| | 125 | |
|
| | 126 | | //public DbSet<SimulationPresents> PresentChecks { get; set; } |
| | 127 | |
|
| | 128 | | //public DbSet<PresentsState> PresentStates { get; set; } |
| | 129 | |
|
| 26 | 130 | | public DbSet<ListOfSpeakers> ListOfSpeakers { get; set; } |
| | 131 | |
|
| 26 | 132 | | public DbSet<Speaker> Speakers { get; set; } |
| | 133 | |
|
| 26 | 134 | | public DbSet<ListOfSpeakersLog> ListOfSpeakersLogs { get; set; } |
| | 135 | |
|
| 28 | 136 | | public DbSet<MunitySetting> Settings { get; set; } |
| | 137 | |
|
| 57 | 138 | | public DbSet<ResaElement> Resolutions { get; set; } |
| | 139 | |
|
| 37 | 140 | | public DbSet<ResaPreambleParagraph> PreambleParagraphs { get; set; } |
| | 141 | |
|
| 68 | 142 | | public DbSet<ResaOperativeParagraph> OperativeParagraphs { get; set; } |
| | 143 | |
|
| 26 | 144 | | public DbSet<ResaSupporter> ResolutionSupporters { get; set; } |
| | 145 | |
|
| 29 | 146 | | public DbSet<ResaDeleteAmendment> ResolutionDeleteAmendments { get; set; } |
| 28 | 147 | | public DbSet<ResaChangeAmendment> ResolutionChangeAmendments { get; set; } |
| 28 | 148 | | public DbSet<ResaMoveAmendment> ResolutionMoveAmendments { get; set; } |
| 26 | 149 | | public DbSet<ResaAddAmendment> ResolutionAddAmendments { get; set; } |
| | 150 | |
|
| 31 | 151 | | public DbSet<ConferenceWebPage> ConferenceWebPages { get; set; } |
| | 152 | |
|
| 27 | 153 | | public DbSet<ConferenceApplicationOptions> ConferenceApplicationOptions { get; set; } |
| | 154 | |
|
| 28 | 155 | | public DbSet<ConferenceApplicationFormula> ConferenceApplicationFormulas { get; set; } |
| 26 | 156 | | public DbSet<ConferenceApplicationField> ConferenceApplicationFields { get; set; } |
| | 157 | |
|
| 25 | 158 | | public DbSet<ConferencePageColorScheme> ConferencePageColorSchemes { get; set; } |
| | 159 | |
|
| | 160 | |
|
| 25 | 161 | | public DbSet<UserNotification> UserNotifications { get; set; } |
| | 162 | |
|
| 25 | 163 | | public DbSet<UserNotificationCategory> UserNotificationCategories { get; set; } |
| | 164 | |
|
| 25 | 165 | | public DbSet<School> Schools { get; set; } |
| | 166 | |
|
| 25 | 167 | | public DbSet<UserInSchool> UsersInSchools { get; set; } |
| | 168 | |
|
| 26 | 169 | | public DbSet<ConferenceDashboardCard> ConferenceDashboardCards { get; set; } |
| | 170 | |
|
| 29 | 171 | | public DbSet<ConferenceWebMenuEntry> ConferenceWebMenuEntries { get; set; } |
| | 172 | |
|
| 25 | 173 | | public DbSet<CommitteeDelegatesElement> ConferenceWebPageDelegatesElements { get; set; } |
| | 174 | |
|
| 25 | 175 | | public DbSet<CommitteeTopicsElement> ConferenceWebPageCommitteeTopicsElements { get; set; } |
| | 176 | |
|
| 25 | 177 | | public DbSet<TeamCardsElement> ConferenceWebPageTeamCardsElements { get; set; } |
| | 178 | |
|
| 25 | 179 | | public DbSet<WebPageTextElement> ConferenceWebPageTextElements { get; set; } |
| | 180 | |
|
| 25 | 181 | | public DbSet<ConferenceRegisterElement> ConferenceWebPageRegisterElements { get; set; } |
| | 182 | |
|
| 25 | 183 | | public DbSet<ConferenceWebsiteFooter> ConferenceWebsiteFooters { get; set; } |
| | 184 | |
|
| 25 | 185 | | public DbSet<UserFriend> UserFriends { get; set; } |
| | 186 | |
|
| 25 | 187 | | public DbSet<UserBlocked> UserBlockedUsers { get; set; } |
| | 188 | |
|
| | 189 | | protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 8 | 190 | | { |
| 8 | 191 | | base.OnModelCreating(modelBuilder); |
| | 192 | |
|
| 8 | 193 | | modelBuilder.Entity<Country>().HasMany(n => n.Translations) |
| 8 | 194 | | .WithOne(n => n.Country) |
| 8 | 195 | | .OnDelete(DeleteBehavior.Cascade); |
| | 196 | |
|
| | 197 | |
|
| | 198 | |
|
| 8 | 199 | | modelBuilder.Entity<CountryNameTranslation>() |
| 8 | 200 | | .HasKey(n => new {n.CountryId, n.LanguageCode}); |
| | 201 | |
|
| 8 | 202 | | modelBuilder.Entity<MunityUser>().HasKey(n => n.Id); |
| | 203 | |
|
| 8 | 204 | | modelBuilder.Entity<MunityUser>().HasMany(n => n.CreatedResolutions).WithOne(a => a.CreationUser).IsRequired |
| | 205 | |
|
| 8 | 206 | | modelBuilder.Entity<OrganizationRole>() |
| 8 | 207 | | .HasOne(n => n.Organization) |
| 8 | 208 | | .WithMany(a => a.Roles); |
| | 209 | |
|
| 8 | 210 | | modelBuilder.Entity<OrganizationMember>() |
| 8 | 211 | | .HasOne(n => n.Organization) |
| 8 | 212 | | .WithMany(n => n.Member); |
| | 213 | |
|
| 8 | 214 | | modelBuilder.Entity<OrganizationRole>() |
| 8 | 215 | | .HasMany(n => n.MembersWithRole) |
| 8 | 216 | | .WithOne(n => n.Role); |
| | 217 | |
|
| 8 | 218 | | modelBuilder.Entity<Project>() |
| 8 | 219 | | .HasOne(n => n.ProjectOrganization) |
| 8 | 220 | | .WithMany(n => n.Projects) |
| 8 | 221 | | .IsRequired(); |
| | 222 | |
|
| | 223 | |
|
| | 224 | | // Conference |
| 8 | 225 | | modelBuilder.Entity<Conference>().HasOne(n => n.ConferenceProject) |
| 8 | 226 | | .WithMany(n => n.Conferences); |
| | 227 | |
|
| | 228 | |
|
| 8 | 229 | | modelBuilder.Entity<AbstractConferenceRole>().HasOne(n => n.Conference).WithMany(n => n.Roles); |
| | 230 | |
|
| 8 | 231 | | modelBuilder.Entity<AbstractConferenceRole>().HasDiscriminator(n => n.RoleType) |
| 8 | 232 | | .HasValue<ConferenceDelegateRole>("DelegateRole") |
| 8 | 233 | | .HasValue<ConferenceSecretaryGeneralRole>("SecretaryGeneralRole") |
| 8 | 234 | | .HasValue<ConferenceTeamRole>("TeamRole") |
| 8 | 235 | | .HasValue<ConferenceVisitorRole>("VisitorRole"); |
| | 236 | |
|
| 8 | 237 | | modelBuilder.Entity<AbstractConferenceRole>().HasMany(n => n.Participations) |
| 8 | 238 | | .WithOne(n => n.Role); |
| | 239 | |
|
| 8 | 240 | | modelBuilder.Entity<ConferenceTeamRole>().HasOne(n => n.TeamRoleGroup).WithMany(n => n.TeamRoles); |
| | 241 | |
|
| | 242 | |
|
| 8 | 243 | | modelBuilder.Entity<Committee>().HasMany(n => n.Sessions).WithOne(n => |
| 8 | 244 | | n.Committee); |
| | 245 | |
|
| 8 | 246 | | modelBuilder.Entity<Committee>().HasOne(n => n.Conference) |
| 8 | 247 | | .WithMany(a => a.Committees).OnDelete(DeleteBehavior.Cascade); |
| | 248 | |
|
| 8 | 249 | | modelBuilder.Entity<Committee>().HasMany(n => n.Resolutions) |
| 8 | 250 | | .WithOne(a => a.Committee).IsRequired(false); |
| | 251 | |
|
| 8 | 252 | | modelBuilder.Entity<Committee>().HasOne(n => n.ResolutlyCommittee) |
| 8 | 253 | | .WithMany(n => n.ChildCommittees).IsRequired(false); |
| | 254 | |
|
| 8 | 255 | | modelBuilder.Entity<Delegation>() |
| 8 | 256 | | .HasOne(n => n.ParentDelegation) |
| 8 | 257 | | .WithMany(a => a.ChildDelegations) |
| 8 | 258 | | .OnDelete(DeleteBehavior.SetNull); |
| | 259 | |
|
| | 260 | |
|
| | 261 | | //modelBuilder.Entity<SimulationUser>().HasOne(n => n.Simulation).WithMany(n => |
| | 262 | | // n.Users); |
| | 263 | |
|
| 8 | 264 | | modelBuilder.Entity<ListOfSpeakers>().HasMany(n => n.AllSpeakers).WithOne(n => n.ListOfSpeakers); |
| | 265 | |
|
| 8 | 266 | | modelBuilder.Entity<Speaker>().HasKey(n => n.Id); |
| | 267 | |
|
| | 268 | | // Resolution |
| 8 | 269 | | modelBuilder.Entity<ResolutionAuth>() |
| 8 | 270 | | .HasMany(n => n.Users).WithOne(n => n.Auth); |
| | 271 | |
|
| | 272 | |
|
| 8 | 273 | | modelBuilder.Entity<ResolutionAuth>() |
| 8 | 274 | | .HasOne(n => n.Simulation) |
| 8 | 275 | | .WithMany(n => n.Resolutions); |
| | 276 | |
|
| 8 | 277 | | modelBuilder.Entity<ResaElement>() |
| 8 | 278 | | .HasMany(n => n.PreambleParagraphs) |
| 8 | 279 | | .WithOne(n => n.ResaElement) |
| 8 | 280 | | .IsRequired(); |
| | 281 | |
|
| 8 | 282 | | modelBuilder.Entity<ResaElement>() |
| 8 | 283 | | .HasMany(n => n.OperativeParagraphs) |
| 8 | 284 | | .WithOne(n => n.Resolution) |
| 8 | 285 | | .IsRequired(); |
| | 286 | |
|
| 8 | 287 | | modelBuilder.Entity<ResaElement>() |
| 8 | 288 | | .HasOne(n => n.Authorization) |
| 8 | 289 | | .WithOne(n => n.Resolution) |
| 8 | 290 | | .IsRequired() |
| 8 | 291 | | .OnDelete(DeleteBehavior.Cascade); |
| | 292 | |
|
| 8 | 293 | | modelBuilder.Entity<ResaOperativeParagraph>() |
| 8 | 294 | | .HasOne(n => n.Parent) |
| 8 | 295 | | .WithMany(n => n.Children) |
| 8 | 296 | | .IsRequired(false) |
| 8 | 297 | | .OnDelete(DeleteBehavior.Cascade); |
| | 298 | |
|
| 8 | 299 | | modelBuilder.Entity<ResaOperativeParagraph>() |
| 8 | 300 | | .HasMany(n => n.DeleteAmendments) |
| 8 | 301 | | .WithOne(n => n.TargetParagraph) |
| 8 | 302 | | .IsRequired(); |
| | 303 | |
|
| 8 | 304 | | modelBuilder.Entity<ResaOperativeParagraph>() |
| 8 | 305 | | .HasMany(n => n.ChangeAmendments) |
| 8 | 306 | | .WithOne(n => n.TargetParagraph) |
| 8 | 307 | | .IsRequired(); |
| | 308 | |
|
| 8 | 309 | | modelBuilder.Entity<ResaOperativeParagraph>() |
| 8 | 310 | | .HasMany(n => n.MoveAmendments) |
| 8 | 311 | | .WithOne(n => n.SourceParagraph) |
| 8 | 312 | | .IsRequired(); |
| | 313 | |
|
| 8 | 314 | | modelBuilder.Entity<ResaElement>() |
| 8 | 315 | | .HasMany(n => n.AddAmendments) |
| 8 | 316 | | .WithOne(n => n.Resolution); |
| | 317 | |
|
| | 318 | | // Simulations |
| 8 | 319 | | modelBuilder.Entity<SimulationRole>().HasOne(n => n.Simulation).WithMany(n => |
| 8 | 320 | | n.Roles); |
| | 321 | |
|
| 8 | 322 | | modelBuilder.Entity<Simulation>().HasMany(n => n.Users).WithOne(n => n.Simulation); |
| | 323 | |
|
| | 324 | |
|
| 8 | 325 | | modelBuilder.Entity<SimulationPresents>() |
| 8 | 326 | | .HasMany(n => n.CheckedUsers) |
| 8 | 327 | | .WithOne(n => n.SimulationPresents); |
| | 328 | |
|
| 8 | 329 | | modelBuilder.Entity<Simulation>().HasMany(n => n.PresentChecks).WithOne(n => n.Simulation); |
| 8 | 330 | | } |
| | 331 | |
|
| 25 | 332 | | public MunityContext(DbContextOptions<MunityContext> options) : base(options) |
| 25 | 333 | | { |
| 25 | 334 | | this.Fluent = new FluentProvider(this); |
| 25 | 335 | | } |
| | 336 | |
|
| | 337 | | public override int SaveChanges() |
| 149 | 338 | | { |
| 149 | 339 | | ChangeTracker.DetectChanges(); |
| | 340 | |
|
| 51266 | 341 | | var markedAsDeleted = ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted); |
| | 342 | |
|
| 489 | 343 | | foreach (var item in markedAsDeleted) |
| 21 | 344 | | { |
| 21 | 345 | | if (item.Entity is IIsDeleted entity) |
| 3 | 346 | | { |
| | 347 | | // Set the entity to unchanged (if we mark the whole entity as Modified, every field gets sent to Db |
| 3 | 348 | | item.State = EntityState.Unchanged; |
| | 349 | | // Only update the IsDeleted flag - only this will get sent to the Db |
| 3 | 350 | | entity.IsDeleted = true; |
| 3 | 351 | | } |
| 21 | 352 | | } |
| | 353 | |
|
| 149 | 354 | | HandleEasyId(); |
| | 355 | |
|
| | 356 | |
|
| 149 | 357 | | return base.SaveChanges(); |
| 149 | 358 | | } |
| | 359 | |
|
| | 360 | | private void HandleEasyId() |
| 165 | 361 | | { |
| 56344 | 362 | | var markedAsNew = ChangeTracker.Entries().Where(x => x.State == EntityState.Added); |
| 3183 | 363 | | foreach (var entityEntry in markedAsNew) |
| 1344 | 364 | | { |
| 1344 | 365 | | switch (entityEntry.Entity) |
| | 366 | | { |
| | 367 | | case Organization organization: |
| 15 | 368 | | HandleEasyIdOrganization(organization); |
| 15 | 369 | | break; |
| | 370 | | case Project project: |
| 9 | 371 | | HandleEasyIdProject(project); |
| 9 | 372 | | break; |
| | 373 | | case Conference conference: |
| 8 | 374 | | HandleEasyIdConference(conference); |
| 8 | 375 | | break; |
| | 376 | | case Committee committee: |
| 14 | 377 | | HandleEasyIdCommittee(committee); |
| 14 | 378 | | break; |
| | 379 | | case Delegation delegation: |
| 39 | 380 | | HandleEasyDelegationId(delegation); |
| 39 | 381 | | break; |
| | 382 | | } |
| 1344 | 383 | | } |
| 165 | 384 | | } |
| | 385 | |
|
| | 386 | |
|
| | 387 | |
|
| | 388 | | private void HandleEasyIdOrganization(Organization organization) |
| 15 | 389 | | { |
| 16 | 390 | | if (!string.IsNullOrWhiteSpace(organization.OrganizationId)) return; |
| | 391 | |
|
| 14 | 392 | | if (string.IsNullOrWhiteSpace(organization.OrganizationShort)) |
| 4 | 393 | | { |
| 4 | 394 | | organization.OrganizationId = Guid.NewGuid().ToString(); |
| 4 | 395 | | return; |
| | 396 | | } |
| | 397 | |
|
| 10 | 398 | | var easyId = Util.IdGenerator.AsPrimaryKey(organization.OrganizationShort); |
| 10 | 399 | | if (string.IsNullOrWhiteSpace(easyId)) return; |
| | 400 | |
|
| 10 | 401 | | if (Organizations.All(n => n.OrganizationId != easyId)) |
| 10 | 402 | | organization.OrganizationId = easyId; |
| | 403 | | else |
| 0 | 404 | | organization.OrganizationId = Guid.NewGuid().ToString(); |
| | 405 | |
|
| 15 | 406 | | } |
| | 407 | |
|
| | 408 | | private void HandleEasyIdProject(Project project) |
| 9 | 409 | | { |
| 9 | 410 | | if (!string.IsNullOrWhiteSpace(project.ProjectId)) return; |
| | 411 | |
|
| 9 | 412 | | if (string.IsNullOrWhiteSpace(project.ProjectShort)) |
| 1 | 413 | | { |
| 1 | 414 | | project.ProjectId = Guid.NewGuid().ToString(); |
| 1 | 415 | | return; |
| | 416 | | } |
| | 417 | |
|
| 8 | 418 | | var easyId = Util.IdGenerator.AsPrimaryKey(project.ProjectShort); |
| 8 | 419 | | if (string.IsNullOrWhiteSpace(easyId)) return; |
| | 420 | |
|
| 8 | 421 | | if (Projects.All(n => n.ProjectId != easyId)) |
| 8 | 422 | | project.ProjectId = easyId; |
| | 423 | | else |
| 0 | 424 | | project.ProjectId = Guid.NewGuid().ToString(); |
| 9 | 425 | | } |
| | 426 | |
|
| | 427 | | private void HandleEasyIdConference(Conference conference) |
| 8 | 428 | | { |
| 8 | 429 | | if (!string.IsNullOrWhiteSpace(conference.ConferenceId)) return; |
| | 430 | |
|
| 8 | 431 | | if (string.IsNullOrWhiteSpace(conference.ConferenceShort)) |
| 0 | 432 | | { |
| 0 | 433 | | conference.ConferenceId = Guid.NewGuid().ToString(); |
| 0 | 434 | | return; |
| | 435 | | } |
| | 436 | |
|
| 8 | 437 | | var easyId = Util.IdGenerator.AsPrimaryKey(conference.ConferenceShort); |
| 8 | 438 | | if (string.IsNullOrWhiteSpace(easyId)) return; |
| | 439 | |
|
| 8 | 440 | | if (Conferences.All(n => n.ConferenceId != easyId)) |
| 8 | 441 | | conference.ConferenceId = easyId; |
| | 442 | | else |
| 0 | 443 | | conference.ConferenceId = Guid.NewGuid().ToString(); |
| 8 | 444 | | } |
| | 445 | |
|
| | 446 | | private void HandleEasyIdCommittee(Committee committee) |
| 14 | 447 | | { |
| 22 | 448 | | if (!string.IsNullOrWhiteSpace(committee.CommitteeId)) return; |
| | 449 | |
|
| 6 | 450 | | if (committee.Conference == null || string.IsNullOrEmpty(committee.CommitteeShort)) |
| 2 | 451 | | { |
| 2 | 452 | | committee.CommitteeId = Guid.NewGuid().ToString(); |
| 2 | 453 | | return; |
| | 454 | | } |
| | 455 | |
|
| 4 | 456 | | var committeeEasy = Util.IdGenerator.AsPrimaryKey(committee.CommitteeShort); |
| 4 | 457 | | if (string.IsNullOrWhiteSpace(committeeEasy)) return; |
| | 458 | |
|
| 4 | 459 | | var easyCommitteeId = committee.Conference.ConferenceId + "-" + |
| 4 | 460 | | committeeEasy; |
| | 461 | |
|
| 4 | 462 | | if (this.Committees.All(n => n.CommitteeId != easyCommitteeId)) |
| 4 | 463 | | committee.CommitteeId = easyCommitteeId; |
| | 464 | | else |
| 0 | 465 | | committee.CommitteeId = Guid.NewGuid().ToString(); |
| 14 | 466 | | } |
| | 467 | |
|
| | 468 | | private void HandleEasyDelegationId(Delegation delegation) |
| 39 | 469 | | { |
| 39 | 470 | | if (!string.IsNullOrWhiteSpace(delegation.DelegationId)) return; |
| | 471 | |
|
| 39 | 472 | | if (delegation.Conference == null || string.IsNullOrWhiteSpace(delegation.Name)) |
| 0 | 473 | | { |
| 0 | 474 | | delegation.DelegationId = Guid.NewGuid().ToString(); |
| 0 | 475 | | return; |
| | 476 | | } |
| | 477 | |
|
| 39 | 478 | | var easyNameDelegation = Util.IdGenerator.AsPrimaryKey(delegation.Name); |
| 39 | 479 | | var easyId = delegation.Conference.ConferenceId + "-" + easyNameDelegation; |
| 676 | 480 | | if (this.Delegations.All(n => n.DelegationId != easyId) && this.ChangeTracker.Entries<Delegation>().All(n => |
| 39 | 481 | | { |
| 39 | 482 | | Debug.WriteLine($"Assigning EasyId to Delegation {delegation.Name}: {easyId}"); |
| 39 | 483 | | delegation.DelegationId = easyId; |
| | 484 | |
|
| 39 | 485 | | } |
| | 486 | | else |
| 0 | 487 | | delegation.DelegationId = Guid.NewGuid().ToString(); |
| | 488 | |
|
| | 489 | |
|
| 39 | 490 | | } |
| | 491 | |
|
| | 492 | | /// <summary> |
| | 493 | | /// Saves the Database Changes but ignores all elements that are protected by soft-deletion. |
| | 494 | | /// This will completely remove these elements |
| | 495 | | /// </summary> |
| | 496 | | /// <returns></returns> |
| | 497 | | public int SaveChangesWithoutSoftDelete() |
| 1 | 498 | | { |
| 1 | 499 | | return base.SaveChanges(); |
| 1 | 500 | | } |
| | 501 | |
|
| | 502 | | /// <summary> |
| | 503 | | /// Saves the Database Changes but ignores all elements that are protected by soft-deletion. |
| | 504 | | /// This will completely remove these elements |
| | 505 | | /// </summary> |
| | 506 | | /// <returns></returns> |
| | 507 | | public Task<int> SaveChangesWithoutSoftDeleteAsync() |
| 0 | 508 | | { |
| 0 | 509 | | return base.SaveChangesAsync(); |
| 0 | 510 | | } |
| | 511 | |
|
| | 512 | | public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) |
| 16 | 513 | | { |
| 16 | 514 | | ChangeTracker.DetectChanges(); |
| 5078 | 515 | | var markedAsDeleted = ChangeTracker.Entries().Where(x => x.State == EntityState.Deleted); |
| | 516 | |
|
| 50 | 517 | | foreach (var item in markedAsDeleted) |
| 1 | 518 | | { |
| 1 | 519 | | if (item.Entity is IIsDeleted entity) |
| 1 | 520 | | { |
| | 521 | | // Set the entity to unchanged (if we mark the whole entity as Modified, every field gets sent to Db |
| 1 | 522 | | item.State = EntityState.Unchanged; |
| | 523 | | // Only update the IsDeleted flag - only this will get sent to the Db |
| 1 | 524 | | entity.IsDeleted = true; |
| 1 | 525 | | } |
| 1 | 526 | | } |
| | 527 | |
|
| 16 | 528 | | HandleEasyId(); |
| | 529 | |
|
| 16 | 530 | | return base.SaveChangesAsync(cancellationToken); |
| 16 | 531 | | } |
| | 532 | |
|
| | 533 | | public static MunityContext FromSqlLite(string databaseName) |
| 5 | 534 | | { |
| 5 | 535 | | var optionsBuilder = new DbContextOptionsBuilder<MunityContext>(); |
| 5 | 536 | | optionsBuilder.UseSqlite($"Data Source={databaseName}.db"); |
| | 537 | | //optionsBuilder.EnableSensitiveDataLogging(); |
| | 538 | | //optionsBuilder.LogTo(Console.WriteLine); |
| 5 | 539 | | var context = new MunityContext(optionsBuilder.Options); |
| 5 | 540 | | context.Database.EnsureCreated(); |
| 5 | 541 | | return context; |
| 5 | 542 | | } |
| | 543 | |
|
| | 544 | | } |
| | 545 | | } |