public QueryConfigObjectGraphType(QueryBuilderFactory queryBuilderFactory)
        {
            Name = "query";

            queryBuilderFactory.Create <MyQuiz>(this, "GetAllMyQuizes")
            .WithParameterBuilder()
            .BeginQuery <MyQuiz>()
            .WithPropertyFromSource(x => x.Owner, ctx => GetEmail(ctx))
            .BuildQueryResult(ctx =>
            {
                ctx.SetResults(ctx.GetQueryResults <MyQuiz>());
            })
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Question>(this, "GetMyQuestions")
            .WithParameterBuilder()
            .BeginQuery <Question>()
            .WithProperty(x => x.QuizId)
            .WithPropertyFromSource(x => x.Owner, ctx => GetEmail(ctx))
            .BuildQueryResult(ctx =>
            {
                ctx.SetResults(ctx.GetQueryResults <Question>());
            })
            .BuildQuery()
            .BuildWithListResult();
        }
        public TestDocumentDbQuery(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Creating document db queries.");

            Name = "query";

            queryBuilderFactory.Create <Model1>(this, "searchModel1", "Search for a single Model 1 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model2>(this, "searchModel2", "Search for a single Model 2 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model5>(this, "searchModel5", "Search for a single Model 5 by Id")
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();
        }
        public QueryConfigObjectGraphType(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            Name = "query";

            // 1) Defines a simple way to get a single product by using the Key
            // which is the Id property in this case.
            queryBuilderFactory.Create <Product>(this, "GetProductById")
            .WithParameterBuilder()                         // 2) Starts the query builder.
            .WithKeys()                                     // 3) Specify the use of Key attribute. This will create a query parameter on the client side to send in.
            .BuildQuery()                                   // 4) Finalize the query builder.
            .BuildWithSingleResult();                       // 5) Specify that we are expected to get back a single entity.

            queryBuilderFactory.Create <CustomerOrder>(this, "GetOrderById")
            .WithParameterBuilder()
            .BeginQuery <Order>()
            .WithProperty(x => x.Id)
            .BuildQueryResult((ctx) =>
            {
                var results = ctx.GetQueryResults <Order>();

                // Only a single order should match.
                var order = results.Single();

                ctx.SetResults(new List <CustomerOrder>
                {
                    new CustomerOrder
                    {
                        Id       = order.Id,
                        IsActive = order.IsActive,
                        Ordered  = order.Ordered
                    }
                });
                ctx.Items["CustomerIdList"] = results.Select(x => (object)x.CustomerId).ToList();
                ctx.Items["ProductIdList"]  = order.ProductIdList.Select(x => (object)x).ToList();
            })
            .ThenWithQuery <Product>()
            .WithPropertyFromSource(y => y.Id, ctx => (List <object>)ctx.Items["ProductIdList"])
            .BuildQueryResult(ctx =>
            {
                var customerOrder      = ctx.GetResults <CustomerOrder>().Single();
                customerOrder.Products = ctx.GetQueryResults <Product>();
            })
            .ThenWithQuery <Customer>()
            .WithPropertyFromSource(y => y.Id, ctx => (List <object>)ctx.Items["CustomerIdList"])
            .BuildQueryResult(ctx =>
            {
                var customerOrder      = ctx.GetResults <CustomerOrder>().Single();
                customerOrder.Customer = ctx.GetQueryResults <Customer>().Single();
            })
            .BuildQuery()
            .BuildWithSingleResult();

            //--End--
        }
Exemplo n.º 4
0
        public TestSearchQuery2(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Creating queries.");

            Name = "query";

            queryBuilderFactory.Create <MySearch4Result>(this, "searchModel4", "Search for all Model4")
            .WithParameterBuilder()
            .BeginSearch()
            .Add <MySearch4>()
            .BuildWithAggregate()
            .BuildQueryResult(ctx =>
            {
                var searches = ctx.GetQueryResults <SearchResultModel>();

                var result        = new MySearch4Result();
                result.Results    = searches.GetTypeList <MySearch4>();
                result.Aggregates = new List <SearchAggregateModel>();

                var agg = ctx.GetSystemItems <SearchResult>();
                agg.ForEach(a => result.Aggregates.AddRange(a.Aggregates));

                ctx.SetResults(new List <MySearch4Result> {
                    result
                });
            }).BuildQuery().BuildWithSingleResult();
        }
 public static void AddGetModel7WithModel8FieldAndConnectionFieldDescription(this TestDocumentDbQuery current, QueryBuilderFactory queryBuilderFactory)
 {
     // Find Model7 coming from Model8
     queryBuilderFactory.Create <Model7>(current, "GetModel7WithModel8FieldAndConnectionFieldDescription", "Get Model7 With Model8 Field And Connection Field Description")
     .WithParameterBuilder()
     .BeginQuery <Model8>()
     // Use field from Model8 as a starting point to search from.
     .WithProperty(x => x.Field)
     .BuildQueryResult(ctx =>
     {
         ctx.Items["model8IdList"] = ctx.GetQueryResults <Model8>().Select(x => (object)x.Id).ToList();
     })
     .WithConnectionEdgeBuilder <Model7ToModel8>()
     // Now, we match Model7ToModel8's DestinationId with Model8 Id.
     .WithDestinationIdFromSource(ctx =>
     {
         return((List <object>)ctx.Items["model8IdList"]);
     })
     .WithProperty(x => x.FieldDescription)
     .BuildConnectionEdgeParameters(ctx =>
     {
         ctx.Items["model7IdList"] = ctx.GetQueryResults <Model7ToModel8>().Select(x => x.Id)
                                     .Distinct()
                                     .Select(x => (object)x).ToList();
     })
     .ThenWithQuery <Model7>()
     .WithPropertyFromSource(x => x.Id, ctx =>
     {
         return((List <object>)ctx.Items["model7IdList"]);
     })
     .BuildQueryResult(ctx => { })
     .BuildQuery()
     .BuildWithListResult();
 }
 public static void AddPublisherQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
 {
     queryBuilderFactory.Create <Publisher>(booksQuery, GetPublisherByIdQuery)
     .WithCache(TimeSpan.FromSeconds(10))
     .WithParameterBuilder()
     .WithProperty(x => x.Id)
     .BuildQuery()
     .BuildWithSingleResult();
 }
Exemplo n.º 7
0
        public QueryConfigObjectGraphType(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            Name = "query";

            //--Begin--

            queryBuilderFactory.Create <Customer>(this, "GetCustomerById", "Get Customer By Id.")
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Order>(this, "GetOrderById", "Get Order By Id.")
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            //--End--
        }
Exemplo n.º 8
0
        private void TestCreateBuilder <T>(Action <IQueryBuilder <T> > assert)
        {
            // arrange
            var target = new QueryBuilderFactory();

            // act
            var result = target.Create <T>();

            // assert
            assert(result);
        }
Exemplo n.º 9
0
        public AdminQuery(QueryBuilderFactory queryBuilderFactory,
                          ILogger logger,
                          IConfiguration configuration)
        {
            logger.LogInformation("Building queries.");

            Name = "query";

            queryBuilderFactory.Create <Organization>(this, "GetOrganizationById")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Organization>(this, "GetAllOrganizations")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithParameterBuilder()
            .BuildQuery()
            .BuildWithListResult();
            _configuration = configuration;
        }
        public static void AddBooksQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
        {
            // Example 1: We are getting a single Book. You are defining the argument yourself to pass into the repository with context. There's no caching and paging support. This is what comes out-of-the-box.

            queryBuilderFactory.Create <Book>(booksQuery, "getBookNoCache")
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            // Example 2: We are getting a single Book. The argument to pass into the repository is defined by the Model with at least one property with the KeyAttribute.
            //            The work is done by the cache repository which will cache the book result for a specific time you have defined. There's no paging support.

            queryBuilderFactory.Create <Book>(booksQuery, "getBook")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            // Example 3: We are getting a list of Books based on an argument. You are defining the key to pass into the repository without having to use context directly.
            //            The cache repository which will cache the book result for a specific time you have defined. There's no paging support.

            queryBuilderFactory.Create <Book>(booksQuery, "getBooksByCategory")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .WithProperty(x => x.Category)
            .BuildQuery()
            .BuildWithListResult();

            // Example 4: We are getting a list of paged Books. Technically, you are able to get all books by using TotalCount, although there's already a default page limit of 10 items per page if you don't specify.
            //            There's no caching support.

            queryBuilderFactory.Create <Book>(booksQuery, "getPagedBooks")
            .WithPaging()
            .BuildWithListResult();

            queryBuilderFactory.Create <Book>(booksQuery, "getPagedBooksByCategory")
            .WithPaging()
            .WithParameterBuilder()
            .WithProperty(x => x.Category, true)
            .BuildQuery()
            .BuildWithListResult();

            // Example 6: We are getting a list of paged Books with a argument to be passed in. You are defining the key to pass into the repository without having to use context directly.
            //            The cache repository which will cache the book result for a specific time you have defined. You will get paged results with a default page limit of 10 items per page if you don't specify.

            queryBuilderFactory.Create <Book>(booksQuery, "getCachedPagedBooksByCategory")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .WithProperty(x => x.Category)
            .BuildQuery()
            .BuildWithListResult();
        }
Exemplo n.º 11
0
        public BooksQuery(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Creating queries.");

            Name = "query";

            this.AddBooksQueries(queryBuilderFactory);

            this.AddPublisherQueries(queryBuilderFactory);

            this.AddBookReviewQueries(queryBuilderFactory);

            queryBuilderFactory.Create <Author>(this, "getAuthorByHomeCity")
            .WithParameterBuilder()
            .WithProperty(x => x.HomeCity)
            .BuildQuery()
            .BuildWithListResult();
        }
 public static void AddGetModel7FromConnectionEdge(this TestDocumentDbQuery current, QueryBuilderFactory queryBuilderFactory)
 {
     // This query is really only meant to demostrate the ability to query for connection edges from the node itself
     // even if user's query does not include the connection model. This is achieved via WithSourceIdFromSource.
     // Typically, the use case may be that the user is performing a search (which doesn't use connection model). This
     // means as a developer, you may want to query the connection edge yourself to figure out the child node hanging on
     // the connection model.
     queryBuilderFactory.Create <Model7>(current, "GetModel7FromConnectionEdge")
     .WithParameterBuilder()
     .BeginQuery <Model7>().WithProperty(x => x.Id)
     .BuildQueryResult(ctx =>
     {
         ctx.Items["idList"] = ctx.GetQueryResults <Model7>().Select(x => (object)x.Id).ToList();
     })
     .WithConnectionEdgeBuilder <Model7ToModel8>()
     .WithSourceIdFromSource <Model7>(x => (List <object>)x.Items["idList"])
     .BuildConnectionEdgeParameters(ctx =>
     {
         var connectionEdges = ctx.GetResults <ConnectionEdge>();
     })
     .BuildQuery()
     .BuildWithSingleResult();
 }
Exemplo n.º 13
0
        public TestDocumentDbQuery(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Creating document db queries.");

            Name = "query";

            queryBuilderFactory.Create <Model1>(this, "searchModel1", "Search for a single Model 1 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model2>(this, "searchModel2", "Search for a single Model 2 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model5>(this, "searchModel5", "Search for a single Model 5 by Id")
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model6>(this, "searchModel6", "Search for a single Model 6 by Id")
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model6>(this, "searchAllModel6", "Get all Model 6")
            .WithParameterBuilder()
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model6>(this, "searchAllModel6ByOptionalField", "Get all Model 6")
            .WithParameterBuilder()
            .WithProperty(x => x.Field, true)
            .WithProperty(x => x.Field2, true)
            .WithProperty(x => x.Field3, true)
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model7>(this, "GetModel7WithModel8Id", "Get Model7")
            .WithParameterBuilder()
            .WithConnectionEdgeBuilder <Model7ToModel8>()
            .WithDestinationId()
            .BuildConnectionEdgeParameters()
            .BuildQuery()
            .BuildWithListResult();

            // Find Model7 coming from Model8
            queryBuilderFactory.Create <Model7>(this, "GetModel7WithModel8Field", "Get Model7 With Model8 Field")
            .WithParameterBuilder()
            .BeginQuery <Model8>()
            // Use field from Model8 as a starting point to search from.
            .WithProperty(x => x.Field)
            .BuildQueryResult(ctx =>
            {
                ctx.Items["model8IdList"] = ctx.GetQueryResults <Model8>().Select(x => (object)x.Id).ToList();
            })
            .WithConnectionEdgeBuilder <Model7ToModel8>()
            // Now, we match Model7ToModel8's DestinationId with Model8 Id.
            .WithDestinationIdFromSource(ctx =>
            {
                return((List <object>)ctx.Items["model8IdList"]);
            })
            .BuildConnectionEdgeParameters(ctx =>
            {
                ctx.Items["model7IdList"] = ctx.GetQueryResults <Model7ToModel8>().Select(x => (object)x.Id).ToList();
            })
            .ThenWithQuery <Model7>()
            .WithPropertyFromSource(x => x.Id, ctx =>
            {
                return((List <object>)ctx.Items["model7IdList"]);
            })
            .BuildQueryResult(ctx => { })
            .BuildQuery()
            .BuildWithListResult();

            this.AddGetModel7WithModel8FieldAndConnectionFieldDescription(queryBuilderFactory);

            this.AddGetModel7FromConnectionEdge(queryBuilderFactory);
        }
Exemplo n.º 14
0
        public TestSearchQuery(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Creating queries.");

            Name = "query";

            queryBuilderFactory.Create <Model1>(this, "searchModel1", "Search for a single Model 1 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model2>(this, "searchModel2", "Search for a single Model 2 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <MySearchResult>(this, "search1", "Searches across Models.")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginSearch()
            .Add <MySearch1>()
            .Add <MySearch2>()
            .Add <MySearch3>()
            .Build()
            .BuildQueryResult(ctx =>
            {
                var searches = ctx.GetQueryResults <SearchResultModel>();
                var search1  = searches.GetTypeList <MySearch1>();
                var search2  = searches.GetTypeList <MySearch2>();
                var search3  = searches.GetTypeList <MySearch3>();

                //ctx.Items["search1"] = search1;
                ctx.Items["search1IdList"] = search1.Select(x => (object)x.Id).ToList();

                //ctx.Items["search2"] = search1;
                ctx.Items["search2IdList"] = search2.Select(x => (object)x.Id).ToList();

                //ctx.Items["search3"] = search1;
                ctx.Items["search3IdList"] = search3.Select(x => (object)x.Id).ToList();
            })
            .ThenWithQuery <Model1>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search1IdList"]).BuildQueryResult(x =>
            {
                var results = x.GetQueryResults <Model1>();
                x.SetResults(results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList());
            })
            .ThenWithQuery <Model2>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search2IdList"]).BuildQueryResult(x =>
            {
                var results        = x.GetQueryResults <Model2>();
                var currentResults = results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList();
                currentResults.AddRange(x.GetResults <MySearchResult>());
                x.SetResults(currentResults);
            })
            .ThenWithQuery <Model3V2>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search3IdList"]).BuildQueryResult(x =>
            {
                var results        = x.GetQueryResults <Model3V2>();
                var currentResults = results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList();
                currentResults.AddRange(x.GetResults <MySearchResult>());
                x.SetResults(currentResults);
            })
            .BuildQuery().BuildWithListResult();

            queryBuilderFactory.Create <MySearchResult2>(this, "searchWithAggregate", "Searches across Models.")
            .WithParameterBuilder()
            .BeginSearch()
            .Add <MySearch3>()
            .BuildWithAggregate()
            .BuildQueryResult(ctx =>
            {
                var searches = ctx.GetSystemItems <SearchResult>();
                var output   = new MySearchResult2();

                var idList = new List <string>();
                searches.ForEach(search =>
                {
                    idList.AddRange(search.Values.Select(y => ((MySearch3)y.Value).Id));
                    output.Aggregates.AddRange(search.Aggregates);
                });

                ctx.Items["Output"]        = output;
                ctx.Items["search3IdList"] = idList.Select(id => (object)id).ToList();
            })
            .ThenWithQuery <Model3V2>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search3IdList"]).BuildQueryResult(ctx =>
            {
                var results    = ctx.GetQueryResults <Model3V2>();
                var output     = (MySearchResult2)ctx.Items["Output"];
                output.Results = results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList();

                ctx.SetResults(new List <MySearchResult2> {
                    output
                });
            }).BuildQuery().BuildWithSingleResult();
        }
