/// <summary> /// Executes the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public SelectWidgetViewModel Execute(GetRecentWidgetAndWidgetCategoryRequest request) { IEnumerable <WidgetCategoryViewModel> categoriesFuture; // If re-loading fake category if (request.CategoryId.HasValue && request.CategoryId == default(Guid)) { categoriesFuture = null; } else { var categoriesQuery = Repository.AsQueryable <CategoryEntity>().Where(c => !c.IsDeleted && !c.CategoryTree.IsDeleted); if (request.CategoryId.HasValue) { categoriesQuery = categoriesQuery.Where(c => c.Id == request.CategoryId.Value); } categoriesFuture = categoriesQuery .OrderBy(c => c.Name) .Select(c => new WidgetCategoryViewModel { CategoryId = c.Id, CategoryName = c.Name }) .ToFuture(); } // Load list of contents var widgetsQuery = Repository.AsQueryable <Root.Models.Widget>() .Where(f => !f.IsDeleted && (f.Original == null || !f.Original.IsDeleted) && (f.Status == ContentStatus.Published || f.Status == ContentStatus.Draft)); var childContentsQuery = UnitOfWork.Session.Query <ChildContent>(); var pageContentsQuery = UnitOfWork.Session.Query <PageContent>(); var pageContentRecentWidgetsFuture = widgetsQuery.Where(t => pageContentsQuery.Any(z => z.Content.Id == t.Id)) .Select(t => new RecentWidget { Widget = t, UsedOn = pageContentsQuery.Where(z => z.Content.Id == t.Id).Max(z => z.ModifiedOn) }) .OrderByDescending(t => t.UsedOn).Take(6).ToFuture(); var childContentRecentWidgetsFuture = widgetsQuery.Where(t => childContentsQuery.Any(z => z.Child.Id == t.Id)) .Select(t => new RecentWidget { Widget = t, UsedOn = childContentsQuery.Where(z => z.Child.Id == t.Id).Max(z => z.ModifiedOn) }) .OrderByDescending(t => t.UsedOn).Take(6).ToFuture(); if (request.CategoryId.HasValue) { if (request.CategoryId.Value.HasDefaultValue()) { widgetsQuery = widgetsQuery.Where(c => c.Categories == null); } else { widgetsQuery = widgetsQuery.Where(wc => wc.Categories.Any(c => c.Id == request.CategoryId.Value)); } } if (!string.IsNullOrWhiteSpace(request.Filter)) { var filter = request.Filter.ToLowerInvariant(); widgetsQuery = widgetsQuery.Where(c => c.Name.ToLower().Contains(filter) || c.Categories.Any(a => a.Category.Name.ToLower().Contains(filter))); } // Load all widgets var contentEntities = widgetsQuery.OrderBy(f => f.Name).ToFuture().ToList(); var pageContentRecentWidgets = pageContentRecentWidgetsFuture.ToList(); var childContentRecentWidgets = childContentRecentWidgetsFuture.ToList(); // Load drafts for published widgets var ids = contentEntities.Where(c => c.Status == ContentStatus.Published).Select(c => c.Id).ToArray(); List <Root.Models.Widget> drafts; if (ids.Length > 0) { drafts = Repository .AsQueryable <Root.Models.Widget>() .Where(c => ids.Contains(c.Original.Id) && c.Status == ContentStatus.Draft && !c.IsDeleted) .Fetch(c => c.Categories) .ToList(); } else { drafts = new List <Root.Models.Widget>(); } // Map to view models var contents = contentEntities.Select(f => CreateWidgetViewModel(f, drafts.FirstOrDefault(d => d.Original.Id == f.Id))).ToList(); var recentWidgets = LeaveTheMostRecentWidgets(pageContentRecentWidgets, childContentRecentWidgets) .Select(f => CreateWidgetViewModel(f.Widget, drafts.FirstOrDefault(d => d.Original.Id == f.Widget.Id))).ToList(); // Remove duplicates, when draft and published versions are found contents = contents.GroupBy(g => g.Id).Select(g => g.First()).ToList(); List <WidgetCategoryViewModel> categories; // Load list of categories and move contents to categories. if (categoriesFuture != null) { categories = categoriesFuture.ToList(); } else { categories = new List <WidgetCategoryViewModel>(); } categories.ForEach(c => c.Widgets = contents.Where(x => x.Categories.Any(cat => Guid.Parse(cat.Key) == c.CategoryId)).Distinct().ToList()); // Move uncategorized contents to fake category var uncategorized = contents.Where(c => c.Categories == null || c.Categories.IsEmpty()).Distinct().ToList(); // Workaround for deleted categories: uncategorized = contents.Where(c => (c.Categories == null || c.Categories.IsEmpty()) && !categories.Any(x => c.Categories.Any(cat => Guid.Parse(cat.Key) == x.CategoryId))).Concat(uncategorized).Distinct().ToList(); if (uncategorized.Any()) { var category = new WidgetCategoryViewModel { CategoryName = PagesGlobalization.AddPageContent_WidgetTab_UncategorizedWidget_Title, Widgets = uncategorized }; categories.Add(category); } // Remove empty categories categories = categories.Where(c => c.Widgets.Any()).ToList(); return(new SelectWidgetViewModel { WidgetCategories = categories, RecentWidgets = recentWidgets }); }
/// <summary> /// Executes the specified request. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public GetWidgetCategoryResponse Execute(GetWidgetCategoryRequest request) { IEnumerable <WidgetCategoryViewModel> categoriesFuture; // If re-loading fake category if (request.CategoryId.HasValue && request.CategoryId == default(Guid)) { categoriesFuture = null; } else { var categoriesQuery = Repository.AsQueryable <CategoryEntity>().Where(c => !c.IsDeleted && !c.CategoryTree.IsDeleted); if (request.CategoryId.HasValue) { categoriesQuery = categoriesQuery.Where(c => c.Id == request.CategoryId.Value); } categoriesFuture = categoriesQuery .OrderBy(c => c.Name) .Select(c => new WidgetCategoryViewModel { CategoryId = c.Id, CategoryName = c.Name }) .ToFuture(); } // Load list of contents var widgetsQuery = Repository.AsQueryable <Root.Models.Widget>().Where(f => !f.IsDeleted && f.Original == null && (f.Status == ContentStatus.Published || f.Status == ContentStatus.Draft)); if (request.CategoryId.HasValue) { if (request.CategoryId.Value.HasDefaultValue()) { widgetsQuery = widgetsQuery.Where(c => c.Categories == null); } else { widgetsQuery = widgetsQuery.Where(wc => wc.Categories.Any(c => c.Id == request.CategoryId.Value)); } } if (!string.IsNullOrWhiteSpace(request.Filter)) { widgetsQuery = widgetsQuery.Where(c => c.Name.ToLower().Contains(request.Filter.ToLowerInvariant())); } // Load all widgets var contentEntities = widgetsQuery.OrderBy(f => f.Name).ToFuture().ToList(); // Load drafts for published widgets var ids = contentEntities.Where(c => c.Status == ContentStatus.Published).Select(c => c.Id).ToArray(); List <Root.Models.Widget> drafts; if (ids.Length > 0) { drafts = Repository .AsQueryable <Root.Models.Widget>() .Where(c => ids.Contains(c.Original.Id) && c.Status == ContentStatus.Draft && !c.IsDeleted) .Fetch(c => c.Categories) .ToList(); } else { drafts = new List <Root.Models.Widget>(); } // Map to view models var contents = contentEntities.Select(f => CreateWidgetViewModel(f, drafts.FirstOrDefault(d => d.Original.Id == f.Id))).ToList(); List <WidgetCategoryViewModel> categories; // Load list of categories and move contents to categories. if (categoriesFuture != null) { categories = categoriesFuture.ToList(); } else { categories = new List <WidgetCategoryViewModel>(); } categories.ForEach(c => c.Widgets = contents.Where(x => x.Categories.Any(cat => Guid.Parse(cat.Key) == c.CategoryId)).Distinct().ToList()); // Move uncategorized contents to fake category var uncategorized = contents.Where(c => c.Categories == null || c.Categories.IsEmpty()).Distinct().ToList(); // Workaround for deleted categories: uncategorized = contents.Where(c => (c.Categories == null || c.Categories.IsEmpty()) && !categories.Any(x => c.Categories.Any(cat => Guid.Parse(cat.Key) == x.CategoryId))).Concat(uncategorized).Distinct().ToList(); if (uncategorized.Any()) { var category = new WidgetCategoryViewModel { CategoryName = PagesGlobalization.AddPageContent_WidgetTab_UncategorizedWidget_Title, Widgets = uncategorized }; categories.Add(category); } // Remove empty categories categories = categories.Where(c => c.Widgets.Any()).ToList(); return(new GetWidgetCategoryResponse { WidgetCategories = categories }); }