Files
mixtape/zero.Backoffice/ZeroBackofficeControllerModelConvention.cs
T
2021-12-09 14:18:38 +01:00

41 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
using zero.Api.Controllers;
using zero.Api.Filters;
namespace zero.Backoffice;
public class ZeroBackofficeControllerModelConvention : ZeroApiControllerModelConvention
{
readonly AttributeRouteModel AppAwareRouteModel;
readonly AttributeRouteModel AppUnawareRouteModel;
readonly Type BaseClass = typeof(ZeroBackofficeController);
readonly bool RuntimeIsAppAware = false;
public ZeroBackofficeControllerModelConvention(string zeroPath, string backofficeApiPath = "api/backoffice", bool isAppAware = false) : base(zeroPath, backofficeApiPath, isAppAware)
{
RuntimeIsAppAware = isAppAware;
AppAwareRouteModel = BuildRouteModel(zeroPath, backofficeApiPath, true);
AppUnawareRouteModel = BuildRouteModel(zeroPath, backofficeApiPath, false);
}
/// <summary>
/// Configure routing model for all backoffice controllers
/// </summary>
public override void Apply(ControllerModel controller)
{
bool hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
if (controller.ControllerType.IsSubclassOf(BaseClass))
{
bool isAppAware = RuntimeIsAppAware && controller.ControllerType.GetCustomAttribute<ZeroSystemApiAttribute>() == null;
controller.Selectors[0].AttributeRouteModel = isAppAware ? AppAwareRouteModel : AppUnawareRouteModel;
}
}
}