using GasStation.Entities; using GasStation.Repositories; namespace GasStation.Forms { public partial class FormSelling : Form { private readonly ISellingRepository _sellingRepository; public FormSelling(ISellingRepository sellingRepository, IGasmanRepository gasmanRepository, IProductRepository productRepository) { InitializeComponent(); _sellingRepository = sellingRepository ?? throw new ArgumentNullException(nameof(sellingRepository)); comboBoxGasman.DataSource = gasmanRepository.ReadGasman(); comboBoxGasman.DisplayMember = "GasmanName"; comboBoxGasman.ValueMember = "ID"; ColumnProduct.DataSource = productRepository.ReadProduct(); ColumnProduct.DisplayMember = "ProductType"; ColumnProduct.ValueMember = "ID"; } private void ButtonSave_Click(object sender, EventArgs e) { try { if (comboBoxGasman.SelectedIndex < 0 || dataGridViewSelling.RowCount < 1) { throw new Exception("Имеются незаполненые поля"); } _sellingRepository.CreateSelling(Selling.CreateSelling(0, (int)comboBoxGasman.SelectedValue!, 0, CreateProductSellingsFromDataGrid())); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private List CreateProductSellingsFromDataGrid() { var list = new List(); foreach (DataGridViewRow row in dataGridViewSelling.Rows) { if (row.Cells["ColumnProduct"].Value == null || row.Cells["ColumnCount"].Value == null) { continue; } list.Add(ProductSelling.CreateSelling(0, Convert.ToInt32(row.Cells["ColumnProduct"].Value), Convert.ToInt32(row.Cells["ColumnCount"].Value))); } return list.GroupBy(x => x.ProductID, x => x.Count, (id, counts) => ProductSelling.CreateSelling(0, id,counts.Sum())).ToList(); ; } } }