ProjectLib/ProjectLibrary/Forms/FTiclet_Extensions.cs

80 lines
2.6 KiB
C#

using ProjectLibrary.Repositores;
using ProjectLibrary.Repositories;
using Unity;
namespace ProjectLibrary.Forms
{
public partial class FTiclet_Extensions : Form
{
private readonly IUnityContainer _container;
private readonly ITicketExtensionsRepository _ticketRepository;
public FTiclet_Extensions(IUnityContainer container, ITicketExtensionsRepository ticketRepository)
{
InitializeComponent();
_container = container ?? throw new ArgumentNullException(nameof(container));
_ticketRepository = ticketRepository ?? throw new ArgumentNullException(nameof(ticketRepository));
}
private void buttonAdd_Click(object sender, EventArgs e)
{
try
{
_container.Resolve<FTicket_Extension>().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<FTicket_Extension>();
form.Id = findId;
form.ShowDialog();
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при изменении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FTiclet_Extensions_Load(object sender, EventArgs e)
{
try
{
LoadList();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadList()
{
dataGridView.DataSource = _ticketRepository.ReadTicketExtensions();
}
private bool TryGetIdentifierFromSelectedRow(out int id)
{
id = 0;
if (dataGridView.SelectedRows.Count < 1)
{
MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
return true;
}
}
}