Exemplo n.º 15
0
        public MyQuery(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Building queries.");

            Name = "query";

            queryBuilderFactory.Create <Exam>(this, "GetExamById")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <TestResult>(this, "GetTestResultById")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <TestResult>(this, "GetTestResultByCandidateById")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithParameterBuilder()
            .WithConnectionEdgeBuilder <Candidate>()
            .WithDestinationId()
            .BuildConnectionEdgeParameters()
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <TestResult>(this, "GetTestResultsByCandidateFirstNameAndTaken")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithCache(TimeSpan.FromSeconds(30))
            .WithParameterBuilder()
            .BeginQuery <Employee>()
            .WithProperty(x => x.FirstName)
            .BuildQueryResult(ctx => ctx.Items["employeeIdList"] = ctx.GetQueryResults <Employee>().Select(x => (object)x.Id).ToList())
            .WithConnectionEdgeBuilder <Candidate>()
            .WithDestinationIdFromSource(ctx => (List <object>)ctx.Items["employeeIdList"])
            .WithProperty(x => x.Taken)
            .BuildConnectionEdgeParameters(ctx =>
            {
                ctx.Items["testResultIdList"] = ctx.GetQueryResults <Candidate>().Select(x => (object)x.Id).ToList();
            })
            .ThenWithQuery <TestResult>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["testResultIdList"])
            .BuildQueryResult(ctx => { })
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <TestResult>(this, "SearchExams")
            .AssertWithClaimsPrincipal(DefaultAssertion)
            .WithCache(TimeSpan.FromSeconds(30))
            .WithParameterBuilder()
            .BeginSearch()
            .Add <EmployeeSearch>().Add <ExamSearch>().Build()
            .BuildQueryResult(ctx =>
            {
                var searches                = ctx.GetQueryResults <SearchResultModel>();
                ctx.Items["examIdList"]     = searches.GetTypeList <ExamSearch>().Select(x => (object)x.Id).ToList();
                ctx.Items["employeeIdList"] = searches.GetTypeList <EmployeeSearch>().Select(x => (object)x.Id).ToList();
            })
            .WithConnectionEdgeBuilder <Candidate>()
            .WithDestinationIdFromSource(ctx => (List <object>)ctx.Items["employeeIdList"])
            .BuildConnectionEdgeParameters(ctx =>
            {
                // Source Id refers to the TestResult.
                ctx.Items["testResultIdList"] = ctx.GetResults <ConnectionEdge>()
                                                .Select(x => (object)x.SourceId).ToList();
            })
            .WithConnectionEdgeBuilder <ExamPublication>()
            .WithSourceIdFromSource <Exam>(ctx => (List <object>)ctx.Items["examIdList"])
            .BuildConnectionEdgeParameters(ctx =>
            {
                // Use Publication Id to find TestResult
                ctx.Items["publicationIdList"] = ctx.GetResults <ConnectionEdge>()
                                                 .Select(x => (object)x.DestinationId).ToList();
            })
            .WithConnectionEdgeBuilder <TestResultPublication>()
            .WithDestinationIdFromSource(ctx => (List <object>)ctx.Items["publicationIdList"])
            .BuildConnectionEdgeParameters(ctx =>
            {
                var list = ((List <object>)ctx.Items["testResultIdList"]).Select(x => (string)x).ToList();
                list.AddRange(ctx.GetResults <ConnectionEdge>().Select(x => x.SourceId));
                ctx.Items["testResultIdList"] = list.Distinct().Select(x => (object)x).ToList();
            })
            // WithConnectionEdgeBuilder allows us to search back to TestResult using the WithSourceIdFromSource.
            // Don't use WithPropertyFromSource as it doesn't allow us to query correctly on connections.
            .WithConnectionEdgeBuilder <TestResultPublication>()
            .WithSourceIdFromSource <TestResult>(ctx => (List <object>)ctx.Items["testResultIdList"])
            .BuildConnectionEdgeParameters(ctx => { })
            .BuildQuery().BuildWithListResult();
        }
