Ejemplo n.º 1
0
        private void SaveType(object sender, RoutedEventArgs e)
        {
            if (TypeList.SelectedItem != null)
            {
                int         typeId                = (TypeList.SelectedItem as FoodPropertie).FoodId;
                ShopContext shopContext           = new ShopContext();
                List <FoodTypeDictionary> DBNames = shopContext.FoodTypeDictionary
                                                    .Where(d => d.FoodTypeID == typeId).ToList();

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

                    FoodTypeDictionary dictionary = new FoodTypeDictionary();
                    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.FoodTypeID = typeId;
                    }
                    dictionaryList.Add(dictionary);
                }

                List <FoodTypeDictionary> 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.FoodTypeDictionary.RemoveRange(DBNames);
                    shopContext.FoodTypeDictionary.AddRange(dictionaryList);
                    shopContext.SaveChanges();
                }
                else
                {
                    MessageBox.Show("Some fields is empty!");
                    return;
                }
                RefreshList();
            }
        }
Ejemplo n.º 2
0
        private void SaveFood(object sender, RoutedEventArgs e)
        {
            if (!AddedImage)
            {
                if (MessageBox.Show("Image is not selected!", "Do you want to choose it?", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    return;
                }
            }

            ShopContext shopContext = new ShopContext();
            FoodType    foodType    = new FoodType();

            if (AddedImage)
            {
                foodType.Image = Image;
            }
            shopContext.FoodType.Add(foodType);

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

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

                FoodTypeDictionary dictionary = new FoodTypeDictionary();
                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 <FoodTypeDictionary> duplicates = dictionaryList.GroupBy(d => d.LanguageID).SelectMany(d => d.Skip(1)).ToList();

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

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

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

            List <int> foodTypeIdList = shopContext.FoodType.Where(f => f.Image == foodType.Image).Select(f => f.FoodTypeID).ToList();

            foreach (var id in foodTypeIdList)
            {
                int idCount = shopContext.FoodTypeDictionary.Where(fd => fd.FoodTypeID == id).Count();
                if (idCount == 0)
                {
                    foreach (var item in dictionaryList)
                    {
                        item.FoodTypeID = id;
                        shopContext.FoodTypeDictionary.Add(item);
                    }
                    break;
                }
            }
            shopContext.SaveChanges();
            FoodTypeNames.Items.Clear();
            FoodTypeNames.Items.Add(new NewLanguage {
                Languages = Languages
            });
        }