コード例 #1
0
        private IDictionary <string, Relationship> GenerateRelationships(object instance, IResourceMapping mapping)
        {
            var relationNames = mapping.GetRelationNames().ToList();

            if (!relationNames.Any())
            {
                return(null);
            }

            var relationships = new Dictionary <string, Relationship>();

            foreach (var relationName in relationNames)
            {
                var relationTypeName = _configuration.ResourceConfigurations[mapping.GetResourceTypeOfRelation(relationName)]?.TypeName;
                if (relationTypeName == null)
                {
                    throw new JsonApiException(CausedBy.Server, $"Resource type for relation {relationName} is not configured");
                }

                var value = mapping.GetRelationValue(instance, relationName);
                if (value == null)
                {
                    return(null);
                }

                if (value is Guid)
                {
                    relationships.Add(relationName, new Relationship(relationTypeName, (Guid)value));
                }
                else if (value is IEnumerable <Guid> )
                {
                    relationships.Add(relationName, new Relationship(relationTypeName, (IEnumerable <Guid>)value));
                }
            }

            return(relationships);
        }
コード例 #2
0
        public Changes <TResource> BuildChanges <TResource>(DocumentData data, IResourceMapping mapping)
        {
            var changes = new Changes <TResource>();

            if (data.Id.HasValue)
            {
                changes.AddChange(resource => mapping.SetId(resource, data.Id.Value));
            }

            if (data.Attributes != null)
            {
                foreach (var attribute in data.Attributes)
                {
                    var attributeType = mapping.GetAttributeType(attribute.Key);
                    if (attributeType == null)
                    {
                        continue;
                    }
                    var value = attribute.Value.ParseAs(attributeType, _jsonSerializer);
                    changes.AddChange(resource => mapping.SetAttributeValue(resource, attribute.Key, value));
                }
            }

            if (data.Relationships != null)
            {
                foreach (var relation in data.Relationships)
                {
                    var relationResourceType = mapping.GetResourceTypeOfRelation(relation.Key);
                    if (relationResourceType == null)
                    {
                        throw new JsonApiException(CausedBy.Client, $"Unknown relation {relation.Key}");
                    }
                    var relationResourceTypeName = GetResourceTypeName(relationResourceType);
                    if (mapping.IsHasManyRelation(relation.Key))
                    {
                        var relationData = relation.Value.Data.ParseAs <IEnumerable <RelationData> >(_jsonSerializer);
                        if (relationData != null)
                        {
                            if (relationData.Any(x => !x.Type.Equals(relationResourceTypeName)))
                            {
                                throw new JsonApiFormatException(CausedBy.Client, $"Not all relations specified for {relation.Key} are of the type {relationResourceTypeName}");
                            }
                            var relationIds = relationData.Select(x => x.Id);
                            changes.AddChange(resource => mapping.SetRelationValue(resource, relation.Key, relationIds));
                        }
                    }
                    else
                    {
                        var relationData = relation.Value.Data.ParseAs <RelationData>(_jsonSerializer);
                        if (relationData != null)
                        {
                            if (!relationData.Type.Equals(relationResourceTypeName))
                            {
                                throw new JsonApiFormatException(CausedBy.Client, $"Relation type specified for {relation.Key} must be {relationResourceTypeName} instead of {relationData.Type}");
                            }
                            var relationId = relationData.Id;
                            changes.AddChange(resource => mapping.SetRelationValue(resource, relation.Key, relationId));
                        }
                    }
                }
            }

            return(changes);
        }