using Microsoft.Extensions.Logging; using MotorPlantContracts.BindingModels; using MotorPlantContracts.BusinessLogicsContracts; using MotorPlantContracts.SearchModels; using MotorPlantDataModels.Models; 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 MotorPlantView { public partial class FormShop : Form { private readonly ILogger _logger; private readonly IShopLogic _logic; public int? _id; private Dictionary _engine; public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; } private void ShopForm_Load(object sender, EventArgs e) { if (_id.HasValue) { _logger.LogInformation("Shop load"); try { var shop = _logic.ReadElement(new ShopSearchModel { Id = _id }); if (shop != null) { textBoxName.Text = shop.ShopName; textBoxAdress.Text = shop.Adress; dateTimePicker.Text = shop.DateOpen.ToString(); _engine = shop.ShopEngines; } LoadData(); } catch (Exception ex) { _logger.LogError(ex, "Load shop error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void LoadData() { _logger.LogInformation("Load engines"); try { if (_engine != null) { foreach (var engine in _engine) { dataGridView.Rows.Add(new object[] { engine.Key, engine.Value.Item1.EngineName, engine.Value.Item1.Price, engine.Value.Item2 }); } } } catch (Exception ex) { _logger.LogError(ex, "Load engines into shop error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxName.Text)) { MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxAdress.Text)) { MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Save shop"); try { var model = new ShopBindingModel { Id = _id ?? 0, ShopName = textBoxName.Text, Adress = textBoxAdress.Text, DateOpen = dateTimePicker.Value.Date, ShopEngines = _engine }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { _logger.LogError(ex, "Save shop error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }