Exemple #1
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string title       = txtTitle.Text;
            string image       = txtImage.Text;
            string description = txtDescription.Text;

            //validate
            if (string.IsNullOrWhiteSpace(title))
            {
                MessageBox.Show("Maybe you forgot the name (title) of the recipe :v");
            }
            else if (string.IsNullOrWhiteSpace(image))
            {
                MessageBox.Show("Recipe with image will be more attractive :v");
            }
            else if (string.IsNullOrWhiteSpace(description))
            {
                MessageBox.Show("Maybe you forgot describe something about your recipe :v");
            }
            else
            {
                if (recipeIngredients.Count == 0 || recipeSteps.Count == 0)
                {
                    MessageBox.Show("Please add at least 1 ingredient and 1 step.");
                }
                else
                {
                    // data send to db here
                    Recipe recipe = new Recipe(user.Email, title, description, Recipe.DEFAULT_STATUS, DateTime.Now, image);

                    RecipeDAO           recipeDAO           = new RecipeDAO();
                    RecipeStepDAO       recipeStepDAO       = new RecipeStepDAO();
                    RecipeIngredientDAO recipeIngredientDAO = new RecipeIngredientDAO();

                    int recipeID = recipeDAO.AddRecipe(recipe);
                    recipeDAO.UpdateImagePath(recipeID, image);

                    recipeStepDAO.AddRecipeSteps(recipeID, recipeSteps);
                    recipeIngredientDAO.AddRecipeIngredients(recipeID, recipeIngredients);

                    //save image
                    String path = projectDirectory + @"\resources\img\" + recipeID;
                    if (Directory.Exists(path) == false) //create folder with name is recipeID if it is not exist
                    {
                        Directory.CreateDirectory(path);
                    }
                    try
                    {
                        foreach (string imageName in imagePaths.Keys)
                        {
                            // if step image is null then let it be
                            if (string.IsNullOrWhiteSpace(imagePaths[imageName]))
                            {
                                continue;
                            }

                            string filename = Path.GetFileName(imagePaths[imageName]);
                            string destFile = Path.Combine(path, filename);
                            File.Copy(imagePaths[imageName], destFile, true);
                        }
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show("Unable to save file " + exp.Message);
                    }
                    MessageBox.Show("Add new recipe successful!");
                    ClearAll();
                }
            }
        }