IssueBookForm.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Npgsql;
  2. using System;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Windows.Forms;
  6. namespace WindowsFormsApp4
  7. {
  8. public partial class IssueBookForm : Form
  9. {
  10. private readonly string connStr = ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString;
  11. public IssueBookForm()
  12. {
  13. InitializeComponent();
  14. }
  15. private void IssueBookForm_Load(object sender, EventArgs e)
  16. {
  17. LoadLibraryName();
  18. LoadReaders();
  19. }
  20. private void LoadLibraryName()
  21. {
  22. string query = "SELECT name FROM libraries WHERE id = @libId";
  23. using (var conn = new NpgsqlConnection(connStr))
  24. using (var cmd = new NpgsqlCommand(query, conn))
  25. {
  26. cmd.Parameters.AddWithValue("libId", Session.LibraryId);
  27. conn.Open();
  28. var result = cmd.ExecuteScalar();
  29. labelLibraryName.Text = result != null ? "Библиотека: " + result.ToString() : "Библиотека: Неизвестно";
  30. }
  31. }
  32. private void LoadReaders()
  33. {
  34. string query = @"
  35. SELECT id, name || ' ' || lastname AS fullname
  36. FROM users
  37. WHERE library_id = @libId AND role_name = 'reader'
  38. ORDER BY lastname";
  39. DataTable dt = new DataTable();
  40. using (var conn = new NpgsqlConnection(connStr))
  41. using (var cmd = new NpgsqlCommand(query, conn))
  42. {
  43. cmd.Parameters.AddWithValue("libId", Session.LibraryId);
  44. conn.Open();
  45. using (var adapter = new NpgsqlDataAdapter(cmd))
  46. {
  47. adapter.Fill(dt);
  48. }
  49. }
  50. comboBoxReader.DisplayMember = "fullname";
  51. comboBoxReader.ValueMember = "id";
  52. comboBoxReader.DataSource = dt;
  53. comboBoxReader.SelectedIndex = -1;
  54. }
  55. private void comboBoxReader_SelectedIndexChanged(object sender, EventArgs e)
  56. {
  57. if (comboBoxReader.SelectedValue == null) return;
  58. LoadAvailableBooks();
  59. }
  60. private void LoadAvailableBooks()
  61. {
  62. string query = @"
  63. SELECT
  64. be.isbn,
  65. b.title || ' (инв. ' || be.isbn || ')' AS book_display
  66. FROM book_exemplar be
  67. JOIN book b ON be.book_id = b.id
  68. WHERE be.status = 'available'
  69. AND be.library_id = @libId
  70. ORDER BY b.title";
  71. DataTable dt = new DataTable();
  72. using (var conn = new NpgsqlConnection(connStr))
  73. using (var cmd = new NpgsqlCommand(query, conn))
  74. {
  75. cmd.Parameters.AddWithValue("libId", Session.LibraryId);
  76. conn.Open();
  77. using (var adapter = new NpgsqlDataAdapter(cmd))
  78. {
  79. adapter.Fill(dt);
  80. }
  81. }
  82. comboBoxBook.DisplayMember = "book_display";
  83. comboBoxBook.ValueMember = "isbn";
  84. comboBoxBook.DataSource = dt;
  85. comboBoxBook.SelectedIndex = -1;
  86. }
  87. private void buttonIssue_Click(object sender, EventArgs e)
  88. {
  89. if (comboBoxReader.SelectedValue == null)
  90. {
  91. MessageBox.Show("Выберите читателя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  92. return;
  93. }
  94. if (comboBoxBook.SelectedValue == null)
  95. {
  96. MessageBox.Show("Выберите книгу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  97. return;
  98. }
  99. int readerId = Convert.ToInt32(comboBoxReader.SelectedValue);
  100. int isbn = Convert.ToInt32(comboBoxBook.SelectedValue);
  101. try
  102. {
  103. using (var conn = new NpgsqlConnection(connStr))
  104. {
  105. conn.Open();
  106. string sql = "SELECT issue_book(@readerId, @employeeId, @isbn, @dueDays)";
  107. using (var cmd = new NpgsqlCommand(sql, conn))
  108. {
  109. cmd.Parameters.AddWithValue("readerId", readerId);
  110. cmd.Parameters.AddWithValue("employeeId", Session.UserId);
  111. cmd.Parameters.AddWithValue("isbn", isbn);
  112. cmd.Parameters.AddWithValue("dueDays", 14);
  113. object result = cmd.ExecuteScalar();
  114. if (result == null || result == DBNull.Value)
  115. {
  116. MessageBox.Show("Функция не вернула номер заказа", "Ошибка");
  117. return;
  118. }
  119. int orderId = Convert.ToInt32(result);
  120. MessageBox.Show($"Книга выдана! № заказа: {orderId}", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
  121. LoadAvailableBooks();
  122. }
  123. }
  124. }
  125. catch (PostgresException ex)
  126. {
  127. MessageBox.Show($"Ошибка БД: {ex.MessageText}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  128. }
  129. catch (Exception ex)
  130. {
  131. MessageBox.Show($"Ошибка: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  132. }
  133. }
  134. }
  135. }