public async Task <T> Create(T value) { T entry = CreateEntity(); MapValues(value, entry); DbSet().Add(entry); await RuinsContext.SaveChangesAsync(); return(entry); }
public async Task <bool> SaveActiveObelisksForSite(int siteId, IEnumerable <Obelisk> obelisks) { var currentActiveObelisks = await RuinsContext.RuinsiteActiveobelisks .Where(e => e.RuinsiteId == siteId) .ToListAsync(); var obelisksToDelete = currentActiveObelisks .Where(g => obelisks.All(og => og.Id != g.ObeliskId)); var obelisksToAdd = obelisks .Where(og => currentActiveObelisks.All(g => g.ObeliskId != og.Id)); // first delete all groups foreach (var obelisk in obelisksToDelete) { RuinsContext.RuinsiteActiveobelisks.Remove(obelisk); } // add all groups where we know the id of foreach (var obelisk in obelisksToAdd) { RuinsContext.RuinsiteActiveobelisks.Add(new RuinsiteActiveobelisks() { RuinsiteId = siteId, ObeliskId = obelisk.Id }); } await RuinsContext.SaveChangesAsync(); return(true); }
public async Task <bool> SaveObeliskGroupsForSite(int siteId, IEnumerable <ObeliskGroup> obeliskGroups) { var currentGroups = await RuinsContext.RuinsiteObeliskgroups .Include(e => e.Obeliskgroup) .Where(e => e.RuinsiteId == siteId) .ToListAsync(); var groupsToDelete = currentGroups .Where(g => !obeliskGroups.Any(og => og.Id == g.Obeliskgroup.Id || og.Name == g.Obeliskgroup.Name)); var groupsToAdd = obeliskGroups .Where(og => !currentGroups.Any(g => g.Obeliskgroup.Id == og.Id || g.Obeliskgroup.Name == og.Name)); // first delete all groups foreach (var group in groupsToDelete) { RuinsContext.RuinsiteObeliskgroups.Remove(group); } // add all groups where we know the id of foreach (var group in groupsToAdd.Where(g => g.Id > 0)) { RuinsContext.RuinsiteObeliskgroups.Add(new RuinsiteObeliskgroups() { RuinsiteId = siteId, ObeliskgroupId = group.Id }); } var groupsToAddWithoutId = groupsToAdd.Where(g => g.Id == 0).Select(g => g.Name).ToArray(); if (groupsToAddWithoutId.Length > 0) { // for the groups only given by name (not id), we need to fetch the entities first var allGroups = await RuinsContext.ObeliskGroup .Where(g => g.Ruintype.RuinSite.Any(s => s.Id == siteId)) .ToListAsync(); foreach (var group in allGroups.Where(g => groupsToAddWithoutId.Contains(g.Name))) { RuinsContext.RuinsiteObeliskgroups.Add(new RuinsiteObeliskgroups() { RuinsiteId = siteId, ObeliskgroupId = group.Id }); } } await RuinsContext.SaveChangesAsync(); return(true); }