using System; using System.Data; using System.Windows.Forms; using Npgsql; namespace PhotoStudioApp { public partial class ClientForm : Form { private string _connectionString; public ClientForm(string connectionString) { InitializeComponent(); _connectionString = connectionString; LoadClients(); } private void LoadClients() { using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); string sql = "select client_id, last_name, first_name, middle_name, phone from clients"; NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn); DataTable dt = new DataTable(); adapter.Fill(dt); dgvClients.DataSource = dt; if (dgvClients.Columns.Contains("client_id")) { dgvClients.Columns["client_id"].Visible = false; } } } private string GetSHA256Hash(string input) { using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create()) { byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input); byte[] hashBytes = sha256.ComputeHash(inputBytes); return BitConverter.ToString(hashBytes).Replace("-", "").ToLower(); } } private void btnAdd_Click(object sender, EventArgs e) { string lastName = Microsoft.VisualBasic.Interaction.InputBox("Введите фамилию", "Добавление клиента"); if (!string.IsNullOrEmpty(lastName)) { string firstName = Microsoft.VisualBasic.Interaction.InputBox("Введите имя", "Добавление клиента"); string middleName = Microsoft.VisualBasic.Interaction.InputBox("Введите отчество (необязательно)", "Добавление клиента"); string phone = Microsoft.VisualBasic.Interaction.InputBox("Введите телефон", "Добавление клиента"); using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); // 1. Создаём пользователя string login = lastName.ToLower() + "_" + firstName.ToLower(); string passwordHash = GetSHA256Hash("123"); // пароль по умолчанию string userSql = "insert into users (login, password_hash, role_id) values (@login, @hash, 2) returning user_id"; NpgsqlCommand userCmd = new NpgsqlCommand(userSql, conn); userCmd.Parameters.AddWithValue("@login", login); userCmd.Parameters.AddWithValue("@hash", passwordHash); int userId = Convert.ToInt32(userCmd.ExecuteScalar()); // 2. Создаём клиента с полученным user_id string clientSql = "insert into clients (user_id, last_name, first_name, middle_name, phone) values (@userId, @lastName, @firstName, @middleName, @phone)"; NpgsqlCommand clientCmd = new NpgsqlCommand(clientSql, conn); clientCmd.Parameters.AddWithValue("@userId", userId); clientCmd.Parameters.AddWithValue("@lastName", lastName); clientCmd.Parameters.AddWithValue("@firstName", firstName); clientCmd.Parameters.AddWithValue("@middleName", middleName); clientCmd.Parameters.AddWithValue("@phone", phone); clientCmd.ExecuteNonQuery(); MessageBox.Show($"Клиент добавлен! Логин: {login}, пароль: 123"); LoadClients(); } } } private void btnEdit_Click(object sender, EventArgs e) { if (dgvClients.SelectedRows.Count > 0) { DataRowView rowView = dgvClients.SelectedRows[0].DataBoundItem as DataRowView; if (rowView != null) { int clientId = (int)rowView["client_id"]; string currentLastName = rowView["last_name"].ToString(); string currentFirstName = rowView["first_name"].ToString(); string currentMiddleName = rowView["middle_name"].ToString(); string currentPhone = rowView["phone"].ToString(); string lastName = Microsoft.VisualBasic.Interaction.InputBox("Фамилия", "Редактирование", currentLastName); string firstName = Microsoft.VisualBasic.Interaction.InputBox("Имя", "Редактирование", currentFirstName); string middleName = Microsoft.VisualBasic.Interaction.InputBox("Отчество", "Редактирование", currentMiddleName); string phone = Microsoft.VisualBasic.Interaction.InputBox("Телефон", "Редактирование", currentPhone); using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); string sql = "update clients set last_name = @lastName, first_name = @firstName, middle_name = @middleName, phone = @phone where client_id = @clientId"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@lastName", lastName); cmd.Parameters.AddWithValue("@firstName", firstName); cmd.Parameters.AddWithValue("@middleName", middleName); cmd.Parameters.AddWithValue("@phone", phone); cmd.Parameters.AddWithValue("@clientId", clientId); cmd.ExecuteNonQuery(); MessageBox.Show("Клиент обновлён"); LoadClients(); } } } else { MessageBox.Show("Выберите клиента для редактирования"); } } private void btnDelete_Click(object sender, EventArgs e) { if (dgvClients.SelectedRows.Count > 0) { DataRowView rowView = dgvClients.SelectedRows[0].DataBoundItem as DataRowView; if (rowView != null) { int clientId = (int)rowView["client_id"]; if (MessageBox.Show("Удалить клиента?", "Подтверждение", MessageBoxButtons.YesNo) == DialogResult.Yes) { using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString)) { conn.Open(); // Сначала удаляем клиента string sql = "delete from clients where client_id = @clientId"; NpgsqlCommand cmd = new NpgsqlCommand(sql, conn); cmd.Parameters.AddWithValue("@clientId", clientId); cmd.ExecuteNonQuery(); MessageBox.Show("Клиент удалён"); LoadClients(); } } } } else { MessageBox.Show("Выберите клиента для удаления"); } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } } }