Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// <summary>
        ///
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env,
                              SpendrDbContext ctx,
                              RoleManager <IdentityRole> roleManager, UserManager <User> userManager)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Spender-API v1"));

            app.UseSerilogRequestLogging();
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            Seeder.SeedDB(ctx, roleManager, userManager).Wait();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Seeds the database with placeholder json data
        /// </summary>
        /// <returns></returns>
        public static async Task SeedDB(SpendrDbContext ctx, RoleManager<IdentityRole>roleManager,  UserManager<User>userManager)
        {
            await ctx.Database.EnsureCreatedAsync();
            var roles = new List<String>() { "Admin", "User" };

            if (!roleManager.Roles.Any())
            {
                for (int i = 0; i < roles.Count; i++)
                {
                    var role = new IdentityRole(roles[i]);
                    await roleManager.CreateAsync(role);
                }

            }
            var cardTransactionCategories = TransformJSONtoPOCO<CardTransactionCategory>("CardTransactionCategory.json");
            //seed the wallet and card transaction categories
            if (!ctx.CardTransactionCategories.Any())
            {
                foreach (var item in cardTransactionCategories)
                {
                   await ctx.CardTransactionCategories.AddAsync(item);
                   await ctx.SaveChangesAsync();
                }
            }
            var walletTransactionCategories = TransformJSONtoPOCO<WalletTransactionCategory>("WalletTransactionCategory.json");
            if (!ctx.WalletTransactionCategories.Any())
            {
                foreach (var item in walletTransactionCategories)
                {
                    await ctx.WalletTransactionCategories.AddAsync(item);
                    await ctx.SaveChangesAsync();
                }

            }
            //seed merchants
            var Merchants = TransformJSONtoPOCO<Merchant>("Merchants.json");
            //seed the wallet and card transaction categories
            if (!ctx.Merchants.Any())
            {
                foreach (var item in Merchants)
                {
                    await ctx.Merchants.AddAsync(item);
                    await ctx.SaveChangesAsync();
                }
            }
            //seed users
            var Users = TransformJSONtoPOCO<User>("Users.json");
            if (!ctx.Users.Any())
            {
                int counter = 0;
                foreach (var user in Users)
                {
                    var result = await userManager.CreateAsync(user, "P@ssword");
                    if (!result.Succeeded)
                    {
                       HandlePreSeederError(result);
                    }
                    _ = counter == 1 ? await userManager.AddToRoleAsync(user, roles[0]) : await userManager.AddToRoleAsync(user, roles[1]);
                    counter++;
                }
            }

        }