| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- // ======================================
- // Author: Ebenezer Monney
- // Email: info@ebenmonney.com
- // Copyright (c) 2017 www.ebenmonney.com
- //
- // ==> Gun4Hire: contact@ebenmonney.com
- // ======================================
- using DAL.Models;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DAL.Core;
- using DAL.Core.Interfaces;
- namespace DAL
- {
- public interface IDatabaseInitializer
- {
- Task SeedAsync();
- }
- public class DatabaseInitializer : IDatabaseInitializer
- {
- private readonly ApplicationDbContext _context;
- private readonly IAccountManager _accountManager;
- private readonly ILogger _logger;
- public DatabaseInitializer(ApplicationDbContext context, IAccountManager accountManager, ILogger<DatabaseInitializer> logger)
- {
- _accountManager = accountManager;
- _context = context;
- _logger = logger;
- }
- public async Task SeedAsync()
- {
- await _context.Database.MigrateAsync().ConfigureAwait(false);
- if (!await _context.Users.AnyAsync())
- {
- const string adminRoleName = "administrator";
- const string userRoleName = "user";
- await ensureRoleAsync(adminRoleName, "Default administrator", ApplicationPermissions.GetAllPermissionValues());
- await ensureRoleAsync(userRoleName, "Default user", new string[] { });
- await createUserAsync("admin", "tempP@ss123", "Inbuilt Administrator", "admin@ebenmonney.com", "+1 (123) 000-0000", new string[] { adminRoleName });
- await createUserAsync("user", "tempP@ss123", "Inbuilt Standard User", "user@ebenmonney.com", "+1 (123) 000-0001", new string[] { userRoleName });
- }
- if (!await _context.Customers.AnyAsync() && !await _context.ProductCategories.AnyAsync())
- {
- Customer cust_1 = new Customer
- {
- Name = "Ebenezer Monney",
- Email = "contact@ebenmonney.com",
- Gender = Gender.Male,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- Customer cust_2 = new Customer
- {
- Name = "Itachi Uchiha",
- Email = "uchiha@narutoverse.com",
- PhoneNumber = "+81123456789",
- Address = "Some fictional Address, Street 123, Konoha",
- City = "Konoha",
- Gender = Gender.Male,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- Customer cust_3 = new Customer
- {
- Name = "John Doe",
- Email = "johndoe@anonymous.com",
- PhoneNumber = "+18585858",
- Address = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
- Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet",
- City = "Lorem Ipsum",
- Gender = Gender.Male,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- Customer cust_4 = new Customer
- {
- Name = "Jane Doe",
- Email = "Janedoe@anonymous.com",
- PhoneNumber = "+18585858",
- Address = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio.
- Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet",
- City = "Lorem Ipsum",
- Gender = Gender.Male,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- ProductCategory prodCat_1 = new ProductCategory
- {
- Name = "None",
- Description = "Default category. Products that have not been assigned a category",
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- Product prod_1 = new Product
- {
- Name = "BMW M6",
- Description = "Yet another masterpiece from the world's best car manufacturer",
- BuyingPrice = 109775,
- SellingPrice = 114234,
- UnitsInStock = 12,
- IsActive = true,
- ProductCategory = prodCat_1,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- Product prod_2 = new Product
- {
- Name = "Nissan Patrol",
- Description = "A true man's choice",
- BuyingPrice = 78990,
- SellingPrice = 86990,
- UnitsInStock = 4,
- IsActive = true,
- ProductCategory = prodCat_1,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow
- };
- Order ordr_1 = new Order
- {
- Discount = 500,
- Cashier = await _context.Users.FirstAsync(),
- Customer = cust_1,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow,
- OrderDetails = new List<OrderDetail>()
- {
- new OrderDetail() {UnitPrice = prod_1.SellingPrice, Quantity=1, Product = prod_1 },
- new OrderDetail() {UnitPrice = prod_2.SellingPrice, Quantity=1, Product = prod_2 },
- }
- };
- Order ordr_2 = new Order
- {
- Cashier = await _context.Users.FirstAsync(),
- Customer = cust_2,
- DateCreated = DateTime.UtcNow,
- DateModified = DateTime.UtcNow,
- OrderDetails = new List<OrderDetail>()
- {
- new OrderDetail() {UnitPrice = prod_2.SellingPrice, Quantity=1, Product = prod_2 },
- }
- };
- _context.Customers.Add(cust_1);
- _context.Customers.Add(cust_2);
- _context.Customers.Add(cust_3);
- _context.Customers.Add(cust_4);
- _context.Products.Add(prod_1);
- _context.Products.Add(prod_2);
- _context.Orders.Add(ordr_1);
- _context.Orders.Add(ordr_2);
- await _context.SaveChangesAsync();
- }
- }
- private async Task ensureRoleAsync(string roleName, string description, string[] claims)
- {
- if ((await _accountManager.GetRoleByNameAsync(roleName)) == null)
- {
- ApplicationRole applicationRole = new ApplicationRole(roleName, description);
- var result = await this._accountManager.CreateRoleAsync(applicationRole, claims);
- if (!result.Item1)
- throw new Exception($"Seeding \"{description}\" role failed. Errors: {string.Join(Environment.NewLine, result.Item2)}");
- }
- }
- private async Task<ApplicationUser> createUserAsync(string userName, string password, string fullName, string email, string phoneNumber, string[] roles)
- {
- ApplicationUser applicationUser = new ApplicationUser
- {
- UserName = userName,
- FullName = fullName,
- Email = email,
- PhoneNumber = phoneNumber,
- EmailConfirmed = true,
- IsEnabled = true
- };
- var result = await _accountManager.CreateUserAsync(applicationUser, roles, password);
- if (!result.Item1)
- throw new Exception($"Seeding \"{userName}\" user failed. Errors: {string.Join(Environment.NewLine, result.Item2)}");
- return applicationUser;
- }
- }
- }
|