103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using ProjectLibrary.Repositories;
|
|
using Unity;
|
|
|
|
namespace ProjectLibrary.Forms
|
|
{
|
|
public partial class FLibraries : Form
|
|
{
|
|
private readonly IUnityContainer _container;
|
|
private readonly ILibraryRepository _libraryRepository;
|
|
|
|
public FLibraries(IUnityContainer container, ILibraryRepository libraryRepository)
|
|
{
|
|
InitializeComponent();
|
|
_container = container ?? throw new ArgumentNullException(nameof(container));
|
|
_libraryRepository = libraryRepository ?? throw new ArgumentNullException(nameof(libraryRepository));
|
|
}
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
_container.Resolve<FLibrary>().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<FLibrary>();
|
|
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
|
|
{
|
|
_libraryRepository.DeleteLibrary(findId);
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void FLibraries_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
private void LoadList()
|
|
{
|
|
dataGridViewOrders.DataSource = _libraryRepository.ReadLibraries();
|
|
}
|
|
private bool TryGetIdentifierFromSelectedRow(out int id)
|
|
{
|
|
id = 0;
|
|
if (dataGridViewOrders.SelectedRows.Count < 1)
|
|
{
|
|
MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
return false;
|
|
}
|
|
|
|
id = Convert.ToInt32(dataGridViewOrders.SelectedRows[0].Cells["Id"].Value);
|
|
return true;
|
|
}
|
|
}
|
|
}
|