using MotorPlantContracts.BusinessLogicsContracts; using MotorPlantContracts.SearchModels; using MotorPlantContracts.VeiwModels; using MotorPlantContracts.ViewModels; using MotorPlantDataModels.Models; namespace MotorPlantView { public partial class FormSupply : Form { private readonly List? _engineList; private readonly List? _shopsList; IShopLogic _shopLogic; IEngineLogic _engineLogic; public int ShopId { get { return Convert.ToInt32(ShopComboBox.SelectedValue); } set { ShopComboBox.SelectedValue = value; } } public int EngineId { get { return Convert.ToInt32(EngineComboBox.SelectedValue); } set { EngineComboBox.SelectedValue = value; } } public IEngineModel? EngineModel { get { if (_engineList == null) { return null; } foreach (var elem in _engineList) { if (elem.Id == EngineId) { return elem; } } return null; } } public int Count { get { return Convert.ToInt32(CountTextBox.Text); } set { CountTextBox.Text = value.ToString(); } } public FormSupply(IEngineLogic engineLogic, IShopLogic shopLogic) { InitializeComponent(); _shopLogic = shopLogic; _engineLogic = engineLogic; _engineList = engineLogic.ReadList(null); _shopsList = shopLogic.ReadList(null); if (_engineList != null) { EngineComboBox.DisplayMember = "EngineName"; EngineComboBox.ValueMember = "Id"; EngineComboBox.DataSource = _engineList; EngineComboBox.SelectedItem = null; } if (_shopsList != null) { ShopComboBox.DisplayMember = "ShopName"; ShopComboBox.ValueMember = "Id"; ShopComboBox.DataSource = _shopsList; ShopComboBox.SelectedItem = null; } } private void SaveButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(CountTextBox.Text)) { MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (EngineComboBox.SelectedValue == null) { MessageBox.Show("Выберите двигатель", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (ShopComboBox.SelectedValue == null) { MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { int count = Convert.ToInt32(CountTextBox.Text); bool res = _shopLogic.MakeSupply( new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) }, _engineLogic.ReadElement(new() { Id = Convert.ToInt32(EngineComboBox.SelectedValue) }), count ); if (!res) { throw new Exception("Ошибка при пополнении. Дополнительная информация в логах"); } MessageBox.Show("Пополнение прошло успешно"); DialogResult = DialogResult.OK; Close(); } catch (Exception er) { MessageBox.Show("Ошибка пополнения"); return; } } private void CancelButton_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }