Example #1
0
        private void SaveNewSchema_Click(object sender, RoutedEventArgs e)
        {
            var contentSchema = new ContentSchema();

            contentSchema.Name = SchemaName.Text;

            SchemaField schemaField;

            for (int i = 0; i < FieldStackPanel.Children.Count; ++i)
            {
                schemaField = (SchemaField)FieldStackPanel.Children[i];
                contentSchema.Fields.Add(
                    new ContentField()
                {
                    ContentFieldType = (ContentFieldType)schemaField.FieldTypeComboBox.SelectedIndex,
                    Name             = schemaField.FieldNameTextBox.Text
                }
                    );
            }

            ProgramManager.Database.InsertContentSchema(contentSchema);

            FieldStackPanel.Children.Clear();
            SchemaName.Clear();
        }
Example #2
0
        public static ContentItem ToModel(this RestContentItem restContentItem, ContentSchema schema)
        {
            if (schema == null)
            {
                if (restContentItem.Schema.Type == JTokenType.String)
                {
                    schema = new ContentSchema(restContentItem.Schema.Value <string>());
                }
                else
                {
                    RestContentSchema restSchema = restContentItem.Schema.ToObject <RestContentSchema>(NewtonJsonExtensions.CreateSerializer());
                    schema = restSchema.ToModel();
                }
            }

            ContentItem contentItem = schema.CreateContentItem();

            contentItem.Id            = restContentItem.Id;
            contentItem.CreatedAt     = restContentItem.CreatedAt;
            contentItem.ModifiedAt    = restContentItem.ModifiedAt;
            contentItem.PublishedAt   = restContentItem.PublishedAt;
            contentItem.Version       = restContentItem.Version;
            contentItem.SchemaVersion = restContentItem.SchemaVersion;

            foreach (var restField in restContentItem.Fields)
            {
                restField.Value.FromRestValue(restField.Key, contentItem, schema);
            }

            return(contentItem);
        }
Example #3
0
        public async Task CreateDataAsync(IDataStorage dataStorage)
        {
            //create brand schema
            ContentSchema schemaBrand = new ContentSchema("Brand")
                                        .AddString("Name")
                                        .AddSlug("Slug")
                                        .AddTextArea("Description");

            //Define schema for product
            ContentSchema schemaProduct = new ContentSchema("Product")
                                          .AddReference("Brand")
                                          .AddString("Name", options => options.IsRequired = true)
                                          .AddSlug("Slug")
                                          .AddBool("IsAvailable", options => options.DefaultValue = true)
                                          .AddFloat("Price")
                                          .AddTextArea("Description", options => options.MaxLength = 255)
                                          .AddAsset("Image")
                                          .AddArray("Attributes", options => options
                                                    .AddString("Name")
                                                    .AddString("Value"));

            ContentItem contentProduct = schemaProduct
                                         .CreateContentItem()
                                         .SetReference("Brand", new ContentItem(Guid.Parse(""), schemaBrand))
                                         .SetString("Name", "ProductA")
                                         .SetBool("IsAvailable", true)
                                         .SetFloat("Price", 9.99)
                                         .SetAsset("Image", new Asset())
                                         .SetTextArea("Description", "...")
                                         .AddArrayFieldItem("Attributes", schemaProduct, item => item
                                                            .SetString("Name", "Size")
                                                            .SetString("Value", "M"));

            //await dataStorage.CreateAsync(schemaProduct);


            var schemas = new Faker <ContentItem>("de")
                          .CustomInstantiator(x => schemaBrand.CreateContentItem())
                          .FinishWith((f, c) =>
            {
                c.SetString("Name", f.Vehicle.Manufacturer());
                c.SetSlug("Slug", c.GetField <StringField>("Name").Value.ToSlug());
                c.SetTextArea("Description", f.Rant.Review());
            })
                          .GenerateLazy(100);

            var product = new Faker <ContentItem>("de")
                          .CustomInstantiator(x => schemaProduct.CreateContentItem())
                          .FinishWith((f, c) =>
            {
                c.SetString("Name", f.Vehicle.Model());
                c.SetSlug("Slug", c.GetField <StringField>("Name").Value.ToSlug());
                c.SetBool("IsAvailable", f.Random.Bool(0.8f));
                c.SetTextArea("Description", f.Rant.Review());
            })
                          .GenerateLazy(100);
        }
