Exemple #1
0
        public ViewRecipePanel(Recipe recipe, User user)
        {
            this.user = user;

            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.recipe        = recipe;
            this.lblTitle.Text = recipe.Title;

            // This will get the current WORKING directory (i.e. \bin\Debug)
            string workingDirectory = AppDomain.CurrentDomain.BaseDirectory;

            // This will get the current PROJECT directory
            string projectDirectory = Directory.GetParent(workingDirectory).Parent.Parent.FullName;

            String path = projectDirectory + @"\resources\" + recipe.Image;

            this.imgRecipe.ImageLocation = path;

            // get all ingredients and render on view
            IngredientDAO dao = new IngredientDAO();

            this.recipe.Ingredients = dao.GetAllIngredientsByRecipe(recipe);
            lblDetails.Text         = "<h3>Ingredients</h3>" + RenderIngredients();

            // get all steps and render on view
            this.recipe.RecipeSteps = RecipeStepDAO.GetRecipeStepsByRecipe(recipe);
            lblDetails.Text        += "<h3>Steps</h3><div style='width: 780px;'>" + RenderSteps() + "</div>";

            // add update button
            if (!this.User.Email.Equals(recipe.Email))
            {
                controlPanel.Hide();
            }
        }
Exemple #2
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();
                }
            }
        }