public List <PreparationSteps> GetPreparationStepsForRecipe(int recipeId) { List <PreparationSteps> preparationSteps = new List <PreparationSteps>(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(SqlRecipeIngredientQuery, conn); cmd.Parameters.AddWithValue("@recipe_id", recipeId); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { PreparationSteps step = new PreparationSteps(); step.RecipeId = Convert.ToInt32(reader["recipe_id"]); step.StepId = Convert.ToInt32(reader["step_id"]); step.Steps = Convert.ToString(reader["steps"]); preparationSteps.Add(step); } } return(preparationSteps); } catch (SqlException ex) { throw; } }
public void SavePreparationSteps(int recipeId, List <string> prepSteps, PreparationSteps prepStep) { try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); foreach (var step in prepSteps) { prepStep.StepId = conn.QueryFirst <int>("INSERT INTO preparation_steps VALUES (@recipe_idValue, @stepValue); SELECT CAST(SCOPE_IDENTITY() as int);", new { recipe_idValue = recipeId, stepValue = step }); } } } catch (SqlException ex) { throw; } }
public ActionResult CreateRecipe(RecipeViewModel model, HttpPostedFileBase recipeImage) { if (model != null && model.QuantityMeasurementIngredient != null && model.PrepSteps != null) { if (recipeImage != null && recipeImage.ContentLength > 0) { var fileName = Path.GetFileName(recipeImage.FileName); var fullPath = Path.Combine(Server.MapPath("~/Recipe-Images"), fileName); recipeImage.SaveAs(fullPath); model.RecipeImageName = fileName; } else { var fileName = "stock.jpg"; var fullPath = Path.Combine(Server.MapPath("~/Recipe-Images"), fileName); //recipeImage.SaveAs(fullPath); model.RecipeImageName = fileName; } List <RecipeIngredient> recipeIngredients = new List <RecipeIngredient>(); PreparationSteps pS = new PreparationSteps(); model.PrepSteps[0].Replace('\n', ' '); List <string> prepSteps = model.PrepSteps[0].Split('\r').ToList(); foreach (var item in model.QuantityMeasurementIngredient) { if (item != "") { RecipeIngredient recipeIngredient = new RecipeIngredient() { Quantity = "", Measurement = "", Ingredient_Name = item, }; recipeIngredients.Add(recipeIngredient); } } string ingredient1 = recipeIngredients[0].Ingredient_Name; Recipe r = new Recipe(); r.Name = model.RecipeName; r.Description = model.RecipeDescription; r.ImageName = model.RecipeImageName; r.CookTimeInMinutes = model.RecipeCookTimeInMinutes; var recipeType = ""; int counter = 0; foreach (var item in model.RecipeType) { if (model.RecipeType.Count == counter) { recipeType += item; } else { recipeType += item + ", "; counter++; } } r.RecipeType = recipeType; if (userDAL.GetUser((string)Session[SessionKeys.EmailAddress]) != null) { r.UserId = (int)Session[SessionKeys.UserId]; recipeDAL.SaveRecipe(r); recipeIngredientDAL.SaveRecipeIngredients(recipeIngredients, r.RecipeId); preparationStepsDAL.SavePreparationSteps(r.RecipeId, prepSteps, pS); //might need to get RECIPEID from DAL TempData["action"] = "save"; return(RedirectToAction("Detail", new { recipeId = r.RecipeId })); } } return(RedirectToAction("CreateRecipe", "Recipe")); }