コード例 #1
0
        static void Main(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder <MyHotelDbContext>();

            optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyHotel;Trusted_Connection=True;MultipleActiveResultSets=true");

            var c = new MyHotelDbContext(optionsBuilder.Options);

            c.Database.EnsureCreated();

            Expression <Func <Guest, bool> > criteria1 = guest => guest.Name.Contains("af");
            Expression <Func <Guest, bool> > criteria2 = guest => criteria1.Invoke(guest) || guest.Id > 1;

            Console.WriteLine($"criteria2 = '{criteria2.Expand()}'");

            var q = c.Guests.AsExpandable().Where(criteria2);

            var results = q.ToArray();

            foreach (var result in results)
            {
                Console.WriteLine($"{result.Name}");
            }
            int y = 0;
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: theodrom/.NetCore3GraphQL
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MyHotelDbContext dbContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseGraphQL <MyHotelSchema>();
            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions());
            //to explorer API navigate https://*DOMAIN*/ui/playground


            app.UseHttpsRedirection();
            app.UseStaticFiles();
            if (!env.IsDevelopment())
            {
                app.UseSpaStaticFiles();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });

            InitializeMapper();
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder <MyHotelDbContext>();

            optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MyHotel;Trusted_Connection=True;MultipleActiveResultSets=true");

            var c = new MyHotelDbContext(optionsBuilder.Options);

            c.Database.EnsureCreated();

            var q = c.Guests.AsExpandable();

            var result = q.ToArray();
            int y      = 0;
        }
コード例 #4
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, MyHotelDbContext dbContext, IMapper mapper)
        {
            mapper.ConfigurationProvider.AssertConfigurationIsValid();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseCookiePolicy();
            app.UseAuthentication();

            app.UseWebSockets();
            app.UseGraphQLWebSockets <MyHotelSchema>("/graphql");
            app.UseGraphQL <MyHotelSchema>();
            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions()); //to explorer API navigate https://*DOMAIN*/ui/playground

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
コード例 #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MyHotelDbContext dbContext, IMapper mapper)
        {
            mapper.ConfigurationProvider.AssertConfigurationIsValid();


            if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();

            app.UseCookiePolicy();
            app.UseAuthentication();

            app.UseGraphQL <MyHotelSchema>();
            app.UseGraphQLPlayground(new GraphQLPlaygroundOptions()); // to explore API navigate https://*DOMAIN*/ui/playground

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath     = "ClientApp";
                spa.Options.StartupTimeout = TimeSpan.FromSeconds(120);

                if (env.EnvironmentName == Microsoft.Extensions.Hosting.Environments.Development)
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
コード例 #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("start");
            var ctx = new MyHotelDbContext();

            var dates = ctx.Reservations.Select(x => DbFunctions.CreateDateTime(x.CheckinDate.Year, 1, 1, 0, 0, 0));

            foreach (var date in dates)
            {
                Console.WriteLine("Normal:" + date);
            }

            var datesDynamic = ctx.Reservations.Select("DbFunctions.CreateDateTime(CheckinDate.Year, 1, 1, 0, 0, 0)");

            foreach (var date in datesDynamic)
            {
                Console.WriteLine("Dynamic:" + date);
            }
        }
コード例 #7
0
 public MyHotelRepository(MyHotelDbContext myHotelDbContext, IMapper mapper)
 {
     _myHotelDbContext = myHotelDbContext;
     _mapper           = mapper;
 }
コード例 #8
0
ファイル: TestController.cs プロジェクト: zhenlei520/LINQKit
 public TestController(ILogger <TestController> logger, MyHotelDbContext context)
 {
     _logger  = logger;
     _context = context;
 }
コード例 #9
0
 public ReservationRepository(MyHotelDbContext myHotelDbContext, IMapper mapper)
 {
     _myHotelDbContext = myHotelDbContext;
     _mapper           = mapper;
 }
コード例 #10
0
 public MyHotelRepository(MyHotelDbContext myHotelDbContext)
 {
     _myHotelDbContext = myHotelDbContext;
 }
コード例 #11
0
 public ReservationRepository(MyHotelDbContext myHotelDbContext)
 {
     _myHotelDbContext = myHotelDbContext;
 }
