public List <SubcategoryBL> GetAllSubCategoriesForRecipe(int idRecipe) { var listSubCategories = subCategoryRepository.GetAllSubCategoriesByRecipeID(idRecipe); var listIDSints = new List <int>(); var listOfSubCategoriesForRecipe = new List <SubcategoryBL>(); foreach (var i in listSubCategories) { listIDSints.Add(i.SubcategoryID); } var listNames = subCategoryRepository.GetNamesForRecipeSubCategories(listIDSints); for (var i = 0; i < listSubCategories.Count(); i++) { for (var j = 0; j < listNames.Count(); j++) { if (listSubCategories[i].SubcategoryID == listNames[j].SubcategoryID) { listOfSubCategoriesForRecipe.Add(new SubcategoryBL() { Name = listNames[j].Name, SubcategoryID = listSubCategories[i].SubcategoryID }); } } } return(listOfSubCategoriesForRecipe); }
public void Update(RecipeBL recipe, List <CategoryRecipeBL> listOfCategories, List <SubcategoryRecipeBL> listOfSubcategories) { var recipeDAL = new Recipe() { Name = recipe.Name, Description = recipe.Description, LongDescription = recipe.LongDescription, RatingID = recipe.RatingID, PictureLocation = recipe.PictureLocation, PrepTime = recipe.PrepTime, CookTime = recipe.CookTime, RecipeID = recipe.RecipeID }; recipeRepositoryG.Update(recipeDAL); List <CategoryRecipe> categoriesRecipe = new List <CategoryRecipe>(); List <SubcategoryRecipe> subcategoriesRecipe = new List <SubcategoryRecipe>(); foreach (var c in listOfCategories) { categoriesRecipe.Add(new CategoryRecipe { CategoryID = c.CategoryID, RecipeID = recipe.RecipeID }); } var listOfAllCategoriesForRecipe = catRepository.GetAllCategoriesByRecipeID(recipe.RecipeID); List <CategoryRecipe> categoriesToInsert = new List <CategoryRecipe>(); foreach (var c in categoriesRecipe) { if (!(listOfAllCategoriesForRecipe.Any(prod => prod.CategoryID == c.CategoryID & prod.RecipeID == c.RecipeID))) { categoriesToInsert.Add(c); } } foreach (var c in listOfAllCategoriesForRecipe) { if (!(categoriesRecipe.Any(prod => prod.CategoryID == c.CategoryID & prod.RecipeID == c.RecipeID))) { categoryRecipeRepository.Delete(c.CategoryRecipeID); } } foreach (var c in categoriesToInsert) { categoryRecipeRepository.Create(c); } foreach (var s in listOfSubcategories) { subcategoriesRecipe.Add(new SubcategoryRecipe { SubcategoryID = s.SubcategoryID, RecipeID = recipe.RecipeID }); } var listOfAllSubcategoriesForRecipe = subcatRepository.GetAllSubCategoriesByRecipeID(recipe.RecipeID); List <SubcategoryRecipe> subcategoriesToInsert = new List <SubcategoryRecipe>(); foreach (var c in subcategoriesRecipe) { if (!(listOfAllSubcategoriesForRecipe.Any(prod => prod.SubcategoryID == c.SubcategoryID & prod.RecipeID == c.RecipeID))) { subcategoriesToInsert.Add(c); } } foreach (var c in listOfAllSubcategoriesForRecipe) { if (!(subcategoriesRecipe.Any(prod => prod.SubcategoryID == c.SubcategoryID & prod.RecipeID == c.RecipeID))) { subcategoryRecipeRepository.Delete(c.SubcategoryRecipeID); } } foreach (var s in subcategoriesToInsert) { subcategoryRecipeRepository.Create(s); } }