예제 #1
0
        private JToken GetRelationshipData(ResourceRelationship relationship, JToken relationshipValues)
        {
            var data = SerializeArrayOrObject(
                relationship.RelatedResource,
                relationshipValues,
                (resource, props) =>
            {
                var values       = SerializeMinimalData(props, relationship.RelatedResource);
                var includedData = values.DeepClone();
                var url          = _urlBuilder.BuildCanonicalPath(
                    relationship.RelatedResource,
                    (string)EnsureHasId(props, relationship.RelatedResource));

                includedData["attributes"]    = SerializeAttributes(props, relationship.RelatedResource);
                includedData["relationships"] = SerializeRelationships(resource, props);
                includedData["links"]         = AddUrl(new JObject(), "self", url);
                if (!IsResourceIncluded(includedData))
                {
                    _includedSection.Add(includedData);
                }

                return(values);
            });

            return(data);
        }
예제 #2
0
        private JToken SerializeRelationship(ResourceRelationship relationship, IDictionary <string, JToken> properties)
        {
            var relationshipValues     = GetValue(relationship.Name, properties);
            var relationshipProperties = relationshipValues as JObject;

            // serialize the links part (so the data can be fetched)
            var objId    = EnsureHasId(properties, _resource);
            var relToken = GetMinimumRelationship(
                objId.ToString(),
                relationship,
                relationshipProperties != null ? GetId(relationshipProperties, relationship.RelatedResource).Value <string>() : null);

            if (relationshipValues == null)
            {
                return(relToken);
            }

            // only include data if it exists, otherwise just assume it should be fetched later
            var data = GetRelationshipData(relationship, relationshipValues);

            if (data != null)
            {
                relToken["data"] = data;
            }

            return(relToken);
        }
예제 #3
0
 /// <summary>
 /// Returns a path in the form `/resource.UrlPath/id/relationships/relationship.UrlPath/`.
 /// </summary>
 /// <param name="resource">The resource this path is related to.</param>
 /// <param name="id">The unique id of the resource.</param>
 /// <param name="relationship">The relationship this path refers to.</param>
 /// <returns>A <see cref="string"/> containing the path.</returns>
 public virtual string BuildRelationshipPath(ApiResource resource, string id, ResourceRelationship relationship)
 {
     return('/'.TrimJoin(
                BuildCanonicalPath(resource, id), "relationships", relationship.UrlPath)
            .EnsureStartsWith("/")
            .EnsureEndsWith("/"));
 }
예제 #4
0
 /// <summary>
 /// Adds a new relationship to the resource, testing for duplicates.
 /// </summary>
 /// <param name="relationship">The relationship to add.</param>
 public void AddRelationship(ResourceRelationship relationship)
 {
     if (!ResourceRelationships.Where(r => r.ResourceRelationshipType == relationship.ResourceRelationshipType && r.ResourceRefId == relationship.ResourceRefId).Any())
     {
         ResourceRelationships.Add(relationship);
     }
 }
 /// <summary>
 /// Returns a path in the form `/resource.UrlPath/id/relationships/relationship.UrlPath/`.
 /// </summary>
 /// <param name="resource">The resource this path is related to.</param>
 /// <param name="id">The unique id of the resource.</param>
 /// <param name="relationship">The relationship this path refers to.</param>
 /// <returns>A <see cref="string"/> containing the path.</returns>
 public virtual string BuildRelationshipPath(ApiResource resource, string id, ResourceRelationship relationship)
 {
     return '/'.TrimJoin(
         BuildCanonicalPath(resource, id), "relationships", relationship.UrlPath)
         .EnsureStartsWith("/")
         .EnsureEndsWith("/");
 }
예제 #6
0
        public ResourceGraphRelationship(ResourceRelationship relationship, bool included, object obj)
        {
            relationship.ThrowIfNull(nameof(relationship));

            Included     = included;
            SourceObject = obj;
            Relationship = relationship;
        }
 /// <summary>
 /// Returns a path in the form `/relatedResource.UrlPath/relatedResource.Id/`.
 /// </summary>
 /// <param name="resource">The resource this path is related to.</param>
 /// <param name="id">The unique id of the resource.</param>
 /// <param name="relationship">The relationship this path refers to.</param>
 /// <param name="relatedResourceId">The id of the related resource.</param>
 /// <returns>A <see cref="string"/> containing the path.</returns>
 public override string BuildRelationshipPath(
     ApiResource resource,
     string id,
     ResourceRelationship relationship,
     string relatedResourceId)
 {
     // empty if no id, because e.g. /api/people != /api/companies/1/employees
     // (all people is not the same as all employees for a company)
     return string.IsNullOrEmpty(relatedResourceId)
         ? null
         : BuildCanonicalPath(relationship.RelatedResource, relatedResourceId);
 }
