public async Task <IActionResult> PostUserRecipeAsync(UserRecipe 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("NewUserRecipe")); } 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.PictureURL == null) { dbContext.Add(Recipe); dbContext.SaveChanges(); return(RedirectToAction("UserRecipes", "Home")); } ModelState.AddModelError("UploadPicture", "A recipe already has a picture with that file name, please rename it and try again"); } return(View("NewUserRecipe")); } } return(RedirectToAction("UserRecipes", "Home")); }
public async Task <IActionResult> UpdatingUserRecipeAsync(int RecipeId, UpdateUserRecipe UpdateRecipe, string Content) { int? UserId = HttpContext.Session.GetInt32("UserId"); User User = dbContext.Users.FirstOrDefault(user => user.UserId == UserId); if (User != null) { UserRecipe Recipe = dbContext.UserRecipes.Include(recipe => recipe.User).FirstOrDefault(recipe => recipe.RecipeId == RecipeId); if (User.AdminState == 1 || User.UserId == Recipe.UserId) { 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.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("UserRecipes", "Home")); } ViewBag.Recipe = Recipe; return(View("UpdateRecipe")); } return(RedirectToAction("UserRecipe", "UserRecipes", new { Title = Recipe.Title })); } return(RedirectToAction("UserRecipes", "Home")); }