public async Task <IActionResult> Import() { int importCount = 0; int existCount = 0; var site = await api.Sites.GetDefaultAsync(); var catalog = await api.Pages .GetBySlugAsync <CatalogPage>("catalog"); var categories = db.Categories.Include(c => c.Products); foreach (var category in categories) { // if the category page already exists, // then skip to the next iteration of the loop CategoryPage categoryPage = await api.Pages.GetBySlugAsync <CategoryPage>( $"catalog/{category.CategoryName.ToLower().Replace(' ', '-')}"); if (categoryPage == null) { importCount++; categoryPage = await CategoryPage.CreateAsync(api); categoryPage.Id = Guid.NewGuid(); categoryPage.SiteId = site.Id; categoryPage.ParentId = catalog.Id; categoryPage.CategoryDetail.CategoryID = category.CategoryID; categoryPage.CategoryDetail.CategoryName = category.CategoryName; categoryPage.CategoryDetail.Description = category.Description; // find image with correct filename for category id var image = (await api.Media.GetAllByFolderIdAsync()) .First(media => media.Type == MediaType.Image && media.Filename == $"category{category.CategoryID}.jpeg"); categoryPage.CategoryDetail.CategoryImage = image; if (categoryPage.Products.Count == 0) { // convert the products for this category into // a list of instances of ProductRegion categoryPage.Products = category.Products .Select(p => new ProductRegion { ProductID = p.ProductID, ProductName = p.ProductName, UnitPrice = p.UnitPrice.HasValue ? p.UnitPrice.Value.ToString("c") : "n/a", UnitsInStock = p.UnitsInStock ?? 0 }).ToList(); } categoryPage.Title = category.CategoryName; categoryPage.MetaDescription = category.Description; categoryPage.NavigationTitle = category.CategoryName; categoryPage.Published = DateTime.Now; await api.Pages.SaveAsync(categoryPage); } else { existCount++; } } TempData["import_message"] = $"{existCount} categories already existed. {importCount} new categories imported."; return(Redirect("~/")); }
public async Task <IActionResult> Import() { int importCount = 0; int existCount = 0; var site = await _api.Sites.GetDefaultAsync(); var catalog = await _api.Pages.GetBySlugAsync <CatalogPage>("catalog"); await foreach (Category c in _db.Categories.Include(c => c.Products).AsAsyncEnumerable()) { var categoryPage = await _api.Pages.GetBySlugAsync <CategoryPage>($"catalog/{c.CategoryName.ToLower().Replace(' ', '-') }"); if (categoryPage == null) { importCount++; categoryPage = await CategoryPage.CreateAsync(_api); categoryPage.Id = Guid.NewGuid(); categoryPage.SiteId = site.Id; categoryPage.ParentId = catalog.Id; categoryPage.CategoryDetail.CategoryId = (int)c.CategoryId; categoryPage.CategoryDetail.CategoryName = c.CategoryName; categoryPage.CategoryDetail.Description = c.Description; var folders = await _api.Media.GetAllFoldersAsync(); var categoriesFolderId = folders.First(folder => folder.Name == "Categories").Id; var byFolder = await _api.Media.GetAllByFolderIdAsync(categoriesFolderId); var image = byFolder.FirstOrDefault(media => media.Type == MediaType.Image && media.Filename == $"category{c.CategoryId}.jpeg"); categoryPage.CategoryDetail.CategoryImage = image ?? categoryPage.CategoryDetail.CategoryImage; if (categoryPage.Products.Count == 0) { categoryPage.Products = c.Products .Select(p => new ProductRegion { ProductId = (int)p.ProductId, ProductName = p.ProductName, UnitPrice = p.Cost.ToString("c"), UnitsInStock = (int)p.Stock }).ToList(); } categoryPage.Title = c.CategoryName; categoryPage.MetaDescription = c.Description; categoryPage.NavigationTitle = c.CategoryName; categoryPage.Published = DateTime.Now; await _api.Pages.SaveAsync(categoryPage); } else { existCount++; } } TempData["import_message"] = $"{existCount} categories already existed. {importCount} new categories imported."; return(Redirect("~/")); }