Загрузка данных


using DiscountApi.Models;

namespace DiscountApi.Data;

public static class DbInitilizaer
{
    public static void Initialize(AppDbContext context)
    {
        // Если данные уже есть — выходим
        if (context.Brands.Any()) return;

        var now = DateTime.Now;

        // Создаём бренды
        var nike = new Brand { ID = 1, Name = "Nike", Description = "Спортивная одежда и обувь" };
        var adidas = new Brand { ID = 2, Name = "Adidas", Description = "Спортивные товары" };
        var zara = new Brand { ID = 3, Name = "Zara", Description = "Одежда" };
        var samsung = new Brand { ID = 4, Name = "Samsung", Description = "Электроника" };

        context.Brands.AddRange(nike, adidas, zara, samsung);
        context.SaveChanges();

        // Создаём купоны (тестовые данные из задания)
        context.Coupons.AddRange(
            new Coupon
            {
                Code = "NIKE20", DiscountPercent = 20,
                StartDate = now.AddDays(-10), EndDate = now.AddDays(20),
                IsActive = true, BrandId = 1
            },
            new Coupon
            {
                Code = "NIKE10", DiscountPercent = 10,
                StartDate = now.AddDays(-30), EndDate = now.AddDays(-5),
                IsActive = false, BrandId = 1
            },
            new Coupon
            {
                Code = "ADIDAS15", DiscountPercent = 15,
                StartDate = now.AddDays(-5), EndDate = now.AddDays(15),
                IsActive = true, BrandId = 2
            },
            new Coupon
            {
                Code = "ZARA30", DiscountPercent = 30,
                StartDate = now.AddDays(-2), EndDate = now.AddDays(10),
                IsActive = true, BrandId = 3
            },
            new Coupon
            {
                Code = "SAMSUNG5", DiscountPercent = 5,
                StartDate = now.AddDays(-1), EndDate = now.AddDays(30),
                IsActive = true, BrandId = 4
            }
        );

        context.SaveChanges();
    }
}