Пример #1
0
        public void UpdateRecipe(Recipe item)
        {
            // update record in db
            bool ok = DBHelper.Update(item);

            if (!ok)
            {
                throw new InvalidOperationException();
            }

            // remove item from old group if category is changed
            bool isRemoved = false;

            foreach (var group in GroupedRecipes)
            {
                if (group.Contains(item) && (string)group.Key != item.Category)
                {
                    group.Remove(item);
                    isRemoved = true;
                }
            }

            // add item to new group if needed
            if (isRemoved)
            {
                GroupedRecipes.GetOrAddGroup(item.Category).Add(item);

                // recount items in categories
                RecipeCategories.CountItemsFrom(GroupedRecipes);
            }
        }
Пример #2
0
        public void DeleteRecipe(Recipe item)
        {
            // delete record from db
            bool ok = DBHelper.Delete(item);

            if (!ok)
            {
                throw new InvalidOperationException();
            }

            // remove item from collection
            Recipes.Remove(item);

            // remove item from grouped collection
            GroupedRecipes.GetOrAddGroup(item.Category).Remove(item);

            // recount items in categories
            RecipeCategories.CountItemsFrom(GroupedRecipes);
        }
Пример #3
0
        public void AddRecipe(Recipe item)
        {
            // add new record to database
            bool ok = DBHelper.Insert(item);

            if (!ok)
            {
                throw new InvalidOperationException();
            }

            // add item to collection
            Recipes.Add(item);

            // add item to grouped collection
            GroupedRecipes.GetOrAddGroup(item.Category).Add(item);

            // recount items in categories
            RecipeCategories.CountItemsFrom(GroupedRecipes);
        }
Пример #4
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Logger.Write("btnSave_Click -> Start", Logger.Level.Info);
        try
        {
            Page.Validate("general");
            if (!Page.IsValid)
            {
                return;
            }

            Recipe recipe;
            bool   isNewRecipe = false;

            if (RecipeId == 0)
            {
                recipe      = new Recipe();
                isNewRecipe = true;
            }
            else
            {
                recipe = BusinessFacade.Instance.GetRecipe(RecipeId);
            }

            if (recipe == null)
            {
                return; //TODO Exception (record is not exist)
            }

            Logger.Write("btnSave_Click -> Set recipe data", Logger.Level.Info);

            if (fuRecipeImage.HasFile && fuRecipeImage.PostedFile != null && ImageHelper.IsImage(fuRecipeImage.PostedFile.FileName))
            {
                Bitmap bitmap = ImageHelper.ResizeImage(new Bitmap(this.fuRecipeImage.PostedFile.InputStream, false), 300, 231);
                RecipePicture = ImageHelper.GetBitmapBytes(bitmap);
            }

            recipe.RecipeName        = txtRecipeName.Text;
            recipe.IsPublic          = chkPublic.Checked;
            recipe.Description       = txtRecipeDesc.Text;
            recipe.Tags              = txtTags.Text;
            recipe.PreparationMethod = txtPreparationMethod.Text;
            recipe.Remarks           = txtRemarks.Text;
            recipe.Tools             = txtTools.Text;

            if (ddlDifficulty.SelectedValue != "0")
            {
                recipe.DifficultyLevel = int.Parse(ddlDifficulty.SelectedValue);
            }
            else
            {
                recipe.DifficultyLevel = null;
            }

            if (!string.IsNullOrEmpty(txtPrepTime.Text))
            {
                recipe.PreperationTime = GetTimeInMinutes(txtPrepTime.Text, int.Parse(ddlPrepTimeUnits.SelectedValue));
            }
            else
            {
                recipe.PreperationTime = null;
            }

            if (!string.IsNullOrEmpty(this.txtCookTime.Text))
            {
                recipe.CookingTime = this.GetTimeInMinutes(this.txtCookTime.Text, int.Parse(this.ddlCookTimeUnits.SelectedValue));
            }
            else
            {
                recipe.CookingTime = null;
            }

            if (!string.IsNullOrEmpty(this.txtServings.Text))
            {
                recipe.Servings = int.Parse(this.txtServings.Text);
            }
            else
            {
                recipe.Servings = 1;
            }

            recipe.VideoLink = this.txtEmbeddedLink.Text;
            //recipe.Picture = this.RecipePicture;

            if (recipe.UserId == 0)
            {
                recipe.UserId = ((BasePage)Page).UserId;
            }

            int returnedRecipeId;
            if (BusinessFacade.Instance.SaveRecipe(recipe, Ingridiants.ListOfIngediants, RecipeCategories.ToList(), isNewRecipe, out returnedRecipeId))
            {
                if (RefreshData != null)
                {
                    RefreshData.Invoke();
                }
            }

            Logger.Write("btnSave_Click -> End and redirect", Logger.Level.Info);

            RecipeCategories = null;

            if (returnedRecipeId != 0)
            {
                Response.Redirect(string.Format("~/RecipeDetails.aspx?RecipeId={0}", returnedRecipeId));
            }
        }
        catch (Exception ex)
        {
            Logger.Write("Save recipe failed", ex, Logger.Level.Error);
        }
    }