Exemplo n.º 16
0
        public TestSearchQuery(QueryBuilderFactory queryBuilderFactory, ILogger logger)
        {
            logger.LogInformation("Creating queries.");

            Name = "query";

            queryBuilderFactory.Create <Model1>(this, "searchModel1", "Search for a single Model 1 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <Model2>(this, "searchModel2", "Search for a single Model 2 by Id")
            .WithCache(TimeSpan.FromSeconds(15))
            .WithParameterBuilder()
            .WithProperty(x => x.Id)
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <MySearchResult>(this, "search1", "Searches across Models.")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginSearch(typeof(MySearch1), typeof(MySearch2), typeof(MySearch3))
            .BuildQueryResult(ctx =>
            {
                var searches = ctx.GetQueryResults <SearchResultModel>();
                var search1  = searches.GetTypeList <MySearch1>();
                var search2  = searches.GetTypeList <MySearch2>();
                var search3  = searches.GetTypeList <MySearch3>();

                ctx.Items["search1"]       = search1;
                ctx.Items["search1IdList"] = search1.Select(x => (object)x.Id).ToList();

                ctx.Items["search2"]       = search1;
                ctx.Items["search2IdList"] = search2.Select(x => (object)x.Id).ToList();

                ctx.Items["search3"]       = search1;
                ctx.Items["search3IdList"] = search3.Select(x => (object)x.Id).ToList();
            })
            .ThenWithQuery <Model1>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search1IdList"]).BuildQueryResult(x =>
            {
                var results = x.GetQueryResults <Model1>();
                x.SetResults(results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList());
            })
            .ThenWithQuery <Model2>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search2IdList"]).BuildQueryResult(x =>
            {
                var results        = x.GetQueryResults <Model2>();
                var currentResults = results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList();
                currentResults.AddRange(x.GetResults <MySearchResult>());
                x.SetResults(currentResults);
            })
            .ThenWithQuery <Model3>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["search3IdList"]).BuildQueryResult(x =>
            {
                var results        = x.GetQueryResults <Model3>();
                var currentResults = results.Select(r => new MySearchResult
                {
                    DateField   = r.DateField,
                    DoubleField = r.DoubleField,
                    Field       = r.Field,
                    Id          = r.Id,
                    IntField    = r.IntField
                }).ToList();
                currentResults.AddRange(x.GetResults <MySearchResult>());
                x.SetResults(currentResults);
            })
            .BuildQuery().BuildWithListResult();
        }
        public TestStorageQueryConfigObjectGraphType(QueryBuilderFactory queryBuilderFactory)
        {
            Name = "query";

            queryBuilderFactory.Create <Model7>(this, "GetModel7WithIdFromHeader", "Get Model7")
            .WithParameterBuilder()
            .WithConnectionEdgeBuilder <Model7ToModel8>()
            .WithSourceIdFromSource <Model7>(ctx => new List <object> {
                (string)ctx.RequestContext.HttpRequest.Request.Headers["Model7IdKey"]
            })
            .BuildConnectionEdgeParameters()
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model7>(this, "GetModel7WithModel8Id", "Get Model7")
            .WithParameterBuilder()
            .WithConnectionEdgeBuilder <Model7ToModel8>()
            .WithDestinationId()
            .BuildConnectionEdgeParameters()
            .BuildQuery()
            .BuildWithListResult();


            // Find Model7 coming from Model8
            queryBuilderFactory.Create <Model7>(this, "GetModel7WithModel8FieldAndConnectionFieldDescription", "Get Model7 With Model8 Field And Connection Field Description")
            .WithParameterBuilder()
            .BeginQuery <Model8>()
            // Use field from Model8 as a starting point to search from.
            .WithProperty(x => x.Field)
            .BuildQueryResult(ctx =>
            {
                ctx.Items["model8IdList"] = ctx.GetQueryResults <Model8>().Select(x => (object)x.Id).ToList();
            })
            .WithConnectionEdgeBuilder <Model7ToModel8>()
            // Now, we match Model7ToModel8's DestinationId with Model8 Id.
            .WithDestinationIdFromSource(ctx =>
            {
                return((List <object>)ctx.Items["model8IdList"]);
            })
            .WithProperty(x => x.FieldDescription)
            .BuildConnectionEdgeParameters(ctx =>
            {
                ctx.Items["model7IdList"] = ctx.GetQueryResults <Model7ToModel8>().Select(x => x.Id)
                                            .Distinct()
                                            .Select(x => (object)x).ToList();
            })
            .ThenWithQuery <Model7>()
            .WithPropertyFromSource(x => x.Id, ctx =>
            {
                return((List <object>)ctx.Items["model7IdList"]);
            })
            .BuildQueryResult(ctx => { })
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model9>(this, "GetModel9ById")
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model9>(this, "GetModel9")
            .WithParameterBuilder()
            .BeginQuery <Model9>()
            .WithPropertyFromSource(x => x.Id, ctx => new List <object> {
                "model9_2"
            })
            .WithPropertyFromSource(x => x.Field, ctx => new List <object> {
                "model9 2"
            })
            .BuildQueryResult(ctx => ctx.SetResults(ctx.GetQueryResults <Model9>()))
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model9>(this, "GetModel9Foo")
            .WithParameterBuilder()
            .BeginQuery <Model9>()
            .WithPropertyFromSource(x => x.Id, ctx => new List <object> {
                "model9_2_foo"
            })
            .WithPropertyFromSource(x => x.Field, ctx => new List <object> {
                "model9 2"
            })
            .BuildQueryResult(ctx => ctx.SetResults(ctx.GetQueryResults <Model9>()))
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model13Parent>(this, "GetModel13Parent")
            .WithParameterBuilder()
            .BeginQuery <Model13Parent>()
            .WithPropertyFromSource(x => x.AccountId, ctx =>
            {
                var accountId = ctx.RequestContext.HttpRequest.Request.Headers["AccountId"].Single();
                ctx.Items["AccountIdList"] = new List <object> {
                    accountId
                };
                return(new List <object> {
                    accountId
                });
            })
            .BuildQueryResult(ctx =>
            {
                ctx.Items["IdList"] = ctx.GetQueryResults <Model13Parent>().Select(x => (object)x.SomeKey).ToList();
            })
            .WithConnectionEdgeBuilder <Model13Edge>()
            .WithSourceIdFromSource <Model13Parent>(ctx => (List <object>)ctx.Items["IdList"])
            .ForDestinationFilter <Model13Child>(x => x.AccountId, ctx => (List <object>)ctx.Items["AccountIdList"])
            .BuildConnectionEdgeParameters()
            .BuildQuery()
            .BuildWithListResult();

            queryBuilderFactory.Create <Model14>(this, "GetModel14ById")
            .WithParameterBuilder()
            .WithKeys()
            .BuildQuery()
            .BuildWithListResult();
        }
        public static void AddBookReviewQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
        {
            queryBuilderFactory.Create <BookReviewOutput>(booksQuery, "GetBookReviewByBookName")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginQuery <Book>()
            .WithProperty(x => x.Name)
            .BuildQueryResult(ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["books"] = ctx.GetQueryResults <Book>();
            })
            .ThenWithQuery <BookReview>()
            .WithPropertyFromSource(x => x.BookId, ctx => ctx.GetItems <Book>("books").Select(y => (object)y.Id).ToList())
            .BuildQueryResult(ctx =>
            {
                var bookReviews = ctx.GetQueryResults <BookReview>();

                ctx.Items["reviewerIdList"] = bookReviews.Select(x => (object)x.ReviewerId).ToList();

                var books = ctx.GetItems <Book>("books");

                ctx.SetResults(bookReviews.Select(br => new BookReviewOutput
                {
                    Id         = br.Id,
                    BookId     = br.BookId,
                    Comments   = br.Comments,
                    ReviewerId = br.ReviewerId,
                    Stars      = br.Stars,
                    Book       = books.Single(x => x.Id == br.BookId)
                }).ToList());
            })
            .ThenWithQuery <Reviewer>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["reviewerIdList"])
            .BuildQueryResult(ctx =>
            {
                var reviewers = ctx.GetQueryResults <Reviewer>();
                ctx.GetResults <BookReviewOutput>().ForEach(
                    x => x.Reviewer = reviewers.Single(y => y.Id == x.ReviewerId));
            })
            .BuildQuery()
            .BuildWithListResult();


            queryBuilderFactory.Create <BookReviewOutput>(booksQuery, "GetBookReviewsWithBookNameAndCategory")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginQuery <Book>()                     // Gives you the ability to query with both book name and category.
            .WithProperty(x => x.Name)
            .WithProperty(x => x.Category)
            .BuildQueryResult(ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["books"] = ctx.GetQueryResults <Book>();
            })
            .ThenWithQuery <BookReview>()                    // Gives you the ability to query with book review stars, written on, active reviews and book Id matches.
            .WithPropertyFromSource(x => x.BookId, ctx => ctx.GetItems <Book>("books").Select(y => (object)y.Id).ToList())
            .WithProperty(x => x.Stars)
            .WithProperty(x => x.Active)
            .WithProperty(x => x.WrittenOn)
            .BuildQueryResult(ctx =>
            {
                var bookReviews = ctx.GetQueryResults <BookReview>();

                ctx.Items["reviewerIdList"] = bookReviews.Select(x => (object)x.ReviewerId).ToList();

                var books = ctx.GetItems <Book>("books");

                ctx.SetResults(bookReviews.Select(br => new BookReviewOutput
                {
                    Id         = br.Id,
                    BookId     = br.BookId,
                    Comments   = br.Comments,
                    ReviewerId = br.ReviewerId,
                    Stars      = br.Stars,
                    Book       = books.Single(x => x.Id == br.BookId),
                    WrittenOn  = br.WrittenOn,
                    Active     = br.Active
                }).ToList());
            })
            .ThenWithQuery <Reviewer>()
            .WithPropertyFromSource(x => x.Id, ctx => (List <object>)ctx.Items["reviewerIdList"])
            .BuildQueryResult(ctx =>
            {
                var reviewers = ctx.GetQueryResults <Reviewer>();
                ctx.GetResults <BookReviewOutput>().ForEach(
                    x => x.Reviewer = reviewers.Single(y => y.Id == x.ReviewerId));
            })
            .BuildQuery()
            .BuildWithListResult();

            // Example of how we are leveraging searches across business domains, reviewers and books.
            queryBuilderFactory.Create <BookReviewOutput>(booksQuery, "SearchBookReviews")
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginSearch()
            .Add <BookSearch>().Add <ReviewerSearch>().Build()
            .BuildQueryResult(ctx =>
            {
                var searches     = ctx.GetQueryResults <SearchResultModel>();
                var bookSearches = searches.GetTypeList <BookSearch>();

                ctx.Items["bookSearches"]       = bookSearches;
                ctx.Items["bookSearchesIdList"] = bookSearches.Select(x => x.Id).ToList();

                var reviewerSearches = searches.GetTypeList <ReviewerSearch>();

                ctx.Items["reviewerSearchesIdList"] = reviewerSearches.Select(x => x.Id).ToList();
            })
            .ThenWithQuery <BookReview>()
            .WithPropertyFromSource(x => x.BookId, ctx => ctx.ConvertItemsToObjectList <string>("bookSearchesIdList"))
            .BuildQueryResult(ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["bookReviews"] = ctx.GetQueryResults <BookReview>();
            })
            .ThenWithQuery <BookReview>()
            .WithPropertyFromSource(x => x.ReviewerId, ctx => ctx.ConvertItemsToObjectList <string>("reviewerSearchesIdList"))
            .BuildQueryResult(ctx =>
            {
                // Combine the results of book reviews from book Id list and reviewer Id List.
                List <BookReview> bookReviews = (List <BookReview>)ctx.Items["bookReviews"];

                bookReviews.AddRange(ctx.GetQueryResults <BookReview>());

                var finalResults = bookReviews.Distinct().Select(br => new BookReviewOutput
                {
                    Id         = br.Id,
                    BookId     = br.BookId,
                    Comments   = br.Comments,
                    ReviewerId = br.ReviewerId,
                    Stars      = br.Stars,
                    WrittenOn  = br.WrittenOn,
                    Active     = br.Active
                }).ToList();

                ctx.Items["bookIdList"]     = finalResults.Select(x => x.BookId).Distinct().ToList();
                ctx.Items["reviewerIdList"] = finalResults.Select(x => x.ReviewerId).Distinct().ToList();

                ctx.SetResults(finalResults);
            })
            .ThenWithQuery <Book>()
            .WithPropertyFromSource(x => x.Id, ctx => ctx.ConvertItemsToObjectList <string>("bookIdList"))
            .BuildQueryResult(ctx =>
            {
                var books = ctx.GetQueryResults <Book>();
                ctx.GetResults <BookReviewOutput>().ForEach(x =>
                                                            x.Book = books.Single(b => b.Id == x.BookId));
            })
            .ThenWithQuery <Reviewer>()
            .WithPropertyFromSource(x => x.Id, ctx => ctx.ConvertItemsToObjectList <string>("reviewerIdList"))
            .BuildQueryResult(ctx =>
            {
                var reviewers = ctx.GetQueryResults <Reviewer>();
                ctx.GetResults <BookReviewOutput>().ForEach(x =>
                                                            x.Reviewer = reviewers.Single(b => b.Id == x.ReviewerId));
            })
            .BuildQuery()
            .BuildWithListResult();
        }
        public static void AddBookAuthorsOutputQueries(this BooksQuery booksQuery, QueryBuilderFactory queryBuilderFactory)
        {
            queryBuilderFactory.Create <BookAuthorsOutput>(booksQuery, "getBookAuthorsByCategory")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            // We need to get all book-authors by book category. In order to perform this, we need to get all books with the category first.
            .BeginWithProperty <Book>(x => x.Category, ctx =>
            {
                // Temporary store books in storage.
                ctx.Items["books"] = ctx.GetQueryResults <Book>();
            })
            // Then, we can get all book authors what has the book ids.
            .ThenWithProperty <BookAuthors>(x => x.BookId, ctx => ctx.GetItems <Book>("books").Select(y => (object)y.Id).ToList(), ctx =>
            {
                // Map initial result.
                var bookAuthors = ctx.GetQueryResults <BookAuthors>();
                var books       = ctx.GetItems <Book>("books");

                ctx.SetResults(bookAuthors.Select(ba => new BookAuthorsOutput
                {
                    Id           = ba.Id,
                    Book         = books.Single(o => o.Id == ba.BookId),
                    AuthorIdList = ba.AuthorIdList,
                    BookId       = ba.BookId,
                    RoyaltyType  = ba.RoyaltyType
                }).ToList());
            })
            // Lastly, we can get all the authors for all the found books.
            .ThenWithProperty <Author>(x => x.Id, ctx =>
            {
                var list = new List <string>();
                ctx.GetResults <BookAuthorsOutput>().ForEach(y => list.AddRange(y.AuthorIdList));
                return(list.Distinct().Select(y => (object)y).ToList());
            }, ctx =>
            {
                // Map authors to result.
                var authors = ctx.GetQueryResults <Author>();

                // Only include authors who are in the AuthorIdList in each individual book author.
                ctx.GetResults <BookAuthorsOutput>().ForEach(ba => ba.Authors = authors.Where(x => ba.AuthorIdList.Contains(x.Id)).ToList());
            })
            .BuildQuery()
            .AssertWithClaimsPrincipal(cp => cp.IsInRole("Eklee.User.Read"))
            .BuildWithListResult();

            queryBuilderFactory.Create <BookAuthorsOutput>(booksQuery, "getBookAuthorsById")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginWithProperty <BookAuthors>(x => x.Id, ctx =>
            {
                ctx.SetResults(ctx.GetQueryResults <BookAuthors>().Select(ba => new BookAuthorsOutput
                {
                    Id           = ba.Id,
                    AuthorIdList = ba.AuthorIdList,
                    BookId       = ba.BookId,
                    RoyaltyType  = ba.RoyaltyType
                }).ToList());
            })
            .ThenWithProperty <Author>(x => x.Id, ctx =>
            {
                var list = new List <string>();
                ctx.GetResults <BookAuthorsOutput>().ForEach(y => list.AddRange(y.AuthorIdList));
                return(list.Distinct().Select(y => (object)y).ToList());
            }, ctx =>
            {
                // Map authors to result.
                var authors = ctx.GetQueryResults <Author>();

                // Only include authors who are in the AuthorIdList in each individual book author.
                ctx.GetResults <BookAuthorsOutput>().ForEach(ba => ba.Authors = authors.Where(x => ba.AuthorIdList.Contains(x.Id)).ToList());
            })
            .BuildQuery()
            .BuildWithSingleResult();

            queryBuilderFactory.Create <BookAuthorsOutput>(booksQuery, "getBookAuthorsByRoyaltyType")
            .WithPaging()
            .WithCache(TimeSpan.FromSeconds(10))
            .WithParameterBuilder()
            .BeginWithProperty <BookAuthors>(x => x.RoyaltyType, ctx =>
            {
                ctx.SetResults(ctx.GetQueryResults <BookAuthors>().Select(ba => new BookAuthorsOutput
                {
                    Id           = ba.Id,
                    AuthorIdList = ba.AuthorIdList,
                    BookId       = ba.BookId,
                    RoyaltyType  = ba.RoyaltyType
                }).ToList());
            })
            .ThenWithProperty <Author>(x => x.Id, ctx =>
            {
                var list = new List <string>();
                ctx.GetResults <BookAuthorsOutput>().ForEach(y => list.AddRange(y.AuthorIdList));
                return(list.Distinct().Select(y => (object)y).ToList());
            }, ctx =>
            {
                // Map authors to result.
                var authors = ctx.GetQueryResults <Author>();

                // Only include authors who are in the AuthorIdList in each individual book author.
                ctx.GetResults <BookAuthorsOutput>().ForEach(ba => ba.Authors = authors.Where(x => ba.AuthorIdList.Contains(x.Id)).ToList());
            })
            .BuildQuery()
            .BuildWithListResult();
        }
