dynamic query support.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PoweredSoft.CQRS.DynamicQuery.Abstractions;
|
||||
using PoweredSoft.DynamicQuery.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.CQRS.DynamicQuery.AspNetCore.Mvc
|
||||
{
|
||||
[ApiController, Route("api/query/[controller]")]
|
||||
public class DynamicQueryController<TUnderlyingQuery, TSource, TDestination> : Controller
|
||||
where TSource : class
|
||||
where TDestination : class
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<IQueryExecutionResult<TDestination>> HandleAsync(
|
||||
[FromBody] DynamicQuery<TSource, TDestination> query,
|
||||
[FromServices]PoweredSoft.CQRS.Abstractions.IQueryHandler<IDynamicQuery<TSource, TDestination>, IQueryExecutionResult<TDestination>> queryHandler
|
||||
)
|
||||
{
|
||||
var result = await queryHandler.HandleAsync(query, HttpContext.RequestAborted);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[ApiController, Route("api/query/[controller]")]
|
||||
public class DynamicQueryController<TUnderlyingQuery, TSource, TDestination, TParams> : Controller
|
||||
where TSource : class
|
||||
where TDestination : class
|
||||
where TParams : class
|
||||
{
|
||||
[HttpPost]
|
||||
public async Task<IQueryExecutionResult<TDestination>> HandleAsync(
|
||||
[FromBody] DynamicQuery<TSource, TDestination, TParams> query,
|
||||
[FromServices] PoweredSoft.CQRS.Abstractions.IQueryHandler<IDynamicQuery<TSource, TDestination>, IQueryExecutionResult<TDestination>> queryHandler
|
||||
)
|
||||
{
|
||||
var result = await queryHandler.HandleAsync(query, HttpContext.RequestAborted);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PoweredSoft.CQRS.Abstractions.Discovery;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace PoweredSoft.CQRS.DynamicQuery.AspNetCore.Mvc
|
||||
{
|
||||
public class DynamicQueryControllerConvention : IControllerModelConvention
|
||||
{
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public DynamicQueryControllerConvention(IServiceProvider serviceProvider)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void Apply(ControllerModel controller)
|
||||
{
|
||||
if (controller.ControllerType.IsGenericType && controller.ControllerType.Name.Contains("DynamicQueryController") && controller.ControllerType.Assembly == typeof(DynamicQueryControllerConvention).Assembly)
|
||||
{
|
||||
var genericType = controller.ControllerType.GenericTypeArguments[0];
|
||||
var queryDiscovery = this.serviceProvider.GetRequiredService<IQueryDiscovery>();
|
||||
var query = queryDiscovery.FindQuery(genericType);
|
||||
controller.ControllerName = query.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PoweredSoft.CQRS.Abstractions.Discovery;
|
||||
using PoweredSoft.CQRS.AspNetCore.Abstractions.Attributes;
|
||||
using PoweredSoft.CQRS.DynamicQuery.Discover;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace PoweredSoft.CQRS.DynamicQuery.AspNetCore.Mvc
|
||||
{
|
||||
public class DynamicQueryControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
|
||||
{
|
||||
private readonly ServiceProvider serviceProvider;
|
||||
|
||||
public DynamicQueryControllerFeatureProvider(ServiceProvider serviceProvider)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
|
||||
{
|
||||
var queryDiscovery = this.serviceProvider.GetRequiredService<IQueryDiscovery>();
|
||||
foreach (var f in queryDiscovery.GetQueries())
|
||||
{
|
||||
var ignoreAttribute = f.QueryType.GetCustomAttribute<QueryControllerIgnoreAttribute>();
|
||||
if (ignoreAttribute != null)
|
||||
continue;
|
||||
|
||||
if (f.Category != "DynamicQuery")
|
||||
continue;
|
||||
|
||||
if (f is DynamicQueryMeta dynamicQueryMeta)
|
||||
{
|
||||
if (dynamicQueryMeta.ParamsType == null)
|
||||
{
|
||||
var controllerType = typeof(DynamicQueryController<,,>).MakeGenericType(f.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType);
|
||||
var controllerTypeInfo = controllerType.GetTypeInfo();
|
||||
feature.Controllers.Add(controllerTypeInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
var controllerType = typeof(DynamicQueryController<,,,>).MakeGenericType(f.QueryType, dynamicQueryMeta.SourceType, dynamicQueryMeta.DestinationType, dynamicQueryMeta.ParamsType);
|
||||
var controllerTypeInfo = controllerType.GetTypeInfo();
|
||||
feature.Controllers.Add(controllerTypeInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace PoweredSoft.CQRS.DynamicQuery.AspNetCore.Mvc
|
||||
{
|
||||
public class DynamicQueryControllerOptions
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user