Пример #1
0
        private JObject SerializeRelationships(ResourceGraphNode node, FieldsetProperty fieldset)
        {
            if (!node.Relationships.Any())
            {
                return(null);
            }

            var response = new JObject();

            foreach (var kv in node.Relationships)
            {
                if (fieldset != null && !fieldset.Fields.Contains(kv.Value.Relationship.Name.ToComparablePropertyName()))
                {
                    continue;
                }

                var relationship = kv.Value.Relationship;

                var item = new JObject();

                var data = SerializeRelationshipData(node, kv.Value);

                var relationshipId = default(string);

                if (data != null &&
                    relationship.Kind == RelationshipKind.BelongsTo &&
                    kv.Value.SourceObject != null)
                {
                    relationshipId = (string)data["id"];
                }

                var links   = new JObject();
                var self    = _urlBuilder.BuildRelationshipPath(node.Resource, node.Key.Id.ToString(), relationship);
                var related = _urlBuilder.BuildRelationshipPath(node.Resource, node.Key.Id.ToString(), relationship, relationshipId);

                if (!string.IsNullOrEmpty(self) && relationship.LinkType.HasFlag(LinkType.Self))
                {
                    AddUrl(links, "self", self);
                }

                if (!string.IsNullOrEmpty(related) && relationship.LinkType.HasFlag(LinkType.Related))
                {
                    AddUrl(links, "related", related);
                }

                if (links.HasValues)
                {
                    item["links"] = links;
                }

                if (data != null && kv.Value != null)
                {
                    item["data"] = data;
                }

                response[_propertyNameConverter.ToJsonPropertyName(kv.Key)] = item;
            }

            return(response);
        }
Пример #2
0
        private JObject SerializeRelationships(ResourceGraphNode node)
        {
            if (node.Relationships.Count == 0)
            {
                return(null);
            }

            var response = new JObject();

            foreach (var kv in node.Relationships)
            {
                var relationship = kv.Value.Relationship;

                var item = new JObject();

                var data = SerializeRelationshipData(node, kv.Value);

                var relationshipId = default(string);

                if (data != null &&
                    relationship.Kind == RelationshipKind.BelongsTo &&
                    kv.Value.SourceObject != null)
                {
                    relationshipId = (string)data["id"];
                }

                var links   = new JObject();
                var self    = _urlBuilder.BuildRelationshipPath(node.Resource, node.Key.Id.ToString(), relationship);
                var related = _urlBuilder.BuildRelationshipPath(node.Resource, node.Key.Id.ToString(), relationship, relationshipId);

                if (!string.IsNullOrEmpty(self) && relationship.LinkType.HasFlag(LinkType.Self))
                {
                    AddUrl(links, "self", self);
                }

                if (!string.IsNullOrEmpty(related) && relationship.LinkType.HasFlag(LinkType.Related))
                {
                    AddUrl(links, "related", related);
                }

                if (links.HasValues)
                {
                    item["links"] = links;
                }

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

                response[kv.Key] = item;
            }

            return(response);
        }
Пример #3
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
            });
        }
Пример #4
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
            });
        }