/// <summary> /// Verify there is a UI template/album mapping for the root album in the current gallery, creating them /// if necessary. /// </summary> /// <param name="rootAlbum">The root album.</param> private static void ConfigureUiTemplateAlbumTable(AlbumDto rootAlbum) { using (var repoUiTmpl = new UiTemplateRepository()) { using (var repoUiTmplA = new UiTemplateAlbumRepository(repoUiTmpl.Context)) { // Make sure each template category has at least one template assigned to the root album. // We do this with a union of two queries: // 1. For categories where there is at least one album assignment, determine if at least one of // those assignments is the root album. // 2. Find categories without any albums at all (this is necessary because the SelectMany() in the first // query won't return any categories that don't have related records in the template/album table). var dtos = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId) .SelectMany(t => t.TemplateAlbums, (t, tt) => new { t.TemplateType, tt.FKAlbumId }) .GroupBy(t => t.TemplateType) .Where(t => t.All(ta => ta.FKAlbumId != rootAlbum.AlbumId)) .Select(t => t.Key) .Union(repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId).GroupBy(t => t.TemplateType).Where(t => t.All(t2 => !t2.TemplateAlbums.Any())).Select(t => t.Key)) ; foreach (var dto in dtos) { // We have a template type without a root album. Find the default template and assign that one. var dto1 = dto; repoUiTmplA.Add(new UiTemplateAlbumDto() { FKUiTemplateId = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId && t.TemplateType == dto1 && t.Name.Equals("default", StringComparison.OrdinalIgnoreCase)).First().UiTemplateId, FKAlbumId = rootAlbum.AlbumId }); } repoUiTmplA.Save(); } } }
/// <summary> /// Verify there is a UI template/album mapping for the root album in the current gallery, creating them /// if necessary. /// </summary> /// <param name="rootAlbum">The root album.</param> private static void ConfigureUiTemplateAlbumTable(AlbumDto rootAlbum) { using (var repoUiTmpl = new UiTemplateRepository()) { using (var repoUiTmplA = new UiTemplateAlbumRepository(repoUiTmpl.Context)) { // Make sure each template category has at least one template assigned to the root album. // We do this with a union of two queries: // 1. For categories where there is at least one album assignment, determine if at least one of // those assignments is the root album. // 2. Find categories without any albums at all (this is necessary because the SelectMany() in the first // query won't return any categories that don't have related records in the template/album table). var dtos = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId) .SelectMany(t => t.TemplateAlbums, (t, tt) => new { t.TemplateType, tt.FKAlbumId }) .GroupBy(t => t.TemplateType) .Where(t => t.All(ta => ta.FKAlbumId != rootAlbum.AlbumId)) .Select(t => t.Key) .Union(repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId).GroupBy(t => t.TemplateType).Where(t => t.All(t2 => !t2.TemplateAlbums.Any())).Select(t => t.Key)) ; foreach (var dto in dtos) { // We have a template type without a root album. Find the default template and assign that one. var dto1 = dto; repoUiTmplA.Add(new UiTemplateAlbumDto() { FKUiTemplateId = repoUiTmpl.Where(t => t.FKGalleryId == rootAlbum.FKGalleryId && t.TemplateType == dto1 && t.Name.Equals("default", StringComparison.OrdinalIgnoreCase)).First().UiTemplateId, FKAlbumId = rootAlbum.AlbumId }); } repoUiTmplA.Save(); } } }