Exemplo n.º 20
0
        public ReadOnlyQueryConfigObjectGraphType(QueryBuilderFactory queryBuilderFactory)
        {
            Name = "query";

            queryBuilderFactory.Create <MyReadOnlyQuiz>(this, "GetQuiz")
            .WithParameterBuilder()
            .BeginQuery <MyQuiz>()
            .WithProperty(x => x.Id)
            .WithPropertyFromSource(x => x.IsPublic, ctx => new List <object> {
                true
            })
            .BuildQueryResult(ctx =>
            {
                var list = ctx.GetQueryResults <MyQuiz>();

                if (list != null)
                {
                    var item = list.SingleOrDefault();
                    if (item != null)
                    {
                        var readonlyQuiz    = new MyReadOnlyQuiz();
                        readonlyQuiz.Id     = item.Id;
                        readonlyQuiz.Name   = item.Name;
                        ctx.Items["QuizId"] = new List <object> {
                            list.Single().Id
                        };
                        ctx.SetResults(new List <MyReadOnlyQuiz> {
                            readonlyQuiz
                        });

                        return;
                    }
                }

                ctx.Items["QuizId"] = new List <object>();
                ctx.SetResults(new List <MyReadOnlyQuiz>());
            })
            .ThenWithQuery <Question>()
            .WithPropertyFromSource(x => x.QuizId, ctx => (List <object>)ctx.Items["QuizId"])
            .BuildQueryResult(ctx =>
            {
                var items = ctx.GetResults <MyReadOnlyQuiz>();
                if (items != null)
                {
                    var item = items.SingleOrDefault();
                    if (item != null)
                    {
                        item.Questions = ctx.GetQueryResults <Question>().Select(x => new MyReadOnlyQuestion
                        {
                            Id          = x.Id,
                            Text        = x.Text,
                            ChoicesJson = x.ChoicesJson,
                            ExplainJson = x.ExplainJson,
                            TagsJson    = x.TagsJson
                        }).ToList();
                    }
                }
            })
            .BuildQuery()
            .BuildWithSingleResult();
        }