From 4b943e43d0dc81d9419774dbf64677bc695c9595 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Mon, 1 Jul 2024 15:55:23 -0700 Subject: [PATCH] Fix Queries UI Fixes #16372 --- .../Controllers/AdminController.cs | 42 ++++++++++--------- .../OrchardCore.Queries.Abstractions/Query.cs | 7 ++++ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/AdminController.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/AdminController.cs index bb5f1dbaa2d..560b53c8636 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/AdminController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Controllers/AdminController.cs @@ -25,13 +25,11 @@ public class AdminController : Controller private const string _optionsSearch = "Options.Search"; private readonly IAuthorizationService _authorizationService; - private readonly PagerOptions _pagerOptions; private readonly INotifier _notifier; private readonly IQueryManager _queryManager; private readonly IEnumerable _querySources; private readonly IDisplayManager _displayManager; private readonly IUpdateModelAccessor _updateModelAccessor; - private readonly IShapeFactory _shapeFactory; protected readonly IStringLocalizer S; protected readonly IHtmlLocalizer H; @@ -39,8 +37,6 @@ public class AdminController : Controller public AdminController( IDisplayManager displayManager, IAuthorizationService authorizationService, - IOptions pagerOptions, - IShapeFactory shapeFactory, IStringLocalizer stringLocalizer, IHtmlLocalizer htmlLocalizer, INotifier notifier, @@ -50,27 +46,31 @@ public class AdminController : Controller { _displayManager = displayManager; _authorizationService = authorizationService; - _pagerOptions = pagerOptions.Value; _queryManager = queryManager; _querySources = querySources; _updateModelAccessor = updateModelAccessor; - _shapeFactory = shapeFactory; _notifier = notifier; S = stringLocalizer; H = htmlLocalizer; } - public async Task Index(ContentOptions options, PagerParameters pagerParameters) + public async Task Index( + [FromServices] IOptions pagerOptions, + [FromServices] IShapeFactory shapeFactory, + ContentOptions options, + PagerParameters pagerParameters) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageQueries)) { return Forbid(); } - var pager = new Pager(pagerParameters, _pagerOptions.GetPageSize()); + var pager = new Pager(pagerParameters, pagerOptions.Value.GetPageSize()); - var queries = await _queryManager.ListQueriesAsync(); - queries = queries.OrderBy(x => x.Name); + // Exclude DefaultSource since it'll mean that there is no registered query-source to handle it. + IEnumerable queries = (await _queryManager.ListQueriesAsync()) + .Where(query => query.Source != Query.DefaultSource) + .OrderBy(x => x.Name); if (!string.IsNullOrWhiteSpace(options.Search)) { @@ -94,7 +94,7 @@ public async Task Index(ContentOptions options, PagerParameters p { Queries = [], Options = options, - Pager = await _shapeFactory.PagerAsync(pager, queries.Count(), routeData), + Pager = await shapeFactory.PagerAsync(pager, queries.Count(), routeData), QuerySourceNames = _querySources.Select(x => x.Name).ToList() }; @@ -115,7 +115,8 @@ public async Task Index(ContentOptions options, PagerParameters p return View(model); } - [HttpPost, ActionName(nameof(Index))] + [HttpPost] + [ActionName(nameof(Index))] [FormValueRequired("submit.Filter")] public ActionResult IndexFilterPOST(QueriesIndexViewModel model) => RedirectToAction(nameof(Index), new RouteValueDictionary @@ -146,7 +147,8 @@ public async Task Create(string id) return View(model); } - [HttpPost, ActionName(nameof(Create))] + [HttpPost] + [ActionName(nameof(Create))] public async Task CreatePost(QueriesCreateViewModel model) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageQueries)) @@ -171,7 +173,7 @@ public async Task CreatePost(QueriesCreateViewModel model) return RedirectToAction(nameof(Index)); } - // If we got this far, something failed, redisplay form + // If we got this far, something failed, redisplay form. model.Editor = editor; return View(model); @@ -186,7 +188,7 @@ public async Task Edit(string id) var query = await _queryManager.GetQueryAsync(id); - if (query == null) + if (query == null || query.Source == Query.DefaultSource) { return NotFound(); } @@ -202,7 +204,8 @@ public async Task Edit(string id) return View(model); } - [HttpPost, ActionName(nameof(Edit))] + [HttpPost] + [ActionName(nameof(Edit))] public async Task EditPost(QueriesEditViewModel model) { if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageQueries)) @@ -212,7 +215,7 @@ public async Task EditPost(QueriesEditViewModel model) var query = await _queryManager.LoadQueryAsync(model.Name); - if (query == null) + if (query == null || query.Source == Query.DefaultSource) { return NotFound(); } @@ -243,7 +246,7 @@ public async Task Delete(string id) var query = await _queryManager.LoadQueryAsync(id); - if (query == null) + if (query == null || query.Source == Query.DefaultSource) { return NotFound(); } @@ -255,7 +258,8 @@ public async Task Delete(string id) return RedirectToAction(nameof(Index)); } - [HttpPost, ActionName(nameof(Index))] + [HttpPost] + [ActionName(nameof(Index))] [FormValueRequired("submit.BulkAction")] public async Task IndexPost(ContentOptions options, IEnumerable itemIds) { diff --git a/src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs b/src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs index 0d3e59062be..f817e284570 100644 --- a/src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs +++ b/src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs @@ -5,6 +5,8 @@ namespace OrchardCore.Queries /// public class Query { + public const string DefaultSource = "Unknown"; + /// /// Initializes a new instance of a . /// @@ -14,6 +16,11 @@ protected Query(string source) Source = source; } + public Query() : this(DefaultSource) + { + + } + /// /// Gets or sets the technical name of the query. ///