//Users action buttons private void Edit_Click(object sender, RoutedEventArgs e) { //Get button control Button Control = (Button)sender; //Set user information to controls var UserData = NovaAPI.APIUsers.users.Find(x => x.id == Control.Tag.ToString()); if (UserData.id == "1") { MessageBox.Show("No se puede modificar el usuario de administrador del sistema."); return; } if (FormGrid.Opacity == 0) { FormGrid.BeginStoryboard((Storyboard)Application.Current.TryFindResource("PopUpGrid")); } //User principal data UserNameTX.Text = UserData.name; UserRealTX.Text = UserData.realname; UserPassword.Password = UserData.name; //User rol and branch data UserRolCB.SelectedItem = NovaAPI.APIRoles.userrols.Find(x => x.rolid == UserData.rolid); UserBranchCB.SelectedItem = NovaAPI.APIBranch.branch.Find(x => x.id == UserData.branchid); //user status data UserStatusCB.IsChecked = UserData.status == "1" ? true : false; //Set photo //------------------------- //Set selected rol id index for edition save SelectedIndex = Control.Tag.ToString(); UserRolCB.Items.Refresh(); UserBranchCB.Items.Refresh(); //Focus editable rol UserNameTX.Focus(); SaveBT.IsEnabled = true; }
//New user action private void NewUserBT_Click(object sender, RoutedEventArgs e) { //From grid animation if (FormGrid.Opacity == 0) { FormGrid.BeginStoryboard((Storyboard)Application.Current.TryFindResource("PopUpGrid")); UserNameTX.Focus(); SaveBT.IsEnabled = true; } else if (UserNameTX.Text.Length == 0) { FormGrid.BeginStoryboard((Storyboard)Application.Current.TryFindResource("FadeInGrid")); ResetForm(); } //Clear branch values if (UserNameTX.Text.Length > 0) { ResetForm(); } }
/// <summary> /// Save user information /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void SaveBT_Click(object sender, RoutedEventArgs e) { NewUserBT.Focus(); if (UserNameTX.Text.Length == 0 || UserNameTX.Text.Length < 5) { MessageBox.Show("El nombre de usuario no puede estar en blanco o ser inferior a 5 caracteres"); UserNameTX.Focus(); return; } else if (UserPassword.Password.Length == 0 || UserPassword.Password.Length < 5) { MessageBox.Show("la contraseña no puede estar en blanco o ser inferior a 5 caracteres"); UserPassword.Focus(); return; } //Get/Set user parameters var Data = new NovaAPI.APIUsers.UsersClass(); Data.id = SelectedIndex; Data.name = UserNameTX.Text; Data.realname = UserRealTX.Text; Data.rolid = ((NovaAPI.APIRoles.Rols)UserRolCB.SelectedItem).rolid; Data.branchid = ((NovaAPI.APIBranch.BranchClass)UserBranchCB.SelectedItem).id; Data.status = UserStatusCB.IsChecked == true ? "1" : "0"; //Password logic if (CheckPassword.Visibility == Visibility.Visible) { Data.hash = GeneralFunctions.GenerateMD5(UserPassword.Password); } //rol json data string requestData = JsonConvert.SerializeObject(Data); bool response; //Modify / Create request if (Data.id.Length > 0) { response = await NovaAPI.APIUsers.GetValues("2", DataConfig.LocalAPI, requestData); } else { response = await NovaAPI.APIUsers.GetValues("1", DataConfig.LocalAPI, requestData); } //Request response if (response) { if (Data.id.Length > 0) { //On user modified var UserData = NovaAPI.APIUsers.users.Find(x => x.id == Data.id); UserData.name = Data.name; UserData.realname = Data.realname; UserData.branchid = Data.branchid; UserData.branchname = NovaAPI.APIBranch.branch.Find(x => x.id == Data.branchid).name; UserData.rolid = Data.rolid; UserData.rolname = NovaAPI.APIRoles.userrols.Find(x => x.rolid == Data.rolid).rolname; UserData.status = Data.status; UsersGrid.Items.Refresh(); FormGrid.BeginStoryboard((Storyboard)Application.Current.TryFindResource("FadeInGrid")); ResetForm(); } else { //On new user created response var UserData = new NovaAPI.APIUsers.UsersClass(); UserData.name = Data.name; UserData.realname = Data.realname; UserData.branchid = Data.branchid; UserData.branchname = NovaAPI.APIBranch.branch.Find(x => x.id == Data.branchid).name; UserData.rolid = Data.rolid; UserData.rolname = NovaAPI.APIRoles.userrols.Find(x => x.rolid == Data.rolid).rolname; UserData.status = Data.status; UserData.id = NovaAPI.APIUsers.LastID; FormGrid.BeginStoryboard((Storyboard)Application.Current.TryFindResource("FadeInGrid")); ResetForm(); NovaAPI.APIUsers.users.Add(UserData); //Reload rol data LoadData(); } } else { MessageBox.Show($"Error al crear el usuario, INFO: {Environment.NewLine}{NovaAPI.APIUsers.Message}"); } }