private void AddDefaultCurrencyRates(BudgetDbContext context) { if (context.CurrencyRates.Any()) { return; } Dictionary <string, long> currencyIds = context.Currencies.ToDictionary(c => c.IsoCode, c => c.Id); var date = new DateTime(2019, 7, 14); CurrencyRate MakeRate(string baseCurrencyCode, string targetCurrencyCode, decimal rate) { return(new CurrencyRate { BaseId = currencyIds[baseCurrencyCode], TargetId = currencyIds[targetCurrencyCode], Rate = rate, Date = date }); } CurrencyRate[] currencyRates = { MakeRate("PLN", "USD", 3.79m), MakeRate("PLN", "EUR", 4.28m), MakeRate("PLN", "RUB", 0.0601292m) }; context.CurrencyRates.AddRange(currencyRates); context.SaveChanges(); }
private static void AddDomains(BudgetDbContext context, Budget orlovBudget, Dictionary <string, string[]> domainsAndCategories) { void AddDomain(string name) { if (orlovBudget.Fields?.Any(d => d.Name == name) == true) { return; } var domain = new Field() { Name = name, Budget = orlovBudget }; context.Fields.Add(domain); } foreach (string domainName in domainsAndCategories.Select(kvp => kvp.Key)) { AddDomain(domainName); } context.SaveChanges(); }
public static void AddCustomOrlovBudgetAndUsers(BudgetDbContext context) { // Add budget itself. if (context.Budgets.Any(b => b.Name == BudgetName)) { return; } var budget = context.Budgets.FirstOrDefault(b => b.Name == BudgetName); if (budget == null) { budget = new Budget() { Name = BudgetName }; context.Budgets.Add(budget); context.SaveChanges(); } AddDomainsAndCategories(context, budget); AddUsers(context, budget); }
public void SeedEverything(BudgetDbContext context) { context.Database.EnsureCreated(); AddCurrencies(context); AddDefaultCurrencyRates(context); OrlovBudgetInitializer.AddCustomOrlovBudgetAndUsers(context); }
static void AddDomainsAndCategories(BudgetDbContext context, Budget orlovBudget) { var domainsAndCategories = new Dictionary <string, string[]> { { "Дети", new[] { "Одежда", "Еда", "Подгузники", "Развлечения" } }, { "Еда", new[] { "Рестораны", "Снэки" } }, { "Жизнь", new[] { "Квартира", "Медицина", "Путешествия", "Развлечения", "Сервисы", "Страховка", "Транспорт", "Одежда", "Чтение" } }, { "Коты", new string [0] }, { "Машина", new[] { "Бензин", "Парковка", "Обслуживание" } } }; AddDomains(context, orlovBudget, domainsAndCategories); AddCategories(context, orlovBudget, domainsAndCategories); }
private void AddCurrencies(BudgetDbContext context) { if (context.Currencies.Any()) { return; } Currency[] currencies = { new Currency { IsoCode = "PLN", Name = "Polish złoty", Symbol = "zł" }, new Currency { IsoCode = "USD", Name = "US dollar", Symbol = "$" }, new Currency { IsoCode = "EUR", Name = "Euro", Symbol = "€" }, new Currency { IsoCode = "RUB", Name = "Russian ruble", Symbol = "₽" } }; context.Currencies.AddRange(currencies); context.SaveChanges(); }
private static void AddUsers(BudgetDbContext context, Budget orlovBudget) { void AddUser(string firstName, string lastName, string email, string login, string password) { User user = context.Users.FirstOrDefault(u => u.Username == login); if (user != null) { return; } user = new User() { FirstName = firstName, LastName = lastName, Email = email, Username = login, IsAdmin = true, PasswordHash = password != null?Encoding.UTF8.GetBytes(password) : null, PasswordSalt = null }; var membership = new Membership() { User = user, Budget = orlovBudget, IsManager = true }; context.Users.Add(user); context.Memberships.Add(membership); } AddUser("Mikhail", "Orlov", "*****@*****.**", "orloffm", null); AddUser("Ekaterina", "Orlova", "*****@*****.**", "egogotha", null); context.SaveChanges(); }
private static void AddCategories(BudgetDbContext context, Budget orlovBudget, Dictionary <string, string[]> domainsAndCategories) { foreach (KeyValuePair <string, string[]> domainAndCategories in domainsAndCategories) { var d = orlovBudget.Fields.Single(e => e.Name == domainAndCategories.Key); void AddCategory(string name, bool isDefault = false) { if (d.Categories?.Any(c => c.Name == name) == true) { return; } var category = new Category { Field = d, Name = name }; if (isDefault) { d.DefaultCategory = category; } context.Categories.Add(category); } AddCategory("-", true); foreach (string category in domainAndCategories.Value) { AddCategory(category); } } context.SaveChanges(); }
public static void Initialize(BudgetDbContext context) { var initializer = new BudgetDbInitializer(); initializer.SeedEverything(context); }