2022-05-10 09:07:07 -07:00
|
|
|
using System.Text.Json;
|
|
|
|
using Core.Entities;
|
2022-05-24 15:35:03 -07:00
|
|
|
using Core.Entities.OrderAggregate;
|
2022-05-10 09:07:07 -07:00
|
|
|
using Infrastructure.Data;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Infrastructure
|
|
|
|
{
|
|
|
|
public class StoreContextSeed
|
|
|
|
{
|
|
|
|
public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (!context.ProductBrands.Any())
|
|
|
|
{
|
|
|
|
var brandsData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
|
|
|
|
var brands = JsonSerializer.Deserialize<List<ProductBrand>>(brandsData);
|
|
|
|
foreach (var item in brands)
|
|
|
|
{
|
|
|
|
context.ProductBrands.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context.ProductTypes.Any())
|
|
|
|
{
|
|
|
|
var typesData = File.ReadAllText("../Infrastructure/Data/SeedData/types.json");
|
|
|
|
var types = JsonSerializer.Deserialize<List<ProductType>>(typesData);
|
|
|
|
foreach (var item in types)
|
|
|
|
{
|
|
|
|
context.ProductTypes.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context.Products.Any())
|
|
|
|
{
|
|
|
|
var productsData = File.ReadAllText("../Infrastructure/Data/SeedData/products.json");
|
|
|
|
var products = JsonSerializer.Deserialize<List<Product>>(productsData);
|
|
|
|
foreach (var item in products)
|
|
|
|
{
|
|
|
|
context.Products.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
}
|
2022-05-24 15:35:03 -07:00
|
|
|
|
|
|
|
if (!context.DeliveryMethod.Any())
|
|
|
|
{
|
|
|
|
var dmData = File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");
|
|
|
|
var methods = JsonSerializer.Deserialize<List<DeliveryMethod>>(dmData);
|
|
|
|
foreach (var item in methods)
|
|
|
|
{
|
|
|
|
context.DeliveryMethod.Add(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-10 09:07:07 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var logger = loggerFactory.CreateLogger<StoreContextSeed>();
|
|
|
|
logger.LogError(ex.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|