using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; using CarRepairShopContracts.SearchModels; using CarRepairShopDataModels.Models; using Microsoft.Extensions.Logging; namespace CarRepairShopView { public partial class FormShop : Form { private readonly ILogger _logger; private readonly IShopLogic _logic; private int? _id; private Dictionary _shopRepairs; public int Id { set { _id = value; } } public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; _shopRepairs = new Dictionary(); } private void FormShop_Load(object sender, EventArgs e) { if (_id.HasValue) { _logger.LogInformation("Shop loading"); try { var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value }); if (view != null) { textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address; textBoxMaximum.Text = view.RepairMaxAmount.ToString(); dateTimePicker.Value = view.DateOpening; _shopRepairs = view.ShopRepairs ?? new Dictionary(); LoadData(); } } catch (Exception ex) { _logger.LogError(ex, "Shop loading error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void LoadData() { _logger.LogInformation("Shop repairs loading"); try { if (_shopRepairs != null) { dataGridView.Rows.Clear(); foreach (var repair in _shopRepairs) { dataGridView.Rows.Add(new object[] { repair.Key, repair.Value.Item1.RepairName, repair.Value.Item2 }); } } } catch (Exception ex) { _logger.LogError(ex, "Shop repairs loading 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(textBoxAddress.Text)) { MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(dateTimePicker.Text)) { MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxMaximum.Text)) { MessageBox.Show("Заполните максимальное количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Shop saving"); try { var model = new ShopBindingModel { Id = _id ?? 0, ShopName = textBoxName.Text, Address = textBoxAddress.Text, DateOpening = dateTimePicker.Value, RepairMaxAmount = Convert.ToInt32(textBoxMaximum.Text), ShopRepairs = _shopRepairs }; 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, "Shop saving error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void ButtonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } } }