| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using Npgsql;
- using System;
- using System.Configuration;
- using System.Data;
- using System.Windows.Forms;
- namespace WindowsFormsApp4
- {
- public partial class AddExemplarsForm : Form
- {
- private readonly string connStr = ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString;
- private int selectedBookId = 0;
- public AddExemplarsForm()
- {
- InitializeComponent();
- }
- private void AddExemplarsForm_Load(object sender, EventArgs e)
- {
- if (Session.LibraryId == null)
- {
- MessageBox.Show("Ошибка: ваша учётная запись не привязана к библиотеке. Обратитесь к администратору.", "Ошибка");
- this.Close();
- return;
- }
- LoadBooks();
- }
- private void LoadBooks()
- {
- string query = @"
- SELECT b.id, b.title || ' (' || a.name || ' ' || a.lastname || ')' AS book_display
- FROM book b
- JOIN authors a ON b.author_id = a.id
- ORDER BY b.title";
- DataTable dt = new DataTable();
- using (var conn = new NpgsqlConnection(connStr))
- using (var da = new NpgsqlDataAdapter(query, conn))
- {
- da.Fill(dt);
- }
- cmbBook.DisplayMember = "book_display";
- cmbBook.ValueMember = "id";
- cmbBook.DataSource = dt;
- cmbBook.SelectedIndex = -1;
- }
- private void cmbBook_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (cmbBook.SelectedValue == null)
- selectedBookId = 0;
- else
- selectedBookId = (int)cmbBook.SelectedValue;
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- if (selectedBookId == 0)
- {
- MessageBox.Show("Выберите книгу.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- int newCount = (int)numericNewCount.Value;
- if (newCount <= 0)
- {
- MessageBox.Show("Количество должно быть больше нуля.", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (Session.LibraryId == null)
- {
- MessageBox.Show("Ошибка: идентификатор библиотеки не определён.", "Ошибка");
- return;
- }
- int libraryId = Session.LibraryId.Value;
- try
- {
- using (var conn = new NpgsqlConnection(connStr))
- {
- conn.Open();
- using (var tran = conn.BeginTransaction())
- {
- long nextIsbn = 1000000000;
- string maxIsbnQuery = "SELECT COALESCE(MAX(isbn), 0) FROM book_exemplar";
- using (var cmd = new NpgsqlCommand(maxIsbnQuery, conn, tran))
- {
- object max = cmd.ExecuteScalar();
- if (max != DBNull.Value && Convert.ToInt64(max) >= nextIsbn)
- nextIsbn = Convert.ToInt64(max) + 1;
- }
- for (int i = 0; i < newCount; i++)
- {
- string insertExemplar = @"
- INSERT INTO book_exemplar (isbn, book_id, status, library_id)
- VALUES (@isbn, @bookId, 'available', @libId)";
- using (var cmd = new NpgsqlCommand(insertExemplar, conn, tran))
- {
- cmd.Parameters.AddWithValue("isbn", nextIsbn + i);
- cmd.Parameters.AddWithValue("bookId", selectedBookId);
- cmd.Parameters.AddWithValue("libId", libraryId);
- cmd.ExecuteNonQuery();
- }
- }
- string updateBook = "UPDATE book SET count = count + @add WHERE id = @id";
- using (var cmd = new NpgsqlCommand(updateBook, conn, tran))
- {
- cmd.Parameters.AddWithValue("add", newCount);
- cmd.Parameters.AddWithValue("id", selectedBookId);
- cmd.ExecuteNonQuery();
- }
- tran.Commit();
- MessageBox.Show($"Успешно добавлено экземпляров: {newCount}.", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Ошибка: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- this.Close();
- }
- }
- }
|