public async override Task <ProductItem> Create(ProductItem product) { var categoryStringList = product.Categories.ToList(); for (int i = 0; i < product.Categories.Count; i++) { categoryStringList[i] = categoryStringList[i].Replace(" ", ""); } foreach (var categoryString in categoryStringList) { if (categoryService.All().Any(x => x.Name == categoryString)) { continue; } await categoryService.Create(new Category(categoryString, "")); } await base.Create(product); // Assigning foreign keys in many-many table foreach (var categoryString in categoryStringList) { var category = categoryService.All().FirstOrDefault(x => x.Name == categoryString); if (category == null) { continue; } await productItemCategoryService.Create(new ProductItemCategory(category.ID, product.ID)); } return(product); }
// Loading all object values into the cart's corresponding entities private async Task SetShoppingCart(ShoppingCart cart) { var itemOrders = itemOrderService.All() .Where(x => x.ShoppingCartID == cart.ID); foreach (var itemOrder in itemOrders) { itemOrder.ProductItem = await productItemService.GetByID(itemOrder.ProductItemID); var productItemCategories = productItemCategoryService.All() .Where(x => x.ProductID == itemOrder.ProductItemID); foreach (var productItemCategory in productItemCategories) { var category = await categoryService.GetByID(productItemCategory.CategoryID); itemOrder.ProductItem.CategoryObjects.Add(category); } itemOrder.ShoppingCart = cart; } cart.ItemOrders = itemOrders.ToList(); }