Пример #1
0
        /// <summary>Seeds the data.</summary>
        /// <param name="context">The context.</param>
        //public static async Task SeedData(CoctailContext context, UserManager<IdentityUser> userManager)
        public static async Task SeedData(CoctailContext context, UserManager <AppUser> userManager, RoleManager <IdentityRole <int> > roleManager)
        {
            if (await context.Coctails.AnyAsync())
            {
                return;
            }

            var coctailsData = await File.ReadAllTextAsync(Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "DataDownloader/Coctails.json"));

            var options = new JsonSerializerOptions
            {
                NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString
            };

            var coctails = JsonSerializer.Deserialize <List <Coctail> >(coctailsData, options);

            if (coctails == null)
            {
                return;
            }

            foreach (var item in coctails)
            {
                item.IsAccepted = true;
                await context.AddAsync(item);

                await context.SaveChangesAsync();
            }

            ////FOR DEVELOPMENT
            var roles = new List <IdentityRole <int> > {
                new IdentityRole <int> {
                    Name = "User"
                },
                new IdentityRole <int> {
                    Name = "Admin"
                }
            };

            foreach (var role in roles)
            {
                await roleManager.CreateAsync(role);
            }

            var admin = new AppUser {
                UserName = "******", Email = "*****@*****.**"
            };
            var defaultUser = new AppUser {
                UserName = "******", Email = "*****@*****.**"
            };

            await userManager.CreateAsync(admin, "Pa$$w0rd");

            await userManager.CreateAsync(defaultUser, "Pa$$w0rd");

            await userManager.AddToRoleAsync(admin, "Admin");

            await userManager.AddToRoleAsync(defaultUser, "User");
        }
Пример #2
0
 public UserRepository(CoctailContext coctailContext)
 {
     _coctailContext = coctailContext;
 }