2021-02-02 01:05:48 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApplicationModels;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using PoweredSoft.CQRS.Abstractions.Discovery;
|
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.CQRS.AspNetCore.Mvc
|
|
|
|
|
{
|
2021-02-02 19:01:29 -05:00
|
|
|
|
public class QueryControllerConvention : IControllerModelConvention
|
2021-02-02 01:05:48 -05:00
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
|
2021-02-02 19:01:29 -05:00
|
|
|
|
public QueryControllerConvention(IServiceProvider serviceProvider)
|
2021-02-02 01:05:48 -05:00
|
|
|
|
{
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Apply(ControllerModel controller)
|
|
|
|
|
{
|
2021-02-02 19:01:29 -05:00
|
|
|
|
if (controller.ControllerType.IsGenericType && controller.ControllerType.Name.Contains("QueryController") && controller.ControllerType.Assembly == typeof(QueryControllerConvention).Assembly)
|
2021-02-02 01:05:48 -05:00
|
|
|
|
{
|
|
|
|
|
var genericType = controller.ControllerType.GenericTypeArguments[0];
|
2021-02-02 19:01:29 -05:00
|
|
|
|
var queryDiscovery = this.serviceProvider.GetRequiredService<IQueryDiscovery>();
|
|
|
|
|
var query = queryDiscovery.FindQuery(genericType);
|
|
|
|
|
controller.ControllerName = query.Name;
|
2021-02-02 01:05:48 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|