ISEbd-22_Rozhkov.I.E._Simple/GasStation/Forms/FormSupply.cs
2024-11-15 18:27:06 +04:00

64 lines
2.2 KiB
C#

using GasStation.Repositories;
using GasStation.Entities;
namespace GasStation.Forms
{
public partial class FormSupply : Form
{
private readonly ISupplyRepository _supplyRepository;
public FormSupply(ISupplyRepository supplyRepository, ISupplierRepository supplierRepository, IProductRepository productRepository)
{
InitializeComponent();
_supplyRepository = supplyRepository ??
throw new ArgumentNullException(nameof(supplyRepository));
comboBoxSupplier.DataSource = supplierRepository.ReadSupplier();
comboBoxSupplier.DisplayMember = "SupplierName";
comboBoxSupplier.ValueMember = "ID";
ColumnProduct.DataSource = productRepository.ReadProduct();
ColumnProduct.DisplayMember = "Cost";
ColumnProduct.ValueMember = "ID";
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (dataGridViewSupply.RowCount < 1 ||
comboBoxSupplier.SelectedIndex < 0)
{
throw new Exception("Имеются незаполненые поля");
}
_supplyRepository.CreateSupply(Supply.CreateSupply(0, (int)comboBoxSupplier.SelectedValue!, 0, CreateListSellingFromDataGrid()));
Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private List<SupplySupply> CreateListSellingFromDataGrid()
{
var list = new List<SupplySupply>();
foreach (DataGridViewRow row in dataGridViewSupply.Rows)
{
if (row.Cells["ColumnProduct"].Value == null || row.Cells["ColumnCount"].Value == null)
{
continue;
}
list.Add(SupplySupply.CreateSupply(0, Convert.ToInt32(row.Cells["ColumnCount"].Value)));
}
return list;
}
}
}