static async Task <(int Count, string Message, Meeting?Entity)> AddNew(ModulesDbContext dbContext, Meeting entity) { dbContext.Add(entity); var result = await dbContext.SaveChangesAsync(); return(result.SaveResult(entity)); }
static async Task <(int Count, string Message, Module?Entity)> AddOrUpdate(ModulesDbContext dbContext, Module entity, ModuleOwnershipRef ownerRef) { var existing = await dbContext.Modules .Include(m => m.ModuleExits) .FirstOrDefaultAsync(m => m.Id == entity.Id); return((existing is null) ? await AddNew(dbContext, entity, ownerRef) : await UpdateExisting(dbContext, entity, existing)); }
static async Task <(int Count, string Message, Module?Entity)> AddNew(ModulesDbContext dbContext, Module entity, ModuleOwnershipRef ownerRef) { if (entity.ModuleOwnerships.Count == 0) { entity.ModuleOwnerships.Add(CreateModuleOwnership(ownerRef)); } dbContext.Add(entity); var result = await dbContext.SaveChangesAsync(); return(result.SaveResult(entity));
static async Task <bool> IfStationWillHaveNoReferringModules(ModulesDbContext dbContext, Module entity, Module existing) { if (existing.StationId.HasValue) { if (entity.StationId is null || entity.StationId != existing.StationId) { return(!await dbContext.Modules.AnyAsync(m => m.Id != existing.Id && m.StationId == existing.StationId)); } } return(false); }
private static async Task <User?> FindByObjectIdAsync(ModulesDbContext dbContext, string?objectId) { if (string.IsNullOrWhiteSpace(objectId)) { return(null); } if (Guid.TryParse(objectId, out var objectGuid)) { return(await dbContext.Users.Where(u => u.ObjectId == objectGuid).Include(u => u.Person).ThenInclude(p => p.Country).SingleOrDefaultAsync()); } return(null); }
static async Task <(int Count, string Message, Meeting?Entity)> AddOrUpdate(ModulesDbContext dbContext, Meeting entity) { var existing = await dbContext.Meetings .Include(m => m.Layouts).ThenInclude(l => l.ResponsibleGroup) .Include(m => m.Layouts).ThenInclude(ms => ms.PrimaryModuleStandard) .Include(m => m.OrganiserGroup).ThenInclude(ag => ag.Country) .SingleOrDefaultAsync(m => m.Id == entity.Id) .ConfigureAwait(false); return((existing is null) ? await AddNew(dbContext, entity).ConfigureAwait(false) : await UpdateExisting(dbContext, entity, existing).ConfigureAwait(false)); }
static async Task <bool> IsSameNameAlreadyExisting(ModulesDbContext dbContext, Module module, ModuleOwnershipRef ownerRef) { var existing = await dbContext.Modules.Where(m => m.Id != module.Id && m.ModuleOwnerships.Any(mo => mo.PersonId == ownerRef.PersonId || mo.GroupId == ownerRef.GroupId)).ToListAsync(); if (existing is null) { return(false); } return(existing.Any(m => m.FullName.Equals(module.FullName, StringComparison.OrdinalIgnoreCase) && (m.ConfigurationLabel is null || m.ConfigurationLabel.Equals(module.ConfigurationLabel, StringComparison.OrdinalIgnoreCase)))); }
static async Task AddLayoutStationAsync(ModulesDbContext dbContext, int participantId, int layoutId, Module module) { if (module.StationId.HasValue) { var existing = await dbContext.LayoutStations.SingleOrDefaultAsync(ls => ls.LayoutId == layoutId && ls.StationId == module.StationId); if (existing is null) { var addedStation = new LayoutStation { LayoutId = layoutId, StationId = module.StationId.Value }; dbContext.LayoutStations.Add(addedStation); } } }
private static async Task <bool> IsGroupOrDataAdministrator(ModulesDbContext dbContext, ClaimsPrincipal?principal, ModuleOwnershipRef ownerRef) { if (principal.IsGlobalAdministrator()) { return(true); } if (principal.IsCountryAdministrator()) { if (await dbContext.Groups.AnyAsync(g => g.Id == ownerRef.GroupId && g.CountryId == principal.CountryId())) { return(true); } } return(await dbContext.GroupMembers.AsNoTracking() .AnyAsync(gm => gm.GroupId == ownerRef.GroupId && gm.PersonId == principal.PersonId() && (gm.IsDataAdministrator || gm.IsGroupAdministrator))); }
internal static bool IsMemberInGroupsInSameDomain(ModulesDbContext dbContext, ClaimsPrincipal?principal, ModuleOwnershipRef ownershipRef) { if (principal.IsAuthenticated() && ownershipRef.IsPerson) { if (principal.PersonId() == ownershipRef.PersonId) { return(false); } var memberships = dbContext.GroupMembers.AsNoTracking().Include(gm => gm.Group) .Where(gm => gm.Group.GroupDomainId > 0 && (gm.PersonId == ownershipRef.PersonId || gm.PersonId == principal.PersonId())) .AsEnumerable() .GroupBy(gm => gm.Group.GroupDomainId) .ToList(); return(memberships.Any(m => m.Count(gm => gm.PersonId == ownershipRef.PersonId || gm.PersonId == principal.PersonId()) > 1)); } return(false); }
static void AddOrRemoveLayouts(ModulesDbContext dbContext, Meeting entity, Meeting existing) { foreach (var layout in entity.Layouts) { var existingGable = existing.Layouts.AsQueryable().FirstOrDefault(g => g.Id == layout.Id); if (existingGable is null) { existing.Layouts.Add(layout); } else { dbContext.Entry(existingGable).CurrentValues.SetValues(layout); } } foreach (var gable in existing.Layouts) { if (!entity.Layouts.Any(mg => mg.Id == gable.Id)) { dbContext.Remove(gable); } } }
static void AddOrRemoveExits(ModulesDbContext dbContext, Module entity, Module existing) { foreach (var exit in entity.ModuleExits) { var existingExit = existing.ModuleExits.AsQueryable().FirstOrDefault(g => g.Id == exit.Id); if (existingExit is null) { existing.ModuleExits.Add(exit); } else { dbContext.Entry(existingExit).CurrentValues.SetValues(exit); } } foreach (var exit in existing.ModuleExits) { if (!entity.ModuleExits.Any(mg => mg.Id == exit.Id)) { dbContext.Remove(exit); } } }
static bool IsUnchanged(ModulesDbContext dbContext, Module entity) => dbContext.Entry(entity).State == EntityState.Unchanged && entity.ModuleExits.All(mg => dbContext.Entry(mg).State == EntityState.Unchanged) && entity.ModuleOwnerships.All(mo => dbContext.Entry(mo).State == EntityState.Unchanged);
static bool IsUnchanged(ModulesDbContext dbContext, Meeting entity) => dbContext.Entry(entity).State == EntityState.Unchanged && entity.Layouts.All(mg => dbContext.Entry(mg).State == EntityState.Unchanged);
static async Task <(int Count, string Message, Meeting?Entity)> UpdateExisting(ModulesDbContext dbContext, Meeting entity, Meeting existing) { dbContext.Entry(existing).CurrentValues.SetValues(entity); AddOrRemoveLayouts(dbContext, entity, existing); if (IsUnchanged(dbContext, existing)) { return((-1).SaveResult(existing)); } var result = await dbContext.SaveChangesAsync(); return(result.SaveResult(existing)); }
static async Task <(int Count, string Message, Meeting?Entity)> UpdateExisting(ModulesDbContext dbContext, Meeting entity, Meeting existing) { if (IsMovedTooFarInTime(existing, entity)) { return(0, "MeetingDatesCannotBeChanged", entity); } dbContext.Entry(existing).CurrentValues.SetValues(entity); AddOrRemoveLayouts(dbContext, entity, existing); if (IsUnchanged(dbContext, existing)) { return((-1).SaveResult(existing)); } var result = await dbContext.SaveChangesAsync().ConfigureAwait(false); return(result.SaveResult(existing)); }
static async Task <(int Count, string Message, Module?Entity)> UpdateExisting(ModulesDbContext dbContext, Module entity, Module existing) { if (await IfStationWillHaveNoReferringModules(dbContext, entity, existing)) { return(Strings.StationMustHaveAtLeastOneModuleReferringToIt.SaveResult(entity)); } dbContext.Entry(existing).CurrentValues.SetValues(entity); AddOrRemoveExits(dbContext, entity, existing); if (IsUnchanged(dbContext, existing)) { return((-1).SaveResult(existing)); } var result = await dbContext.SaveChangesAsync(); return(result.SaveResult(existing));