//lägger till ett recept public Recipe AddRecipe(string name, List<List<object>> ingredients) { if (!Regex.IsMatch(name, @"^[a-zA-Z ]+$")) { errorMessage = "Var god fyll i ett receptnamn (endast bokstäver)"; throw new ArgumentException(); } if (!ingredients.Any()) { errorMessage = "Var god lägg till minst 1 ingrediens"; throw new ArgumentException(); } try { CreateMissingIngredients(ingredients); } catch (InvalidOperationException) { //felmedelandet = stack trace från createMissingIngredients(); throw new ArgumentException(); } Recipe newRecipe = new Recipe { recipeName = name }; thDb.Recipe.Add(newRecipe); try { thDb.SaveChanges(); } catch (DbUpdateException) { errorMessage = "Recept finns redan i databas, välj ett annat namn"; thDb.Recipe.Remove(newRecipe); throw new ArgumentException(); } foreach (List<object> l in ingredients) { IngredientRecipe iR = new IngredientRecipe { recipeName = name, ingredientName = (l[0] as string).ToLower(), amount = (int)l[1], unit = l[2] as string }; try { thDb.IngredientRecipe.Add(iR); thDb.SaveChanges(); } catch(DbUpdateException) { errorMessage = "Var god gruppera ingredienserna"; thDb.IngredientRecipe.Remove(iR); thDb.Recipe.Remove(newRecipe); thDb.SaveChanges(); throw new ArgumentException(); } } return newRecipe; }
public Recipe(TacoService.Recipe recipe) { Name = recipe.recipeName; Instructions = recipe.instructions; Servings = (int)recipe.servings; CookingTime = (int)recipe.cookingTime; Category = recipe.category; ingredientRecipe = new List<IngredientRecipe>(); foreach(TacoService.IngredientRecipe i in recipe.IngredientRecipe) { IngredientRecipe r = new IngredientRecipe(i); ingredientRecipe.Add(r); } }