public MenuWindow(int employeeId, ThucDon_MonAn selectedProductMenu) { EmployeeId = employeeId; SelectedProductMenu = selectedProductMenu; InitializeComponent(); var tmp = db.MonAns.Where(x => x.IsDeleted == false).ToList(); cbbName.ItemsSource = tmp; }
private void BtnAccept_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show("Do you want to change present??", "Question", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { try { string productName = cbbName.Text; string menuName = txbName.Text; var menu = db.ThucDons.Where(s => s.TenThucDon == menuName && s.IsDeleted == false).SingleOrDefault(); var product = db.MonAns.Where(s => s.TenMonAn == productName).SingleOrDefault(); if (SelectedProductMenu == null) { if (menu == null) { ThucDon newMenu = new ThucDon { TenThucDon = menuName, IsDeleted = false }; db.ThucDons.Add(newMenu); db.SaveChanges(); var findNewMenuID = db.ThucDons.FirstOrDefault(s => s.TenThucDon == menuName); int menuId = findNewMenuID.ThucDonID; int productId = product.MonAnID; ThucDon_MonAn newProductMenu = new ThucDon_MonAn { ThucDonID = findNewMenuID.ThucDonID, MonAnID = product.MonAnID }; db.ThucDon_MonAn.Add(newProductMenu); db.SaveChanges(); } else if (menu != null) { var menuProduct = db.ThucDon_MonAn.Where(s => s.MonAnID == product.MonAnID && s.ThucDonID == menu.ThucDonID).SingleOrDefault(); if (menuProduct == null) { ThucDon_MonAn newProductMenu = new ThucDon_MonAn { ThucDonID = menu.ThucDonID, MonAnID = product.MonAnID }; db.ThucDon_MonAn.Add(newProductMenu); db.SaveChanges(); } else { MessageBox.Show("This Menu Already Has This Product"); return; } } } else { var findMenu = db.ThucDons.Where(s => s.ThucDonID == SelectedProductMenu.ThucDonID && s.IsDeleted == false).FirstOrDefault(); if (menu == null) { findMenu.TenThucDon = menuName; db.SaveChanges(); } else { MessageBox.Show("Menu name exsited"); return; } } db.SaveChanges(); MainWindow mainWindow = new MainWindow(EmployeeId); this.Close(); mainWindow.ShowDialog(); } catch { MessageBox.Show("Fail"); } } }