Ejemplo n.º 1
0
        public IActionResult CommentOnRecipe(int RecipeId, Comment Comment)
        {
            int?        UserId = HttpContext.Session.GetInt32("UserId");
            User        User   = dbContext.Users.Include(user => user.Recipes).Include(user => user.Comments).FirstOrDefault(user => user.UserId == UserId);
            AdminRecipe Recipe = dbContext.AdminRecipes.Include(recipe => recipe.User).Include(recipe => recipe.Comments).FirstOrDefault(recipe => recipe.RecipeId == RecipeId);

            if (UserId != null)
            {
                Comment.UserId   = User.UserId;
                Comment.User     = User;
                Comment.Recipe   = Recipe;
                Comment.RecipeId = Recipe.RecipeId;
                TryValidateModel(Comment);
                ModelState.Remove("User.ConfirmPassword");
                ModelState.Remove("Recipe.User.ConfirmPassword");
                ModelState.Remove("User");
                ModelState.Remove("Recipe");
                string check = RegexCheck(Comment, RegEx);
                if (check != null)
                {
                    ModelState.AddModelError(check, "Please use only letters, numbers, periods, commas, hyphens, or exclamation points");
                }
                if (ModelState.IsValid)
                {
                    dbContext.Add(Comment);
                    dbContext.SaveChanges();
                }
            }
            return(RedirectToAction("Recipe", new { Title = Recipe.Title }));
        }
