2024-11-20 19:31:06 +04:00
|
|
|
|
using ProjectLibrary.Entities;
|
|
|
|
|
using ProjectLibrary.Entities.Enums;
|
|
|
|
|
using ProjectLibrary.Repositores;
|
|
|
|
|
using ProjectLibrary.Repositories;
|
|
|
|
|
|
|
|
|
|
namespace ProjectLibrary.Forms
|
2024-11-19 14:49:58 +04:00
|
|
|
|
{
|
|
|
|
|
public partial class FBook : Form
|
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
private readonly IBookRepository _bookRepository;
|
|
|
|
|
private int? _bookId;
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
public int Id
|
2024-11-19 14:49:58 +04:00
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
set
|
2024-11-19 14:49:58 +04:00
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var book = _bookRepository.ReadBookById(value);
|
|
|
|
|
if (book == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Книга не найдена.");
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
txtAuthor.Text = book.Author;
|
|
|
|
|
txtName.Text = book.Name;
|
|
|
|
|
cmbType.SelectedItem = book.Type;
|
|
|
|
|
comboBoxLibrary.SelectedItem = book.LibraryID;
|
|
|
|
|
_bookId = value;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
public FBook(IBookRepository bookRepository, ILibraryRepository libraryRepository)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(bookRepository));
|
|
|
|
|
comboBoxLibrary.DataSource = libraryRepository.ReadLibraries();
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
2024-11-19 14:49:58 +04:00
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(txtAuthor.Text) || string.IsNullOrWhiteSpace(txtName.Text) || comboBoxLibrary.SelectedItem == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Имеются незаполненные поля.");
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
var book = Book.CreateEntity(
|
|
|
|
|
_bookId ?? 0,
|
|
|
|
|
txtAuthor.Text,
|
|
|
|
|
txtName.Text,
|
|
|
|
|
(BookType)cmbType.SelectedValue,
|
|
|
|
|
Convert.ToInt32(comboBoxLibrary.SelectedItem)
|
|
|
|
|
);
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
if (_bookId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_bookRepository.UpdateBook(book);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_bookRepository.CreateBook(book);
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
2024-11-19 14:49:58 +04:00
|
|
|
|
{
|
2024-11-20 19:31:06 +04:00
|
|
|
|
Close();
|
2024-11-19 14:49:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|