예제 #8
0
        private JToken GetMinimumRelationship(string id, ResourceRelationship relationship, string relationshipId)
        {
            var links = new JObject();

            AddUrl(links, "self", _urlBuilder.BuildRelationshipPath(_resource, id, relationship));
            AddUrl(links, "related", _urlBuilder.BuildRelationshipPath(_resource, id, relationship, relationshipId));

            return(new JObject
            {
                ["links"] = links
            });
        }
예제 #9
0
 /// <summary>
 /// Returns a path in the form `/relatedResource.UrlPath/relatedResource.Id/`.
 /// </summary>
 /// <param name="resource">The resource this path is related to.</param>
 /// <param name="id">The unique id of the resource.</param>
 /// <param name="relationship">The relationship this path refers to.</param>
 /// <param name="relatedResourceId">The id of the related resource.</param>
 /// <returns>A <see cref="string"/> containing the path.</returns>
 public override string BuildRelationshipPath(
     ApiResource resource,
     string id,
     ResourceRelationship relationship,
     string relatedResourceId)
 {
     // empty if no id, because e.g. /api/people != /api/companies/1/employees
     // (all people is not the same as all employees for a company)
     return(string.IsNullOrEmpty(relatedResourceId)
         ? null
         : BuildCanonicalPath(relationship.RelatedResource, relatedResourceId));
 }
예제 #10
0
        private JToken GetRelationshipData(ResourceRelationship relationship, JToken relationshipValues)
        {
            var data = SerializeArrayOrObject(
                relationship.RelatedResource,
                relationshipValues,
                (resource, props) =>
            {
                var values       = SerializeMinimalData(props, relationship.RelatedResource);
                var includedData = values.DeepClone();
                var url          = _urlBuilder.BuildCanonicalPath(
                    relationship.RelatedResource,
                    (string)EnsureHasId(props, relationship.RelatedResource));

                includedData["attributes"]    = SerializeAttributes(props, relationship.RelatedResource);
                includedData["relationships"] = SerializeRelationships(resource, props);
                includedData["links"]         = AddUrl(new JObject(), "self", url);

                var includes = _includingContext?.Includes?.Select(x => x.Name).ToList();
                if (includes != null)
                {
                    var newIncludes = new List <string>();
                    foreach (var include in includes)
                    {
                        var temp = include.Split('.');
                        newIncludes.AddRange(temp);
                    }
                    includes.AddRange(newIncludes.Except(includes));
                }

                if (includes != null && includes?.Count() != 0)
                {
                    if (includes.Contains(relationship.Name.ToPascalCase()) && !IsResourceIncluded(includedData))
                    {
                        _includedSection.Add(includedData);
                    }
                }
                else if (!IsResourceIncluded(includedData) && (_includingContext == null || !_includingContext.DisableDefaultIncluded))
                {
                    _includedSection.Add(includedData);
                }

                return(values);
            });

            return(data);
        }
        private static IControllerRelation CreateControllerRelation <TModel>([NotNull] ResourceRelationship resourceRelationship)
        {
            switch (resourceRelationship.Kind)
            {
            case RelationshipKind.BelongsTo:
                return(new ControllerToOneRelation <TModel>(resourceRelationship.PropertyName, resourceRelationship.IdPropertyName,
                                                            resourceRelationship.RelatedResource.ResourceType));

            case RelationshipKind.HasMany:
                return(new ControllerToManyRelation <TModel>(resourceRelationship.PropertyName, resourceRelationship.IdPropertyName,
                                                             resourceRelationship.RelatedResource.ResourceType));

            default:
                throw new ArgumentOutOfRangeException(nameof(ResourceRelationship.Kind),
                                                      $@"{nameof(ResourceRelationship)}.{nameof(ResourceRelationship.Kind)} does not have a valid value.");
            }
        }
