| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using Npgsql;
- using System;
- using System.Configuration;
- using System.Data;
- using System.Windows.Forms;
- namespace WindowsFormsApp4
- {
- public partial class IssueBookForm : Form
- {
- private readonly string connStr = ConfigurationManager.ConnectionStrings["PostgreSQL"].ConnectionString;
- public IssueBookForm()
- {
- InitializeComponent();
- }
- private void IssueBookForm_Load(object sender, EventArgs e)
- {
- LoadLibraryName();
- LoadReaders();
- }
- private void LoadLibraryName()
- {
- string query = "SELECT name FROM libraries WHERE id = @libId";
- using (var conn = new NpgsqlConnection(connStr))
- using (var cmd = new NpgsqlCommand(query, conn))
- {
- cmd.Parameters.AddWithValue("libId", Session.LibraryId);
- conn.Open();
- var result = cmd.ExecuteScalar();
- labelLibraryName.Text = result != null ? "Библиотека: " + result.ToString() : "Библиотека: Неизвестно";
- }
- }
- private void LoadReaders()
- {
- string query = @"
- SELECT id, name || ' ' || lastname AS fullname
- FROM users
- WHERE library_id = @libId AND role_name = 'reader'
- ORDER BY lastname";
- DataTable dt = new DataTable();
- using (var conn = new NpgsqlConnection(connStr))
- using (var cmd = new NpgsqlCommand(query, conn))
- {
- cmd.Parameters.AddWithValue("libId", Session.LibraryId);
- conn.Open();
- using (var adapter = new NpgsqlDataAdapter(cmd))
- {
- adapter.Fill(dt);
- }
- }
- comboBoxReader.DisplayMember = "fullname";
- comboBoxReader.ValueMember = "id";
- comboBoxReader.DataSource = dt;
- comboBoxReader.SelectedIndex = -1;
- }
- private void comboBoxReader_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (comboBoxReader.SelectedValue == null) return;
- LoadAvailableBooks();
- }
- private void LoadAvailableBooks()
- {
- string query = @"
- SELECT
- be.isbn,
- b.title || ' (инв. ' || be.isbn || ')' AS book_display
- FROM book_exemplar be
- JOIN book b ON be.book_id = b.id
- WHERE be.status = 'available'
- AND be.library_id = @libId
- ORDER BY b.title";
- DataTable dt = new DataTable();
- using (var conn = new NpgsqlConnection(connStr))
- using (var cmd = new NpgsqlCommand(query, conn))
- {
- cmd.Parameters.AddWithValue("libId", Session.LibraryId);
- conn.Open();
- using (var adapter = new NpgsqlDataAdapter(cmd))
- {
- adapter.Fill(dt);
- }
- }
- comboBoxBook.DisplayMember = "book_display";
- comboBoxBook.ValueMember = "isbn";
- comboBoxBook.DataSource = dt;
- comboBoxBook.SelectedIndex = -1;
- }
- private void buttonIssue_Click(object sender, EventArgs e)
- {
- if (comboBoxReader.SelectedValue == null)
- {
- MessageBox.Show("Выберите читателя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- if (comboBoxBook.SelectedValue == null)
- {
- MessageBox.Show("Выберите книгу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- int readerId = Convert.ToInt32(comboBoxReader.SelectedValue);
- int isbn = Convert.ToInt32(comboBoxBook.SelectedValue);
- try
- {
- using (var conn = new NpgsqlConnection(connStr))
- {
- conn.Open();
- string sql = "SELECT issue_book(@readerId, @employeeId, @isbn, @dueDays)";
- using (var cmd = new NpgsqlCommand(sql, conn))
- {
- cmd.Parameters.AddWithValue("readerId", readerId);
- cmd.Parameters.AddWithValue("employeeId", Session.UserId);
- cmd.Parameters.AddWithValue("isbn", isbn);
- cmd.Parameters.AddWithValue("dueDays", 14);
- object result = cmd.ExecuteScalar();
- if (result == null || result == DBNull.Value)
- {
- MessageBox.Show("Функция не вернула номер заказа", "Ошибка");
- return;
- }
- int orderId = Convert.ToInt32(result);
- MessageBox.Show($"Книга выдана! № заказа: {orderId}", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
- LoadAvailableBooks();
- }
- }
- }
- catch (PostgresException ex)
- {
- MessageBox.Show($"Ошибка БД: {ex.MessageText}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- catch (Exception ex)
- {
- MessageBox.Show($"Ошибка: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- }
|