Example #1
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSwagger();

            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
            });
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();
            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            using var scope   = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope();
            using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
            if (context.Database.EnsureCreated())
            {
                var roles        = RoleScript.Roles();
                var admin        = AdministratorScript.ApplicationUser();
                var adminUser    = UserRoleScript.UserRole(roles.FirstOrDefault(x => x.NormalizedName.Equals("ADMINISTRATOR")), admin);
                var types        = TypeScript.GetTypes();
                var company      = CompanyScript.GetCompany();
                var user         = UserScript.ApplicationUser();
                var userRole     = UserRoleScript.UserRole(roles.FirstOrDefault(x => x.NormalizedName.Equals("USER")), user);
                var vehiclesList = VehicleScript.GetVehicles();

                context.Roles.AddRangeAsync(roles).Wait();

                context.ApplicationUser.Add(admin);

                context.UserRoles.Add(adminUser);

                context.Type.AddRangeAsync(types).Wait();

                context.Company.Add(company);

                context.ApplicationUser.Add(user);

                context.UserRoles.Add(userRole);

                context.UserCompany.Add(new UserCompany {
                    CompanyID = company.ID, UserID = user.Id
                });

                context.SaveChangesAsync().Wait();
            }
        }