-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68790b9
commit a543137
Showing
35 changed files
with
241 additions
and
753 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Modules/Grand.Module.Api/Attributes/EnableQueryAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Linq.Dynamic.Core; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Grand.Module.Api.Attributes; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public class EnableQueryAttribute : ActionFilterAttribute | ||
{ | ||
public override void OnActionExecuted(ActionExecutedContext context) | ||
{ | ||
if (context.Result is not ObjectResult result || result.Value == null) | ||
return; | ||
|
||
if (result.Value is IQueryable queryable) | ||
{ | ||
queryable = ApplyQueryOptions(queryable, context.HttpContext.Request.Query, context.HttpContext.Response); | ||
result.Value = queryable; | ||
} | ||
} | ||
|
||
private IQueryable ApplyQueryOptions(IQueryable queryable, IQueryCollection query, HttpResponse response) | ||
{ | ||
if (query.TryGetValue("$filter", out var filter)) | ||
queryable = queryable.Where(filter.ToString()); | ||
|
||
if (query.TryGetValue("$orderby", out var orderBy)) | ||
queryable = queryable.OrderBy(orderBy.ToString()); | ||
|
||
if (query.TryGetValue("$select", out var select)) | ||
queryable = queryable.Select($"new({select})"); | ||
|
||
if (query.TryGetValue("$skip", out var skipValue) && int.TryParse(skipValue, out var skip)) | ||
queryable = queryable.Skip(skip); | ||
|
||
if (query.TryGetValue("$top", out var topValue) && int.TryParse(topValue, out var top)) | ||
queryable = queryable.Take(top); | ||
|
||
if (query.TryGetValue("$count", out var countValue) && bool.TryParse(countValue, out var includeCount) && includeCount) | ||
{ | ||
var totalCount = queryable.Count(); | ||
response?.Headers?.Append("X-Total-Count", totalCount.ToString()); | ||
} | ||
|
||
return queryable; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...nfrastructure/ModelValidationAttribute.cs → ...pi/Attributes/ModelValidationAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletions
10
...le.Api/Controllers/BaseODataController.cs → ...dule.Api/Controllers/BaseApiController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.