Esempio n. 1
0
        private void btnDishesDelete_Click(object sender, EventArgs e)
        {
            if (lbActWithDishes.SelectedItem == null)
            {
                return;
            }

            using (var fc = new FitLifeDataContent())
            {
                var dish = (DishInfo)lbActWithDishes.SelectedItem;
                if (!(DialogResult.Yes == MessageBox.Show($"Are u sure about deleting   <{dish.Name}>   element", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)))
                {
                    return;
                }

                fc.Dishes.Remove(fc.Dishes.Find(dish.Id));
                fc.SaveChanges();
                UpdateMyData();
            }

            //if (lbActWithDishes.Items.Count == 0)
            //{
            //    lbDishesProducts.Items?.Clear();
            //    lblDishesDishCalories.Text = string.Empty;
            //    lblDishesDishName.Text = string.Empty;
            //}
        }
Esempio n. 2
0
        private void btnProductsDelete_Click(object sender, EventArgs e)
        {
            if (lbActWithProducts.SelectedItem == null)
            {
                return;
            }

            using (var fc = new FitLifeDataContent())
            {
                var prod = (Product)lbActWithProducts.SelectedItem;
                if (!(DialogResult.Yes == MessageBox.Show($"Are u sure about deleting   <{prod.Name}>   element\nAll dishes that include this product will be deleted", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)))
                {
                    return;
                }

                DishKiller(fc, prod.Id);

                fc.Products.Remove(fc.Products.Find(prod.Id));
                fc.SaveChanges();
                UpdateMyData();
            }

            //if (lbActWithProducts.Items.Count == 0)
            //{
            //    lblProductsProductCalories.Text = string.Empty;
            //    lblProductsProductName.Text = string.Empty;
            //}
        }
Esempio n. 3
0
 private void DishKiller(FitLifeDataContent fc, int id)
 {
     foreach (var item in _dishes)
     {
         foreach (var prod in item.Products)
         {
             if (prod.Id == id)
             {
                 fc.Dishes.Remove(fc.Dishes.Find(item.Id));
             }
         }
     }
 }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbName.Text) || string.IsNullOrWhiteSpace(tbCalories.Text))
            {
                MessageBox.Show("Fields can't be empty!");
                return;
            }


            using (var fc = new FitLifeDataContent())
            {
                if (_product == null)
                {
                    _product = new Product()
                    {
                        Name = tbName.Text, Calories = int.Parse(tbCalories.Text)
                    };
                    fc.Products.Add(_product);
                }
                else
                {
                    if (_product.Name != tbName.Text)
                    {
                        _product.Name = tbName.Text;
                        changed       = true;
                    }

                    if (_product.Calories != int.Parse(tbCalories.Text))
                    {
                        _product.Calories = int.Parse(tbCalories.Text);
                        changed           = true;
                    }

                    if (changed)
                    {
                        if (DialogResult.OK == MessageBox.Show("Are you sure?", "Product will be changed!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
                        {
                            fc.Entry(_product).State = System.Data.Entity.EntityState.Modified;
                        }
                        else
                        {
                            this.Close();
                        }
                    }
                }

                fc.SaveChanges();
                this.Close();
            }
        }
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbName.Text))
            {
                MessageBox.Show("Enter dish Name!");
                return;
            }

            if (lbIngredients.Items.Count == 0)
            {
                MessageBox.Show("Choose ingridients");
                return;
            }

            string tmpIds = string.Empty;

            foreach (var item in lbIngredients.Items)
            {
                tmpIds += $"{((Product)item).Id} ";  
            }

            tmpIds = tmpIds.Substring(0, tmpIds.Length - 1);

            var dish = new Dish() { Name = tbName.Text, Calories = int.Parse(tbCalories.Text), ProductsIds = tmpIds };

            using (var fc = new FitLifeDataContent())
            {
                if (_dishInfo != null)
                {
                    dish.Id = _dishInfo.Id;

                    if (DialogResult.OK == MessageBox.Show("Are you sure?", "Dish will be changed!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
                        fc.Entry(dish).State = System.Data.Entity.EntityState.Modified;
                    else
                        this.Close();         
                }
                else
                {
                    fc.Dishes.Add(dish);
                }

                fc.SaveChanges();
                this.Close();
            }

        }
Esempio n. 6
0
        private void UpdateMyData()
        {
            using (var fc = new FitLifeDataContent())
            {
                _products.Clear();
                _dishes.Clear();
                foreach (var item in fc.Products)
                {
                    _products.Add(item);
                }

                foreach (var item in fc.Dishes)
                {
                    _dishes.Add(new DishInfo(item));
                }
            }

            lbActWithProducts_SelectedIndexChanged(this, new EventArgs());
            lbActWithDishes_SelectedIndexChanged(this, new EventArgs());
        }