private void EditAccessLevelButton_Click(object sender, RoutedEventArgs e) { try { if (AccessLevelChoiceComboBox.SelectedIndex == -1) { throw new Exception("You need to select access level!"); } var userId = UserList[UsersListView.SelectedIndex].UserId; UserAccess newUserAccess = UserAccess.Admin; var index = AccessLevelChoiceComboBox.SelectedIndex; if (index != -1) { switch (index) { case 0: newUserAccess = UserAccess.Admin; break; case 1: newUserAccess = UserAccess.Recepcjonista; break; case 2: newUserAccess = UserAccess.PracownikMedyczny; break; default: break; } using (var context = new DataBaseContext()) { DbUser user = context.Users.SingleOrDefault(x => x.Id == userId); if (user.UserAccess == UserAccess.Admin && context.Users.Count(x => x.UserAccess == UserAccess.Admin) == 1) { throw new Exception("You can't change access level for the only user with Admin status!"); } user.UserAccess = newUserAccess; context.SaveChanges(); } UserList = UserForUsersManagementTab.GetRepresentation(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void DeleteUserButton_Click(object sender, RoutedEventArgs e) { try { var userId = UserList[UsersListView.SelectedIndex].UserId; using (var context = new DataBaseContext()) { DbUser user = context.Users.SingleOrDefault(x => x.Id == userId); if (user.UserAccess == UserAccess.Admin && context.Users.Count(x => x.UserAccess == UserAccess.Admin) == 1) { throw new Exception("You can't delete the only user with Admin status!"); } context.Users.Remove(user); context.SaveChanges(); } UserList = UserForUsersManagementTab.GetRepresentation(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void SaveNewUserButton_Click(object sender, RoutedEventArgs e) { try { if (LoginTextBox.Text == "" || PasswordTextBox.Password == "") { throw new Exception("Name and surname must be provided!"); } if (UserList.Exists(x => x.Username == LoginTextBox.Text)) { throw new Exception("User already exists!"); } if (AccessLevelChoiceComboBox.SelectedIndex == -1) { throw new Exception("You need to select access level!"); } UserAccess newUserAccess = UserAccess.Admin; var index = AccessLevelChoiceComboBox.SelectedIndex; if (index != -1) { switch (index) { case 0: newUserAccess = UserAccess.Admin; break; case 1: newUserAccess = UserAccess.Recepcjonista; break; case 2: newUserAccess = UserAccess.PracownikMedyczny; break; default: break; } using (var context = new DataBaseContext()) { if (newUserAccess == UserAccess.PracownikMedyczny) { if (FirstNameTextBox.Text == "" || LastNameTextBox.Text == "") { throw new Exception("Name and surname must be provided!"); } var NewMedicalWorker = new DbMedicalWorker() { FirstName = FirstNameTextBox.Text, LastName = LastNameTextBox.Text, Availabilities = new List <DbAvailability>(), Specializations = new List <DbSpecialization>(), Visits = new List <DbVisit>() }; context.MedicalWorkers.Add(NewMedicalWorker); context.SaveChanges(); context.Users.Add(new DbUser() { Login = LoginTextBox.Text, Password = PasswordTextBox.Password, UserAccess = newUserAccess, MedicalWorker = NewMedicalWorker }); context.SaveChanges(); } else { context.Users.Add(new DbUser() { Login = LoginTextBox.Text, Password = PasswordTextBox.Password, UserAccess = newUserAccess }); context.SaveChanges(); } } UserList = UserForUsersManagementTab.GetRepresentation(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }