/// <summary> /// Genere les nouveaux controllers des ressources /// </summary> public void PopulateControllerRessource(ControllerFeature feature) { IEnumerable <Type> ressourcesToController = this.Assembly.GetExportedTypes().Where(x => x.GetCustomAttribute <ControllerJsonApiAttribute>() != null); foreach (Type typeRessource in ressourcesToController) { if (this._controllersGeneric.Any(c => c.BaseType.GetPrimaryRessourceType() == typeRessource)) { continue; } ControllerJsonApiAttribute controllerAttribute = typeRessource.GetCustomAttribute <ControllerJsonApiAttribute>(); feature.Controllers.Add(new ControllerMaker(typeRessource, controllerAttribute.ReadOnly).MakeController()); this.PopulateControllerRelationShip(feature, typeRessource); } }
public void Apply(ControllerModel controller) { if (controller.ControllerType.IsGenericType) { // Le parametre généric de la ressource est en premiere position Type primaryRessourceType = controller.ControllerType.GetPrimaryRessourceType(); // Route à appliquer au controller string route; // Cas d'un Controlleur simple if (!controller.ControllerType.IsControllerRelation()) { ControllerJsonApiAttribute controllerAttribute = primaryRessourceType.GetCustomAttribute <ControllerJsonApiAttribute>(); route = controllerAttribute.Route; // Cas d'un Controlleur de relation } else { Type secondaryRessourceType = controller.ControllerType.GetSecondaryRessourceType(); ControllerJsonApiAttribute controllerAttribute = secondaryRessourceType.GetCustomAttribute <ControllerJsonApiAttribute>(); PropertyInfo propertyRelation = secondaryRessourceType.GetProperties().FirstOrDefault(p => p.GetPropertyTypeSample() == primaryRessourceType); if (controllerAttribute == null || propertyRelation == null) { throw new NotImplementedException(); } route = controllerAttribute.Route + "/{id}/" + AttributeHandling.GetLabelProperty(propertyRelation); } controller.Selectors.Clear(); controller.Selectors.Add(new SelectorModel { AttributeRouteModel = new AttributeRouteModel(new RouteAttribute(route)) }); } }