88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using ProjectLibrary.Repositories;
|
|
using Unity;
|
|
|
|
namespace ProjectLibrary.Forms
|
|
{
|
|
public partial class FOrders : Form
|
|
{
|
|
private readonly IUnityContainer _container;
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
public FOrders(IUnityContainer container, IOrderRepository orderRepository)
|
|
{
|
|
InitializeComponent();
|
|
_container = container ?? throw new ArgumentNullException(nameof(container));
|
|
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
|
}
|
|
|
|
private void LoadList()
|
|
{
|
|
dataGridViewOrders.DataSource = _orderRepository.ReadOrders();
|
|
dataGridViewOrders.Columns["Id"].Visible = false;
|
|
dataGridViewOrders.Columns["OrderDate"].DefaultCellStyle.Format = "dd.MM.yyyy";
|
|
dataGridViewOrders.Columns["ReturnDate"].DefaultCellStyle.Format = "dd.MM.yyyy";
|
|
}
|
|
|
|
private void buttonAdd_Click(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
_container.Resolve<FOrder>().ShowDialog();
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при добавлении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private void buttonRemove_Click(object sender, EventArgs e)
|
|
{
|
|
if (!TryGetIdentifierFromSelectedRow(out var findId))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (MessageBox.Show("Удалить запись?", "Удаление", MessageBoxButtons.YesNo) != DialogResult.Yes)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
_orderRepository.DeleteOrder(findId);
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при удалении", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private void FOrders_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
LoadList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show(ex.Message, "Ошибка при загрузке", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
}
|
|
}
|