From 372ebcf2f087f7821a01788bddaabd2075f3bc60 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Sun, 26 Mar 2023 14:15:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormStore.Designer.cs | 1 - .../SoftwareInstallation/FormStore.cs | 110 +++++++----------- 2 files changed, 44 insertions(+), 67 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs index 39d097d..cd3037f 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormStore.Designer.cs @@ -80,7 +80,6 @@ this.NameComboBox.Name = "NameComboBox"; this.NameComboBox.Size = new System.Drawing.Size(174, 23); this.NameComboBox.TabIndex = 3; - this.NameComboBox.SelectedIndexChanged += new System.EventHandler(this.NameComboBox_SelectedIndexChanged); // // AdressTextBox // diff --git a/SoftwareInstallation/SoftwareInstallation/FormStore.cs b/SoftwareInstallation/SoftwareInstallation/FormStore.cs index a4058b4..dfbbba5 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormStore.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormStore.cs @@ -1,47 +1,28 @@ using Microsoft.Extensions.Logging; using SofrwareInstallationContracts.BindingModels; using SofrwareInstallationContracts.BusinessLogicsContracts; +using SofrwareInstallationContracts.SearchModels; using SofrwareInstallationContracts.ViewModels; using SoftwareInstallationDataModels.Models; +using System.Reflection; +using System.Windows.Forms; namespace SoftwareInstallationView { public partial class FormStore : Form { - private readonly List? _listStores; private readonly IStoreLogic _logic; private readonly ILogger _logger; - public int Id { get; set; } + private int? _id; + private Dictionary _listStores; + public int Id { set { _id = value; } } public FormStore(ILogger logger, IStoreLogic logic) { InitializeComponent(); _logger = logger; - _listStores = logic.ReadList(null); + _listStores = new(); _logic = logic; - if (_listStores != null) - { - NameComboBox.DisplayMember = "StoreName"; - NameComboBox.ValueMember = "Id"; - NameComboBox.DataSource = _listStores; - NameComboBox.SelectedItem = null; - } - } - - private IStoreModel? GetStore(int id) - { - if (_listStores == null) - { - return null; - } - foreach (var elem in _listStores) - { - if (elem.Id == id) - { - return elem; - } - } - return null; } private void SaveButton_Click(object sender, EventArgs e) @@ -51,50 +32,34 @@ namespace SoftwareInstallationView MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - if (string.IsNullOrEmpty(AdressTextBox.Text)) { MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _logger.LogInformation("Сохранение магазина"); - try { - DateTime.TryParse(OpeningDatePicker.Text, out var dateTime); - StoreBindingModel model = new() + var model = new StoreBindingModel { + Id = _id ?? 0, StoreName = NameComboBox.Text, StoreAdress = AdressTextBox.Text, - OpeningDate = dateTime, + OpeningDate = OpeningDatePicker.Value.Date, PackageMaxCount = (int)VolumeNumericUpDown.Value }; - var vmodel = GetStore(Id); - bool operationResult = false; - - if (vmodel != null) - { - model.Id = vmodel.Id; - operationResult = _logic.Update(model); - } - else - { - operationResult = _logic.Create(model); - } - + 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, "Ошибка сохранения изделия"); + _logger.LogError(ex, "Ошибка сохранения магазина"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -107,39 +72,52 @@ namespace SoftwareInstallationView private void FormStore_Load(object sender, EventArgs e) { - LoadData(); + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var view = _logic.ReadElement(new StoreSearchModel + { + Id = _id.Value + }); + if (view != null) + { + NameComboBox.Text = view.StoreName; + AdressTextBox.Text = view.StoreAdress; + OpeningDatePicker.Text = view.OpeningDate.ToString(); + VolumeNumericUpDown.Value = view.PackageMaxCount; + _listStores = view.StorePackages ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } } private void LoadData(bool extendDate = true) { + _logger.LogInformation("Загрузка изделий магазина"); try { - var model = GetStore(extendDate ? Id : Convert.ToInt32(NameComboBox.SelectedValue)); - if (model != null) + if (_listStores != null) { - NameComboBox.Text = model.StoreName; - AdressTextBox.Text = model.StoreAdress; - OpeningDatePicker.Text = Convert.ToString(model.OpeningDate); - VolumeNumericUpDown.Value = model.PackageMaxCount; DataGridView.Rows.Clear(); - foreach (var el in model.StorePackages.Values) + foreach (var elem in _listStores) { - DataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 }); + DataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.PackageName, elem.Value.Item2 }); } } - _logger.LogInformation("Загрузка магазинов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки магазинов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + _logger.LogError(ex, "Ошибка загрузки изделий магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - - private void NameComboBox_SelectedIndexChanged(object sender, EventArgs e) - { - LoadData(false); - } - } }