Skip to content

Commit

Permalink
Fix Queries UI
Browse files Browse the repository at this point in the history
Fixes #16372
  • Loading branch information
MikeAlhayek committed Jul 1, 2024
1 parent b95eb7c commit 4b943e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@ 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<IQuerySource> _querySources;
private readonly IDisplayManager<Query> _displayManager;
private readonly IUpdateModelAccessor _updateModelAccessor;
private readonly IShapeFactory _shapeFactory;

protected readonly IStringLocalizer S;
protected readonly IHtmlLocalizer H;

public AdminController(
IDisplayManager<Query> displayManager,
IAuthorizationService authorizationService,
IOptions<PagerOptions> pagerOptions,
IShapeFactory shapeFactory,
IStringLocalizer<AdminController> stringLocalizer,
IHtmlLocalizer<AdminController> htmlLocalizer,
INotifier notifier,
Expand All @@ -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<IActionResult> Index(ContentOptions options, PagerParameters pagerParameters)
public async Task<IActionResult> Index(
[FromServices] IOptions<PagerOptions> 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<Query> queries = (await _queryManager.ListQueriesAsync())
.Where(query => query.Source != Query.DefaultSource)
.OrderBy(x => x.Name);

if (!string.IsNullOrWhiteSpace(options.Search))
{
Expand All @@ -94,7 +94,7 @@ public async Task<IActionResult> 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()
};

Expand All @@ -115,7 +115,8 @@ public async Task<IActionResult> 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
Expand Down Expand Up @@ -146,7 +147,8 @@ public async Task<IActionResult> Create(string id)
return View(model);
}

[HttpPost, ActionName(nameof(Create))]
[HttpPost]
[ActionName(nameof(Create))]
public async Task<IActionResult> CreatePost(QueriesCreateViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageQueries))
Expand All @@ -171,7 +173,7 @@ public async Task<IActionResult> 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);
Expand All @@ -186,7 +188,7 @@ public async Task<IActionResult> Edit(string id)

var query = await _queryManager.GetQueryAsync(id);

if (query == null)
if (query == null || query.Source == Query.DefaultSource)
{
return NotFound();
}
Expand All @@ -202,7 +204,8 @@ public async Task<IActionResult> Edit(string id)
return View(model);
}

[HttpPost, ActionName(nameof(Edit))]
[HttpPost]
[ActionName(nameof(Edit))]
public async Task<IActionResult> EditPost(QueriesEditViewModel model)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageQueries))
Expand All @@ -212,7 +215,7 @@ public async Task<IActionResult> EditPost(QueriesEditViewModel model)

var query = await _queryManager.LoadQueryAsync(model.Name);

if (query == null)
if (query == null || query.Source == Query.DefaultSource)
{
return NotFound();
}
Expand Down Expand Up @@ -243,7 +246,7 @@ public async Task<IActionResult> Delete(string id)

var query = await _queryManager.LoadQueryAsync(id);

if (query == null)
if (query == null || query.Source == Query.DefaultSource)
{
return NotFound();
}
Expand All @@ -255,7 +258,8 @@ public async Task<IActionResult> Delete(string id)
return RedirectToAction(nameof(Index));
}

[HttpPost, ActionName(nameof(Index))]
[HttpPost]
[ActionName(nameof(Index))]
[FormValueRequired("submit.BulkAction")]
public async Task<ActionResult> IndexPost(ContentOptions options, IEnumerable<string> itemIds)
{
Expand Down
7 changes: 7 additions & 0 deletions src/OrchardCore/OrchardCore.Queries.Abstractions/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace OrchardCore.Queries
/// </summary>
public class Query
{
public const string DefaultSource = "Unknown";

/// <summary>
/// Initializes a new instance of a <see cref="Query"/>.
/// </summary>
Expand All @@ -14,6 +16,11 @@ protected Query(string source)
Source = source;
}

public Query() : this(DefaultSource)
{

}

/// <summary>
/// Gets or sets the technical name of the query.
/// </summary>
Expand Down

0 comments on commit 4b943e4

Please sign in to comment.