コード例 #1
0
        /// <summary>
        /// Configures the needed services for the webapp and creates the initial data for the database
        /// </summary>
        /// <param name="services">servicecollection</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath         = "/Login/UserLogin/";
                options.Cookie.Expiration = TimeSpan.FromDays(14);
                options.Cookie.HttpOnly   = true;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            if (_env.IsEnvironment("Development"))
            {
                ConnectionString = Configuration.GetConnectionString("DevelopmentDb");
            }
            else if (_env.IsEnvironment("Test"))
            {
                ConnectionString = Configuration.GetConnectionString("TestDb");
            }
            else if (_env.IsEnvironment("Production"))
            {
                ConnectionString = Configuration.GetConnectionString("ProductionDb");
            }
            else
            {
                throw new Exception("Environment not defined.");
            }


            using (DataContext dbContext = DataContextFactory.GetDataContext(ConnectionString))
            {
                DbCreator.CreateDbIfNotExist(dbContext, _env.IsEnvironment("Development"));
                UserService    userService = new UserService(new UserRepository(dbContext));
                DatasetService dsService   = new DatasetService(new DatasetRepository(dbContext));

                if (Configuration.GetValue <bool>("RegenerateStaticDatasets"))
                {
                    DatasetDto xor               = dsService.GetByName(XorDataset);
                    DatasetDto dice              = dsService.GetByName(DiceDataset);
                    DatasetDto or                = dsService.GetByName(OrDataset);
                    DatasetDto numbers           = dsService.GetByName(NumbersDataset);
                    DatasetDto validationNumbers = dsService.GetByName(ValidationNumbersDataset);
                    DatasetDto letters           = dsService.GetByName(LettersDataset);
                    if (xor != null)
                    {
                        dsService.Delete(xor.Id, xor.UserId);
                    }
                    if (dice != null)
                    {
                        dsService.Delete(dice.Id, dice.UserId);
                    }
                    if (or != null)
                    {
                        dsService.Delete(or.Id, or.UserId);
                    }
                    if (numbers != null)
                    {
                        dsService.Delete(numbers.Id, numbers.UserId);
                    }
                    if (validationNumbers != null)
                    {
                        dsService.Delete(validationNumbers.Id, validationNumbers.UserId);
                    }
                    if (letters != null)
                    {
                        dsService.Delete(letters.Id, letters.UserId);
                    }
                }

                foreach (UserDto user in GetInitialUsers())
                {
                    try
                    {
                        userService.Create(user);
                    }
                    catch { };
                }
                dbContext.SaveChanges();

                foreach (DatasetDto dsDto in GetInitalDatasets(dbContext.Users.First().Id))
                {
                    try
                    {
                        dsService.Update(dsDto, dbContext.Users.First().Id);
                    }
                    catch (Exception ex)
                    {
                    };
                }
            }
        }