using foodmarket.Domain.Catalog; using foodmarket.Infrastructure.Persistence; using Microsoft.EntityFrameworkCore; namespace foodmarket.Api.Seed; // Seeds global reference data (countries, currencies). Runs in all environments. public class SystemReferenceSeeder : IHostedService { private readonly IServiceProvider _services; public SystemReferenceSeeder(IServiceProvider services) { _services = services; } public async Task StartAsync(CancellationToken ct) { using var scope = _services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); await SeedCountriesAsync(db, ct); await SeedCurrenciesAsync(db, ct); await db.SaveChangesAsync(ct); } public Task StopAsync(CancellationToken ct) => Task.CompletedTask; private static async Task SeedCountriesAsync(AppDbContext db, CancellationToken ct) { // Kazakhstan first, then common trade partners. var wanted = new[] { new Country { Code = "KZ", Name = "Казахстан", SortOrder = 1 }, new Country { Code = "RU", Name = "Россия", SortOrder = 2 }, new Country { Code = "CN", Name = "Китай", SortOrder = 3 }, new Country { Code = "TR", Name = "Турция", SortOrder = 4 }, new Country { Code = "BY", Name = "Беларусь", SortOrder = 5 }, new Country { Code = "UZ", Name = "Узбекистан", SortOrder = 6 }, new Country { Code = "KG", Name = "Кыргызстан", SortOrder = 7 }, new Country { Code = "DE", Name = "Германия", SortOrder = 10 }, new Country { Code = "US", Name = "США", SortOrder = 11 }, new Country { Code = "KR", Name = "Южная Корея", SortOrder = 12 }, new Country { Code = "IT", Name = "Италия", SortOrder = 13 }, new Country { Code = "PL", Name = "Польша", SortOrder = 14 }, }; var existingCodes = await db.Countries.Select(c => c.Code).ToListAsync(ct); foreach (var c in wanted) { if (!existingCodes.Contains(c.Code)) { db.Countries.Add(c); } } } private static async Task SeedCurrenciesAsync(AppDbContext db, CancellationToken ct) { var wanted = new[] { new Currency { Code = "KZT", Name = "Тенге", Symbol = "₸", MinorUnit = 2 }, new Currency { Code = "RUB", Name = "Российский рубль", Symbol = "₽", MinorUnit = 2 }, new Currency { Code = "USD", Name = "Доллар США", Symbol = "$", MinorUnit = 2 }, new Currency { Code = "EUR", Name = "Евро", Symbol = "€", MinorUnit = 2 }, new Currency { Code = "CNY", Name = "Китайский юань", Symbol = "¥", MinorUnit = 2 }, }; var existingCodes = await db.Currencies.Select(c => c.Code).ToListAsync(ct); foreach (var c in wanted) { if (!existingCodes.Contains(c.Code)) { db.Currencies.Add(c); } } } }