Exemple #1
0
        /// <summary>
        /// Bind object property Id
        /// <param name="type">Type object</param>
        /// <param name="modelToBind">Instance object</param>
        /// </summary>
        private void BindId(Type type, object modelToBind, JToken item)
        {
            string id = this.ReadId(item);

            if (id == null)
            {
                return;
            }

            List <PropertyInfo> listeIdProperty = AttributeHandling.GetIdsProperties(type).Where(prop => prop.CanWrite).ToList();

            if (!listeIdProperty.Any())
            {
                throw new NotImplementedJsonApiException($"{type.Name} has not attribute id");
            }

            if (listeIdProperty.Count == 1)
            {
                // Check constraint Id on main Model
                if (modelToBind == this._modelToBind)
                {
                    if (this._constraintId > 0 && id != this._constraintId.ToString())
                    {
                        throw new InvalidPropertyException("id", id, this._constraintId.ToString());
                    }

                    if (this._isPostRequest && id != "0")
                    {
                        throw new InvalidPropertyException("id", id, "empty", "POST Method");
                    }
                }

                this.BindIdValue(listeIdProperty.First(), modelToBind, item, id);
            }
            else
            {
                string[] ids = id.Split(Constants.SEPERATOR_IDS);

                if (listeIdProperty.Count != ids.Length)
                {
                    this._error.Create(Constants.ERROR_STATUT_JSONAPI, $"Error Format on attribute Id", "Id value is incorrect", item.ToString());
                    return;
                }

                foreach (PropertyInfo idProperty in listeIdProperty)
                {
                    this.BindIdValue(idProperty, modelToBind, item, ids[listeIdProperty.IndexOf(idProperty)]);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Create base object (id, type)
        /// </summary>
        /// <param name="model"></param>
        /// <param name="type"></param>
        /// <param name="extandData"></param>
        /// <returns></returns>
        private IJsonApiObject CreateJsonApiOject(object model, Type type, bool extandData)
        {
            IJsonApiObject returnObject = extandData ? new JsonApiData() : new JsonApiDataBase();

            IEnumerable <PropertyInfo> idsProperties = AttributeHandling.GetIdsProperties(type);

            if (idsProperties.Any())
            {
                returnObject.id = string.Join(Constants.SEPERATOR_IDS, idsProperties.Select(p => p.GetValue(model).ToString()));
            }

            string labelType = AttributeHandling.GetLabelProperty(type);

            returnObject.type = (!type.Name.Contains("AnonymousType")) ? labelType : null;

            return(returnObject);
        }
Exemple #3
0
        private IEnumerable <PropertyInfo> GetListProperties(Type type, string typeJson)
        {
            IEnumerable <PropertyInfo> properties = type.GetProperties()
                                                    .Where(p => !AttributeHandling.GetIdsProperties(type).Contains(p))
                                                    .Where(p => !AttributeHandling.IsIgnoreJsonApi(p))
                                                    .Select(p => p);

            // Si des champs sont selectionnés, on récupere seulement les champs selectionés et les champs relations
            List <string> listSelected = this._fields.Where(m => m.Key == typeJson).Select(m => m.Value).FirstOrDefault();

            if (listSelected != null)
            {
                properties = properties.Where(p =>
                                              listSelected.Contains(AttributeHandling.GetLabelProperty(p)) ||
                                              !Utils.IsTypeSystem(p.PropertyType) ||
                                              !Utils.HasGenericTypeSystem(p.PropertyType));
            }

            return(properties);
        }