private void AddSubGroupButton_Click_1(object sender, RoutedEventArgs e) { using (var context = new ExpansesManagerContext()) { AddWindow addWindow = new AddWindow(); addWindow.ShowDialog(); SubGroup subGroup = new SubGroup() { Name = addWindow.textBox.Text, }; context.Groups.FirstOrDefault(g => g.Name == TreeView1.SelectedItem.ToString()).SubGroups.Add(subGroup); context.SaveChanges(); TreeViewItem group = new TreeViewItem() { Header = TreeView1.SelectedItem.ToString(), }; TreeViewItem newSubGroup = new TreeViewItem(); newSubGroup.Header = addWindow.textBox.Text; group.Items.Add(newSubGroup); } }
private void SaveButton_Click(object sender, RoutedEventArgs e) { bool isPasswordDiff = OldPasswordBox.Password != PasswordBox.Password && OldPasswordBox.Password != RepeatPasswordBox.Password; bool isOldPassCorrect = _user.Password == OldPasswordBox.Password; bool isPasswordSame = PasswordBox.Password == RepeatPasswordBox.Password; if (isOldPassCorrect && isPasswordDiff && isPasswordSame) { using (var context = new ExpansesManagerContext()) { _user.Password = PasswordBox.Password; context.Users.AddOrUpdate(_user); context.SaveChanges(); } UsernameTextBox.Clear(); PasswordBox.Clear(); RepeatPasswordBox.Clear(); OldPasswordBox.Clear(); ErrorLabel.Content = "Successfully Changed Password !"; } else { ErrorLabel.Content = Checks.PasswordsDoNotMatch; } }
private void RegisterButton_Click(object sender, RoutedEventArgs e) { if (Checks.UsernameIsTaken(UsernameTextBox.Text)) { ErrorLabel.Content = Checks.UsernameIsAlreadyTaken; return; } if (PasswordBox.Password != RepeatPasswordBox.Password) { ErrorLabel.Content = Checks.PasswordsDoNotMatch; return; } if (!Checks.PasswordIsValid(PasswordBox.Password)) { ErrorLabel.Content = Checks.PasswordIsInvalid; return; } if (Checks.EmailIsValid(EmailtextBox.Text)) { ErrorLabel.Content = Checks.EmailIsNotValid; return; } using (var context = new ExpansesManagerContext()) { User user = new User() { Username = UsernameTextBox.Text, Password = PasswordBox.Password, Email = EmailtextBox.Text, DateRegistered = DateTime.UtcNow, }; context.Users.Add(user); context.SaveChanges(); } UsernameTextBox.Clear(); PasswordBox.Clear(); RepeatPasswordBox.Clear(); EmailtextBox.Clear(); ErrorLabel.Content = "Successfully registered!"; }
private void DeleteElement_OnClick(object sender, RoutedEventArgs e) { var btn = sender as Button; var element = btn?.Tag as ElementViewModel; if (element != null) { using (var db = new ExpansesManagerContext()) { (this.ElementsGrid.ItemsSource as ObservableCollection <ElementViewModel>).Remove(element); var removeDbelement = db.Elements.Find(element.Id); if (removeDbelement != null) { db.Elements.Remove(removeDbelement); db.SaveChanges(); } } } }
private void DeleteSubGroup_OnClick(object sender, RoutedEventArgs e) { var btn = sender as Button; var subGroup = btn?.Tag as SubGroupViewModel; if (subGroup != null) { using (var db = new ExpansesManagerContext()) { (this.SubGroupsGrid.ItemsSource as ObservableCollection <SubGroupViewModel>).Remove(subGroup); var removeDbSubGroup = db.SubGroups.Find(subGroup.Id); if (removeDbSubGroup != null) { db.SubGroups.Remove(removeDbSubGroup); db.SaveChanges(); } } } }
private void AddGroupButton_Click(object sender, EventArgs e) { using (var context = new ExpansesManagerContext()) { AddWindow addWindow = new AddWindow(); addWindow.ShowDialog(); Group group = new Group(); group.Name = addWindow.textBox.Text; group.UserId = AuthenticationManager.GetCurrentUser().Id; context.Groups.Add(group); context.SaveChanges(); TreeViewItem newGroup = new TreeViewItem(); newGroup.Header = addWindow.textBox.Text; TreeViewGroups.Items.Add(newGroup); } }
private void DeleteGroup_OnClick(object sender, RoutedEventArgs e) { var btn = sender as Button; var group = btn?.Tag as GroupViewModel; if (group != null) { using (var db = new ExpansesManagerContext()) { var removeDbGroup = db.Groups.Find(group.Id); if (removeDbGroup != null) { db.Groups.Remove(removeDbGroup); db.SaveChanges(); var currentUser = db.Users.Find(AuthenticationManager.GetCurrentUser().Id); var vm = this.GroupsGrid.DataContext as MainAppViewModel; vm.Groups = new ObservableCollection <GroupViewModel>(Mapper.Instance.Map <IEnumerable <Group>, ObservableCollection <GroupViewModel> >(currentUser.Groups)); this.GroupsGrid.ItemsSource = null; this.GroupsGrid.ItemsSource = vm.Groups; } } } }
private void RemoveGroupButton_Click(object sender, RoutedEventArgs e) { using (var context = new ExpansesManagerContext()) { if (TreeView1.SelectedItem == null) { MessageBox.Show("Please select first."); return; } //context.Groups.FirstOrDefault(g => g.Name == TreeView1.SelectedItem.ToString() && g.IsActive == true).IsActive = false; var group = context.Groups.FirstOrDefault(g => g.Name == TreeView1.SelectedItem.ToString() && g.IsActive == true); context.Groups.Remove(group); context.SaveChanges(); } TreeViewItem newGroup = new TreeViewItem(); newGroup.Name = TreeView1.SelectedItem.ToString(); MainApp mApp = new MainApp(); this.Close(); mApp.ShowDialog(); }
private void ButtonSaveChanges_OnClick(object sender, RoutedEventArgs e) { var vm = this.GroupsGrid.DataContext as MainAppViewModel; var gridGroups = Mapper.Instance.Map <ObservableCollection <GroupViewModel>, IEnumerable <Group> >(vm.Groups); using (var db = new ExpansesManagerContext()) { foreach (var group in gridGroups) { if (group.Id > 0) { var dbGroup = db.Groups.Find(group.Id); if (dbGroup != null) { if (dbGroup.Name != group.Name) { dbGroup.Name = group.Name; db.SaveChanges(); } var existingSubGroups = group.SubGroups.Where(s => s.Id != 0).ToList(); existingSubGroups.ForEach(eg => { var dbSubGroup = dbGroup.SubGroups.FirstOrDefault(g => g.Id == eg.Id); if (dbSubGroup != null) { if (dbSubGroup.Name != eg.Name) { dbSubGroup.Name = eg.Name; db.SaveChanges(); } foreach (var element in eg.Elements) { var dbElement = dbSubGroup.Elements.FirstOrDefault(el => el.Id == element.Id); if (dbElement != null && element.Id > 0) { if (dbElement.Name != element.Name || dbElement.Price != element.Price || dbElement.DateBought != element.DateBought) { dbElement.Name = element.Name; dbElement.Price = element.Price; dbElement.DateBought = element.DateBought; db.SaveChanges(); } } else { dbSubGroup.Elements.Add(element); db.SaveChanges(); } } } }); var newSubGroups = group.SubGroups.Where(s => s.Id == 0).ToList(); if (newSubGroups.Count > 0) { newSubGroups.ForEach(sg => { dbGroup.SubGroups.Add(sg); db.SaveChanges(); foreach (var element in sg.Elements) { var dbElement = sg.Elements.FirstOrDefault(el => el.Id == element.Id); if (dbElement != null && element.Id > 0) { if (dbElement.Name != element.Name || dbElement.Price != element.Price || dbElement.DateBought != element.DateBought) { dbElement.Name = element.Name; dbElement.Price = element.Price; dbElement.DateBought = element.DateBought; db.SaveChanges(); } } else { sg.Elements.Add(element); db.SaveChanges(); } } }); } } } } var newGroups = gridGroups.Where(g => g.Id == 0).ToList(); var currentUser = db.Users.Find(AuthenticationManager.GetCurrentUser().Id); newGroups.ForEach(g => { currentUser.Groups.Add(g); db.SaveChanges(); }); vm.Groups = new ObservableCollection <GroupViewModel>(Mapper.Instance.Map <IEnumerable <Group>, ObservableCollection <GroupViewModel> >(currentUser.Groups)); } this.GroupsGrid.UpdateLayout(); MessageBox.Show("Successfully saved"); }