Files
mixtape/zero.Backoffice/ZeroBackofficeControllerModelConvention.cs
T

41 lines
1.4 KiB
C#
Raw Normal View History

2021-12-03 14:45:49 +01:00
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
using zero.Api.Controllers;
using zero.Api.Filters;
2021-11-23 15:43:21 +01:00
2021-11-30 15:35:50 +01:00
namespace zero.Backoffice;
2021-11-23 15:43:21 +01:00
2021-12-03 14:45:49 +01:00
public class ZeroBackofficeControllerModelConvention : ZeroApiControllerModelConvention
2021-11-23 15:43:21 +01:00
{
2021-12-03 14:45:49 +01:00
readonly AttributeRouteModel AppAwareRouteModel;
readonly AttributeRouteModel AppUnawareRouteModel;
2021-11-23 15:43:21 +01:00
2021-11-30 15:35:50 +01:00
readonly Type BaseClass = typeof(ZeroBackofficeController);
2021-11-23 15:43:21 +01:00
2021-12-03 14:45:49 +01:00
readonly bool RuntimeIsAppAware = false;
2021-11-23 15:43:21 +01:00
2021-12-03 14:45:49 +01:00
2021-12-09 14:18:38 +01:00
public ZeroBackofficeControllerModelConvention(string zeroPath, string backofficeApiPath = "api/backoffice", bool isAppAware = false) : base(zeroPath, backofficeApiPath, isAppAware)
2021-11-23 15:43:21 +01:00
{
2021-12-03 14:45:49 +01:00
RuntimeIsAppAware = isAppAware;
AppAwareRouteModel = BuildRouteModel(zeroPath, backofficeApiPath, true);
AppUnawareRouteModel = BuildRouteModel(zeroPath, backofficeApiPath, false);
2021-11-23 15:43:21 +01:00
}
/// <summary>
/// Configure routing model for all backoffice controllers
/// </summary>
2021-12-03 14:45:49 +01:00
public override void Apply(ControllerModel controller)
2021-11-23 15:43:21 +01:00
{
2021-12-03 14:45:49 +01:00
bool hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
2021-11-23 15:43:21 +01:00
2021-12-03 14:45:49 +01:00
if (controller.ControllerType.IsSubclassOf(BaseClass))
{
bool isAppAware = RuntimeIsAppAware && controller.ControllerType.GetCustomAttribute<ZeroSystemApiAttribute>() == null;
controller.Selectors[0].AttributeRouteModel = isAppAware ? AppAwareRouteModel : AppUnawareRouteModel;
2021-11-23 15:43:21 +01:00
}
}
}