Ejemplo n.º 1
0
        public async Task SeedAsync(CatalogContext context, IHostingEnvironment env, IOptions <CatalogSettings> settings, ILogger <CatalogContextSeed> logger)
        {
            var policy = CreatePolicy(logger, nameof(CatalogContextSeed));

            await policy.ExecuteAsync(async() =>
            {
                var useCustomizationData = settings.Value.UseCustomizationData;
                var contentRootPath      = env.ContentRootPath;
                var picturePath          = env.WebRootPath;

                if (!context.CatalogBrands.Any())
                {
                    await context.CatalogBrands.AddRangeAsync(useCustomizationData
                        ? GetCatalogBrandsFromFile(contentRootPath, logger)
                        : GetPreconfiguredCatalogBrands());

                    await context.SaveChangesAsync();
                }

                if (!context.CatalogTypes.Any())
                {
                    await context.CatalogTypes.AddRangeAsync(useCustomizationData
                        ? GetCatalogTypesFromFile(contentRootPath, logger)
                        : GetPreconfiguredCatalogTypes());

                    await context.SaveChangesAsync();
                }

                if (!context.CatalogItems.Any())
                {
                    await context.CatalogItems.AddRangeAsync(useCustomizationData
                        ? GetCatalogItemsFromFile(contentRootPath, context, logger)
                        : GetPreconfiguredCatalogItems());

                    await context.SaveChangesAsync();

                    GetCatalogItemPictures(contentRootPath, picturePath);
                }
            });
        }
        public async Task SeedAsync(CatalogContext context, IHostingEnvironment env, IOptions <CatalogSettings> settings,
                                    ILogger <CatalogContextSeed> logger)
        {
            _env = env;
            var policy = CreatePolicy(logger, nameof(CatalogContextSeed));

            await policy.ExecuteAsync(async() =>
            {
                var useCustomizationData = settings.Value.UseCustomizationData;
                var contentRootPath      = env.ContentRootPath;
                if (!context.Manufacturers.Any())
                {
                    const string manufacturer = "Manufacturer";
                    await context.SpResetIdentity(manufacturer);
                    GetPictures(contentRootPath, _env.WebRootPath + "/" + manufacturer, manufacturer + ".zip");

                    if (useCustomizationData)
                    {
                        var manufacturers = GetManufacturerFromFile(contentRootPath, logger);
                        foreach (var manufacturer1 in manufacturers)
                        {
                            await context.Manufacturers.AddAsync(manufacturer1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.Manufacturers.AddRangeAsync(GetPreconfiguredManufacturer());

                        await context.SaveChangesAsync();
                    }

                    DeleteAllFilesWithinDir(_env.WebRootPath + "/" + manufacturer);
                    ValidateFileDirExists(manufacturer);
                }

                if (!context.Categories.Any())
                {
                    const string category = "Category";
                    await context.SpResetIdentity(category);
                    GetPictures(contentRootPath, _env.WebRootPath + "/" + category, category + ".zip");

                    if (useCustomizationData)
                    {
                        var categories = GetCategoryFromFile(contentRootPath, logger);
                        foreach (var category1 in categories)
                        {
                            await context.Categories.AddAsync(category1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.Categories.AddRangeAsync(GetPreconfiguredCategory());

                        await context.SaveChangesAsync();
                    }

                    DeleteAllFilesWithinDir(_env.WebRootPath + "/" + category);
                    ValidateFileDirExists(category);
                }

                if (!context.Users.Any())
                {
                    if (useCustomizationData)
                    {
                        var users = GetUserFromFile(contentRootPath, logger);
                        foreach (var user1 in users)
                        {
                            await context.Users.AddAsync(user1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.Users.AddRangeAsync(GetPreconfiguredUser());

                        await context.SaveChangesAsync();
                    }
                }

                if (!context.Products.Any())
                {
                    if (useCustomizationData)
                    {
                        var products = GetProductFromFile(contentRootPath, logger);
                        foreach (var product1 in products)
                        {
                            await context.Products.AddAsync(product1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.Products.AddRangeAsync(GetPreconfiguredProduct());

                        await context.SaveChangesAsync();
                    }
                }

                if (!context.ProductColors.Any())
                {
                    await context.SpResetIdentity("ProductColor");
                    if (useCustomizationData)
                    {
                        var productcolors = GetProductColorFromFile(contentRootPath, logger);
                        foreach (var productcolor1 in productcolors)
                        {
                            await context.ProductColors.AddAsync(productcolor1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.ProductColors.AddRangeAsync(GetPreconfiguredProductColor());

                        await context.SaveChangesAsync();
                    }
                }

                if (!context.ProductRatings.Any())
                {
                    await context.SpResetIdentity("ProductRating");
                    if (useCustomizationData)
                    {
                        var productratings = GetProductRatingFromFile(contentRootPath, logger);
                        foreach (var productrating1 in productratings)
                        {
                            await context.ProductRatings.AddAsync(productrating1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.ProductRatings.AddRangeAsync(GetPreconfiguredProductRating());

                        await context.SaveChangesAsync();
                    }
                }

                if (!context.ProductImages.Any())
                {
                    const string productImage = "ProductImage";
                    await context.SpResetIdentity(productImage);
                    GetPictures(contentRootPath, _env.WebRootPath + "/" + productImage, productImage + ".zip");

                    if (useCustomizationData)
                    {
                        var productimages = GetProductImageFromFile(contentRootPath, logger);
                        foreach (var productimage1 in productimages)
                        {
                            await context.ProductImages.AddAsync(productimage1);
                            await context.SaveChangesAsync();
                        }
                    }
                    else
                    {
                        await context.ProductImages.AddRangeAsync(GetPreconfiguredProductImage());

                        await context.SaveChangesAsync();
                    }

                    DeleteAllFilesWithinDir(_env.WebRootPath + "/" + productImage);
                    ValidateFileDirExists(productImage);
                }
            });
        }