Ejemplo n.º 1
0
        private void SaveFood(object sender, RoutedEventArgs e)
        {
            if (FoodType.SelectedItem == null)
            {
                if (MessageBox.Show(@"Food type is not selected!", "Do you want to choose it?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    return;
                }
            }
            if (!AddedImage)
            {
                if (MessageBox.Show("Image is not selected!", "Do you want to choose it?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    return;
                }
            }
            ShopContext shopContext = new ShopContext();
            Food        food        = new Food();

            if (AddedImage)
            {
                food.Image = Image;
            }
            if (FoodType.SelectedItem != null)
            {
                string type   = FoodType.SelectedItem.ToString();
                int    typeId = shopContext.FoodTypeDictionary.Where(t => t.Name == type).Select(t => t.FoodTypeID).First();
                food.FoodTypeID = typeId;
            }
            shopContext.Food.Add(food);

            List <FoodDictionary> dictionaryList = new List <FoodDictionary>();

            for (int i = 0; i < FoodNames.Items.Count; i++)
            {
                ContentPresenter c        = (ContentPresenter)FoodNames.ItemContainerGenerator.ContainerFromItem(FoodNames.Items[i]);
                ComboBox         comboBox = c.ContentTemplate.FindName("NewFoodLanguage", c) as ComboBox;
                TextBox          text     = c.ContentTemplate.FindName("NewFoodName", c) as TextBox;

                FoodDictionary dictionary = new FoodDictionary();
                if (text.Text == "")
                {
                    continue;
                }
                else
                {
                    dictionary.Name = text.Text;
                }

                if (comboBox.SelectedItem == null)
                {
                    continue;
                }
                else
                {
                    string lang   = comboBox.SelectedItem.ToString();
                    int    langId = shopContext.Language.Where(l => l.Name == lang).Select(l => l.LanguageID).First();
                    dictionary.LanguageID = langId;
                }
                dictionaryList.Add(dictionary);
            }

            List <FoodDictionary> duplicatesLanguages = dictionaryList.GroupBy(d => d.LanguageID).SelectMany(d => d.Skip(1)).ToList();

            if (duplicatesLanguages.Count != 0)
            {
                MessageBox.Show("Duplicated languages!");
                return;
            }

            foreach (var item in dictionaryList)
            {
                if (shopContext.FoodDictionary.Where(f => f.Name == item.Name && f.LanguageID == item.LanguageID).Count() > 0)
                {
                    MessageBox.Show("Food is existed!");
                    return;
                }
            }

            if (dictionaryList.Count > 0)
            {
                shopContext.SaveChanges();
            }
            else
            {
                MessageBox.Show("Some fields is empty!");
                return;
            }

            List <int> foodIdList = shopContext.Food.Where(f => f.FoodTypeID == food.FoodTypeID && f.Image == food.Image).Select(f => f.FoodID).ToList();

            foreach (var id in foodIdList)
            {
                int idCount = shopContext.FoodDictionary.Where(fd => fd.FoodID == id).Count();
                if (idCount == 0)
                {
                    foreach (var item in dictionaryList)
                    {
                        item.FoodID = id;
                        shopContext.FoodDictionary.Add(item);
                    }
                    break;
                }
            }
            shopContext.SaveChanges();
            FoodType.SelectedItem = null;
            FoodType.Text         = "Type";
            FoodNames.Items.Clear();
            FoodNames.Items.Add(new NewLanguage {
                Languages = Languages
            });
        }
Ejemplo n.º 2
0
        private void SaveFood(object sender, RoutedEventArgs e)
        {
            if (FoodList.SelectedItem != null)
            {
                int         foodId      = (FoodList.SelectedItem as FoodPropertie).FoodId;
                ShopContext shopContext = new ShopContext();

                string lang;
                int    langId;
                if (FoodLanguageBox.SelectedItem != null)
                {
                    lang = FoodLanguageBox.SelectedItem.ToString();
                }
                else
                {
                    lang = MainLanguage;
                }
                langId = shopContext.Language.Where(l => l.Name == lang).Select(l => l.LanguageID).First();

                int?   foodTypeId   = shopContext.Food.Where(f => f.FoodID == foodId).Select(f => f.FoodTypeID).First();
                string foodTypeName = string.Empty;
                if (foodTypeId.HasValue)
                {
                    foodTypeName = shopContext.FoodTypeDictionary
                                   .Where(t => t.FoodTypeID == foodTypeId && t.LanguageID == langId)
                                   .Select(t => t.Name).FirstOrDefault();
                }

                List <FoodDictionary> DBNames = shopContext.FoodDictionary
                                                .Where(d => d.FoodID == foodId).ToList();

                Food   Food       = shopContext.Food.Where(f => f.FoodID == foodId).First();
                string DBFoodType = FoodTypeChangeBox.Text;
                if (foodTypeName != DBFoodType)
                {
                    foodTypeId      = shopContext.FoodTypeDictionary.Where(t => t.Name == DBFoodType).Select(t => t.FoodTypeID).First();
                    Food.FoodTypeID = foodTypeId;
                }

                List <FoodDictionary> dictionaryList = new List <FoodDictionary>();
                for (int i = 0; i < FoodNames.Items.Count; i++)
                {
                    ContentPresenter c        = (ContentPresenter)FoodNames.ItemContainerGenerator.ContainerFromItem(FoodNames.Items[i]);
                    ComboBox         comboBox = c.ContentTemplate.FindName("FoodLanguage", c) as ComboBox;
                    TextBox          text     = c.ContentTemplate.FindName("FoodName", c) as TextBox;

                    FoodDictionary dictionary = new FoodDictionary();
                    if (text.Text == "")
                    {
                        continue;
                    }
                    else
                    {
                        dictionary.Name = text.Text;
                    }

                    if (comboBox.SelectedItem == null)
                    {
                        continue;
                    }
                    else
                    {
                        string userLang   = comboBox.SelectedItem.ToString();
                        int    userLangId = shopContext.Language.Where(l => l.Name == userLang).Select(l => l.LanguageID).First();
                        dictionary.LanguageID = userLangId;
                        dictionary.FoodID     = foodId;
                    }
                    dictionaryList.Add(dictionary);
                }

                List <FoodDictionary> duplicates = dictionaryList.GroupBy(d => d.LanguageID).SelectMany(d => d.Skip(1)).ToList();
                if (duplicates.Count != 0)
                {
                    MessageBox.Show("Duplicated languages!");
                    return;
                }

                if (dictionaryList.Count > 0)
                {
                    shopContext.FoodDictionary.RemoveRange(DBNames);
                    shopContext.FoodDictionary.AddRange(dictionaryList);
                    shopContext.SaveChanges();
                }
                else
                {
                    MessageBox.Show("Some fields is empty!");
                    return;
                }
                RefreshList();
            }
        }