예제 #12
0
        private JToken GetRelationshipData(SerializationContext serializationContext, ResourceRelationship relationship, JToken relationshipValues)
        {
            var data = SerializeArrayOrObject(
                serializationContext,
                relationshipValues,
                (context, props) =>
            {
                var values       = SerializeMinimalData(context, props, relationship.RelatedResource);
                var includedData = values.DeepClone();
                var url          = _urlBuilder.BuildCanonicalPath(
                    relationship.RelatedResource,
                    EnsureHasId(props, relationship.RelatedResource).Value <string>());

                includedData["attributes"] = SerializeAttributes(context, props, relationship.RelatedResource);
                includedData["links"]      = AddUrl(context.BaseUrl, new JObject(), "self", url);
                if (!IsResourceIncluded(context, includedData))
                {
                    context.IncludedSections.Add(includedData);
                }

                return(values);
            });

            return(data);
        }
 public string BuildRelationshipPath(ApiResource resource, string id, ResourceRelationship relationship,
                                     string relatedResourceId)
 {
     return(string.Empty);
 }
        private JToken GetMinimumRelationship(SerializationContext serializationContext, string id, ResourceRelationship relationship, string relationshipId)
        {
            var links = new JObject();
            AddUrl(serializationContext.BaseUrl, links, "self", _urlBuilder.BuildRelationshipPath(serializationContext.Resource, id, relationship));
            AddUrl(serializationContext.BaseUrl, links, "related", _urlBuilder.BuildRelationshipPath(serializationContext.Resource, id, relationship, relationshipId));

            return new JObject
            {
                ["links"] = links
            };
        }
        private JToken GetRelationshipData(SerializationContext serializationContext, ResourceRelationship relationship, JToken relationshipValues)
        {
            var data = SerializeArrayOrObject(
                serializationContext,
                relationshipValues,
                (context, props) =>
                {
                    var values = SerializeMinimalData(context, props, relationship.RelatedResource);
                    var includedData = values.DeepClone();
                    var url = _urlBuilder.BuildCanonicalPath(
                        relationship.RelatedResource,
                        EnsureHasId(props, relationship.RelatedResource).Value<string>());

                    includedData["attributes"] = SerializeAttributes(context, props, relationship.RelatedResource);
                    includedData["links"] = AddUrl(context.BaseUrl, new JObject(), "self", url);
                    if (!IsResourceIncluded(context, includedData))
                    {
                        context.IncludedSections.Add(includedData);
                    }

                    return values;
                });
            return data;
        }
        private JToken SerializeRelationship(SerializationContext serializationContext, ResourceRelationship relationship, IDictionary<string, JToken> properties)
        {
            var relationshipValues = GetValue(relationship.Name, properties);
            var relationshipProperties = relationshipValues as JObject;

            // serialize the links part (so the data can be fetched)
            var objId = EnsureHasId(properties, serializationContext.Resource);
            var relToken = GetMinimumRelationship(
                serializationContext,
                objId.ToString(),
                relationship,
                relationshipProperties != null ? GetId(relationshipProperties, relationship.RelatedResource).Value<string>() : null);

            if (relationshipValues == null)
            {
                return relToken;
            }

            // only include data if it exists, otherwise just assume it should be fetched later
            var data = GetRelationshipData(serializationContext, relationship, relationshipValues);
            if (data != null)
            {
                relToken["data"] = data;
            }

            return relToken;
        }
예제 #17
0
        private JToken GetMinimumRelationship(SerializationContext serializationContext, string id, ResourceRelationship relationship, string relationshipId)
        {
            var links = new JObject();

            AddUrl(serializationContext.BaseUrl, links, "self", _urlBuilder.BuildRelationshipPath(serializationContext.Resource, id, relationship));
            AddUrl(serializationContext.BaseUrl, links, "related", _urlBuilder.BuildRelationshipPath(serializationContext.Resource, id, relationship, relationshipId));

            return(new JObject
            {
                ["links"] = links
            });
        }
        private JToken GetMinimumRelationship(string id, ResourceRelationship relationship, string relationshipId)
        {
            var links = new JObject();
            AddUrl(links, "self", _urlBuilder.BuildRelationshipPath(_resource, id, relationship));
            AddUrl(links, "related", _urlBuilder.BuildRelationshipPath(_resource, id, relationship, relationshipId));

            return new JObject
            {
                ["links"] = links
            };
        }
예제 #19
0
        private JToken GetRelationshipData(ResourceRelationship relationship, JToken relationshipValues)
        {
            var data = SerializeArrayOrObject(
                relationship.RelatedResource,
                relationshipValues,
                (resource, props) =>
                {
                    var values = SerializeMinimalData(props, relationship.RelatedResource);
                    var includedData = values.DeepClone();
                    var url = _urlBuilder.BuildCanonicalPath(
                        relationship.RelatedResource,
                        (string)EnsureHasId(props, relationship.RelatedResource));

                    includedData["attributes"] = SerializeAttributes(props, relationship.RelatedResource);
                    includedData["relationships"] = SerializeRelationships(resource, props);
                    includedData["links"] = AddUrl(new JObject(), "self", url);
                    if (!IsResourceIncluded(includedData))
                    {
                        _includedSection.Add(includedData);
                    }

                    return values;
                });
            return data;
        }