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);
                }
            });
        }
Example #2
0
        private IEnumerable <CatalogItem> GetCatalogItemsFromFile(string contentRootPath, CatalogContext context, ILogger <CatalogContextSeed> logger)
        {
            string csvFileCatalogItems = Path.Combine(contentRootPath, "Setup", "CatalogItems.csv");

            if (!File.Exists(csvFileCatalogItems))
            {
                return(GetPreconfiguredItems());
            }

            string[] csvheaders;
            try
            {
                string[] requiredHeaders = { "catalogtypename", "catalogbrandname", "description", "name", "price", "pictureFileName" };
                string[] optionalheaders = { "availablestock", "restockthreshold", "maxstockthreshold", "onreorder" };
                csvheaders = GetHeaders(csvFileCatalogItems, requiredHeaders, optionalheaders);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message);
                return(GetPreconfiguredItems());
            }

            var catalogTypeIdLookup  = context.CatalogTypes.ToDictionary(ct => ct.Type, ct => ct.Id);
            var catalogBrandIdLookup = context.CatalogBrands.ToDictionary(ct => ct.Brand, ct => ct.Id);

            return(File.ReadAllLines(csvFileCatalogItems)
                   .Skip(1)      // skip header row
                   .Select(row => Regex.Split(row, ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"))
                   .SelectTry(column => CreateCatalogItem(column, csvheaders, catalogTypeIdLookup, catalogBrandIdLookup))
                   .OnCaughtException(ex => { logger.LogError(ex, "EXCEPTION ERROR: {Message}", ex.Message); return null; })
                   .Where(x => x != null));
        }