From 8bca5845e52a103474030b2312daf99009112e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 6 Feb 2023 01:31:42 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=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 --- ConfectionaryListImplement/Shop.cs | 3 +- Confectionery/FormShop.Designer.cs | 1 + Confectionery/FormShop.cs | 73 +++++++++++-------------- Confectionery/FormViewShops.Designer.cs | 1 + Confectionery/FormViewShops.cs | 18 ++---- 5 files changed, 40 insertions(+), 56 deletions(-) diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs index c1a7630..fcce77b 100644 --- a/ConfectionaryListImplement/Shop.cs +++ b/ConfectionaryListImplement/Shop.cs @@ -52,7 +52,8 @@ namespace ConfectioneryListImplement Id = Id, Name = Name, Address = Address, - Pastries = Pastries + Pastries = Pastries, + DateOpening = DateOpening, }; } } diff --git a/Confectionery/FormShop.Designer.cs b/Confectionery/FormShop.Designer.cs index 15049d1..694a22f 100644 --- a/Confectionery/FormShop.Designer.cs +++ b/Confectionery/FormShop.Designer.cs @@ -163,6 +163,7 @@ this.Controls.Add(this.label1); this.Name = "FormShop"; this.Text = "Просмотр изделий магазина"; + this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index 1b2c9f9..b29d8b4 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -23,34 +23,21 @@ namespace ConfectioneryView private readonly List? _listShops; private readonly IShopLogic _logic; private readonly ILogger _logger; - private IShopModel? _currentShopModel; public int Id { - get - { - return Convert.ToInt32(comboBoxShop.SelectedValue); - } - set - { - comboBoxShop.SelectedValue = value; - } - } - public IShopModel? ShopModel - { - get => _currentShopModel; + get; set; } - private IShopModel? GetShop() + private IShopModel? GetShop(int id) { - var list = _logic.ReadList(null); - if (list == null) + if (_listShops == null) { return null; } - foreach (var elem in list) + foreach (var elem in _listShops) { - if (elem.Id == this.Id) + if (elem.Id == id) { return elem; } @@ -73,26 +60,22 @@ namespace ConfectioneryView } } - private void LoadData() + private void LoadData(bool extendDate = true) { try { - var currentShop = _logic.ReadElement(new() { Id = this.Id}); - if (currentShop != null) - { - var vmodel = GetShop(); - if (vmodel != null) - { - comboBoxShop.Text = vmodel.Name; - textBoxAddress.Text = vmodel.Address; - textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening); - } + var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue)); + if (model != null) + { + comboBoxShop.Text = model.Name; + textBoxAddress.Text = model.Address; + textBoxDateOpening.Text = Convert.ToString(model.DateOpening); + dataGridView.Rows.Clear(); - foreach (var el in currentShop.Pastries.Values) + foreach (var el in model.Pastries.Values) { dataGridView.Rows.Add(new object[]{el.Item1.PastryName, el.Item1.Price, el.Item2 }); } - } _logger.LogInformation("Загрузка магазинов"); } @@ -106,7 +89,7 @@ namespace ConfectioneryView private void ComboBoxShop_SelectedIndexChanged(object sender, EventArgs e) { - LoadData(); + LoadData(false); } private void ButtonSave_Click(object sender, EventArgs e) @@ -126,13 +109,6 @@ namespace ConfectioneryView _logger.LogInformation("Сохранение изделия"); try { - var vmodel = GetShop(); - if (vmodel != null) - { - _currentShopModel = vmodel; - return; - } - // Создаем новый магазин если не нашли такого DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); ShopBindingModel model = new() { @@ -140,8 +116,18 @@ namespace ConfectioneryView Address = textBoxAddress.Text, DateOpening = dateTime }; - var operationResult = _logic.Create(model); - _currentShopModel = model; + var vmodel = GetShop(Id); + bool operationResult = false; + + if (vmodel != null) + { + model.Id = vmodel.Id; + operationResult = _logic.Update(model); + } + else + { + operationResult = _logic.Create(model); + } if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -163,5 +149,10 @@ namespace ConfectioneryView DialogResult = DialogResult.Cancel; Close(); } + + private void FormShop_Load(object sender, EventArgs e) + { + LoadData(); + } } } diff --git a/Confectionery/FormViewShops.Designer.cs b/Confectionery/FormViewShops.Designer.cs index 82f254a..894b8f9 100644 --- a/Confectionery/FormViewShops.Designer.cs +++ b/Confectionery/FormViewShops.Designer.cs @@ -104,6 +104,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormViewShops"; this.Text = "Просмотр магазинов"; + this.Load += new System.EventHandler(this.FormShops_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/Confectionery/FormViewShops.cs b/Confectionery/FormViewShops.cs index d7fa00a..419130c 100644 --- a/Confectionery/FormViewShops.cs +++ b/Confectionery/FormViewShops.cs @@ -26,7 +26,9 @@ namespace ConfectioneryView if (list != null) { dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Pastries"].Visible = false; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } @@ -57,24 +59,12 @@ namespace ConfectioneryView var service = Program.ServiceProvider?.GetService(typeof(FormShop)); if (service is FormShop form) { - var id = form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { - var model = form.ShopModel; - if (model != null) - { - _logic.Update(new() - { - Id = id, - Address = model.Address, - Name = model.Name, - DateOpening= model.DateOpening, - }); - } - + LoadData(); } } - LoadData(); } } private void ButtonDel_Click(object sender, EventArgs e)