//assets
        public async Task <Asset> GetAssetAsync(Guid id)
        {
            var response = await Client.GetAsync($"api/asset/{id}");

            RestAsset restAsset = await response.Content.ParseJsonAsync <RestAsset>();

            return(restAsset.ToModel());
        }
Ejemplo n.º 2
0
        public async Task InvokeAsync(
            HttpContext context,
            IAssetStorage assetStore,
            JsonService jsonService)
        {
            Guid id = Guid.Parse((string)context.GetRouteValue("id"));

            var entity = await assetStore.GetAssetAsync(id);

            RestAsset restAsset = entity.ToRest();

            string json = jsonService.Serialize(restAsset);

            await context.Response.WriteAsync(json);
        }
Ejemplo n.º 3
0
        public async Task InvokeAsync(
            HttpContext context,
            IAssetStorage assetStore,
            JsonService jsonService)
        {
            RestAsset restAsset = await jsonService.Deserialize <RestAsset>(context.Request.Body);

            Asset asset = restAsset.ToModel();

            await assetStore.CreateAsync(asset);

            var r = new ResourceCreated()
            {
                Id = asset.Id
            };

            string json = jsonService.Serialize(r);

            await context.Response.WriteAsync(json);
        }
Ejemplo n.º 4
0
        public static void FromRestValue(this JToken bsonValue, string fieldName, IContentElement contentItem, IContentSchema schema)
        {
            if (contentItem.Fields.TryGetValue(fieldName, out ContentField contentField))
            {
                switch (bsonValue.Type)
                {
                case JTokenType.Null:
                    break;

                case JTokenType.String:
                    if (contentField is SingleValueContentField <string> stringField)
                    {
                        stringField.Value = bsonValue.Value <string>();
                    }
                    else if (contentField is SingleValueContentField <Guid?> guidField)
                    {
                        guidField.Value = Guid.Parse(bsonValue.Value <string>());
                    }
                    break;

                case JTokenType.Integer:
                    if (contentField is SingleValueContentField <short?> shortField)
                    {
                        shortField.Value = bsonValue.Value <short?>();
                    }
                    else if (contentField is SingleValueContentField <int?> intField)
                    {
                        intField.Value = bsonValue.Value <int?>();
                    }
                    else if (contentField is SingleValueContentField <long?> longField)
                    {
                        longField.Value = bsonValue.Value <long?>();
                    }
                    break;

                case JTokenType.Float:
                    ((SingleValueContentField <double?>)contentField).Value = bsonValue.Value <double>();
                    break;

                case JTokenType.Boolean:
                    ((SingleValueContentField <bool?>)contentField).Value = bsonValue.Value <bool>();
                    break;

                //case JTokenType.Guid:
                //    ((SingleContentField<Guid?>)contentField).Value = bsonValue.Value<Guid>();
                //    break;

                case JTokenType.Array:
                    if (contentField is ArrayField arrayField)
                    {
                        if (schema.Fields.TryGetValue(fieldName, out ContentSchemaField definition))
                        {
                            ArrayFieldOptions arrayOptions = definition.Options as ArrayFieldOptions;

                            foreach (JObject item in bsonValue)
                            {
                                ArrayFieldItem arrayFieldItem = arrayOptions.CreateArrayField();

                                foreach (var subitem in item)
                                {
                                    FromRestValue(subitem.Value, subitem.Key, arrayFieldItem, arrayOptions);
                                }

                                arrayField.Items.Add(arrayFieldItem);
                            }
                        }
                    }
                    break;

                case JTokenType.Object:
                    if (contentField is ReferenceField referenceField)
                    {
                        if (bsonValue.HasValues)
                        {
                            RestContentItem restContentItem = bsonValue.ToObject <RestContentItem>(NewtonJsonExtensions.CreateSerializer());
                            referenceField.ContentItem = restContentItem.ToModel();
                        }
                    }
                    else if (contentField is EmbedField embedField)
                    {
                        RestContentEmbedded restContentItem = bsonValue.ToObject <RestContentEmbedded>(NewtonJsonExtensions.CreateSerializer());
                        embedField.ContentEmbedded = restContentItem.ToModel();
                    }
                    else if (contentField is AssetField assetField)
                    {
                        if (bsonValue.HasValues)
                        {
                            RestAsset restAsset = bsonValue.ToObject <RestAsset>();
                            assetField.Asset = restAsset.ToModel();
                        }
                    }
                    else
                    {
                        ContentField c = (ContentField)bsonValue.ToObject(contentField.GetType(), NewtonJsonExtensions.CreateSerializer());

                        contentItem.Fields[fieldName] = c;
                    }
                    break;
                }
            }
        }