using Publication.Entites; using Publication.Repositories; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Publication.Forms; public partial class FormPrintingHouse : Form { private readonly IPrintingHouseRepository printingHouseRepository; public FormPrintingHouse(IPrintingHouseRepository _printingHouseRepository, IMaterialRepository materialRepository, IOrderRepository orderRepository) { InitializeComponent(); printingHouseRepository = _printingHouseRepository ?? throw new ArgumentNullException(nameof(_printingHouseRepository)); comboBoxMaterials.DataSource = materialRepository.ReadMaterials(); comboBoxMaterials.DisplayMember = "Material"; comboBoxMaterials.ValueMember = "Id"; ColumnOrders.DataSource = orderRepository.ReadOrders(); ColumnOrders.DisplayMember = "Id"; ColumnOrders.ValueMember = "Id"; } private void ButtonSave_Click(object sender, EventArgs e) { try { if (dataGridView1.RowCount < 1 || comboBoxMaterials.SelectedIndex < 0) { throw new Exception("Имеются незаполненные поля"); } printingHouseRepository.CreatePrintingHouse ( PrintingHouses.CreateEntity ( 1, textBoxTitle.Text, textBoxPhone.Text, textBoxAddress.Text, (int)comboBoxMaterials.SelectedValue!, CreateListPrintingHouseOrdersFromDataGrid() ) ); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private IEnumerable CreateListPrintingHouseOrdersFromDataGrid() { var list = new List(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells["ColumnOrders"].Value == null || row.Cells["ColumnCopy"].Value == null) { continue; } list.Add ( PrintingHouseOrders.CreateEntity ( 0, Convert.ToInt32(row.Cells["ColumnOrders"].Value), Convert.ToInt32(row.Cells["ColumnCopy"].Value) ) ); } return list.GroupBy(x => x.OrderId, x => x.Count, (id, counts) => PrintingHouseOrders.CreateEntity(0, id, counts.Sum())).ToList(); } private void ButtonBreak_Click(object sender, EventArgs e) => Close(); }