ServiceForm.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Data;
  3. using System.Windows.Forms;
  4. using Npgsql;
  5. namespace PhotoStudioApp
  6. {
  7. public partial class ServiceForm : Form
  8. {
  9. private string _connectionString;
  10. public ServiceForm(string connectionString)
  11. {
  12. InitializeComponent();
  13. _connectionString = connectionString;
  14. LoadServices();
  15. }
  16. private void LoadServices()
  17. {
  18. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  19. {
  20. conn.Open();
  21. string sql = "select service_id, service_name, service_type, price from services";
  22. NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn);
  23. DataTable dt = new DataTable();
  24. adapter.Fill(dt);
  25. // Скрываем service_id
  26. dgvServices.DataSource = dt;
  27. if (dgvServices.Columns.Contains("service_id"))
  28. {
  29. dgvServices.Columns["service_id"].Visible = false;
  30. }
  31. }
  32. }
  33. private void btnAdd_Click(object sender, EventArgs e)
  34. {
  35. // Простая форма для добавления
  36. string name = Microsoft.VisualBasic.Interaction.InputBox("Введите название услуги", "Добавление");
  37. if (!string.IsNullOrEmpty(name))
  38. {
  39. string type = Microsoft.VisualBasic.Interaction.InputBox("Введите тип услуги (photo, video, event)", "Добавление");
  40. string priceStr = Microsoft.VisualBasic.Interaction.InputBox("Введите стоимость", "Добавление");
  41. if (decimal.TryParse(priceStr, out decimal price))
  42. {
  43. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  44. {
  45. conn.Open();
  46. string sql = "insert into services (service_name, service_type, price) values (@name, @type, @price)";
  47. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  48. cmd.Parameters.AddWithValue("@name", name);
  49. cmd.Parameters.AddWithValue("@type", type);
  50. cmd.Parameters.AddWithValue("@price", price);
  51. cmd.ExecuteNonQuery();
  52. MessageBox.Show("Услуга добавлена");
  53. LoadServices();
  54. }
  55. }
  56. else
  57. {
  58. MessageBox.Show("Некорректная стоимость");
  59. }
  60. }
  61. }
  62. private void btnEdit_Click(object sender, EventArgs e)
  63. {
  64. if (dgvServices.SelectedRows.Count > 0)
  65. {
  66. DataRowView rowView = dgvServices.SelectedRows[0].DataBoundItem as DataRowView;
  67. if (rowView != null)
  68. {
  69. int serviceId = (int)rowView["service_id"];
  70. string currentName = rowView["service_name"].ToString();
  71. string currentType = rowView["service_type"].ToString();
  72. string currentPrice = rowView["price"].ToString();
  73. string name = Microsoft.VisualBasic.Interaction.InputBox("Название услуги", "Редактирование", currentName);
  74. string type = Microsoft.VisualBasic.Interaction.InputBox("Тип услуги", "Редактирование", currentType);
  75. string priceStr = Microsoft.VisualBasic.Interaction.InputBox("Стоимость", "Редактирование", currentPrice);
  76. if (decimal.TryParse(priceStr, out decimal price))
  77. {
  78. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  79. {
  80. conn.Open();
  81. string sql = "update services set service_name = @name, service_type = @type, price = @price where service_id = @serviceId";
  82. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  83. cmd.Parameters.AddWithValue("@name", name);
  84. cmd.Parameters.AddWithValue("@type", type);
  85. cmd.Parameters.AddWithValue("@price", price);
  86. cmd.Parameters.AddWithValue("@serviceId", serviceId);
  87. cmd.ExecuteNonQuery();
  88. MessageBox.Show("Услуга обновлена");
  89. LoadServices();
  90. }
  91. }
  92. else
  93. {
  94. MessageBox.Show("Некорректная стоимость");
  95. }
  96. }
  97. }
  98. else
  99. {
  100. MessageBox.Show("Выберите услугу для редактирования");
  101. }
  102. }
  103. private void btnDelete_Click(object sender, EventArgs e)
  104. {
  105. if (dgvServices.SelectedRows.Count > 0)
  106. {
  107. DataRowView rowView = dgvServices.SelectedRows[0].DataBoundItem as DataRowView;
  108. if (rowView != null)
  109. {
  110. int serviceId = (int)rowView["service_id"];
  111. if (MessageBox.Show("Удалить услугу?", "Подтверждение", MessageBoxButtons.YesNo) == DialogResult.Yes)
  112. {
  113. using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString))
  114. {
  115. conn.Open();
  116. string sql = "delete from services where service_id = @serviceId";
  117. NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
  118. cmd.Parameters.AddWithValue("@serviceId", serviceId);
  119. cmd.ExecuteNonQuery();
  120. MessageBox.Show("Услуга удалена");
  121. LoadServices();
  122. }
  123. }
  124. }
  125. }
  126. else
  127. {
  128. MessageBox.Show("Выберите услугу для удаления");
  129. }
  130. }
  131. private void btnClose_Click(object sender, EventArgs e)
  132. {
  133. this.Close();
  134. }
  135. }
  136. }