public ActionResult DeleteRecipe(int id) { var entities = new RecipesMVC.Models.EF_DB.RecipesAppEntities(); if (entities.Recipes.Any(u => u.ID == id)) { var u = new RecipesMVC.Models.EF_DB.Recipe { ID = id }; entities.Recipes.Attach(u); entities.Recipes.Remove(u); entities.SaveChanges(); entities.Dispose(); return(Json(new { Message = "SUCCESS" }, JsonRequestBehavior.AllowGet)); } entities.Dispose(); return(Json(new { Message = "ERROR", Error = "RecipeNotFound" }, JsonRequestBehavior.AllowGet)); }
public ActionResult Edit(RecipesMVC.Models.EF_DB.Recipe edited) { if (edited.Title != null) { if (Regex.IsMatch(edited.Title, @"^[A-Za-z\u0590-\u05fe.\-_, ]+$")) { string RouteURLbyTitle = edited.Title.Replace(" ", String.Empty).Replace("_", String.Empty).Replace(".", String.Empty).Replace("-", String.Empty); if (edited.publicStatus.HasValue) { if (edited.Instructions != null && edited.Ingredients != null) { if (edited.Ingredients.Split(';').Length > 2 && edited.Instructions.Split(';').Length > 2) { if (edited.RecipeYield.HasValue) { if (edited.TotalTime.HasValue) { RecipesMVC.Models.EF_DB.RecipesAppEntities entities = new Models.EF_DB.RecipesAppEntities(); if (entities.Recipes.Any(re => re.ID == edited.ID)) { var recipe = (from re in entities.Recipes where re.ID == edited.ID select re).First(); recipe.Title = edited.Title; recipe.Description = edited.Description; recipe.publicStatus = edited.publicStatus; recipe.Ingredients = edited.Ingredients; recipe.Instructions = edited.Instructions; recipe.CategoryID = edited.CategoryID; recipe.RecipeYield = edited.RecipeYield; recipe.TotalTime = edited.TotalTime; if (!(recipe.RouteURL != null)) { recipe.RouteURL = RouteURLbyTitle; } //Next Version: Image Upload. } else { entities.Dispose(); return(Json(new { Message = "ERROR" }, JsonRequestBehavior.AllowGet)); } //TODO: Add flag after entities object dispose() try { entities.SaveChanges(); return(Json(new { Message = "SUCCESS" }, JsonRequestBehavior.AllowGet)); } catch { System.Diagnostics.Debug.WriteLine("Entity Framework DataBase error.\n at RecipeController," + "Via Edit"); } finally { entities.Dispose(); } } } } } } } } return(Json(new { Message = "ERROR" }, JsonRequestBehavior.AllowGet)); }
private string GenerateRecipeSchema(RecipesMVC.Models.EF_DB.Recipe recipe) { string ToReturn = "{"; ToReturn += "\"@context\": \"http://schema.org\","; ToReturn += "\"@type\": \"Recipe\","; ToReturn += "\"name\": \"" + recipe.Title + "\","; ToReturn += "\"image\": [\"" + "CHANGE TO IMAGE UPLOAD LINK!!!" + "\"],"; ToReturn += "\"author\": {\"@type\": \"Person\",\"name\": \"" + recipe.User.UserName + "\"},"; ToReturn += "\"datePublished\": \"" + recipe.UploadedAt + "\","; ToReturn += "\"description\": \"" + recipe.Description + "\","; TimeSpan preptime = recipe.PrepTime.Value; string prepTimeString = "PT"; if (preptime.Hours > 0) { prepTimeString += preptime.Hours.ToString() + "H"; } if (preptime.Minutes > 0) { prepTimeString += preptime.Minutes.ToString() + "M"; } ToReturn += "\"prepTime\": \"" + prepTimeString + "\","; TimeSpan cooktime = recipe.CookTime.Value; string cookTimeString = "PT"; if (cooktime.Hours > 0) { cookTimeString += cooktime.Hours.ToString() + "H"; } if (cooktime.Minutes > 0) { cookTimeString += cooktime.Minutes.ToString() + "M"; } ToReturn += "\"cookTime\": \"" + cookTimeString + "\","; TimeSpan totaltime = recipe.TotalTime.Value; string totalTimeString = "PT"; if (totaltime.Hours > 0) { totalTimeString += totaltime.Hours.ToString() + "H"; } if (totaltime.Minutes > 0) { totalTimeString += totaltime.Minutes.ToString() + "M"; } ToReturn += "\"totalTime\": \"" + totalTimeString + "\","; ToReturn += "\"recipeYield\": \"" + recipe.RecipeYield + " servings\","; ToReturn += "\"recipeCategory\": \"" + recipe.Category + "\","; ToReturn += "\"keywords\": \"" + recipe.KeyWords + "\","; // var SubQuery = (from u in entities.Users where (u.UserName == Credentials.LoginUser && u.Password == Credentials.LoginPwd) select u); float avg = float.Parse(ViewBag.avgRatings); ToReturn += "\"aggregateRating\": { \"@type\": \"AggregateRating\", \"ratingValue\": \"" + avg + "\", \"ratingCount\": \"" + recipe.Ratings.Count + "\" },"; ToReturn += "\"RecipeIngredient\":["; int cntr = 0; foreach (string ingredient in recipe.Ingredients.Split(';')) { if (cntr != 0) { ToReturn += ","; } ToReturn += "\"" + ingredient + "\""; cntr++; } ToReturn += "],"; ToReturn += "\"RecipeInstructions\":["; cntr = 0; foreach (string instruction in recipe.Instructions.Split(';')) { if (cntr != 0) { ToReturn += ","; } ToReturn += "{\"@type\": \"HowToStep\","; ToReturn += "\"text\":\"" + instruction + "\"}"; cntr++; } ToReturn += "]"; return(ToReturn + "}"); }