2023-11-04 15:24:56 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using OpenHarbor.CQRS.Abstractions.Discovery;
|
|
|
|
|
using OpenHarbor.CQRS.AspNetCore.Abstractions.Attributes;
|
|
|
|
|
|
|
|
|
|
namespace OpenHarbor.CQRS.AspNetCore.Mvc;
|
|
|
|
|
|
2024-12-22 11:59:19 -05:00
|
|
|
|
public class QueryControllerFeatureProvider(ServiceProvider serviceProvider)
|
|
|
|
|
: IApplicationFeatureProvider<ControllerFeature>
|
2023-11-04 15:24:56 -04:00
|
|
|
|
{
|
2024-12-22 11:59:19 -05:00
|
|
|
|
public void PopulateFeature(IEnumerable<ApplicationPart> parts, ControllerFeature feature)
|
2023-11-04 15:24:56 -04:00
|
|
|
|
{
|
2024-12-22 11:59:19 -05:00
|
|
|
|
var queryDiscovery = serviceProvider.GetRequiredService<IQueryDiscovery>();
|
|
|
|
|
foreach (var queryMeta in queryDiscovery.GetQueries())
|
2023-11-04 15:24:56 -04:00
|
|
|
|
{
|
2024-12-22 11:59:19 -05:00
|
|
|
|
var ignoreAttribute = queryMeta.QueryType.GetCustomAttribute<QueryControllerIgnoreAttribute>();
|
2023-11-04 15:24:56 -04:00
|
|
|
|
if (ignoreAttribute != null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-12-22 11:59:19 -05:00
|
|
|
|
if (queryMeta.Category != "BasicQuery")
|
2023-11-04 15:24:56 -04:00
|
|
|
|
continue;
|
|
|
|
|
|
2024-12-22 11:59:19 -05:00
|
|
|
|
var controllerType = typeof(QueryController<,>).MakeGenericType(queryMeta.QueryType, queryMeta.QueryResultType);
|
2023-11-04 15:24:56 -04:00
|
|
|
|
var controllerTypeInfo = controllerType.GetTypeInfo();
|
|
|
|
|
feature.Controllers.Add(controllerTypeInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|