Пример #1
0
        /// <summary>
        ///  Récupere la liste des champs donnés dans la Query String pour un type de modele
        ///  Si des "include" sont demandés, les champs clés des relations sont ajoutés également dans la liste des champs
        /// </summary>
        public override List <string> GetListeFields(Type type, string relationPath)
        {
            IEnumerable <PropertyInfo> relationshipProperties =
                AttributeRelationsHandling.GetRelationshipProperties(type)
                .Where(p => Utils.IsRelationInInclude(relationPath + "." + AttributeHandling.GetLabelProperty(p.Item1), this._includes))
                .Select(p => p.Item1);

            return(this.GetListeFieldsFromRelations(type, relationshipProperties));
        }
Пример #2
0
        /// <summary>
        /// Récupere la liste des champs donnés dans la Query String pour un type de modele
        /// </summary>
        /// <returns></returns>
        public virtual List <string> GetListeFields(Type type)
        {
            List <string> fields = this.GetListeFields(AttributeHandling.GetLabelProperty(type));

            if (fields != null)
            {
                //Récupere les vrais noms des propriétés
                List <PropertyInfo> fieldsModel = AttributeHandling.GetListeProperties(type, fields).ToList();
                fields = fieldsModel.Select(f => f.Name).ToList();
            }

            return(fields);
        }
Пример #3
0
        /// <summary>
        /// Parcours les relations et effectue pour chacune d'elle une requete de récupération. Les résultats trouvés correspondant sont affectés au relations.
        /// </summary>
        /// <param name="depth">Niveau de profondeur de relation transmise récursivement</param>
        public void BuildRelation(int depth = 0, string baseRelationPath = null)
        {
            if (depth >= 5)
            {
                return;
            }

            if (!this._informationRessource.HaveType)
            {
                return;
            }

            if (baseRelationPath != null)
            {
                baseRelationPath += ".";
            }

            // Parcours des relations
            foreach (Tuple <PropertyInfo, RelationshipJsonApiAttribute> relation in AttributeRelationsHandling.GetRelationshipProperties(this._informationRessource.TypeRessource))
            {
                string relationPath = baseRelationPath + AttributeHandling.GetLabelProperty(relation.Item1);

                if (!this._queryService.IsInclude(relationPath))
                {
                    continue;
                }

                //Instantiation de la classe RelationsPropertyEngine avec son type de ressource
                Type typeEngine = typeof(RelationsPropertyEngine <>).MakeGenericType(relation.Item1.GetPropertyTypeSample());
                Activator.CreateInstance(typeEngine, new object[] {
                    this._serviceProvider,
                    this._queryService,
                    this._informationRessource,
                    relation.Item1,
                    relation.Item2,
                    relationPath,
                    depth
                });
            }
        }
Пример #4
0
        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))
                });
            }
        }
Пример #5
0
        private T GetJsonApi <T>(JToken source) where T : new()
        {
            var model = new T();

            if (source == null)
            {
                return(model);
            }

            try {
                var type    = new JProperty("type", AttributeHandling.GetLabelProperty(new T().GetType()));
                var attr    = new JProperty("attributes", source);
                var jobject = new JObject {
                    type,
                    attr
                };

                return((T) new JsonApiReader(jobject.ToString(), model).GetModel(true));
            } catch (JsonApiException ex) {
                ex.Error.GetErrors().ForEach(e => this._jsonapi.Error.Create(e));

                throw new ArgumentException($"Query invalid|Element in query string is not in expected format");
            }
        }
Пример #6
0
 /// <summary>
 /// Définit si un type de modele est présent dans le parametre "include" de la Query String
 /// </summary>
 public bool IsInclude <T>() => this.IsInclude(AttributeHandling.GetLabelProperty(typeof(T)));
Пример #7
0
        /// <summary>
        /// Récuperer l'instance de la classe de critéres à utiliser pour le datasource
        /// </summary>
        /// <returns></returns>
        private object GetCriteres()
        {
            Type       typeCriteres = this._foreignKeyAttribute.Criteres ?? AttributeRelationsHandling.GetCriteresType(typeof(T));
            MethodInfo filterMethod = this._queryService.GetType().GetMethod(nameof(IQueryService.FilterOnInclude)).MakeGenericMethod(typeCriteres);
            object     criteres     = filterMethod.Invoke(this._queryService, new object[] { AttributeHandling.GetLabelProperty(this._relationProperty) });

            if (this._foreignKeyAttribute.Property == null)
            {
                if (this._typeRelation == TypeRelation.Many)
                {
                    (criteres as ICriteresBase).ListeId = this.GetListeIdentifiantMany().ConvertListObjectToListInt();
                }
                else
                {
                    (criteres as ICriteresBase).ListeId = this.GetListeIdentifiant().ConvertListObjectToListInt();
                }

                (criteres as ICriteresBase).SkipTake = null;
            }
            else
            {
                typeCriteres.GetProperty(this._foreignKeyAttribute.Property).SetValue(criteres, this.GetListeIdentifiant());
            }

            return(criteres);
        }