Beispiel #1
0
        public AddRecipeViewModel()
        {
            StepIngredList  = new ObservableCollection <RecipeStepIngredientVM>();
            IngredientsList = new ObservableCollection <IngredientListVM>();
            Keywords        = new ObservableCollection <Keyword>();

            NewUserRcpFav = new UserRecipeFavorite()
            {
                UserId   = userDetailsID,
                Favorite = false,
                RecipeId = 0
            };

            NewRcp = new RecipeVM()
            {
                RecipeName  = "",
                Description = "",
                OwnerId     = userDetailsID,
                Shared      = false,
                Notes       = "",
                //MyRating = 0,
                //ShareRating = 0,
                //NumShareRatings = 0,
                RecipePic = "",
                //PrepTime = 0,
                //CookTime = 0,
                ServingSize = ""
            };
        }
Beispiel #2
0
        public IActionResult GetMyRecipes(int id)
        {
            var myRecipePntrs = from recipePtr in _context.Recipes
                                where recipePtr.OwnerId == id
                                select recipePtr;
            var tempList = myRecipePntrs.ToList();

            List <UserRecipeVM> myRecipes = new List <UserRecipeVM>();

            foreach (Recipe recipePtr in tempList)
            {
                var recipeObj = new UserRecipeFavorite();
                recipeObj = _context.UserRecipeFavorite.FirstOrDefault(u => u.RecipeId == recipePtr.Id && u.UserId == recipePtr.OwnerId);

                var recipe = new UserRecipeVM();
                recipe.UserId          = recipeObj.UserId;
                recipe.RecipeId        = recipeObj.RecipeId;
                recipe.Favorite        = recipeObj.Favorite;
                recipe.RecipeName      = recipePtr.RecipeName;
                recipe.Description     = recipePtr.Description;
                recipe.OwnerId         = recipePtr.OwnerId;
                recipe.Shared          = recipePtr.Shared;
                recipe.Notes           = recipePtr.Notes;
                recipe.MyRating        = recipePtr.MyRating;
                recipe.ShareRating     = recipePtr.ShareRating;
                recipe.NumShareRatings = recipePtr.NumShareRatings;
                recipe.RecipePic       = recipePtr.RecipePic;
                recipe.PrepTime        = recipePtr.PrepTime;
                recipe.CookTime        = recipePtr.CookTime;
                recipe.RecipeServings  = recipePtr.RecipeServings;
                recipe.ServingSize     = recipePtr.ServingSize;

                myRecipes.Add(recipe);
            }

            if (myRecipes.Count() == 0)
            {
                return(NotFound());
            }

            return(new ObjectResult(myRecipes));
        }
Beispiel #3
0
        public IActionResult FavRecipeToggle(int userId, int recipeId)
        {
            // Make sure user and recipe IDs are valid
            if (_context.Users.FirstOrDefault(u => u.Id == userId) == null || _context.Recipes.FirstOrDefault(r => r.Id == recipeId) == null)
            {
                return(NotFound());
            }

            // Add entry if it does not already exists in UserRecipeFavorites table otherwise toggle Favorite
            var favRecipe = _context.UserRecipeFavorite.FirstOrDefault(r => r.UserId == userId && r.RecipeId == recipeId);

            if (favRecipe == null)
            {
                var newFavRecipe = new UserRecipeFavorite();
                newFavRecipe.UserId   = userId;
                newFavRecipe.RecipeId = recipeId;
                newFavRecipe.Favorite = true;
                _context.UserRecipeFavorite.Add(newFavRecipe);
            }
            else
            {
                favRecipe.UserId   = userId;
                favRecipe.RecipeId = recipeId;
                if (favRecipe.Favorite == false)
                {
                    favRecipe.Favorite = true;
                }
                else
                {
                    favRecipe.Favorite = false;
                }
                _context.UserRecipeFavorite.Update(favRecipe);
            }
            _context.SaveChanges();
            return(new NoContentResult());
        }
