-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryExecutorExtensions.cs
More file actions
34 lines (32 loc) · 1.33 KB
/
QueryExecutorExtensions.cs
File metadata and controls
34 lines (32 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
namespace Infragistics.QueryBuilder.Executor
{
public static class QueryExecutorExtensions
{
public static IServiceCollection AddQueryBuilder<TMyDbContext, TResults>(this IServiceCollection services) where TResults : class
{
services.AddScoped<QueryBuilderService<TMyDbContext, TResults>>();
return services;
}
public static IEndpointRouteBuilder UseQueryBuilder<TMyDbContext, TResults>(this IEndpointRouteBuilder endpoints, string path) where TMyDbContext : class where TResults : class
{
endpoints.MapPost(path, ([FromBody] Query query, QueryBuilderService<TMyDbContext, TResults> queryBuilderService) =>
{
if (query != null)
{
var result = queryBuilderService.RunQuery(query);
return Results.Ok(result);
}
else
{
return Results.BadRequest("Wrong or missing query");
}
}).WithTags(["QueryBuilder"]).Accepts<Query>("application/json").Produces<TResults>();
return endpoints;
}
}
}