Esempio n. 1
0
        public static async Task SeedDataAsync(SkinetContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                // var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductTypes.Any())
                {
                    var productsTypeData = File.ReadAllText("../Infrastructure/Data/SeedData/types.json");
                    var productTypes     = JsonConvert.DeserializeObject <List <ProductType> >(productsTypeData);

                    foreach (var type in productTypes)
                    {
                        context.ProductTypes.Add(type);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.ProductBrands.Any())
                {
                    var productsBrandData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
                    var productBrands     = JsonConvert.DeserializeObject <List <ProductBrand> >(productsBrandData);

                    foreach (var brand in productBrands)
                    {
                        context.ProductBrands.Add(brand);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData = File.ReadAllText("../Infrastructure/Data/SeedData/products.json");
                    var products     = JsonConvert.DeserializeObject <List <Product> >(productsData);

                    foreach (var product in products)
                    {
                        context.Products.Add(product);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var deliveryMethodData = File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");
                    var deliveryMethods    = JsonConvert.DeserializeObject <List <DeliveryMethod> >(deliveryMethodData);

                    foreach (var deliveryMethod in deliveryMethods)
                    {
                        context.DeliveryMethods.Add(deliveryMethod);
                    }
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <SeedDataContext>();
                logger.LogError("An error ocured when seeding data", ex);
            }
        }
Esempio n. 2
0
        public static async Task SeedAsync(SkinetContext ctx, ILoggerFactory logFactory)
        {
            try
            {
                if (!ctx.ProductBrands.Any())
                {
                    var brandsData = await File.ReadAllTextAsync("../Infrastructure/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var brand in brands)
                    {
                        ctx.ProductBrands.Add(brand);
                    }

                    await ctx.SaveChangesAsync();
                }

                if (!ctx.ProductTypes.Any())
                {
                    var typesData = await File.ReadAllTextAsync("../Infrastructure/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var type in types)
                    {
                        ctx.ProductTypes.Add(type);
                    }

                    await ctx.SaveChangesAsync();
                }

                if (!ctx.Products.Any())
                {
                    var productsData = await File.ReadAllTextAsync("../Infrastructure/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var product in products)
                    {
                        ctx.Products.Add(product);
                    }

                    await ctx.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                var logger = logFactory.CreateLogger <SkinetContextSeed>();
                logger.LogError(e.Message);
            }
        }
Esempio n. 3
0
 public UnitOfWork(SkinetContext ctx)
 {
     _ctx = ctx;
 }
Esempio n. 4
0
 public GenericRepository(SkinetContext ctx)
 {
     _ctx = ctx;
 }
Esempio n. 5
0
 public ProductRepository(SkinetContext ctx)
 {
     _ctx = ctx;
 }