Beispiel #4
0
        async void OnSaveRecipeClicked(object sender, EventArgs e)
        {
            var newRecipe     = new AddEditRecipe();
            var isNewRecipe   = true;
            var newUserRecFav = new UserRecipeFavorite();

            newUserRecFav.UserId         = userDetailsID;
            newUserRecFav.RecipeId       = 0;
            newUserRecFav.Favorite       = false;
            newRecipe.userRecipeFavorite = newUserRecFav;

            var tempRecipe = new RecipeVM();

            tempRecipe.OwnerId         = userDetailsID;
            tempRecipe.RecipeName      = App.AddRecipeViewModel.NewRcp.RecipeName;
            tempRecipe.Description     = App.AddRecipeViewModel.NewRcp.Description;
            tempRecipe.Shared          = false;
            tempRecipe.MyRating        = 0;
            tempRecipe.ShareRating     = 0m;
            tempRecipe.NumShareRatings = 0m;
            tempRecipe.PrepTime        = App.AddRecipeViewModel.NewRcp.PrepTime;
            tempRecipe.CookTime        = App.AddRecipeViewModel.NewRcp.CookTime;
            tempRecipe.RecipeServings  = App.AddRecipeViewModel.NewRcp.RecipeServings;
            tempRecipe.RecipePic       = App.AddRecipeViewModel.NewRcp.RecipePic;

            newRecipe.recipe = tempRecipe;

            List <IngredientListVM> ingredientList = new List <IngredientListVM>();

            foreach (IngredientListVM tempIngred in App.AddRecipeViewModel.IngredientsList)
            {
                var ingred = new IngredientListVM();
                ingred.IngredName = tempIngred.IngredName;
                ingred.IngredQty  = tempIngred.IngredQty;
                ingred.IngredUnit = tempIngred.IngredUnit;
                ingredientList.Add(ingred);
            }

            List <RecipeStepIngredientVM> stepList = new List <RecipeStepIngredientVM>();

            foreach (RecipeStepIngredientVM tempStep in App.AddRecipeViewModel.StepIngredList)
            {
                var step = new RecipeStepIngredientVM();
                List <IngredientListVM> emptyList = new List <IngredientListVM>();
                step.StepNumber  = tempStep.StepNumber;
                step.Instruction = tempStep.Instruction;
                if (step.StepNumber == 1)
                {
                    step.IngredientList = ingredientList;
                }
                else
                {
                    step.IngredientList = emptyList;
                }
                stepList.Add(step);
            }
            newRecipe.RecipeStepIngredients = stepList;

            var keyword = new Keyword();

            keyword.SearchWord = " ";
            List <Keyword> keywords = new List <Keyword>();

            keywords.Add(keyword);

            newRecipe.Keywords = keywords;

            await App.cabeMgr.SaveRecipeAsync(newRecipe, isNewRecipe);

            await Navigation.PopAsync();
        }
Beispiel #5
0
        public IActionResult GetSharedRecipes(int userId)
        {
            //var userId = 240;
            List <Recipe> tempSharedList = new List <Recipe>();
            //if (searchStr != null)
            //{
            //    var tmpSharedRecipes = from recipe in _context.Recipes
            //                           where (recipe.Description.Contains('%' + searchStr + '%'))
            //                           select recipe;
            //    tempSharedList = tmpSharedRecipes.ToList();

            //}
            //else
            //{
            var tmpSharedRecipes = from recipe in _context.Recipes
                                   where recipe.Shared == true
                                   select recipe;

            tempSharedList = tmpSharedRecipes.ToList();
            //var tempSharedList = tmpSharedRecipes.ToList();

            //}

            List <UserRecipeVM> sharedRecipes = new List <UserRecipeVM>();

            foreach (Recipe recipePtr in tempSharedList)
            {
                var recipe    = new UserRecipeVM();
                var recipeObj = new UserRecipeFavorite();
                recipeObj = _context.UserRecipeFavorite.FirstOrDefault(u => u.RecipeId == recipePtr.Id && u.UserId == userId);
                if (recipeObj != null)
                {
                    recipe.Favorite = recipeObj.Favorite;
                }
                else
                {
                    recipe.Favorite = false;
                }
                recipe.UserId          = userId;
                recipe.RecipeId        = recipePtr.Id;
                recipe.RecipeName      = recipePtr.RecipeName;
                recipe.Description     = recipePtr.Description;
                recipe.OwnerId         = recipePtr.OwnerId;
                recipe.Shared          = recipePtr.Shared;
                recipe.Notes           = recipePtr.Notes;
                recipe.MyRating        = recipePtr.MyRating;
                recipe.ShareRating     = recipePtr.ShareRating;
                recipe.NumShareRatings = recipePtr.NumShareRatings;
                recipe.RecipePic       = recipePtr.RecipePic;
                recipe.PrepTime        = recipePtr.PrepTime;
                recipe.CookTime        = recipePtr.CookTime;
                recipe.RecipeServings  = recipePtr.RecipeServings;
                recipe.ServingSize     = recipePtr.ServingSize;

                sharedRecipes.Add(recipe);
            }

            if (sharedRecipes.Count() == 0)
            {
                return(NotFound());
            }
            return(new ObjectResult(sharedRecipes));
        }