コード例 #12
0
        public MyHotelQuery(MyHotelRepository myHotelRepository, MyHotelDbContext myHotelDbContext, IQueryArgumentInfoListBuilder builder, IMapper mapper)
        {
            var buildingsQueryArgumentList = builder.Build <BuildingType>().SupportOrderBy();

            Field <ListGraphType <BuildingType> >("buildings",
                                                  arguments: buildingsQueryArgumentList.ToQueryArguments(),
                                                  resolve: context => myHotelDbContext.Buildings
                                                  .ApplyQueryArguments(buildingsQueryArgumentList, context)
                                                  .ProjectTo <BuildingModel>(mapper.ConfigurationProvider)
                                                  .ToList()
                                                  );

            var guestQueryArgumentList = builder.Build <GuestType>().SupportOrderBy();

            Field <ListGraphType <GuestType> >("guests",
                                               arguments: guestQueryArgumentList.ToQueryArguments(),
                                               resolve: context => myHotelRepository.GetGuestsQuery()
                                               .ApplyQueryArguments(guestQueryArgumentList, context)
                                               .ProjectTo <GuestModel>(mapper.ConfigurationProvider)
                                               .ToList()
                                               );

            var roomQueryArgumentList = builder.Build <RoomType>().SupportOrderBy().Exclude("Id");

            Field <ListGraphType <RoomType> >("rooms",
                                              arguments: roomQueryArgumentList.ToQueryArguments(),
                                              resolve: context => myHotelRepository.GetRoomsQuery()
                                              .ApplyQueryArguments(roomQueryArgumentList, context)
                                              .ProjectTo <RoomModel>(mapper.ConfigurationProvider)
                                              .ToList()
                                              );

            var roomWithPagingQueryArgumentList = builder.Build <RoomType>().SupportOrderBy().SupportPaging();

            Field <ListGraphType <RoomType> >("roomsWithPaging",
                                              arguments: roomWithPagingQueryArgumentList.ToQueryArguments(),
                                              resolve: context => myHotelRepository.GetRoomsQuery()
                                              .ApplyQueryArguments(roomWithPagingQueryArgumentList, context)
                                              .ProjectTo <RoomModel>(mapper.ConfigurationProvider)
                                              .ToList()
                                              );

            var flatRoomQueryArgumentList = builder.Build <FlatRoomType>().SupportOrderBy();

            Field <ListGraphType <FlatRoomType> >("flatrooms",
                                                  arguments: flatRoomQueryArgumentList.ToQueryArguments(),
                                                  resolve: context => myHotelRepository.GetRoomsQuery()
                                                  .ApplyQueryArguments(flatRoomQueryArgumentList, context)
                                                  .ProjectTo <FlatRoomModel>(mapper.ConfigurationProvider)
                                                  .ToList()
                                                  );

            var reservationQueryArgumentList = builder.Build <ReservationType>().SupportOrderBy();

            Field <ListGraphType <ReservationType> >("reservations",
                                                     arguments: new QueryArguments(reservationQueryArgumentList.Select(q => q.QueryArgument)),
                                                     resolve: context => myHotelRepository.GetReservationsQuery()
                                                     .ApplyQueryArguments(reservationQueryArgumentList, context)
                                                     .ProjectTo <ReservationModel>(mapper.ConfigurationProvider)
                                                     .ToList()
                                                     );

            Field <ListGraphType <ReservationType> >("reservations2",
                                                     arguments: new QueryArguments(new List <QueryArgument>
            {
                new QueryArgument <IdGraphType>
                {
                    Name = "id"
                },
                new QueryArgument <DateGraphType>
                {
                    Name = "checkinDate"
                },
                new QueryArgument <DateGraphType>
                {
                    Name = "checkoutDate"
                },
                new QueryArgument <BooleanGraphType>
                {
                    Name = "roomAllowedSmoking"
                },
                new QueryArgument <RoomStatusType>
                {
                    Name = "roomStatus"
                },
                new QueryArgument <IntGraphType>
                {
                    Name = "roomWindows"
                },
                new QueryArgument <IntGraphType>
                {
                    Name = "roomBeds"
                }
            }),
                                                     resolve: context =>
            {
                var query = myHotelRepository.GetReservationsQuery().AsQueryable();

                Console.WriteLine("context.Arguments = " + JsonConvert.SerializeObject(context.Arguments));

                var user = (ClaimsPrincipal)context.UserContext;
                var isUserAuthenticated = ((ClaimsIdentity)user.Identity).IsAuthenticated;

                var reservationId = context.GetArgument <int?>("id");
                if (reservationId.HasValue)
                {
                    if (reservationId.Value <= 0)
                    {
                        context.Errors.Add(new ExecutionError("reservationId must be greater than zero!"));
                        return(new List <Reservation>());
                    }

                    query = query.Where(r => r.Id == reservationId.Value);
                }

                var checkinDate = context.GetArgument <DateTime?>("checkinDate");
                if (checkinDate.HasValue)
                {
                    query = query.Where(r => r.CheckinDate.Date == checkinDate.Value.Date);
                }

                var checkoutDate = context.GetArgument <DateTime?>("checkoutDate");
                if (checkoutDate.HasValue)
                {
                    query = query.Where(r => r.CheckoutDate.Date >= checkoutDate.Value.Date);
                }

                var allowedSmoking = context.GetArgument <bool?>("roomAllowedSmoking");
                if (allowedSmoking.HasValue)
                {
                    query = query.Where(r => r.Room.AllowedSmoking == allowedSmoking.Value);
                }

                var roomStatus = context.GetArgument <RoomStatus?>("roomStatus");
                if (roomStatus.HasValue)
                {
                    query = query.Where(r => r.Room.Status == roomStatus.Value);
                }

                var roomWindows = context.GetArgument <int?>("roomWindows");
                if (roomWindows.HasValue)
                {
                    query = query.Where(r => r.Room.RoomDetail.Windows == roomWindows);
                }

                var roomBeds = context.GetArgument <int?>("roomBeds");
                if (roomBeds.HasValue)
                {
                    query = query.Where(r => r.Room.RoomDetail.Beds == roomBeds);
                }

                return(query.ProjectTo <ReservationModel>(mapper.ConfigurationProvider).ToList());
            }
                                                     );
        }