public async Task <IActionResult> Import() { 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()}"); if (categoryPage == null) { categoryPage = CategoryPage.Create(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.GetAllAsync()) .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); } return(Redirect("~/")); }