Ejemplo n.º 1
0
        private Query ParseQuery(QueryContext context, string query, ISchemaEntity schema)
        {
            using (Profiler.TraceMethod <ContentQueryService>())
            {
                try
                {
                    var model = modelBuilder.BuildEdmModel(context.App, schema, context.IsFrontendClient);

                    var result = model.ParseQuery(query).ToQuery();

                    if (result.Sort.Count == 0)
                    {
                        result.Sort.Add(new SortNode(new List <string> {
                            "lastModified"
                        }, SortOrder.Descending));
                    }

                    if (result.Take > options.MaxResults)
                    {
                        result.Take = options.MaxResults;
                    }

                    return(result);
                }
                catch (NotSupportedException)
                {
                    throw new ValidationException("OData operation is not supported.");
                }
                catch (ODataException ex)
                {
                    throw new ValidationException($"Failed to parse query: {ex.Message}", ex);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IReadOnlyList <IContentEntity> > QueryAsync(IAppEntity app, Guid schemaId, bool nonPublished, HashSet <Guid> ids, string odataQuery)
        {
            var contentEntities = (List <IContentEntity>)null;

            await ForSchemaAsync(app.Id, schemaId, async (collection, schemaEntity) =>
            {
                IFindFluent <MongoContentEntity, MongoContentEntity> cursor;
                try
                {
                    var model = modelBuilder.BuildEdmModel(schemaEntity, app);

                    var parser = model.ParseQuery(odataQuery);

                    cursor =
                        collection
                        .Find(parser, ids, schemaEntity.Id, schemaEntity.Schema, nonPublished)
                        .Take(parser)
                        .Skip(parser)
                        .Sort(parser, schemaEntity.Schema);
                }
                catch (NotSupportedException)
                {
                    throw new ValidationException("This odata operation is not supported");
                }
                catch (NotImplementedException)
                {
                    throw new ValidationException("This odata operation is not supported");
                }
                catch (ODataException ex)
                {
                    throw new ValidationException($"Failed to parse query: {ex.Message}", ex);
                }

                var entities = await cursor.ToListAsync();

                foreach (var entity in entities)
                {
                    entity.ParseData(schemaEntity.Schema);
                }

                contentEntities = entities.OfType <IContentEntity>().ToList();
            });

            return(contentEntities);
        }
Ejemplo n.º 3
0
        public Task Should_throw_if_query_is_invalid()
        {
            A.CallTo(() => appProvider.GetSchemaAsync(appId, schemaId, false))
            .Returns(schema);

            A.CallTo(() => modelBuilder.BuildEdmModel(schema, app))
            .Throws(new ODataException());

            return(Assert.ThrowsAsync <ValidationException>(() => sut.QueryAsync(context.WithSchemaId(schemaId), Query.Empty.WithODataQuery("query"))));
        }
Ejemplo n.º 4
0
        public Task Should_throw_if_query_is_invalid()
        {
            A.CallTo(() => appProvider.GetSchemaAsync(appId, schemaId, false))
            .Returns(schema);

            A.CallTo(() => modelBuilder.BuildEdmModel(schema, app))
            .Throws(new ODataException());

            return(Assert.ThrowsAsync <ValidationException>(() => sut.QueryAsync(app, schemaId.ToString(), user, false, "query")));
        }
Ejemplo n.º 5
0
        public Task Should_throw_if_query_is_invalid()
        {
            SetupClaims();
            SetupSchema();

            A.CallTo(() => modelBuilder.BuildEdmModel(schema, app))
            .Throws(new ODataException());

            return(Assert.ThrowsAsync <ValidationException>(() => sut.QueryAsync(context.WithSchemaId(schemaId), Q.Empty.WithODataQuery("query"))));
        }
Ejemplo n.º 6
0
        public ODataQueryTests()
        {
            var builder = new EdmModelBuilder(new MemoryCache(Options.Create(new MemoryCacheOptions())));

            var schemaEntity = new Mock <ISchemaEntityWithSchema>();

            schemaEntity.Setup(x => x.Id).Returns(Guid.NewGuid());
            schemaEntity.Setup(x => x.Version).Returns(3);
            schemaEntity.Setup(x => x.Schema).Returns(schema);

            edmModel = builder.BuildEdmModel(schemaEntity.Object, languages);
        }
Ejemplo n.º 7
0
        private ODataUriParser ParseQuery(IAppEntity app, string query, ISchemaEntity schema)
        {
            try
            {
                var model = modelBuilder.BuildEdmModel(schema, app);

                return(model.ParseQuery(query));
            }
            catch (ODataException ex)
            {
                throw new ValidationException($"Failed to parse query: {ex.Message}", ex);
            }
        }
Ejemplo n.º 8
0
        public ODataQueryTests()
        {
            schemaDef.Update(new SchemaProperties {
                Hints = "The User"
            });

            schemaDef.AddField(new StringField(1, "firstName", Partitioning.Language,
                                               new StringFieldProperties {
                Label = "FirstName", IsRequired = true, AllowedValues = new[] { "1", "2" }
            }));
            schemaDef.AddField(new StringField(2, "lastName", Partitioning.Language,
                                               new StringFieldProperties {
                Hints = "Last Name", Editor = StringFieldEditor.Input
            }));

            schemaDef.AddField(new BooleanField(3, "isAdmin", Partitioning.Invariant,
                                                new BooleanFieldProperties()));

            schemaDef.AddField(new NumberField(4, "age", Partitioning.Invariant,
                                               new NumberFieldProperties {
                MinValue = 1, MaxValue = 10
            }));

            schemaDef.AddField(new DateTimeField(5, "birthday", Partitioning.Invariant,
                                                 new DateTimeFieldProperties()));

            schemaDef.AddField(new AssetsField(6, "pictures", Partitioning.Invariant,
                                               new AssetsFieldProperties()));

            schemaDef.AddField(new ReferencesField(7, "friends", Partitioning.Invariant,
                                                   new ReferencesFieldProperties()));

            schemaDef.AddField(new StringField(8, "dashed-field", Partitioning.Invariant,
                                               new StringFieldProperties()));

            var builder = new EdmModelBuilder(new MemoryCache(Options.Create(new MemoryCacheOptions())));

            var schema = A.Dummy <ISchemaEntity>();

            A.CallTo(() => schema.Id).Returns(Guid.NewGuid());
            A.CallTo(() => schema.Version).Returns(3);
            A.CallTo(() => schema.SchemaDef).Returns(schemaDef);

            var app = A.Dummy <IAppEntity>();

            A.CallTo(() => app.Id).Returns(Guid.NewGuid());
            A.CallTo(() => app.Version).Returns(3);
            A.CallTo(() => app.PartitionResolver).Returns(languagesConfig.ToResolver());

            edmModel = builder.BuildEdmModel(schema, app);
        }
Ejemplo n.º 9
0
        private ODataUriParser ParseQuery(QueryContext context, string query, ISchemaEntity schema)
        {
            using (Profiler.TraceMethod<ContentQueryService>())
            {
                try
                {
                    var model = modelBuilder.BuildEdmModel(schema, context.App);

                    return model.ParseQuery(query);
                }
                catch (ODataException ex)
                {
                    throw new ValidationException($"Failed to parse query: {ex.Message}", ex);
                }
            }
        }
Ejemplo n.º 10
0
        public ODataQueryTests()
        {
            var builder = new EdmModelBuilder(new MemoryCache(Options.Create(new MemoryCacheOptions())));

            var schema = A.Dummy <ISchemaEntity>();

            A.CallTo(() => schema.Id).Returns(Guid.NewGuid());
            A.CallTo(() => schema.Version).Returns(3);
            A.CallTo(() => schema.SchemaDef).Returns(schemaDef);

            var app = A.Dummy <IAppEntity>();

            A.CallTo(() => app.Id).Returns(Guid.NewGuid());
            A.CallTo(() => app.Version).Returns(3);
            A.CallTo(() => app.PartitionResolver).Returns(languagesConfig.ToResolver());

            edmModel = builder.BuildEdmModel(schema, app);
        }
Ejemplo n.º 11
0
        public ODataQueryTests()
        {
            var builder = new EdmModelBuilder(new MemoryCache(Options.Create(new MemoryCacheOptions())));

            var schemaEntity = new Mock <ISchemaEntity>();

            schemaEntity.Setup(x => x.Id).Returns(Guid.NewGuid());
            schemaEntity.Setup(x => x.Version).Returns(3);
            schemaEntity.Setup(x => x.Schema).Returns(schema);

            var appEntity = new Mock <IAppEntity>();

            appEntity.Setup(x => x.Id).Returns(Guid.NewGuid());
            appEntity.Setup(x => x.Version).Returns(3);
            appEntity.Setup(x => x.PartitionResolver).Returns(languagesConfig.ToResolver());

            edmModel = builder.BuildEdmModel(schemaEntity.Object, appEntity.Object);
        }
Ejemplo n.º 12
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IEdmModel model1 = EdmModelBuilder.GetEdmModel();
            IEdmModel model2 = EdmModelBuilder.BuildEdmModel();
            IEdmModel model3 = EnumsEdmModel.GetConventionModel();
            IEdmModel model4 = EnumsEdmModel.GetExplicitModel();

            services.AddControllers();

            services.AddOData(opt =>
                              opt
                              .AddModel(model1)
                              .AddModel("odata", model2)
                              .AddModel("v{version}", model1)
                              .AddModel("convention", model3)
                              .AddModel("explicit", model4))
            .AddConvention <MyEntitySetRoutingConvention>();
        }
Ejemplo n.º 13
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            IEdmModel model1 = EdmModelBuilder.GetEdmModel();
            IEdmModel model2 = EdmModelBuilder.BuildEdmModel();
            IEdmModel model3 = EnumsEdmModel.GetConventionModel();
            IEdmModel model4 = EnumsEdmModel.GetExplicitModel();

            services.AddControllers().AddOData(opt =>
                                               opt
                                               .AddRouteComponents(model1)
                                               .AddRouteComponents("odata", model2)
                                               .AddRouteComponents("v{version}", model1)
                                               .AddRouteComponents("convention", model3)
                                               .AddRouteComponents("explicit", model4)
                                               .Conventions.Add(new MyEntitySetRoutingConvention()));

            services.AddSwaggerGen();
        }