ClientForm.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. using Npgsql;
  5. namespace PhotoStudioApp
  6. {
  7. public partial class ClientForm : Form
  8. {
  9. private string _connectionString;
  10. public ClientForm(string connectionString)
  11. {
  12. InitializeComponent();
  13. _connectionString = connectionString;
  14. LoadClients();
  15. }
  16. private void LoadClients()
  17. {
  18. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  19. {
  20. conn.Open();
  21. string sql = "select client_id, last_name, first_name, middle_name, phone from clients";
  22. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn);
  23. DataTable dt = new DataTable();
  24. adapter.Fill(dt);
  25. dgvClients.DataSource = dt;
  26. if (dgvClients.Columns.Contains("client_id"))
  27. {
  28. dgvClients.Columns["client_id"].Visible = false;
  29. }
  30. }
  31. }
  32. private string GetSHA256Hash(string input)
  33. {
  34. using (System.Security.Cryptography.SHA256 sha256 = System.Security.Cryptography.SHA256.Create())
  35. {
  36. byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
  37. byte[] hashBytes = sha256.ComputeHash(inputBytes);
  38. return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
  39. }
  40. }
  41. private void btnAdd_Click(object sender, EventArgs e)
  42. {
  43. string lastName = Microsoft.VisualBasic.Interaction.InputBox("Введите фамилию", "Добавление клиента");
  44. if (!string.IsNullOrEmpty(lastName))
  45. {
  46. string firstName = Microsoft.VisualBasic.Interaction.InputBox("Введите имя", "Добавление клиента");
  47. string middleName = Microsoft.VisualBasic.Interaction.InputBox("Введите отчество (необязательно)", "Добавление клиента");
  48. string phone = Microsoft.VisualBasic.Interaction.InputBox("Введите телефон", "Добавление клиента");
  49. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  50. {
  51. conn.Open();
  52. // 1. Создаём пользователя
  53. string login = lastName.ToLower() + "_" + firstName.ToLower();
  54. string passwordHash = GetSHA256Hash("123"); // пароль по умолчанию
  55. string userSql = "insert into users (login, password_hash, role_id) values (@login, @hash, 2) returning user_id";
  56. NpgsqlCommand userCmd = new NpgsqlCommand(userSql, conn);
  57. userCmd.Parameters.AddWithValue("@login", login);
  58. userCmd.Parameters.AddWithValue("@hash", passwordHash);
  59. int userId = Convert.ToInt32(userCmd.ExecuteScalar());
  60. // 2. Создаём клиента с полученным user_id
  61. string clientSql = "insert into clients (user_id, last_name, first_name, middle_name, phone) values (@userId, @lastName, @firstName, @middleName, @phone)";
  62. NpgsqlCommand clientCmd = new NpgsqlCommand(clientSql, conn);
  63. clientCmd.Parameters.AddWithValue("@userId", userId);
  64. clientCmd.Parameters.AddWithValue("@lastName", lastName);
  65. clientCmd.Parameters.AddWithValue("@firstName", firstName);
  66. clientCmd.Parameters.AddWithValue("@middleName", middleName);
  67. clientCmd.Parameters.AddWithValue("@phone", phone);
  68. clientCmd.ExecuteNonQuery();
  69. MessageBox.Show($"Клиент добавлен! Логин: {login}, пароль: 123");
  70. LoadClients();
  71. }
  72. }
  73. }
  74. private void btnEdit_Click(object sender, EventArgs e)
  75. {
  76. if (dgvClients.SelectedRows.Count > 0)
  77. {
  78. DataRowView rowView = dgvClients.SelectedRows[0].DataBoundItem as DataRowView;
  79. if (rowView != null)
  80. {
  81. int clientId = (int)rowView["client_id"];
  82. string currentLastName = rowView["last_name"].ToString();
  83. string currentFirstName = rowView["first_name"].ToString();
  84. string currentMiddleName = rowView["middle_name"].ToString();
  85. string currentPhone = rowView["phone"].ToString();
  86. string lastName = Microsoft.VisualBasic.Interaction.InputBox("Фамилия", "Редактирование", currentLastName);
  87. string firstName = Microsoft.VisualBasic.Interaction.InputBox("Имя", "Редактирование", currentFirstName);
  88. string middleName = Microsoft.VisualBasic.Interaction.InputBox("Отчество", "Редактирование", currentMiddleName);
  89. string phone = Microsoft.VisualBasic.Interaction.InputBox("Телефон", "Редактирование", currentPhone);
  90. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  91. {
  92. conn.Open();
  93. string sql = "update clients set last_name = @lastName, first_name = @firstName, middle_name = @middleName, phone = @phone where client_id = @clientId";
  94. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  95. cmd.Parameters.AddWithValue("@lastName", lastName);
  96. cmd.Parameters.AddWithValue("@firstName", firstName);
  97. cmd.Parameters.AddWithValue("@middleName", middleName);
  98. cmd.Parameters.AddWithValue("@phone", phone);
  99. cmd.Parameters.AddWithValue("@clientId", clientId);
  100. cmd.ExecuteNonQuery();
  101. MessageBox.Show("Клиент обновлён");
  102. LoadClients();
  103. }
  104. }
  105. }
  106. else
  107. {
  108. MessageBox.Show("Выберите клиента для редактирования");
  109. }
  110. }
  111. private void btnDelete_Click(object sender, EventArgs e)
  112. {
  113. if (dgvClients.SelectedRows.Count > 0)
  114. {
  115. DataRowView rowView = dgvClients.SelectedRows[0].DataBoundItem as DataRowView;
  116. if (rowView != null)
  117. {
  118. int clientId = (int)rowView["client_id"];
  119. if (MessageBox.Show("Удалить клиента?", "Подтверждение", MessageBoxButtons.YesNo) == DialogResult.Yes)
  120. {
  121. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  122. {
  123. conn.Open();
  124. // Сначала удаляем клиента
  125. string sql = "delete from clients where client_id = @clientId";
  126. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  127. cmd.Parameters.AddWithValue("@clientId", clientId);
  128. cmd.ExecuteNonQuery();
  129. MessageBox.Show("Клиент удалён");
  130. LoadClients();
  131. }
  132. }
  133. }
  134. }
  135. else
  136. {
  137. MessageBox.Show("Выберите клиента для удаления");
  138. }
  139. }
  140. private void btnClose_Click(object sender, EventArgs e)
  141. {
  142. this.Close();
  143. }
  144. }
  145. }