Beispiel #1
0
        public static void FromApiResource(this JsonApiDocument document, object data, JsonApiResource apiResource, string baseUrl = null)
        {
            switch (data)
            {
            case null:
                return;

            case IEnumerable <object> _:
                throw new Exception("data cannot be a collection");
            }

            var rObject = new JsonApiResourceObject();

            rObject.FromApiResource(data, apiResource, baseUrl);
            document.Data = rObject;
            if (!string.IsNullOrWhiteSpace(baseUrl))
            {
                document.Links = new JsonApiLinksObject
                {
                    Self = new JsonApiLink
                    {
                        Href = $"{baseUrl}{apiResource.UrlPath}/{rObject.Id}"
                    }
                };
            }
        }
Beispiel #2
0
        public static void FromApiResource(this JsonApiCollectionDocument document, IEnumerable data, JsonApiResource apiResource, string baseUrl = null)
        {
            if (data == null)
            {
                return;
            }
            var resourceObjects = new List <JsonApiResourceObject>();

            foreach (var item in data)
            {
                var rObject = new JsonApiResourceObject();
                rObject.FromApiResource(item, apiResource, baseUrl);
                resourceObjects.Add(rObject);
            }
            document.Data = resourceObjects;
            if (!string.IsNullOrWhiteSpace(baseUrl))
            {
                document.Links = new JsonApiLinksObject
                {
                    Self = new JsonApiLink
                    {
                        Href = $"{baseUrl}{apiResource.UrlPath}"
                    }
                };
            }
        }
Beispiel #3
0
        private static void ProcessIncludeTree(IJsonApiDocument document, IncludePathNode includePathTree, object data, string baseUrl)
        {
            var relationship = includePathTree.IncludeApiResourceRelationship;
            var dataType     = data.GetType();
            var propertyInfo = dataType.GetProperty(relationship.PropertyName);

            if (propertyInfo == null)
            {
                throw new Exception($"Property {relationship.PropertyName} does not exist for type {dataType.Name}.");
            }
            var value = propertyInfo.GetValueFast(data);

            if (value == null)
            {
                return;
            }
            if (relationship.Kind == RelationshipKind.BelongsTo)
            {
                var jsonResourceObject = new JsonApiResourceObject();
                jsonResourceObject.FromApiResource(value, relationship.RelatedResource, baseUrl);
                if (document.Included == null)
                {
                    document.Included = new JsonApi.Dictionaries.JsonApiResourceObjectDictionary();
                }
                document.Included.AddResource(jsonResourceObject);
                if (includePathTree.Child != null)
                {
                    // jump down the rabbit hole
                    ProcessIncludeTree(document, includePathTree.Child, value, baseUrl);
                }
            }
            else
            {
                if (value is IEnumerable <object> collection && collection.Any())
                {
                    if (document.Included == null)
                    {
                        document.Included = new JsonApi.Dictionaries.JsonApiResourceObjectDictionary();
                    }
                    foreach (var item in collection)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        var jsonResourceObject = new JsonApiResourceObject();
                        jsonResourceObject.FromApiResource(item, relationship.RelatedResource, baseUrl);
                        document.Included.AddResource(jsonResourceObject);
                        if (includePathTree.Child != null)
                        {
                            // jump down the rabbit hole
                            ProcessIncludeTree(document, includePathTree.Child, item, baseUrl);
                        }
                    }
                }
            }
        }