Advertisement
Guest User

Initializator.cs

a guest
Aug 3rd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.65 KB | None | 0 0
  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using SharedLibrary.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.Entity;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10.  
  11. namespace SharedLibrary.Utils
  12. {
  13.     public class AppDatabaseInitializator
  14.     {
  15.         public static void Seed(ApplicationDbContext context)
  16.         {
  17.  
  18.             InitializeRoles(context);
  19.             InitializeUsers(context);
  20.             InitializeBooks(context);
  21.         }
  22.  
  23.         private static void InitializeUsers(ApplicationDbContext context)
  24.         {
  25.             var userStore = new UserStore<ApplicationUser>(context);
  26.             var userManager = new ApplicationUserManager(userStore);
  27.  
  28.             var user1 = new ApplicationUser() { FirstName = "Mitre", LastName = "Mitrevski", Email = "mitre.mitrevski@gmail.com" };
  29.             var user2 = new ApplicationUser() { FirstName = "Petre", LastName = "Petrevski", Email = "petre.petrevski@gmail.com" };
  30.             var user3 = new ApplicationUser() { FirstName = "Stanko", LastName = "Stankovski", Email = "stanko.stankovski@gmail.com" };
  31.  
  32.            
  33.             CreateUser(user1, userManager);
  34.             CreateUser(user2, userManager);
  35.             CreateUser(user3, userManager);
  36.  
  37.             context.SaveChanges();
  38.         }
  39.  
  40.         private static void CreateUser(ApplicationUser user, UserManager<ApplicationUser> userManager)
  41.         {
  42.  
  43.             var appUser = userManager.FindByEmail(user.Email);
  44.  
  45.             if (appUser == null)
  46.             {
  47.                 //IdentityResult result = userManager.Create(user, "Test123@");
  48.                 var task = Task.Run(async () => await userManager.CreateAsync(user, "Test123@"));
  49.                 var result = task.Result;
  50.                 // referencing result should unwrap any errors.
  51.  
  52.                 if (!result.Succeeded)
  53.                     throw new Exception(result.Errors.ToString());
  54.             }
  55.         }
  56.  
  57.         private static void InitializeBooks(ApplicationDbContext context)
  58.         {
  59.             List<Book> books = new List<Book>() {
  60.                 new Book() { Id = 1, Name = "The Alchemist", Authors = "Paulo Coelho" },
  61.                 new Book() { Id = 2, Name = "The Very Hungry Caterpillar", Authors = "Eric Carie" },
  62.                 new Book() { Id = 3, Name = "Goodnight Moon", Authors = "Margaret Wise Brown" },
  63.                 new Book() { Name = "The Outsider: A Novel", Authors = "Stephen King" },
  64.                 new Book() { Name = "Just Mercy: A Story of Justice and Redemption", Authors = "Bryan Stevenson" }
  65.             };
  66.  
  67.             context.Books.AddRange(books);
  68.             context.SaveChanges();
  69.         }
  70.  
  71.         private static void InitializeRoles(ApplicationDbContext context)
  72.         {
  73.             var roles = context.Roles.ToList();
  74.  
  75.             var adminRole = new IdentityRole("Administrator");
  76.             var editorRole = new IdentityRole("Editor");
  77.             var standardUserRole = new IdentityRole("StandardUser");
  78.  
  79.             if (roles == null || roles.Count == 0)
  80.             {
  81.  
  82.                 context.Roles.Add(adminRole);
  83.                 context.Roles.Add(editorRole);
  84.                 context.Roles.Add(standardUserRole);
  85.  
  86.             } else {
  87.  
  88.                 if (!roles.Any(role => role.Name == "Administrator")) context.Roles.Add(adminRole);
  89.                 if (!roles.Any(role => role.Name == "Editor")) context.Roles.Add(editorRole);
  90.                 if (!roles.Any(role => role.Name == "StandardUser")) context.Roles.Add(standardUserRole);
  91.  
  92.             }
  93.  
  94.             context.SaveChanges();
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement