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;
|
2024-12-08 13:24:07 +04:00
|
|
|
|
//checkedListBox1.SelectedItem = book.TypeBookID;
|
|
|
|
|
char[] listInd = Convert.ToString((int)book.TypeBookID, 2).ToCharArray();
|
|
|
|
|
for (int i = 0; i < listInd.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
checkedListBox1.SetItemChecked(i, listInd[i]=='1');
|
|
|
|
|
}
|
2024-11-20 19:31:06 +04:00
|
|
|
|
_bookId = value;
|
2024-12-03 20:14:30 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-12-08 13:24:07 +04:00
|
|
|
|
public FBook(IBookRepository bookRepository)
|
2024-11-20 19:31:06 +04:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(bookRepository));
|
2024-12-03 20:14:30 +04:00
|
|
|
|
foreach(var elem in Enum.GetValues(typeof(BookType)))
|
|
|
|
|
{
|
|
|
|
|
if (!elem.Equals(BookType.None)) checkedListBox1.Items.Add(elem);
|
|
|
|
|
}
|
2024-11-20 19:31:06 +04:00
|
|
|
|
}
|
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-12-08 13:24:07 +04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(txtAuthor.Text) || string.IsNullOrWhiteSpace(txtName.Text))
|
2024-11-20 19:31:06 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Имеются незаполненные поля.");
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
if (_bookId.HasValue)
|
|
|
|
|
{
|
2024-12-08 13:24:07 +04:00
|
|
|
|
_bookRepository.UpdateBook(CreateBook(_bookId.Value));
|
2024-11-20 19:31:06 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-08 13:24:07 +04:00
|
|
|
|
_bookRepository.CreateBook(CreateBook(0));
|
2024-11-20 19:31:06 +04:00
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
Close();
|
2024-12-08 13:24:07 +04:00
|
|
|
|
|
2024-11-20 19:31:06 +04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Ошибка при сохранении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-08 13:24:07 +04:00
|
|
|
|
private Book CreateBook(int bookId)
|
2024-11-19 14:49:58 +04:00
|
|
|
|
{
|
2024-12-08 13:24:07 +04:00
|
|
|
|
BookType selectedType = BookType.None;
|
2024-12-03 20:14:30 +04:00
|
|
|
|
|
2024-12-08 13:24:07 +04:00
|
|
|
|
foreach (var item in checkedListBox1.CheckedItems)
|
2024-12-03 20:14:30 +04:00
|
|
|
|
{
|
2024-12-08 13:24:07 +04:00
|
|
|
|
if (item is BookType type)
|
2024-12-03 20:14:30 +04:00
|
|
|
|
{
|
2024-12-08 13:24:07 +04:00
|
|
|
|
selectedType |= type; // Это должно работать, если тип является BookType
|
2024-12-03 20:14:30 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-08 13:24:07 +04:00
|
|
|
|
|
|
|
|
|
return Book.CreateEntity(bookId, txtAuthor.Text, txtName.Text, selectedType);
|
2024-12-03 20:14:30 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-08 13:24:07 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ButtonCancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2024-11-19 14:49:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|