105 lines
3.2 KiB
C#
105 lines
3.2 KiB
C#
|
using ProjectLibrary.Repositores;
|
|||
|
using Unity;
|
|||
|
|
|||
|
namespace ProjectLibrary.Forms
|
|||
|
{
|
|||
|
public partial class FBooks : Form
|
|||
|
{
|
|||
|
private readonly IUnityContainer _container;
|
|||
|
private readonly IBookRepository _bookRepository;
|
|||
|
|
|||
|
public FBooks(IUnityContainer container, IBookRepository bookRepository)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
_container = container ?? throw new ArgumentNullException(nameof(container));
|
|||
|
_bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(bookRepository));
|
|||
|
}
|
|||
|
|
|||
|
private void LoadList()
|
|||
|
{
|
|||
|
dataGridViewBooks.DataSource = _bookRepository.ReadBooks();
|
|||
|
}
|
|||
|
|
|||
|
private bool TryGetIdentifierFromSelectedRow(out int id)
|
|||
|
{
|
|||
|
id = 0;
|
|||
|
if (dataGridViewBooks.SelectedRows.Count < 1)
|
|||
|
{
|
|||
|
MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
id = Convert.ToInt32(dataGridViewBooks.SelectedRows[0].Cells["Id"].Value);
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
_container.Resolve<FBook>().ShowDialog();
|
|||
|
LoadList();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void buttonUpdate_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var form = _container.Resolve<FBook>();
|
|||
|
form.Id = findId;
|
|||
|
form.ShowDialog();
|
|||
|
LoadList();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void buttonRemove_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
_bookRepository.DeleteBook(findId);
|
|||
|
LoadList();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void FBooks_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
LoadList();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|