Skip to content

Commit

Permalink
Merge branch 'main' into ma/site-settings-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Jun 4, 2024
2 parents 2d3244e + c6d0e6a commit 45d9139
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ public bool TryMapAreaControllerRoute(IEndpointRouteBuilder routes, ControllerAc
pattern = controllerAttribute.Template;
}

var area = descriptor.RouteValues["area"];
var controller = descriptor.ControllerName;
var action = descriptor.ActionName;

routes.MapAreaControllerRoute(
name: name?
.Replace("{area}", area)
.Replace("{controller}", controller)
.Replace("{action}", action) ?? descriptor.DisplayName,
areaName: area,
pattern: $"{_adminUrlPrefix}/{pattern.TrimStart('/').Replace("{action}", action)}",
defaults: new { controller, action }
var (area, controller, action) = RoutingHelper.GetMvcRouteValues(descriptor);

routes.MapControllerRoute(
name: RoutingHelper.ReplaceMvcPlaceholders(name, area, controller, action) ?? descriptor.DisplayName,
pattern: $"{_adminUrlPrefix}/{RoutingHelper.ReplaceMvcPlaceholders(pattern.TrimStart('/'), area, controller, action)}",
defaults: new { area, controller, action }
);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace OrchardCore.Placements.Models
{
public class PlacementsDocument : Document
{
public Dictionary<string, PlacementNode[]> Placements { get; } = new Dictionary<string, PlacementNode[]>(StringComparer.OrdinalIgnoreCase);
public Dictionary<string, PlacementNode[]> Placements { get; init; } = new(StringComparer.OrdinalIgnoreCase);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Fluid.Values;
using Microsoft.Extensions.Options;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Records;
using OrchardCore.Data;
using OrchardCore.Json;
using OrchardCore.Liquid;
Expand Down Expand Up @@ -71,7 +72,32 @@ public async Task<IQueryResults> ExecuteQueryAsync(Query query, IDictionary<stri
IEnumerable<long> documentIds;

using var transaction = await connection.BeginTransactionAsync(_session.Store.Configuration.IsolationLevel);
documentIds = await connection.QueryAsync<long>(rawQuery, parameters, transaction);
var queryResult = await connection.QueryAsync(rawQuery, parameters, transaction);

string column = null;

documentIds = queryResult.Select(row =>
{
var rowDictionary = (IDictionary<string, object>)row;

if (column == null)
{
if (rowDictionary.ContainsKey(nameof(ContentItemIndex.DocumentId)))
{
column = nameof(ContentItemIndex.DocumentId);
}
else
{
column = rowDictionary
.FirstOrDefault(kv => kv.Value is long).Key
?? rowDictionary.First().Key;
}
}

return rowDictionary.TryGetValue(column, out var documentIdObject) && documentIdObject is long documentId
? documentId
: 0;
});

sqlQueryResults.Items = await _session.GetAsync<ContentItem>(documentIds.ToArray());

Expand Down
4 changes: 2 additions & 2 deletions src/OrchardCore.Themes/TheBlogTheme/Recipes/blog.recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -1177,9 +1177,9 @@
{
"Source": "Sql",
"Name": "RecentBlogPosts",
"Template": "SELECT * FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Template": "SELECT DocumentId FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Schema": "[js:base64('ew0KICAgICJ0eXBlIjogIkNvbnRlbnRJdGVtL0Jsb2dQb3N0Ig0KfQ==')]",
"ReturnContentItems": true
"ReturnDocuments": true
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
{
"Source": "Sql",
"Name": "RecentBlogPosts",
"Template": "SELECT * FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Template": "SELECT DocumentId FROM ContentItemIndex WHERE ContentType='BlogPost' ORDER BY CreatedUtc DESC LIMIT 3",
"Schema": "[js:base64('ew0KICAgICJ0eXBlIjogIkNvbnRlbnRJdGVtL0Jsb2dQb3N0Ig0KfQ==')]",
"ReturnContentItems": true
"ReturnDocuments": true
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public class DefaultAreaControllerRouteMapper : IAreaControllerRouteMapper

public bool TryMapAreaControllerRoute(IEndpointRouteBuilder routes, ControllerActionDescriptor descriptor)
{
routes.MapAreaControllerRoute(
var (area, controller, action) = RoutingHelper.GetMvcRouteValues(descriptor);

routes.MapControllerRoute(
name: descriptor.DisplayName,
areaName: descriptor.RouteValues["area"],
pattern: DefaultAreaPattern.Replace("{action}", descriptor.ActionName),
defaults: new { controller = descriptor.ControllerName, action = descriptor.ActionName }
pattern: RoutingHelper.ReplaceMvcPlaceholders(DefaultAreaPattern, area, controller, action),
defaults: new { area, controller, action }
);

return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Routing;

namespace OrchardCore.Mvc.Routing
namespace OrchardCore.Mvc.Routing;

public interface IAreaControllerRouteMapper
{
public interface IAreaControllerRouteMapper
{
int Order { get; }
bool TryMapAreaControllerRoute(IEndpointRouteBuilder routes, ControllerActionDescriptor descriptor);
}
int Order { get; }
bool TryMapAreaControllerRoute(IEndpointRouteBuilder routes, ControllerActionDescriptor descriptor);
}
19 changes: 19 additions & 0 deletions src/OrchardCore/OrchardCore.Mvc.Core/Routing/RoutingHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Mvc.Controllers;

namespace OrchardCore.Mvc.Routing;

public static class RoutingHelper
{
public static string ReplaceMvcPlaceholders(string name, string area, string controller, string action) =>
name?
.Replace("{area}", area)
.Replace("{controller}", controller)
.Replace("{action}", action);

public static (string Area, string Controller, string Action) GetMvcRouteValues(ControllerActionDescriptor descriptor)
{
var area = descriptor.RouteValues["area"];

return (area, descriptor.ControllerName, descriptor.ActionName);
}
}
3 changes: 3 additions & 0 deletions src/docs/reference/modules/Queries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ For Lucene queries with custom object schema, you are limited to elements stored

For SQL queries, you can expose any column where property name is a column alias from the query.

!!! note
When returning documents from a SQL query, make sure your query returns a list of document IDs. This is commonly available in the `DocumentId` column, but check the tables you're querying.

Here is an example of a custom Query from a manually added table in a database :

```sql
Expand Down

0 comments on commit 45d9139

Please sign in to comment.