AddExemplarsForm.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 AddExemplarsForm : Form
  9. {
  10. private readonly string connStr = ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString;
  11. private int selectedBookId = 0;
  12. public AddExemplarsForm()
  13. {
  14. InitializeComponent();
  15. }
  16. private void AddExemplarsForm_Load(object sender, EventArgs e)
  17. {
  18. if (Session.LibraryId == null)
  19. {
  20. MessageBox.Show("Ошибка: ваша учётная запись не привязана к библиотеке. Обратитесь к администратору.", "Ошибка");
  21. this.Close();
  22. return;
  23. }
  24. LoadBooks();
  25. }
  26. private void LoadBooks()
  27. {
  28. string query = @"
  29. SELECT b.id, b.title || ' (' || a.name || ' ' || a.lastname || ')' AS book_display
  30. FROM book b
  31. JOIN authors a ON b.author_id = a.id
  32. ORDER BY b.title";
  33. DataTable dt = new DataTable();
  34. using (var conn = new NpgsqlConnection(connStr))
  35. using (var da = new NpgsqlDataAdapter(query, conn))
  36. {
  37. da.Fill(dt);
  38. }
  39. cmbBook.DisplayMember = "book_display";
  40. cmbBook.ValueMember = "id";
  41. cmbBook.DataSource = dt;
  42. cmbBook.SelectedIndex = -1;
  43. }
  44. private void cmbBook_SelectedIndexChanged(object sender, EventArgs e)
  45. {
  46. if (cmbBook.SelectedValue == null)
  47. selectedBookId = 0;
  48. else
  49. selectedBookId = (int)cmbBook.SelectedValue;
  50. }
  51. private void btnAdd_Click(object sender, EventArgs e)
  52. {
  53. if (selectedBookId == 0)
  54. {
  55. MessageBox.Show("Выберите книгу.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  56. return;
  57. }
  58. int newCount = (int)numericNewCount.Value;
  59. if (newCount <= 0)
  60. {
  61. MessageBox.Show("Количество должно быть больше нуля.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  62. return;
  63. }
  64. if (Session.LibraryId == null)
  65. {
  66. MessageBox.Show("Ошибка: идентификатор библиотеки не определён.", "Ошибка");
  67. return;
  68. }
  69. int libraryId = Session.LibraryId.Value;
  70. try
  71. {
  72. using (var conn = new NpgsqlConnection(connStr))
  73. {
  74. conn.Open();
  75. using (var tran = conn.BeginTransaction())
  76. {
  77. long nextIsbn = 1000000000;
  78. string maxIsbnQuery = "SELECT COALESCE(MAX(isbn), 0) FROM book_exemplar";
  79. using (var cmd = new NpgsqlCommand(maxIsbnQuery, conn, tran))
  80. {
  81. object max = cmd.ExecuteScalar();
  82. if (max != DBNull.Value && Convert.ToInt64(max) >= nextIsbn)
  83. nextIsbn = Convert.ToInt64(max) + 1;
  84. }
  85. for (int i = 0; i < newCount; i++)
  86. {
  87. string insertExemplar = @"
  88. INSERT INTO book_exemplar (isbn, book_id, status, library_id)
  89. VALUES (@isbn, @bookId, 'available', @libId)";
  90. using (var cmd = new NpgsqlCommand(insertExemplar, conn, tran))
  91. {
  92. cmd.Parameters.AddWithValue("isbn", nextIsbn + i);
  93. cmd.Parameters.AddWithValue("bookId", selectedBookId);
  94. cmd.Parameters.AddWithValue("libId", libraryId);
  95. cmd.ExecuteNonQuery();
  96. }
  97. }
  98. string updateBook = "UPDATE book SET count = count + @add WHERE id = @id";
  99. using (var cmd = new NpgsqlCommand(updateBook, conn, tran))
  100. {
  101. cmd.Parameters.AddWithValue("add", newCount);
  102. cmd.Parameters.AddWithValue("id", selectedBookId);
  103. cmd.ExecuteNonQuery();
  104. }
  105. tran.Commit();
  106. MessageBox.Show($"Успешно добавлено экземпляров: {newCount}.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
  107. this.DialogResult = DialogResult.OK;
  108. this.Close();
  109. }
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. MessageBox.Show($"Ошибка: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  115. }
  116. }
  117. private void btnCancel_Click(object sender, EventArgs e)
  118. {
  119. this.DialogResult = DialogResult.Cancel;
  120. this.Close();
  121. }
  122. }
  123. }