Beispiel #6
0
        public IActionResult Create([FromBody] AddEditRecipeVM recipeInput)
        //public IActionResult Post([FromBody] Object jsonRecipeInput)
        {
            //var jsonString = jsonRecipeInput.ToString();
            //AddEditRecipeVM recipeInput = JsonConvert.DeserializeObject<AddEditRecipeVM>(jsonString);

            if (ModelState.IsValid)
            {
                // Validate data here before writing to database
                if (_context.Users.FirstOrDefault(u => u.Id == recipeInput.userRecipeFavorite.UserId) == null || _context.Users.FirstOrDefault(u => u.Id == recipeInput.recipe.OwnerId) == null)
                {
                    return(NotFound());
                }

                var editRecipe = false;
                List <MenuRecipe>         menuRecipeEdits    = new List <MenuRecipe>();
                List <UserRecipeFavorite> userRecipeFavEdits = new List <UserRecipeFavorite>();
                if (recipeInput.recipe.RecipeId != 0)
                {
                    editRecipe = true;

                    // Save list of MenuRecipe entries
                    var tempMenuRecipes = from recipePtr in _context.MenuRecipe
                                          where recipePtr.RecipeId == recipeInput.recipe.RecipeId
                                          select recipePtr;
                    menuRecipeEdits = tempMenuRecipes.ToList();

                    // Delete MenuRecipe entries
                    _context.MenuRecipe.RemoveRange(_context.MenuRecipe.Where(m => m.RecipeId == recipeInput.recipe.RecipeId));
                    _context.SaveChanges();

                    // Delete StepIngredients entries
                    var tempSteps = from stepPtr in _context.Steps
                                    where stepPtr.RecipeId == recipeInput.recipe.RecipeId
                                    select stepPtr;
                    var delStepList = tempSteps.ToList();
                    foreach (Step delStepPtr in delStepList)
                    {
                        _context.StepIngredient.RemoveRange(_context.StepIngredient.Where(s => s.StepId == delStepPtr.Id));
                    }
                    _context.SaveChanges();

                    // Delete RecipeKeyword entries
                    _context.RecipeKeyword.RemoveRange(_context.RecipeKeyword.Where(r => r.RecipeId == recipeInput.recipe.RecipeId));
                    _context.SaveChanges();

                    // Save list of UserRecipeFavorite entries if recipe is shared
                    var curRecipe = new Recipe();
                    curRecipe = _context.Recipes.FirstOrDefault(r => r.Id == recipeInput.recipe.RecipeId);
                    var curRecipeShared = false;
                    if (curRecipe != null)
                    {
                        curRecipeShared = curRecipe.Shared;
                    }
                    if (recipeInput.recipe.Shared == true && curRecipeShared == true)
                    {
                        var tempUserRecFav = from userRecipePtr in _context.UserRecipeFavorite
                                             where userRecipePtr.RecipeId == recipeInput.recipe.RecipeId && userRecipePtr.UserId != recipeInput.recipe.OwnerId
                                             select userRecipePtr;
                        userRecipeFavEdits = tempUserRecFav.ToList();
                    }

                    // Delete UserRecipeFavorite entries
                    _context.UserRecipeFavorite.RemoveRange(_context.UserRecipeFavorite.Where(u => u.RecipeId == recipeInput.recipe.RecipeId));
                    _context.SaveChanges();

                    // Delete Steps entries
                    foreach (Step delStepPtr in delStepList)
                    {
                        _context.Steps.RemoveRange(_context.Steps.Where(s => s.RecipeId == delStepPtr.RecipeId));
                    }
                    _context.SaveChanges();

                    // Delete Recipes entry
                    _context.Recipes.RemoveRange(_context.Recipes.Where(r => r.Id == recipeInput.recipe.RecipeId));
                    _context.SaveChanges();
                }

                // Add Recipe table entry
                var tempRecipe = new Recipe();
                tempRecipe.RecipeName      = recipeInput.recipe.RecipeName;
                tempRecipe.Description     = recipeInput.recipe.Description;
                tempRecipe.OwnerId         = recipeInput.recipe.OwnerId;
                tempRecipe.Shared          = recipeInput.recipe.Shared;
                tempRecipe.Notes           = recipeInput.recipe.Notes;
                tempRecipe.MyRating        = recipeInput.recipe.MyRating;
                tempRecipe.ShareRating     = recipeInput.recipe.ShareRating;
                tempRecipe.NumShareRatings = recipeInput.recipe.NumShareRatings;
                tempRecipe.RecipePic       = recipeInput.recipe.RecipePic;
                tempRecipe.PrepTime        = recipeInput.recipe.PrepTime;
                tempRecipe.CookTime        = recipeInput.recipe.CookTime;
                tempRecipe.RecipeServings  = recipeInput.recipe.RecipeServings;
                tempRecipe.ServingSize     = recipeInput.recipe.ServingSize;
                _context.Recipes.Add(tempRecipe);
                _context.SaveChanges();

                // Add entry to UserRecipeFavorite join table
                var tempUserRecipeFavorite = new UserRecipeFavorite();
                tempUserRecipeFavorite.UserId   = recipeInput.userRecipeFavorite.UserId;
                tempUserRecipeFavorite.RecipeId = tempRecipe.Id;
                tempUserRecipeFavorite.Favorite = recipeInput.userRecipeFavorite.Favorite;
                _context.UserRecipeFavorite.Add(tempUserRecipeFavorite);
                _context.SaveChanges();

                // Add steps to Steps table
                List <RecipeStepIngredientVM> tempRecipeStepIngredient = new List <RecipeStepIngredientVM>();
                tempRecipeStepIngredient = recipeInput.RecipeStepIngredients;

                // Add each step to Steps table
                foreach (RecipeStepIngredientVM stepItem in tempRecipeStepIngredient)
                {
                    var tempStepItem = new Step();
                    tempStepItem.RecipeId    = tempRecipe.Id;
                    tempStepItem.StepNumber  = stepItem.StepNumber;
                    tempStepItem.Instruction = stepItem.Instruction;
                    _context.Steps.Add(tempStepItem);
                    _context.SaveChanges();

                    // Add each ingredient to Ingredient and StepIngredient tables
                    List <IngredientListVM> tempIngredList = new List <IngredientListVM>();
                    tempIngredList = stepItem.IngredientList;

                    foreach (IngredientListVM ingredItem in tempIngredList)
                    {
                        var tempIngredId = 0;
                        var tempIngred   = new Ingredient();
                        tempIngred = _context.Ingredients.FirstOrDefault(i => i.IngredName.ToLower() == ingredItem.IngredName.ToLower());
                        if (tempIngred == null)
                        {
                            var newIngred = new Ingredient();
                            newIngred.IngredName = ingredItem.IngredName;
                            _context.Ingredients.Add(newIngred);
                            _context.SaveChanges();
                            tempIngredId = newIngred.Id;
                        }
                        else
                        {
                            tempIngredId = tempIngred.Id;
                        }  // end add ingredient name

                        var tempStepIngred = new StepIngredient();
                        tempStepIngred.StepId       = tempStepItem.Id;
                        tempStepIngred.IngredientId = tempIngredId;
                        tempStepIngred.IngredQty    = ingredItem.IngredQty;
                        tempStepIngred.IngredUnit   = ingredItem.IngredUnit;
                        _context.StepIngredient.Add(tempStepIngred);
                        _context.SaveChanges();
                    } // end add ingredItem loop
                }     // end add stepItem loop

                // Add searchwords to Keywords table
                List <Keyword> keywordList = new List <Keyword>();
                keywordList = recipeInput.Keywords;
                if (keywordList != null)
                {
                    foreach (Keyword tempKeyword in keywordList)
                    {
                        var tempKeywordId  = 0;
                        var tempSearchWord = new Keyword();
                        tempSearchWord = _context.Keywords.FirstOrDefault(k => k.SearchWord.ToLower() == tempKeyword.SearchWord.ToLower());
                        if (tempSearchWord == null)
                        {
                            var newKeyword = new Keyword();
                            newKeyword.SearchWord = tempKeyword.SearchWord;
                            _context.Keywords.Add(newKeyword);
                            _context.SaveChanges();
                            tempKeywordId = newKeyword.Id;
                        }
                        else
                        {
                            tempKeywordId = tempSearchWord.Id;
                        }

                        // Add entry to RecipeKeyword join table
                        var tempRecipeKeyword = new RecipeKeyword();
                        tempRecipeKeyword.RecipeId  = tempRecipe.Id;
                        tempRecipeKeyword.KeywordId = tempKeywordId;
                        _context.RecipeKeyword.Add(tempRecipeKeyword);
                        _context.SaveChanges();
                    }
                }

                if (editRecipe == true)
                {
                    // Re-enter MenuRecipe entries with new RecipeId
                    foreach (MenuRecipe recipePtr in menuRecipeEdits)
                    {
                        var tempMenuRecipe = new MenuRecipe();
                        tempMenuRecipe.MenuId       = recipePtr.MenuId;
                        tempMenuRecipe.RecipeId     = tempRecipe.Id;
                        tempMenuRecipe.MenuServings = recipePtr.MenuServings;
                        _context.MenuRecipe.Add(tempMenuRecipe);
                    }
                    _context.SaveChanges();

                    // Re-enter UserRecipeFavorites entries with new RecipeId for shared recipes
                    foreach (UserRecipeFavorite userRecipePtr in userRecipeFavEdits)
                    {
                        var tempUserRecipe = new UserRecipeFavorite();
                        tempUserRecipe.UserId   = userRecipePtr.UserId;
                        tempUserRecipe.RecipeId = tempRecipe.Id;
                        tempUserRecipe.Favorite = userRecipePtr.Favorite;
                        _context.UserRecipeFavorite.Add(tempUserRecipe);
                    }
                    _context.SaveChanges();
                }
                return(CreatedAtRoute("", new { id = tempRecipe.Id }, recipeInput));
            }
            return(BadRequest());
        }