Example #4
0
        public static ContentEmbedded ToModel(this RestContentEmbedded restContentItem)
        {
            ContentSchema schema = restContentItem.Schema.ToModel();

            ContentEmbedded contentItem = schema.CreateContentEmbedded();

            foreach (var restField in restContentItem.Fields)
            {
                restField.Value.FromRestValue(restField.Key, contentItem, schema);
            }

            return(contentItem);
        }
        public async Task InvokeAsync(
            HttpContext context,
            ISchemaStorage schemaStorage,
            JsonService jsonService)
        {
            Guid id = Guid.Parse((string)context.GetRouteValue("id"));

            RestContentSchema input = await jsonService.Deserialize <RestContentSchema>(context.Request.Body);

            ContentSchema m = input.ToModel();

            await schemaStorage.UpdateAsync(m);
        }
Example #6
0
        public async Task InvokeAsync(
            HttpContext context,
            ISchemaStorage schemaStorage,
            JsonService jsonService)
        {
            string name = (string)context.GetRouteValue("name");

            RestContentSchema input = await jsonService.Deserialize <RestContentSchema>(context.Request.Body);

            ContentSchema m = input.ToModel();

            await schemaStorage.CreateAsync(m);

            var result = new ResourceCreated()
            {
                Id = m.Id
            };

            string json = jsonService.Serialize(result);

            await context.Response.WriteAsync(json);
        }
        public async Task <QueryResult <ContentItem> > Query(string schemaName, QueryParameters queryParameters)
        {
            ContentSchema schema = await GetContentSchemaAsync(schemaName);

            var items = GetMongoCollection(schemaName);

            List <FilterDefinition <MongoContentItem> > query = new List <FilterDefinition <MongoContentItem> >();

            if (string.IsNullOrEmpty(queryParameters.SearchPattern) == false)
            {
                List <FilterDefinition <MongoContentItem> > patternQuery = new List <FilterDefinition <MongoContentItem> >();

                foreach (string field in schema.Fields
                         .Where(x => x.Value.FieldType == ContentFieldManager.Default.GetContentFieldName <StringField>())
                         .Select(x => x.Key))
                {
                    patternQuery.Add(Builders <MongoContentItem> .Filter.Regex($"Fields.{field}", new BsonRegularExpression(queryParameters.SearchPattern, "i")));
                }

                if (patternQuery.Count > 1)
                {
                    query.Add(Builders <MongoContentItem> .Filter.Or(patternQuery));
                }
                else if (patternQuery.Count == 1)
                {
                    query.Add(patternQuery.First());
                }
                else
                {
                }
            }

            var findOptions = new FindOptions <MongoContentItem>()
            {
                Limit = queryParameters.Top,
                Skip  = queryParameters.Skip
            };

            //use default order?
            if (queryParameters.OrderFields.Any() == false)
            {
                queryParameters.OrderFields = schema.OrderFields.ToList();
            }

            //order
            foreach (FieldOrder orderField in queryParameters.OrderFields)
            {
                if (orderField.Asc)
                {
                    findOptions.Sort = Builders <MongoContentItem> .Sort.Ascending(orderField.Name);
                }
                else
                {
                    findOptions.Sort = Builders <MongoContentItem> .Sort.Descending(orderField.Name);
                }
            }

            //query
            foreach (FieldQuery fieldQuery in queryParameters.Fields)
            {
                string v = fieldQuery.Value;
                object v2;


                if (v == null)
                {
                    v2 = BsonNull.Value;
                }
                else
                {
                    v2 = fieldQuery.ValueType switch
                    {
                        QueryFieldType.Guid => Guid.Parse(v),
                        QueryFieldType.String => v,
                        QueryFieldType.Double => double.Parse(v),
                        _ => throw new Exception()
                    };
                }

                query.Add(Builders <MongoContentItem> .Filter.Eq(fieldQuery.Name, v2));
            }

            FilterDefinition <MongoContentItem> q;

            if (query.Count >= 2)
            {
                q = Builders <MongoContentItem> .Filter.And(query);
            }
            else if (query.Count == 1)
            {
                q = query.First();
            }
            else
            {
                q = Builders <MongoContentItem> .Filter.Empty;
            }

            var cursor = await items.FindAsync(q, findOptions);

            long totalCount = await items.CountDocumentsAsync(q);

            IList <MongoContentItem> result = await cursor.ToListAsync();

            QueryResult <ContentItem> queryResult = new QueryResult <ContentItem>();

            queryResult.Items      = result.Select(x => x.ToModel(schema)).ToList();
            queryResult.Offset     = queryParameters.Skip;
            queryResult.Count      = queryResult.Items.Count;
            queryResult.TotalCount = totalCount;

            return(queryResult);
        }