Example #1
0
        public static void IncludeRelation(this IJsonApiDocument document, JsonApiResource dataApiResource, object data, string path, string baseUrl = null)
        {
            // TODO: include relations for collections

            // parse paths
            var subPaths = path.Split(',');

            if (data is IEnumerable <object> collection)
            {
                foreach (var item in collection)
                {
                    foreach (var includePath in subPaths)
                    {
                        // generate tree
                        var includePathTree = GenerateIncludeTree(dataApiResource, includePath);
                        // process tree
                        ProcessIncludeTree(document, includePathTree, item, baseUrl);
                    }
                }
            }
            else
            {
                foreach (var includePath in subPaths)
                {
                    // generate tree
                    var includePathTree = GenerateIncludeTree(dataApiResource, includePath);
                    // process tree
                    ProcessIncludeTree(document, includePathTree, data, baseUrl);
                }
            }
        }
Example #2
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);
                        }
                    }
                }
            }
        }
Example #3
0
        public static object ToObject(this IJsonApiDocument document, JsonApiResource apiResource, Type targetType, out Func <int, string, bool> foundAttributes)
        {
            switch (document)
            {
            case JsonApiDocument doc:
                return(doc.ToObjectInternal(apiResource, targetType, out foundAttributes));

            case JsonApiCollectionDocument collDoc:
                return(collDoc.ToObjectInternal(apiResource, targetType, out foundAttributes));

            default:
                throw new ArgumentException($"Parameter {nameof(document)} does not have a supported type. (Type={document?.GetType()})");
            }
        }
Example #4
0
        public CreatedResult Created(IJsonApiDocument document)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Meta is null)
            {
                document.Meta = new JsonApiMeta();
            }

            document.Meta["method"] = this.Request.Method;
            document.Meta["status"] = StatusCodes.Status201Created.ToString();
            return(this.Created(document.Links.Self, document));
        }
Example #5
0
        public OkObjectResult Ok(IJsonApiDocument document)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Meta is null)
            {
                document.Meta = new JsonApiMeta();
            }

            document.Meta["method"] = this.Request.Method;
            document.Meta["status"] = StatusCodes.Status200OK.ToString();
            return(this.Ok((object)(document)));
        }
Example #6
0
        public ObjectResult InternalServerError(IJsonApiDocument document, string errorTitle = null, string errorDetail = null)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Errors is null)
            {
                document.Errors = new List <JsonApiError>();
            }

            document.Errors.Insert(
                0,
                new JsonApiError
            {
                Status = StatusCodes.Status500InternalServerError.ToString(),
                Title  = errorTitle,
                Detail = errorDetail,
            });
            return(this.StatusCode(StatusCodes.Status500InternalServerError, document));
        }
Example #7
0
        public BadRequestObjectResult BadRequest(IJsonApiDocument document, string errorTitle = null, string errorDetail = null)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Errors is null)
            {
                document.Errors = new List <JsonApiError>();
            }

            document.Errors.Insert(
                0,
                new JsonApiError
            {
                Status = StatusCodes.Status400BadRequest.ToString(),
                Title  = errorTitle,
                Detail = errorDetail,
            });
            return(this.BadRequest((object)(document)));
        }
Example #8
0
        public ObjectResult UnprocessableEntity(IJsonApiDocument document, string errorTitle = null, string errorDetail = null)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Errors is null)
            {
                document.Errors = new List <JsonApiError>();
            }

            document.Errors.Insert(
                0,
                new JsonApiError
            {
                Status = StatusCodes.Status422UnprocessableEntity.ToString(),
                Title  = errorTitle,
                Detail = errorDetail,
            });
            return(this.StatusCode(StatusCodes.Status422UnprocessableEntity, document));
        }
Example #9
0
        public ObjectResult MethodNotAllowed(IJsonApiDocument document, string errorTitle = null, string errorDetail = null)
        {
            if (document is null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Errors is null)
            {
                document.Errors = new List <JsonApiError>();
            }

            document.Errors.Insert(
                0,
                new JsonApiError
            {
                Status = StatusCodes.Status405MethodNotAllowed.ToString(),
                Title  = errorTitle,
                Detail = errorDetail,
            });
            return(this.StatusCode(StatusCodes.Status405MethodNotAllowed, document));
        }
Example #10
0
 public static void IncludeRelation <TResource>(this IJsonApiDocument document, object data, string path, string baseUrl = null) where TResource : JsonApiResource
 {
     document.IncludeRelation(Activator.CreateInstance <TResource>(), data, path, baseUrl);
 }