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..9b640b0 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() { - 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; } @@ -77,22 +64,18 @@ namespace ConfectioneryView { 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(); + 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("Загрузка магазинов"); } @@ -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(); + 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.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)