Example #1
0
 public static void ClearData(BEIdentityContext context)
 {
     context.Database.ExecuteSqlRaw("Delete from dbo.Suppliers");
     context.Database.ExecuteSqlRaw("Delete from dbo.Orders");
     context.Database.ExecuteSqlRaw("Delete from dbo.Categories");
     ResetIdentity(context);
 }
Example #2
0
 public static void InitializeData(BEIdentityContext context)
 {
     //Ensure the database exists and is up to date
     context.Database.Migrate();
     ClearData(context);
     SeedData(context);
 }
Example #3
0
        internal static void ResetIdentity(BEIdentityContext context)
        {
            var tables = new[] { "Suppliers", "Products", "Ratings", "Orders", "CartLines", "Payments", "Categories" };

            foreach (var itm in tables)
            {
                var rawSqlString = $"DBCC CHECKIDENT (\"dbo.{itm}\", RESEED, 0);";
                context.Database.ExecuteSqlRaw(rawSqlString);
            }
        }
Example #4
0
        internal static void SeedData(BEIdentityContext context)
        {
            try
            {
                if (!context.Categories.Any())
                {
                    context.Categories.AddRange(SampleData.GetAllCategories());
                    context.SaveChanges();
                }
                if (!context.Suppliers.Any())
                {
                    var cat1 = context.Categories.FirstOrDefault();
                    var cat2 = context.Categories.Skip(1).FirstOrDefault();
                    context.Suppliers.AddRange(SampleData.GetSuppliers(new List <Category> {
                        cat1, cat2
                    }));
                    context.SaveChanges();
                }

                if (!context.Orders.Any())
                {
                    var prod1 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.FirstOrDefault();
                    var prod2 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.Skip(1).FirstOrDefault();
                    context.Orders.AddRange(SampleData.GetAllOrders(new List <Product> {
                        prod1, prod2
                    }));
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #5
0
 public ProductsController(IProductRepo repo, BEIdentityContext ctx)
 {
     _repo = repo;
 }
Example #6
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider services, IHostApplicationLifetime lifetime)
        {
            app.UseCors("AllowAll");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

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

            //For development
            app.UseSwagger();
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/swagger/v1/swagger.json",
                                        "dbo API");
            });

            //For deployment
            if ((Configuration["INITDB"] ?? "false") == "true")
            {
                System.Console.WriteLine("Preparing Database...");
                SeedData.SeedDataBase(services.GetRequiredService <BEIdentityContext>());
                System.Console.WriteLine("Database Preparation Complete");
                //Enable to deploy
                //lifetime.StopApplication();
            }

            //To create default admin and user
            BEIdentityContext.CreateAdminAccount(app.ApplicationServices, Configuration).Wait();
            BEIdentityContext.CreateUserAccount(app.ApplicationServices, Configuration).Wait();

            //For development
            //app.UseSpa(spa =>
            //{
            //    string strategy = Configuration
            //        .GetValue<string>("DevTools:ConnectionStrategy");
            //    if (strategy == "proxy")
            //    {
            //        //spa.UseProxyToSpaDevelopmentServer("http://127.0.0.1:4200");
            //        spa.UseProxyToSpaDevelopmentServer("http://127.0.0.1:8100");
            //        //spa.UseProxyToSpaDevelopmentServer("http://192.168.2.4:8100");
            //    }
            //    else if (strategy == "managed")
            //    {
            //        spa.Options.SourcePath = "../ClientApp";
            //        spa.UseAngularCliServer("start");
            //    }
            //});
        }
 public SupplierRepo(BEIdentityContext context) : base(context)
 {
 }
