Example #1
0
        /// <inheritdoc/>
        public virtual void BeforeRead <TEntity>(ResourcePipeline pipeline, string stringId = null) where TEntity : class, IIdentifiable
        {
            var hookContainer = _executorHelper.GetResourceHookContainer <TEntity>(ResourceHook.BeforeRead);

            hookContainer?.BeforeRead(pipeline, false, stringId);
            var contextEntity    = _graph.GetContextEntity(typeof(TEntity));
            var calledContainers = new List <PrincipalType>()
            {
                typeof(TEntity)
            };

            foreach (var relationshipPath in _context.IncludedRelationships)
            {
                RecursiveBeforeRead(contextEntity, relationshipPath.Split('.').ToList(), pipeline, calledContainers);
            }
        }
Example #2
0
        public static RelationshipAttribute GetRelationshipAttribute <T>(this IResourceGraph resourceGraph,
                                                                         string publicRelationshipName)
        {
            ContextEntity contextEntity = resourceGraph.GetContextEntity(typeof(T));

            return(contextEntity.Relationships.SingleOrDefault(r => r.Is(publicRelationshipName)));
        }
Example #3
0
        public static string GetPublicRelationshipName <T>(this IResourceGraph resourceGraph,
                                                           string internalPropertyName)
        {
            ContextEntity contextEntity = resourceGraph.GetContextEntity(typeof(T));

            return(contextEntity.Relationships
                   .SingleOrDefault(r => r.InternalRelationshipName == internalPropertyName)?.PublicRelationshipName);
        }
        public Document Build(IIdentifiable entity)
        {
            var contextEntity = _resourceGraph.GetContextEntity(entity.GetType());

            var resourceDefinition = _scopedServiceProvider?.GetService(contextEntity.ResourceType) as IResourceDefinition;
            var document           = new Document
            {
                Data = GetData(contextEntity, entity, resourceDefinition),
                Meta = GetMeta(entity)
            };

            if (ShouldIncludePageLinks(contextEntity))
            {
                document.Links = _jsonApiContext.PageManager.GetPageLinks(new LinkBuilder(_jsonApiContext));
            }

            document.Included = AppendIncludedObject(document.Included, contextEntity, entity);

            return(document);
        }
        public static ResourceSchema Build(IResourceGraph resourceGraph,
                                           IEnumerable <ResourceDescriptor> resourceDescriptors)
        {
            var models = new Dictionary <string, ResourceSchemaModel>();

            foreach (ContextEntity resourceType in resourceDescriptors
                     .Select(rd => resourceGraph.GetContextEntity(rd.ResourceType)))
            {
                models[Camelize(resourceType.EntityName.Singularize())] = CreateModel(resourceGraph, resourceType);
            }
            var schema = new ResourceSchema
            {
                Models = models
            };

            return(schema);
        }
        private static ResourceSchemaRelationship CreateRelationship(IResourceGraph contextGraph,
                                                                     ContextEntity resourceType, RelationshipAttribute relationship)
        {
            ContextEntity relationshipType = contextGraph.GetContextEntity(relationship.Type);

            SchemaInfoAttribute schemaInfo = GetSchemaInfo(resourceType, relationship);
            string inverse     = null;
            bool   isDependent = false;

            if (schemaInfo != null)
            {
                if (schemaInfo.Inverse != null)
                {
                    RelationshipAttribute inverseRelationship = relationshipType.Relationships
                                                                .FirstOrDefault(r => r.InternalRelationshipName == schemaInfo.Inverse);
                    inverse = inverseRelationship?.PublicRelationshipName;
                }
                isDependent = schemaInfo.IsDependent;
            }

            if (inverse == null)
            {
                // check if the related resource type has a relationship back to this type that is marked as the
                // inverse of the current relationship
                foreach (RelationshipAttribute candidate in relationshipType.Relationships
                         .Where(r => r.Type.IsAssignableFrom(resourceType.EntityType)))
                {
                    SchemaInfoAttribute candidateSchemaInfo = GetSchemaInfo(relationshipType, candidate);
                    if (candidateSchemaInfo?.Inverse == relationship.InternalRelationshipName)
                    {
                        inverse = candidate.PublicRelationshipName;
                        break;
                    }
                }
            }

            return(new ResourceSchemaRelationship
            {
                Type = relationship.IsHasMany ? "hasMany" : "hasOne",
                Model = Camelize(relationshipType.EntityName.Singularize()),
                Inverse = Camelize(inverse),
                Dependent = isDependent ? "remove" : null
            });
        }
Example #7
0
        public JsonApiClient(

            HttpClient httpClient,
            IJsonApiDeSerializer deserializer,
            IJsonApiClientSerializer <TModel> serializer,
            IResourceGraph contextGraph,
            IJsonApiClientAuthProvider authProvider = null
            )
        {
            _deserializer = deserializer;
            _httpClient   = httpClient;
            _serializer   = serializer;
            _authProvider = authProvider;
            _contextGraph = contextGraph;

            var contextEntity = contextGraph.GetContextEntity(typeof(TModel));

            _endPoint = contextEntity.EntityName;
        }
        private void UpdateResourceObject(ResourceObject resourceObj)
        {
            if (resourceObj == null || resourceObj.Relationships == null)
            {
                return;
            }

            ContextEntity contextEntity = _resourceGraph.GetContextEntity(resourceObj.Type);

            foreach (RelationshipAttribute relationship in contextEntity.Relationships)
            {
                RelationshipData oldData = resourceObj.Relationships[relationship.PublicRelationshipName];
                var newData = new XFRelationshipData
                {
                    IsHasOne   = relationship.IsHasOne,
                    Links      = oldData.Links,
                    SingleData = oldData.SingleData,
                    ManyData   = oldData.ManyData
                };
                resourceObj.Relationships[relationship.PublicRelationshipName] = newData;
            }
        }
Example #9
0
 public ResourceDefinition(IResourceGraph graph)
 {
     _contextEntity             = graph.GetContextEntity(typeof(T));
     _instanceAttrsAreSpecified = InstanceOutputAttrsAreSpecified();
 }