Ejemplo n.º 2
0
        public IActionResult Recipe(string Title)
        {
            int?        UserId = HttpContext.Session.GetInt32("UserId");
            User        User   = dbContext.Users.Include(user => user.Ratings).FirstOrDefault(user => user.UserId == UserId);
            AdminRecipe Recipe = dbContext.AdminRecipes.Include(recipe => recipe.User).Include(recipe => recipe.Likes).Include(recipe => recipe.Comments).Include(recipe => recipe.Ratings).FirstOrDefault(recipe => recipe.Title == Title);

            if (User != null)
            {
                Rating Rating = User.Ratings.FirstOrDefault(rating => rating.RecipeId == Recipe.RecipeId);
                ViewBag.Rating = Rating;
                bool Liked = false;
                if (Recipe.Likes.FirstOrDefault(like => like.UserId == UserId) != null)
                {
                    Liked        = true;
                    ViewBag.Like = Recipe.Likes.FirstOrDefault(like => like.UserId == UserId);
                }
                ViewBag.Liked = Liked;
            }
            List <Comment> Comments = dbContext.Comments.Include(comment => comment.Recipe).Include(comment => comment.User).Where(comment => comment.RecipeId == Recipe.RecipeId).ToList();

            Comments.Reverse();
            ViewBag.Comments = Comments;
            ViewBag.User     = User;
            ViewBag.Recipe   = Recipe;
            ViewBag.Title    = Recipe.Title;
            return(View());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> RecipeTitleAsync(string Title)
        {
            bool        found  = false;
            AdminRecipe Recipe = await dbContext.AdminRecipes.FirstOrDefaultAsync(recipe => recipe.Title == Title);

            if (Recipe != null)
            {
                found = true;
            }
            ViewBag.Found   = found;
            ViewBag.Partial = true;
            return(View("RecipeTitlePartial"));
        }
Ejemplo n.º 4
0
        public IActionResult UpdateRecipe(string Title)
        {
            int? UserId = HttpContext.Session.GetInt32("UserId");
            User User   = dbContext.Users.FirstOrDefault(user => user.UserId == UserId);

            if (User != null && User.AdminState == 1)
            {
                AdminRecipe Recipe = dbContext.AdminRecipes.Include(recipe => recipe.User).FirstOrDefault(recipe => recipe.Title == Title);
                ViewBag.Recipe = Recipe;
                ViewBag.Title  = "Updating Admin Recipe";
                return(View());
            }
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 5
0
        public IActionResult UnLikeRecipe(int RecipeId)
        {
            int?        UserId = HttpContext.Session.GetInt32("UserId");
            User        User   = dbContext.Users.Include(user => user.Likes).FirstOrDefault(user => user.UserId == UserId);
            AdminRecipe Recipe = dbContext.AdminRecipes.FirstOrDefault(recipe => recipe.RecipeId == RecipeId);

            if (User != null)
            {
                Like Like = User.Likes.FirstOrDefault(like => like.RecipeId == RecipeId);
                dbContext.Remove(Like);
                dbContext.SaveChanges();
            }
            return(RedirectToAction("Recipe", new { Title = Recipe.Title }));
        }
Ejemplo n.º 6
0
        public IActionResult DeleteCommentOnRecipe(int RecipeId, int CommentId)
        {
            int?        UserId  = HttpContext.Session.GetInt32("UserId");
            User        User    = dbContext.Users.FirstOrDefault(user => user.UserId == UserId);
            AdminRecipe Recipe  = dbContext.AdminRecipes.Include(recipe => recipe.Comments).FirstOrDefault(recipe => recipe.RecipeId == RecipeId);
            Comment     Comment = dbContext.Comments.FirstOrDefault(comment => comment.CommentId == CommentId);

            if (UserId != Comment.UserId || User.AdminState == 1)
            {
                dbContext.Remove(Comment);
                dbContext.SaveChanges();
            }
            return(RedirectToAction("Recipe", new { Title = Recipe.Title }));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> PostAdminRecipeAsync(AdminRecipe Recipe)
        {
            int? UserId = HttpContext.Session.GetInt32("UserId");
            User User   = dbContext.Users.FirstOrDefault(user => user.UserId == UserId);

            if (User != null)
            {
                if (User.AdminState == 1)
                {
                    Recipe.UserId = User.UserId;
                    if (dbContext.Recipes.Any(recipe => recipe.Title == Recipe.Title))
                    {
                        ModelState.AddModelError("Title", "A recipe already has that title");
                        return(View("NewAdminRecipe"));
                    }
                    if (Recipe.UploadPicture != null)
                    {
                        var container = Recipe.GetBlobContainer(configuration.GetSection("PictureBlobInfo:AzureStorageConnectionString").Value, "foodforumpictures");
                        var Content   = ContentDispositionHeaderValue.Parse(Recipe.UploadPicture.ContentDisposition);
                        var FileName  = Content.FileName.ToString().Trim('"');
                        var blockBlob = container.GetBlockBlobReference(FileName);
                        BlobRequestOptions options = new BlobRequestOptions();
                        options.SingleBlobUploadThresholdInBytes = 16777216;
                        await blockBlob.UploadFromStreamAsync(Recipe.UploadPicture.OpenReadStream());

                        Recipe.PictureURL = blockBlob.Uri.AbsoluteUri;
                    }
                    string check = RegexCheck(Recipe, RegEx);
                    if (check != null)
                    {
                        ModelState.AddModelError(check, "Please use only letters, numbers, periods, commas, hyphens, or exclamation points");
                    }
                    if (ModelState.IsValid)
                    {
                        if (!dbContext.Recipes.Any(recipe => recipe.PictureURL == Recipe.PictureURL) || Recipe.UploadPicture == null)
                        {
                            dbContext.Add(Recipe);
                            dbContext.SaveChanges();
                            return(RedirectToAction("Index", "Home"));
                        }
                        ModelState.AddModelError("UploadPicture", "A recipe already has a picture with that file name, please rename it and try again");
                    }
                    return(View("NewAdminRecipe"));
                }
            }
            return(RedirectToAction("UserRecipes", "Home"));
        }
Ejemplo n.º 8
0
        public IActionResult DeleteRecipe(int RecipeId)
        {
            int?        UserId = HttpContext.Session.GetInt32("UserId");
            User        User   = dbContext.Users.FirstOrDefault(user => user.UserId == UserId);
            AdminRecipe Recipe = dbContext.AdminRecipes.FirstOrDefault(recipe => recipe.RecipeId == RecipeId);

            if (Recipe != null)
            {
                if (User != null && User.AdminState == 1 || User.UserId == Recipe.UserId)
                {
                    dbContext.Remove(Recipe);
                    dbContext.SaveChanges();
                    return(RedirectToAction("Index", "Home"));
                }
                return(RedirectToAction("Recipe", "Recipes", new { Title = Recipe.Title }));
            }
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 9
0
        public IActionResult UnAdminifyRecipe(int RecipeId)
        {
            int? AdminId = HttpContext.Session.GetInt32("UserId");
            User Admin   = dbContext.Users.FirstOrDefault(user => user.UserId == AdminId);

            if (Admin != null && Admin.AdminState == 1)
            {
                AdminRecipe AdminRecipe = dbContext.AdminRecipes.Include(recipe => recipe.Likes).Include(recipe => recipe.Comments).Include(recipe => recipe.Ratings).FirstOrDefault(recipe => recipe.RecipeId == RecipeId);
                UserRecipe  Recipe      = new UserRecipe();
                Recipe.Title              = AdminRecipe.Title;
                Recipe.Content            = AdminRecipe.Content;
                Recipe.PictureURL         = AdminRecipe.PictureURL;
                Recipe.IngredientOne      = AdminRecipe.IngredientOne;
                Recipe.IngredientTwo      = AdminRecipe.IngredientOne;
                Recipe.IngredientThree    = AdminRecipe.IngredientThree;
                Recipe.IngredientFour     = AdminRecipe.IngredientFour;
                Recipe.IngredientFive     = AdminRecipe.IngredientFive;
                Recipe.IngredientSix      = AdminRecipe.IngredientOne;
                Recipe.IngredientSix      = AdminRecipe.IngredientSeven;
                Recipe.IngredientEight    = AdminRecipe.IngredientEight;
                Recipe.IngredientNine     = AdminRecipe.IngredientNine;
                Recipe.IngredientTen      = AdminRecipe.IngredientTen;
                Recipe.IngredientEleven   = AdminRecipe.IngredientEleven;
                Recipe.IngredientTwelve   = AdminRecipe.IngredientTwelve;
                Recipe.IngredientThirteen = AdminRecipe.IngredientThirteen;
                Recipe.IngredientFourteen = AdminRecipe.IngredientFourteen;
                Recipe.IngredientFifteen  = AdminRecipe.IngredientFifteen;
                AdminRecipe.RecipeId      = 99999;
                Recipe.RecipeId           = RecipeId;
                Recipe.Ingredients        = AdminRecipe.Ingredients;
                Recipe.UserId             = AdminRecipe.UserId;
                Recipe.User      = AdminRecipe.User;
                Recipe.Likes     = AdminRecipe.Likes;
                Recipe.Comments  = AdminRecipe.Comments;
                Recipe.Ratings   = AdminRecipe.Ratings;
                Recipe.CreatedAt = AdminRecipe.CreatedAt;
                dbContext.Remove(AdminRecipe);
                dbContext.Add(Recipe);
                dbContext.SaveChanges();
            }
            return(RedirectToAction("UserSubmissions", "Home"));
        }
Ejemplo n.º 10
0
        public IActionResult UpdateRateRecipe(Rating Rating, int RecipeId)
        {
            int?        UserId     = HttpContext.Session.GetInt32("UserId");
            User        User       = dbContext.Users.Include(user => user.Ratings).FirstOrDefault(user => user.UserId == UserId);
            AdminRecipe Recipe     = dbContext.AdminRecipes.FirstOrDefault(recipe => recipe.RecipeId == RecipeId);
            Rating      UserRating = User.Ratings.FirstOrDefault(rating => rating.RecipeId == RecipeId);

            if (UserRating != null)
            {
                if (UserId == UserRating.UserId)
                {
                    if (Rating.Rate > 0 && Rating.Rate < 6)
                    {
                        UserRating.Rate = Rating.Rate;
                        dbContext.SaveChanges();
                    }
                }
            }
            return(RedirectToAction("Recipe", new { Title = Recipe.Title }));
        }
Ejemplo n.º 11
0
 public string RegexCheck(AdminRecipe Recipe, string pattern)
 {
     if (!Regex.IsMatch(Recipe.Title, pattern))
     {
         return("Title");
     }
     if (Recipe.Note != null && !Regex.IsMatch(Recipe.Note, pattern))
     {
         return("Note");
     }
     if (!Regex.IsMatch(Recipe.IngredientOne, pattern))
     {
         return("IngredientOne");
     }
     if (!Regex.IsMatch(Recipe.IngredientTwo, pattern))
     {
         return("IngredientTwo");
     }
     if (!Regex.IsMatch(Recipe.IngredientThree, pattern))
     {
         return("IngredientThree");
     }
     if (Recipe.IngredientFour != null && !Regex.IsMatch(Recipe.IngredientFour, pattern))
     {
         return("IngredientFour");
     }
     if (Recipe.IngredientFive != null && !Regex.IsMatch(Recipe.IngredientFive, pattern))
     {
         return("IngredientFive");
     }
     if (Recipe.IngredientSix != null && !Regex.IsMatch(Recipe.IngredientSix, pattern))
     {
         return("IngredientSix");
     }
     if (Recipe.IngredientSeven != null && !Regex.IsMatch(Recipe.IngredientSeven, pattern))
     {
         return("IngredientSeven");
     }
     if (Recipe.IngredientEight != null && !Regex.IsMatch(Recipe.IngredientEight, pattern))
     {
         return("IngredientEight");
     }
     if (Recipe.IngredientNine != null && !Regex.IsMatch(Recipe.IngredientNine, pattern))
     {
         return("IngredientNine");
     }
     if (Recipe.IngredientTen != null && !Regex.IsMatch(Recipe.IngredientTen, pattern))
     {
         return("IngredientTen");
     }
     if (Recipe.IngredientEleven != null && !Regex.IsMatch(Recipe.IngredientEleven, pattern))
     {
         return("IngredientEleven");
     }
     if (Recipe.IngredientTwelve != null && !Regex.IsMatch(Recipe.IngredientTwelve, pattern))
     {
         return("IngredientTwelve");
     }
     if (Recipe.IngredientThirteen != null && !Regex.IsMatch(Recipe.IngredientThirteen, pattern))
     {
         return("IngredientThirteen");
     }
     if (Recipe.IngredientFourteen != null && !Regex.IsMatch(Recipe.IngredientFourteen, pattern))
     {
         return("IngredientFourteen");
     }
     if (Recipe.IngredientFifteen != null && !Regex.IsMatch(Recipe.IngredientFifteen, pattern))
     {
         return("IngredientFifteen");
     }
     if (!Regex.IsMatch(Recipe.Content, pattern))
     {
         return("Content");
     }
     if (Recipe.PictureURL != null && !Regex.IsMatch(Recipe.PictureURL, pattern))
     {
         return("PictureUrl");
     }
     return(null);
 }
Ejemplo n.º 12
0
        public async Task <IActionResult> UpdatingRecipeAsync(int RecipeId, UpdateRecipe UpdateRecipe, string Content)
        {
            int? UserId = HttpContext.Session.GetInt32("UserId");
            User User   = dbContext.Users.FirstOrDefault(user => user.UserId == UserId);

            if (User != null)
            {
                AdminRecipe Recipe = dbContext.AdminRecipes.Include(recipe => recipe.User).FirstOrDefault(recipe => recipe.RecipeId == RecipeId);
                if (User.AdminState == 1)
                {
                    if (Request.Form["deletePicture"] == "on")
                    {
                        UpdateRecipe.UploadPicture = null;
                        Recipe.PictureURL          = null;
                    }
                    if (UpdateRecipe.UploadPicture != null)
                    {
                        var container              = Recipe.GetBlobContainer(configuration.GetSection("PictureBlobInfo:AzureStorageConnectionString").Value, "foodforumpictures");
                        var PictureContent         = ContentDispositionHeaderValue.Parse(UpdateRecipe.UploadPicture.ContentDisposition);
                        var FileName               = PictureContent.FileName.ToString().Trim('"');
                        var blockBlob              = container.GetBlockBlobReference(FileName);
                        BlobRequestOptions options = new BlobRequestOptions();
                        options.SingleBlobUploadThresholdInBytes = 16777216;
                        await blockBlob.UploadFromStreamAsync(UpdateRecipe.UploadPicture.OpenReadStream());

                        UpdateRecipe.PictureURL = blockBlob.Uri.AbsoluteUri;
                        if (UpdateRecipe.PictureURL != null && !dbContext.Recipes.Any(recipe => recipe.PictureURL == UpdateRecipe.PictureURL))
                        {
                            Recipe.PictureURL = UpdateRecipe.PictureURL;
                        }
                    }
                    Recipe.Title = UpdateRecipe.Title;
                    Recipe TitleCheck = dbContext.Recipes.FirstOrDefault(recipe => recipe.Title == Recipe.Title);
                    if (TitleCheck != null && TitleCheck.RecipeId != Recipe.RecipeId)
                    {
                        ViewBag.Recipe = Recipe;
                        ModelState.AddModelError("Title", "A recipe already has that title");
                        return(View("UpdateRecipe"));
                    }
                    Recipe.Note                 = UpdateRecipe.Note;
                    Recipe.Content              = Content;
                    Recipe.IngredientOne        = UpdateRecipe.IngredientOne;
                    Recipe.IngredientTwo        = UpdateRecipe.IngredientTwo;
                    Recipe.IngredientThree      = UpdateRecipe.IngredientThree;
                    Recipe.IngredientFour       = UpdateRecipe.IngredientFour;
                    Recipe.IngredientFive       = UpdateRecipe.IngredientFive;
                    Recipe.IngredientSix        = UpdateRecipe.IngredientSix;
                    Recipe.IngredientSeven      = UpdateRecipe.IngredientSeven;
                    Recipe.IngredientEight      = UpdateRecipe.IngredientEight;
                    Recipe.IngredientNine       = UpdateRecipe.IngredientNine;
                    Recipe.IngredientTen        = UpdateRecipe.IngredientTen;
                    Recipe.IngredientEleven     = UpdateRecipe.IngredientEleven;
                    Recipe.IngredientTwelve     = UpdateRecipe.IngredientTwelve;
                    Recipe.IngredientThirteen   = UpdateRecipe.IngredientThirteen;
                    Recipe.IngredientFourteen   = UpdateRecipe.IngredientFourteen;
                    Recipe.IngredientFifteen    = UpdateRecipe.IngredientFifteen;
                    Recipe.UpdatedAt            = UpdateRecipe.CreatedAt;
                    Recipe.User.ConfirmPassword = null;
                    TryValidateModel(Recipe);
                    ModelState.Remove("User.ConfirmPassword");
                    if (UpdateRecipe.PictureURL != null && dbContext.Recipes.Any(recipe => recipe.PictureURL == Recipe.PictureURL))
                    {
                        ModelState.AddModelError("UploadPicture", "A recipe already has a picture with that file name, please rename it and try again");
                    }
                    string check = RegexCheck(Recipe, RegEx);
                    if (check != null)
                    {
                        ModelState.AddModelError(check, "Please use only letters, numbers, periods, commas, hyphens, or exclamation points");
                    }
                    if (ModelState.IsValid)
                    {
                        dbContext.SaveChanges();
                        return(RedirectToAction("Index", "Home"));
                    }
                    ViewBag.Recipe = Recipe;
                    return(View("UpdateRecipe"));
                }
                return(RedirectToAction("Recipe", "Recipes", new { Title = Recipe.Title }));
            }
            return(RedirectToAction("Index", "Home"));
        }