public async Task <RuinSiteWithObeliskData> GetForSiteEditor(int siteId) { var siteTask = RuinsContext .RuinSite .AsNoTracking() .Include(rs => rs.Body.System) .SingleAsync(s => s.Id == siteId); var obeliskGroupTask = RuinsContext .RuinSite .AsNoTracking() .Where(rs => rs.Id == siteId) .SelectMany(rs => rs.Ruintype.ObeliskGroup) .ToListAsync().ConfigureAwait(false); var obeliskTasks = RuinsContext .RuinSite .AsNoTracking() .Where(rs => rs.Id == siteId) .SelectMany(rs => rs.Ruintype.ObeliskGroup) .SelectMany(og => og.Obelisk) .ToListAsync().ConfigureAwait(false); var availableObeliskGroupsTask = RuinsContext .RuinsiteObeliskgroups .AsNoTracking() .Where(rsao => rsao.RuinsiteId == siteId) .Select(rsao => rsao.ObeliskgroupId) .ToListAsync().ConfigureAwait(false); var activeObeliskTask = RuinsContext .RuinsiteActiveobelisks .AsNoTracking() .Where(rsao => rsao.RuinsiteId == siteId) .Select(rsao => rsao.ObeliskId) .ToListAsync().ConfigureAwait(false); var result = new RuinSiteWithObeliskData(await siteTask) { Body = null, // prevent cyclic reference in serializable data, and we don't need the actual body instance here }; // prevent cyclic reference in data: var availableGroups = new HashSet <int>(await availableObeliskGroupsTask); result.ObeliskGroups.AddRange((await obeliskGroupTask).Select(og => new ObeliskGroupWithActiveState(og) { Active = availableGroups.Contains(og.Id) })); var activeObelisks = new HashSet <int>(await activeObeliskTask); result.Obelisks.AddRange((await obeliskTasks).Select(o => new ObeliskWithActiveState(o) { Active = activeObelisks.Contains(o.Id) })); return(result); }
public async Task <RuinSiteWithObeliskData> SaveFromEditor(RuinSiteWithObeliskData data) { // need to serialize.. sadly await CreateOrUpdateById(data.Id, data); await SaveObeliskGroupsForSite(data.Id, data.ObeliskGroups.Where(og => og.Active)); var activeGroups = data.ObeliskGroups.Select(g => g.Id).ToImmutableHashSet(); await SaveActiveObelisksForSite(data.Id, data.Obelisks.Where(o => o.Active && activeGroups.Contains(o.ObeliskgroupId))); return(await GetForSiteEditor(data.Id)); }