Esempio n. 1
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
                {
                    Links = new Links
                    {
                        Self    = linkBuilder.GetSelfRelationLink(contextEntity.EntityName, entity.StringId, r.PublicRelationshipName),
                        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);
            });
        }