//method to get the model if only the recipeid is known
        private RecipeViewModel GetRecipeViewModelByRecipeId(int? id)
        {
            db = new ApplicationDbContext();
            Recipe recipe = db.Recipes.FirstOrDefault(r => r.RecipeId == id);
            IQueryable<Ingredient> ingredients = db.Ingredients.Where(i => i.Recipe.RecipeId == id);

            RecipeViewModel model = new RecipeViewModel() { Recipe = recipe, Ingredients = ingredients.ToList() };
            return model;
        }
示例#2
0
        public static Boolean isAuthorized(int id, IPrincipal User)
        {
            if(!User.Identity.IsAuthenticated)
            {
                return false;
            }
            ApplicationDbContext db = new ApplicationDbContext();
            Recipe recipe = db.Recipes.Find(id);
            RecipeViewModel rmodel = new RecipeViewModel() { Recipe = recipe, Ingredients = recipe.Ingredients.ToList() };

            var authorized = checkForEditDeleteRights(rmodel, db, User.Identity.Name);
            return authorized;
        }
 private Boolean checkForEditDeleteRights(RecipeViewModel recipe)
 {
     ApplicationUser appUser = db.Users.FirstOrDefault(x => x.UserName == UserName); // get current user
     var adminRole = db.Roles.FirstOrDefault(x => x.Name == "admin");
     bool isAdmin = false;
     if (adminRole != null)
     {
         var usersWithAdminRole = adminRole.Users.FirstOrDefault(x => x.UserId == appUser.Id);
         if (usersWithAdminRole != null)
             isAdmin = true;
     }
     if (appUser.Equals(recipe.Recipe.User) || isAdmin)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var authorized = base.AuthorizeCore(httpContext);
            if(!authorized)
            {
                return false;
            }

            var rd = httpContext.Request.RequestContext.RouteData;
            int id;
            Int32.TryParse((string)rd.Values["id"], out id);
            UserName = httpContext.User.Identity.Name;

            Recipe recipe = db.Recipes.Find(id);
            RecipeViewModel model = new RecipeViewModel() { Recipe = recipe, Ingredients = recipe.Ingredients.ToList() };

            rd.Values["model"] = model;
            authorized = checkForEditDeleteRights(model);
            return authorized;
        }
        public ActionResult Create(RecipeViewModel completeRecipe, HttpPostedFileBase file)
        {
            try
            {
                //add ingredients
                foreach (Ingredient ingredient in completeRecipe.Ingredients)
                {
                    if (ingredient.IngredientName == null)
                    {
                        completeRecipe.Ingredients.Remove(ingredient);
                        break;
                    }

                    ingredient.Recipe = completeRecipe.Recipe;
                    completeRecipe.Recipe.Ingredients.Add(ingredient);
                    
                    if (ModelState.IsValid)
                    {
                        db.Ingredients.Add(ingredient);
                    }
                }
                //add user
                var appUser = db.Users.FirstOrDefault(x => x.UserName == User.Identity.Name);
                completeRecipe.Recipe.User = appUser;
                appUser.Recipes.Add(completeRecipe.Recipe);
                //add picture
                PictureUpload(completeRecipe, file);
                //revert counter again
                RecipeViewModel.IngredientCounter.Instance.IngredientCount = 0;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                RecipeViewModel.IngredientCounter.Instance.IngredientCount = 0;
                return View();
            }
        }
 public PartialViewResult AddIngredient(RecipeViewModel model)
 {
     //new ingredient
     RecipeViewModel.IngredientCounter.Instance.IngredientCount++;
     return PartialView("~/Views/Ingredients/CreatePartial.cshtml", model);
 }
        public string PictureUpload(RecipeViewModel model, HttpPostedFileBase file)
        {
            if (file != null)
            {
                string fileName = Format(Guid.NewGuid() + Path.GetExtension(file.FileName));
                //uploaded images are deleted in appharbor with each deploy and maybe after a certain amount of time
                string uploadDir = @"\Images";
                if (!Directory.Exists(Server.MapPath(uploadDir)))
                {
                    Directory.CreateDirectory(Server.MapPath(uploadDir));
                }

                var imagePath = Path.Combine(Server.MapPath(uploadDir), fileName);
                var imageUrl = Path.Combine(uploadDir, fileName);
                file.SaveAs(imagePath);
                model.Recipe.PictureUrl = imageUrl;

                return imageUrl;
            }

            return Empty;
        }
 public ActionResult Delete(RecipeViewModel model)
 {
     return View(model);
 }
        public ActionResult Edit(RecipeViewModel recipeViewModel, HttpPostedFileBase file, bool deleteImage)
        {
            try
            {
                IList<Ingredient> ingredients = recipeViewModel.Ingredients;

                RecipeViewModel recipeViewModelByRecipeId = GetRecipeViewModelByRecipeId(recipeViewModel.Recipe.RecipeId);

                if (ModelState.IsValid)
                {
                    //the posted edited data has to be changed in the current entity
                    recipeViewModelByRecipeId.Recipe.Title = recipeViewModel.Recipe.Title;
                    recipeViewModelByRecipeId.Recipe.Instructions = recipeViewModel.Recipe.Instructions;
                    recipeViewModelByRecipeId.Recipe.InitialServings = recipeViewModel.Recipe.InitialServings;

                    IList<Ingredient> toDelete = recipeViewModelByRecipeId.Recipe.Ingredients.ToList();

                    foreach (Ingredient ingredient in toDelete)
                    {
                        //since ingredients are in a different db context i will just remove them and add them again
                        db.Ingredients.Remove(ingredient);
                    }
                    db.SaveChanges();
                    foreach (Ingredient ingredient in ingredients)
                    {
                        if (!string.IsNullOrEmpty(ingredient.IngredientName))
                        {
                            //adding the new ingredients that have the changes from the edit form
                            ingredient.Recipe = recipeViewModelByRecipeId.Recipe;
                            recipeViewModelByRecipeId.Recipe.Ingredients.Add(ingredient);
                        }
                    }

                    //when setting entity state to modified, it will be saved using the savechanges below
                    db.Entry(recipeViewModelByRecipeId.Recipe).State = EntityState.Modified;

                    db.SaveChanges();
                    RecipeViewModel.IngredientCounter.Instance.IngredientCount = 0;

                    if (file != null && !deleteImage)
                    {
                        //editing picture
                        string url = PictureUpload(recipeViewModelByRecipeId, file);
                        recipeViewModel.Recipe.PictureUrl = url;
                        db.SaveChanges();
                        RedirectToAction("Index");
                    }
                    if (deleteImage)
                    {
                        //deleting image if desired
                        recipeViewModel.Recipe.PictureUrl = null;
                        recipeViewModelByRecipeId.Recipe.PictureUrl = null;
                        db.SaveChanges();
                    }
                }

                return RedirectToAction("Index", recipeViewModelByRecipeId);
            }
            catch
            {
                RecipeViewModel.IngredientCounter.Instance.IngredientCount = 0;
                return View();
            }
        }
 public ActionResult Edit(RecipeViewModel model)
 {
     RecipeViewModel recipeViewModel = GetRecipeViewModelByRecipeId(model.Recipe.RecipeId);
     //the counter here has to be -1 because it will be incremented in a loop
     //i know it's ugly but i tried to get the ingredient stuff to work for about 40 hours (yes, i was surprised myself)
     RecipeViewModel.IngredientCounter.Instance.IngredientCount = 0;
     return View(recipeViewModel);
 }