Beispiel #1
0
        private void _addRelationships(DocumentData data, ContextEntity contextEntity, IIdentifiable entity)
        {
            var linkBuilder = new LinkBuilder(_jsonApiContext);

            contextEntity.Relationships.ForEach(r =>
            {
                var relationshipData = new RelationshipData
                {
                    Links = new Links
                    {
                        Self    = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName),
                        Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.Id.ToString(), r.RelationshipName)
                    }
                };

                if (_hasRelationship(r.RelationshipName))
                {
                    var navigationEntity = _jsonApiContext.ContextGraph
                                           .GetRelationship(entity, r.RelationshipName);

                    if (navigationEntity is IEnumerable)
                    {
                        relationshipData.ManyData = _getRelationships((IEnumerable <object>)navigationEntity, r.RelationshipName);
                    }
                    else
                    {
                        relationshipData.SingleData = _getRelationship(navigationEntity, r.RelationshipName);
                    }
                }

                data.Relationships.Add(r.RelationshipName.Dasherize(), relationshipData);
            });
        }
Beispiel #2
0
        private void AddRelationships(DocumentData data, ContextEntity contextEntity, IIdentifiable entity)
        {
            data.Relationships = new Dictionary <string, RelationshipData>();
            var linkBuilder = new LinkBuilder(_jsonApiContext);

            contextEntity.Relationships.ForEach(r =>
            {
                var relationshipData = new RelationshipData();

                if (r.DocumentLinks.HasFlag(Link.None) == false)
                {
                    relationshipData.Links = new Links();
                    if (r.DocumentLinks.HasFlag(Link.Self))
                    {
                        relationshipData.Links.Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName);
                    }

                    if (r.DocumentLinks.HasFlag(Link.Related))
                    {
                        relationshipData.Links.Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName);
                    }
                }

                if (RelationshipIsIncluded(r.PublicRelationshipName))
                {
                    var navigationEntity = _jsonApiContext.ContextGraph
                                           .GetRelationship(entity, r.InternalRelationshipName);

                    if (navigationEntity == null)
                    {
                        relationshipData.SingleData = null;
                    }
                    else if (navigationEntity is IEnumerable)
                    {
                        relationshipData.ManyData = GetRelationships((IEnumerable <object>)navigationEntity);
                    }
                    else
                    {
                        relationshipData.SingleData = GetRelationship(navigationEntity);
                    }
                }

                data.Relationships.Add(r.PublicRelationshipName, relationshipData);
            });
        }
        private RelationshipData GetRelationshipData(RelationshipAttribute attr, ContextEntity contextEntity, IIdentifiable entity)
        {
            var linkBuilder = new LinkBuilder(_jsonApiContext);

            var relationshipData = new RelationshipData();

            if (attr.DocumentLinks.HasFlag(Link.None) == false)
            {
                relationshipData.Links = new Links();
                if (attr.DocumentLinks.HasFlag(Link.Self))
                {
                    relationshipData.Links.Self = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.StringId, attr.PublicRelationshipName);
                }

                if (attr.DocumentLinks.HasFlag(Link.Related))
                {
                    relationshipData.Links.Related = linkBuilder.GetRelatedRelationLink(contextEntity.EntityName, entity.StringId, attr.PublicRelationshipName);
                }
            }

            // this only includes the navigation property, we need to actually check the navigation property Id
            var navigationEntity = _jsonApiContext.ResourceGraph.GetRelationshipValue(entity, attr);

            if (navigationEntity == null)
            {
                relationshipData.SingleData = attr.IsHasOne
                    ? GetIndependentRelationshipIdentifier((HasOneAttribute)attr, entity)
                    : null;
            }
            else if (navigationEntity is IEnumerable)
            {
                relationshipData.ManyData = GetRelationships((IEnumerable <object>)navigationEntity);
            }
            else
            {
                relationshipData.SingleData = GetRelationship(navigationEntity);
            }

            return(relationshipData);
        }