Beispiel #1
0
        public async Task <IActionResult> PostShoppingListRecipe([FromBody] ShoppingListRecipe shoppingListRecipe)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.ShoppingListRecipe.Add(shoppingListRecipe);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ShoppingListRecipeExists(shoppingListRecipe.ShoppingListId, shoppingListRecipe.RecipeId))
                {
                    return(new StatusCodeResult(StatusCodes.Status409Conflict));
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetShoppingListRecipe", new { shoppingListId = shoppingListRecipe.ShoppingListId, recipeId = shoppingListRecipe.RecipeId }, shoppingListRecipe));
        }
Beispiel #2
0
        public ShoppingList AddRecipeToShoppingList(int recipeId)
        {
            var shoppingList = new ShoppingList {
                Name = "Untitled"
            };

            var recipe             = _recipeRepository.Get(x => x.Id == recipeId, x => x.RecipeIngredients);
            var shoppingListRecipe = new ShoppingListRecipe
            {
                Recipe       = recipe,
                ShoppingList = shoppingList
            };

            shoppingList.ShoppingListRecipes.Add(shoppingListRecipe);

            foreach (var recipeIngredient in recipe.RecipeIngredients)
            {
                var shoppingListIngredient = new ShoppingListIngredient
                {
                    Ingredient        = recipeIngredient.Ingredient,
                    Quantity          = recipeIngredient.Quantity,
                    UnitOfMeasurement = recipeIngredient.UnitOfMeasurement,
                    Weight            = recipeIngredient.Weight
                };

                shoppingList.ShoppingListIngredients.Add(shoppingListIngredient);
            }

            _shoppingListRepository.Create(shoppingList);
            _shoppingListRepository.SaveChanges();

            return(shoppingList);
        }
        public async Task <IActionResult> Edit(int shoppingListId, int recipeId, [Bind("ShoppingListId,RecipeId,Quantity")] ShoppingListRecipe shoppingListRecipe)
        {
            if (shoppingListId != shoppingListRecipe.ShoppingListId || recipeId != shoppingListRecipe.RecipeId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(shoppingListRecipe);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ShoppingListRecipeExists(shoppingListRecipe.ShoppingListId, shoppingListRecipe.RecipeId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ShoppingListId"] = new SelectList(_context.ShoppingList, nameof(ShoppingList.Id), nameof(ShoppingList.Name), shoppingListRecipe.ShoppingListId);
            ViewData["RecipeId"]       = new SelectList(_context.Recipe, nameof(Recipe.Id), nameof(Recipe.Name), shoppingListRecipe.RecipeId);
            return(View(shoppingListRecipe));
        }
        public async Task <IActionResult> Create([Bind("ShoppingListId,RecipeId,Quantity")] ShoppingListRecipe shoppingListRecipe)
        {
            if (ModelState.IsValid)
            {
                _context.Add(shoppingListRecipe);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ShoppingListId"] = new SelectList(_context.ShoppingList, nameof(ShoppingList.Id), nameof(ShoppingList.Name), shoppingListRecipe.ShoppingListId);
            ViewData["RecipeId"]       = new SelectList(_context.Recipe, nameof(Recipe.Id), nameof(Recipe.Name), shoppingListRecipe.RecipeId);
            return(View(shoppingListRecipe));
        }
Beispiel #5
0
        public void AddRecipesToShoppingList(int shoppingListId, IEnumerable <Recipe> recipes)
        {
            var shoppingList = _shoppingListRepository.Get(x => x.Id == shoppingListId, x => x.ShoppingListRecipes, x => x.ShoppingListIngredients);

            foreach (var recipe in recipes)
            {
                _recipeRepository.Get(r => r.Id == recipe.Id, r => r.RecipeIngredients);
                var shoppingListRecipe = new ShoppingListRecipe
                {
                    Recipe       = recipe,
                    ShoppingList = shoppingList
                };
                shoppingList.ShoppingListRecipes.Add(shoppingListRecipe);
            }

            List <RecipeIngredient> allRecipeIngredients = recipes.SelectMany(r => r.RecipeIngredients).ToList();

            foreach (var recipeIngredient in allRecipeIngredients)
            {
                var shoppingListIngredient = shoppingList.ShoppingListIngredients.FirstOrDefault(s => s.IngredientId == recipeIngredient.IngredientId);
                if (shoppingListIngredient == null)
                {
                    shoppingListIngredient = new ShoppingListIngredient
                    {
                        Ingredient        = recipeIngredient.Ingredient,
                        Quantity          = recipeIngredient.Quantity,
                        UnitOfMeasurement = recipeIngredient.UnitOfMeasurement,
                        Weight            = recipeIngredient.Weight
                    };
                    shoppingList.ShoppingListIngredients.Add(shoppingListIngredient);
                    continue;
                }

                shoppingListIngredient.Quantity += recipeIngredient.Quantity;
                shoppingListIngredient.Weight   += recipeIngredient.Weight;
            }

            _shoppingListRepository.Update(shoppingList);
            _shoppingListRepository.SaveChanges();
        }
Beispiel #6
0
        public async Task <IActionResult> PutShoppingListRecipe([FromRoute] int shoppingListId, [FromRoute] int recipeId, [FromBody] ShoppingListRecipe shoppingListRecipe)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (shoppingListId != shoppingListRecipe.ShoppingListId || recipeId != shoppingListRecipe.RecipeId)
            {
                return(BadRequest());
            }

            _context.Entry(shoppingListRecipe).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ShoppingListRecipeExists(shoppingListId, recipeId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }