public async Task <IActionResult> Index(PagerParameters pagerParameters) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageTemplates)) { return(Unauthorized()); } var siteSettings = await _siteService.GetSiteSettingsAsync(); var pager = new Pager(pagerParameters, siteSettings.PageSize); var templatesDocument = await _templatesManager.GetTemplatesDocumentAsync(); var count = templatesDocument.Templates.Count; var templates = templatesDocument.Templates.OrderBy(x => x.Key) .Skip(pager.GetStartIndex()) .Take(pager.PageSize); var pagerShape = New.Pager(pager).TotalItemCount(count); var model = new TemplateIndexViewModel { Templates = templates.Select(x => new TemplateEntry { Name = x.Key, Template = x.Value }).ToList(), Pager = pagerShape }; return(View(model)); }
public async Task <IActionResult> Index(ContentOptions options, PagerParameters pagerParameters) { if (!options.AdminTemplates && !await _authorizationService.AuthorizeAsync(User, Permissions.ManageTemplates)) { return(Forbid()); } if (options.AdminTemplates && !await _authorizationService.AuthorizeAsync(User, AdminTemplatesPermissions.ManageAdminTemplates)) { return(Forbid()); } var siteSettings = await _siteService.GetSiteSettingsAsync(); var pager = new Pager(pagerParameters, siteSettings.PageSize); var templatesDocument = options.AdminTemplates ? await _adminTemplatesManager.GetTemplatesDocumentAsync() : await _templatesManager.GetTemplatesDocumentAsync() ; var templates = templatesDocument.Templates.ToList(); if (!string.IsNullOrWhiteSpace(options.Search)) { templates = templates.Where(x => x.Key.Contains(options.Search, StringComparison.OrdinalIgnoreCase)).ToList(); } var count = templates.Count; templates = templates.OrderBy(x => x.Key) .Skip(pager.GetStartIndex()) .Take(pager.PageSize).ToList(); var pagerShape = (await New.Pager(pager)).TotalItemCount(count); var model = new TemplateIndexViewModel { Templates = templates.Select(x => new TemplateEntry { Name = x.Key, Template = x.Value }).ToList(), Options = options, Pager = pagerShape }; model.Options.ContentsBulkAction = new List <SelectListItem>() { new SelectListItem() { Text = S["Delete"], Value = nameof(ContentsBulkAction.Remove) } }; return(View("Index", model)); }
public async Task <ActionResult> Index(string author, bool?showTemplates, string selectedAuthor, string selectedLanguage, int?selectedCategoryId, string search) { ApplicationUser user = null; string selectedAuthor2 = selectedAuthor; if (User.Identity.IsAuthenticated) { user = await db.Users.FirstOrDefaultAsync(u => u.UserName == User.Identity.Name); if (Request.HttpMethod == "POST") { AntiForgery.Validate(); // Update user. if (!string.IsNullOrEmpty(author) && showTemplates != null && (user.Author != author || user.ShowTemplates != showTemplates)) { // Fix selectedAuthor if same as author which is being modified. if (selectedAuthor2 == user.Author) { selectedAuthor2 = author; } // Update db. user.Author = author; user.ShowTemplates = showTemplates.GetValueOrDefault(); await db.SaveChangesAsync(); } } else { // On GET, initialize selectedUser to the current user. On POST, user // could have changed it. selectedAuthor2 = user.Author; } } // Then do search. search = search?.Trim(); // base query IQueryable <MvvmTemplate> templates; if (user == null) { templates = from t in db.MvvmTemplates where t.Enabled && t.ApplicationUser.ShowTemplates select t; } else { // If logged in, also show all templates for the current user no matter // the user's ShowTemplates flag or Enabled flags on the templates. templates = from t in db.MvvmTemplates where t.ApplicationUserId == user.Id || user.UserName == Secrets.AdminUserName || (t.Enabled && t.ApplicationUser.ShowTemplates) select t; } // add author condition if (!string.IsNullOrEmpty(selectedAuthor2)) { templates = templates.Where(t => t.ApplicationUser.Author == selectedAuthor2); } // add language condition if (!string.IsNullOrEmpty(selectedLanguage)) { templates = templates.Where(t => t.Language == selectedLanguage); } // add category condition if (selectedCategoryId != null) { templates = templates.Where(t => t.MvvmTemplateCategoryId == selectedCategoryId); } // add search text condition if (!string.IsNullOrWhiteSpace(search)) { templates = templates.Where( t => t.Name.ToLower().Contains(search.ToLower()) || t.View.ToLower().Contains(search.ToLower()) || t.ViewModel.ToLower().Contains(search)); } // Leave off view and view model text fields since they won't be needed on the client. var query = templates.Select(t => new Template { Author = t.ApplicationUser.Author, Name = t.Name, Id = t.Id, Category = db.MvvmTemplateCategories.FirstOrDefault(c => c.Id == t.MvvmTemplateCategoryId).Name, Language = t.Language, Enabled = t.Enabled }); string curUserName = user?.UserName; var authorsQuery = from u in db.Users where (u.ShowTemplates && u.MvvmTemplates.Any(t => t.Enabled)) || (curUserName != null && u.UserName == curUserName) || (string.IsNullOrEmpty(selectedAuthor2) && u.Author == selectedAuthor2) select u; var authorsList = await authorsQuery.ToListAsync(); // Generate model. var model = new TemplateIndexViewModel( user?.Author, user != null && user.ShowTemplates, await query.ToListAsync(), authorsList, selectedAuthor2, selectedCategoryId.GetValueOrDefault(), await db.MvvmTemplateCategories.ToListAsync(), string.IsNullOrWhiteSpace(selectedLanguage) ? null : selectedLanguage, string.IsNullOrWhiteSpace(search) ? null : search); return(View(model)); }
public ActionResult IndexFilterPOST(TemplateIndexViewModel model) { return(RedirectToAction(nameof(Index), new RouteValueDictionary { { "Options.Search", model.Options.Search } })); }