protected virtual async Task <AutorouteDocument> CreateDocumentAsync() { var indexes = await Session.QueryIndex <AutoroutePartIndex>(i => i.Published).ListAsync(); var entries = indexes.Select(i => new AutorouteEntry(i.ContentItemId, i.Path, i.ContainedContentItemId, i.JsonPath)); var document = new AutorouteDocument(); AddEntries(document, entries); return(document); }
private static void RemoveEntries(AutorouteDocument document, IEnumerable <AutorouteEntry> entries) { foreach (var entry in entries) { // Evict all entries related to a container item from autoroute entries. var entriesToRemove = document.Paths.Values.Where(x => x.ContentItemId == entry.ContentItemId && !String.IsNullOrEmpty(x.ContainedContentItemId)); foreach (var entryToRemove in entriesToRemove) { document.Paths.Remove(entryToRemove.ContainedContentItemId); document.ContentItemIds.Remove(entryToRemove.Path); } document.Paths.Remove(entry.ContentItemId); document.ContentItemIds.Remove(entry.Path); } }
private static void AddEntries(AutorouteDocument document, IEnumerable <AutorouteEntry> entries) { // Evict all entries related to a container item from autoroute entries. // This is necessary to account for deletions, disabling of an item, or disabling routing of contained items. foreach (var entry in entries.Where(x => String.IsNullOrEmpty(x.ContainedContentItemId))) { var entriesToRemove = document.Paths.Values.Where(x => x.ContentItemId == entry.ContentItemId && !String.IsNullOrEmpty(x.ContainedContentItemId)); foreach (var entryToRemove in entriesToRemove) { document.Paths.Remove(entryToRemove.ContainedContentItemId); document.ContentItemIds.Remove(entryToRemove.Path); } } foreach (var entry in entries) { if (document.Paths.TryGetValue(entry.ContentItemId, out var previousContainerEntry) && String.IsNullOrEmpty(entry.ContainedContentItemId)) { document.ContentItemIds.Remove(previousContainerEntry.Path); } if (!String.IsNullOrEmpty(entry.ContainedContentItemId) && document.Paths.TryGetValue(entry.ContainedContentItemId, out var previousContainedEntry)) { document.ContentItemIds.Remove(previousContainedEntry.Path); } document.ContentItemIds[entry.Path] = entry; if (!String.IsNullOrEmpty(entry.ContainedContentItemId)) { document.Paths[entry.ContainedContentItemId] = entry; } else { document.Paths[entry.ContentItemId] = entry; } } }