Ejemplo n.º 1
0
        //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 List<Recipe> RecipeList()
        {
            Controller controller = new Controller();

            List<Recipe> recipes = new List<Recipe>();

            foreach (TacoService.Recipe i in controller.RecipeList())
            {
                Recipe r = new Recipe(i);
                recipes.Add(r);
            }

            return recipes;
        }
Ejemplo n.º 3
0
 //meny
 public Menu AddRecipeToMenu(string username, Recipe recipe, string weekNumber, string weekDay, string plannedQuantity)
 {
     return dal.AddRecipeToMenu(username, recipe, weekNumber, weekDay, plannedQuantity);
 }
Ejemplo n.º 4
0
        /*
         * meny
         *
         **/
        //lägger till ett recept till aktuell meny
        public Menu AddRecipeToMenu(string username, Recipe recipe, string weekNumber, string weekDay, string plannedQuantity)
        {
            Menu newMenuEntry = null;
            MenuRecipe newMapping = null;
            try
            {

                newMenuEntry = new Menu { userName = username, weekNumber = weekNumber, weekDay = weekDay };
                newMapping = new MenuRecipe
                {
                    userName = username,
                    recipeName = recipe.recipeName,
                    weekNumber = weekNumber,
                    weekDay = weekDay,
                    servings = int.Parse(plannedQuantity)
                };

                thDb.Menu.Add(newMenuEntry);
                thDb.MenuRecipe.Add(newMapping);
                thDb.SaveChanges();

            }
            catch (DbUpdateException)
            {
                thDb.Menu.Remove(newMenuEntry);
                thDb.MenuRecipe.Remove(newMapping);
                errorMessage = "Det finns redan ett recept på vald dag";
                throw new ArgumentException();
            }

            return newMenuEntry;
        }