2021-02-02 12:19:59 -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 CommandControllerConvention : IControllerModelConvention
|
2021-02-02 12:19:59 -05:00
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
|
2021-02-02 19:01:29 -05:00
|
|
|
|
public CommandControllerConvention(IServiceProvider serviceProvider)
|
2021-02-02 12:19:59 -05:00
|
|
|
|
{
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Apply(ControllerModel controller)
|
|
|
|
|
{
|
2023-10-02 11:25:45 -04:00
|
|
|
|
if (!controller.ControllerType.IsGenericType)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!controller.ControllerType.Name.Contains("CommandController"))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (controller.ControllerType.Assembly != typeof(CommandControllerConvention).Assembly)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var genericType = controller.ControllerType.GenericTypeArguments[0];
|
|
|
|
|
var commandDiscovery = this.serviceProvider.GetRequiredService<ICommandDiscovery>();
|
|
|
|
|
var command = commandDiscovery.FindCommand(genericType);
|
|
|
|
|
controller.ControllerName = command.LowerCamelCaseName;
|
2021-02-02 12:19:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|