Example #8
0
        public static void SeedDataBase(BEIdentityContext context)
        {
            context.Database.Migrate();


            try
            {
                if (!context.Categories.Any())
                {
                    context.Categories.AddRange(Data.GetAllCategories());
                    context.SaveChanges();
                }

                if (!context.Suppliers.Any())
                {
                    var cat1 = context.Categories.FirstOrDefault();
                    var cat2 = context.Categories.Skip(1).FirstOrDefault();
                    var cat3 = context.Categories.Skip(2).FirstOrDefault();
                    var cat4 = context.Categories.Skip(3).FirstOrDefault();
                    var cat5 = context.Categories.Skip(4).FirstOrDefault();
                    context.Suppliers.AddRange(
                        Data.GetSuppliers(new List <Category> {
                        cat1, cat2, cat3, cat4, cat5
                    }));
                    context.SaveChanges();
                }

                if (!context.Orders.Any())
                {
                    var prod1 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.FirstOrDefault();
                    var prod2 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.Skip(1).FirstOrDefault();
                    //var prod3 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(2).FirstOrDefault();
                    //var prod4 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(3).FirstOrDefault();
                    //var prod5 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(4).FirstOrDefault();
                    var prod6 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.Skip(5).FirstOrDefault();
                    var prod7 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.Skip(6).FirstOrDefault();
                    var prod8 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.Skip(7).FirstOrDefault();
                    var prod9 = context.Suppliers
                                .Include(c => c.Products).FirstOrDefault()?
                                .Products.Skip(8).FirstOrDefault();
                    var prod10 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(9).FirstOrDefault();
                    var prod11 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(10).FirstOrDefault();
                    //var prod12 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(11).FirstOrDefault();
                    //var prod13 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(12).FirstOrDefault();
                    var prod14 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(13).FirstOrDefault();
                    //var prod15 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(14).FirstOrDefault();
                    //var prod16 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(15).FirstOrDefault();
                    var prod17 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(16).FirstOrDefault();
                    //var prod18 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(17).FirstOrDefault();
                    var prod19 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(18).FirstOrDefault();
                    var prod20 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(19).FirstOrDefault();
                    var prod21 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(20).FirstOrDefault();
                    //var prod22 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(21).FirstOrDefault();
                    //var prod23 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(22).FirstOrDefault();
                    //var prod24 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(23).FirstOrDefault();
                    //var prod25 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(24).FirstOrDefault();
                    var prod26 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(25).FirstOrDefault();
                    var prod27 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(26).FirstOrDefault();
                    var prod28 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(27).FirstOrDefault();
                    var prod29 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(28).FirstOrDefault();
                    //var prod30 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(29).FirstOrDefault();
                    //var prod31 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(30).FirstOrDefault();
                    //var prod32 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(31).FirstOrDefault();
                    //var prod33 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(32).FirstOrDefault();
                    //var prod34 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(33).FirstOrDefault();
                    //var prod35 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(34).FirstOrDefault();
                    //var prod36 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(35).FirstOrDefault();
                    //var prod37 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(36).FirstOrDefault();
                    var prod38 = context.Suppliers
                                 .Include(c => c.Products).FirstOrDefault()?
                                 .Products.Skip(37).FirstOrDefault();
                    //var prod39 = context.Suppliers
                    //    .Include(c => c.Products).FirstOrDefault()?
                    //    .Products.Skip(38).FirstOrDefault();



                    context.Orders.AddRange(Data.GetAllOrders(new List <Product>
                    {
                        prod1,
                        prod2,
                        //prod3,
                        //prod4,
                        //prod5,
                        prod6,
                        prod7,
                        prod8,
                        prod9,
                        prod10,
                        prod11,
                        //prod12,
                        //prod13,
                        prod14,
                        //prod15,
                        //prod16,
                        prod17,
                        //prod18,
                        prod19,
                        prod20,
                        prod21,
                        //prod22,
                        //prod23,
                        //prod24,
                        //prod25,
                        prod26,
                        prod27,
                        prod28,
                        prod29,
                        //prod30,
                        //prod31,
                        //prod32,
                        //prod33,
                        //prod34,
                        //prod35,
                        //prod36,
                        //prod37,
                        prod38,
                        //prod39
                    }));
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #9
0
 protected void ResetContext()
 {
     Context = new BEIdentityContextFactory().CreateDbContext(null);
 }
Example #10
0
 public static void DropAndCreateDatabase(BEIdentityContext context)
 {
     context.Database.EnsureDeleted();
     context.Database.Migrate();
 }
 public OrderRepo(BEIdentityContext context) : base(context)
 {
 }
 public CategoryRepo(BEIdentityContext context) : base(context)
 {
 }
 public CartLineRepo(BEIdentityContext context, IProductRepo productRepo) : base(context)
 {
     _productRepo = productRepo;
 }
Example #14
0
 public ProductRepo(BEIdentityContext context) : base(context)
 {
 }
Example #15
0
 protected RepoBase(BEIdentityContext context)
 {
     Context = context;
     Table   = Context.Set <T>();
 }