diff --git a/PrecastConcretePlant/PrecastConcretePlant/FormShop.Designer.cs b/PrecastConcretePlant/PrecastConcretePlant/FormShop.Designer.cs index d74a4af..ca6cec6 100644 --- a/PrecastConcretePlant/PrecastConcretePlant/FormShop.Designer.cs +++ b/PrecastConcretePlant/PrecastConcretePlant/FormShop.Designer.cs @@ -49,20 +49,19 @@ // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(12, 9); + this.label1.Location = new System.Drawing.Point(14, 12); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(116, 15); + this.label1.Size = new System.Drawing.Size(150, 20); this.label1.TabIndex = 0; this.label1.Text = "Название магазина:"; // // comboBoxShop // - this.comboBoxShop.FormattingEnabled = true; - this.comboBoxShop.Location = new System.Drawing.Point(12, 27); + this.comboBoxShop.Location = new System.Drawing.Point(14, 39); + this.comboBoxShop.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.comboBoxShop.Name = "comboBoxShop"; - this.comboBoxShop.Size = new System.Drawing.Size(141, 23); - this.comboBoxShop.TabIndex = 1; - this.comboBoxShop.SelectedIndexChanged += new System.EventHandler(this.ComboBoxShop_SelectedIndexChanged); + this.comboBoxShop.Size = new System.Drawing.Size(138, 28); + this.comboBoxShop.TabIndex = 12; // // dataGridView // @@ -74,59 +73,68 @@ this.PastryName, this.Price, this.Count}); - this.dataGridView.Location = new System.Drawing.Point(12, 116); + this.dataGridView.Location = new System.Drawing.Point(14, 155); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(583, 249); + this.dataGridView.Size = new System.Drawing.Size(666, 332); this.dataGridView.TabIndex = 2; // // PastryName // this.PastryName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; this.PastryName.HeaderText = "Имя изделия"; + this.PastryName.MinimumWidth = 6; this.PastryName.Name = "PastryName"; // // Price // this.Price.HeaderText = "Цена"; + this.Price.MinimumWidth = 6; this.Price.Name = "Price"; + this.Price.Width = 125; // // Count // this.Count.HeaderText = "Количество"; + this.Count.MinimumWidth = 6; this.Count.Name = "Count"; + this.Count.Width = 125; // // label2 // this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(159, 9); + this.label2.Location = new System.Drawing.Point(182, 12); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(97, 15); + this.label2.Size = new System.Drawing.Size(124, 20); this.label2.TabIndex = 3; this.label2.Text = "Адрес магазина:"; // // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(386, 9); + this.label3.Location = new System.Drawing.Point(441, 12); this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(100, 15); + this.label3.Size = new System.Drawing.Size(126, 20); this.label3.TabIndex = 4; this.label3.Text = "Время открытия:"; // // textBoxAddress // - this.textBoxAddress.Location = new System.Drawing.Point(159, 27); + this.textBoxAddress.Location = new System.Drawing.Point(182, 36); + this.textBoxAddress.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.textBoxAddress.Name = "textBoxAddress"; - this.textBoxAddress.Size = new System.Drawing.Size(221, 23); + this.textBoxAddress.Size = new System.Drawing.Size(252, 27); this.textBoxAddress.TabIndex = 5; // // buttonCancel // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCancel.Location = new System.Drawing.Point(492, 371); + this.buttonCancel.Location = new System.Drawing.Point(562, 495); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(103, 23); + this.buttonCancel.Size = new System.Drawing.Size(118, 31); this.buttonCancel.TabIndex = 7; this.buttonCancel.Text = "Отмена"; this.buttonCancel.UseVisualStyleBackColor = true; @@ -135,9 +143,10 @@ // buttonSave // this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonSave.Location = new System.Drawing.Point(366, 372); + this.buttonSave.Location = new System.Drawing.Point(418, 496); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.buttonSave.Name = "buttonSave"; - this.buttonSave.Size = new System.Drawing.Size(120, 22); + this.buttonSave.Size = new System.Drawing.Size(137, 29); this.buttonSave.TabIndex = 8; this.buttonSave.Text = "Сохранить"; this.buttonSave.UseVisualStyleBackColor = true; @@ -145,32 +154,34 @@ // // textBoxDateOpening // - this.textBoxDateOpening.Location = new System.Drawing.Point(386, 27); + this.textBoxDateOpening.Location = new System.Drawing.Point(441, 36); + this.textBoxDateOpening.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.textBoxDateOpening.Name = "textBoxDateOpening"; - this.textBoxDateOpening.Size = new System.Drawing.Size(200, 23); + this.textBoxDateOpening.Size = new System.Drawing.Size(228, 27); this.textBoxDateOpening.TabIndex = 9; // // ReinforcedMaxCountLable // this.ReinforcedMaxCountLable.AutoSize = true; - this.ReinforcedMaxCountLable.Location = new System.Drawing.Point(12, 53); + this.ReinforcedMaxCountLable.Location = new System.Drawing.Point(14, 71); this.ReinforcedMaxCountLable.Name = "ReinforcedMaxCountLable"; - this.ReinforcedMaxCountLable.Size = new System.Drawing.Size(140, 15); + this.ReinforcedMaxCountLable.Size = new System.Drawing.Size(177, 20); this.ReinforcedMaxCountLable.TabIndex = 10; this.ReinforcedMaxCountLable.Text = "Вместимость магазина: "; // // VolumeNumericUpDown // - this.VolumeNumericUpDown.Location = new System.Drawing.Point(12, 71); + this.VolumeNumericUpDown.Location = new System.Drawing.Point(14, 95); + this.VolumeNumericUpDown.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.VolumeNumericUpDown.Name = "VolumeNumericUpDown"; - this.VolumeNumericUpDown.Size = new System.Drawing.Size(141, 23); + this.VolumeNumericUpDown.Size = new System.Drawing.Size(161, 27); this.VolumeNumericUpDown.TabIndex = 11; // // FormShop // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(607, 405); + this.ClientSize = new System.Drawing.Size(694, 540); this.Controls.Add(this.VolumeNumericUpDown); this.Controls.Add(this.ReinforcedMaxCountLable); this.Controls.Add(this.textBoxDateOpening); @@ -182,6 +193,7 @@ this.Controls.Add(this.dataGridView); this.Controls.Add(this.comboBoxShop); this.Controls.Add(this.label1); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.Name = "FormShop"; this.Text = "Просмотр изделий магазина"; this.Load += new System.EventHandler(this.FormShop_Load); diff --git a/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs b/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs index fa9a530..17af0d7 100644 --- a/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs +++ b/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.BusinessLogicsContracts; +using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantDataModels.Models; using System; @@ -19,127 +20,75 @@ namespace ConfectioneryView { public partial class FormShop : Form { - private readonly List? _listShops; private readonly IShopLogic _logic; private readonly ILogger _logger; - - public int Id - { - get; set; - } - - private IShopModel? GetShop(int id) - { - if (_listShops == null) - { - return null; - } - foreach (var elem in _listShops) - { - if (elem.Id == id) - { - return elem; - } - } - return null; - } - + private int? _id; + private Dictionary _listShops; + public int Id { set { _id = value; } } public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; - _listShops = logic.ReadList(null); + _listShops = new(); _logic = logic; - if (_listShops != null) - { - comboBoxShop.DisplayMember = "Name"; - comboBoxShop.ValueMember = "Id"; - comboBoxShop.DataSource = _listShops; - comboBoxShop.SelectedItem = null; - } } private void LoadData(bool extendDate = true) { + _logger.LogInformation("Загрузка изделий магазина"); try { - 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); - VolumeNumericUpDown.Value = model.ReinforcedMaxCount; + if (_listShops != null) + { dataGridView.Rows.Clear(); - foreach (var el in model.ShopReinforcedies.Values) + foreach (var elem in _listShops) { - dataGridView.Rows.Add(new object[]{el.Item1.ReinforcedName, el.Item1.Price, el.Item2 }); + dataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.ReinforcedName, 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 ComboBoxShop_SelectedIndexChanged(object sender, EventArgs e) - { - LoadData(false); - } - private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(comboBoxShop.Text)) { - MessageBox.Show("Заполните название", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxAddress.Text)) { - MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Сохранение магазина"); try { - DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); - ShopBindingModel model = new() + var model = new ShopBindingModel { + Id = _id ?? 0, Name = comboBoxShop.Text, Address = textBoxAddress.Text, - DateOpening = dateTime, + DateOpening = textBoxDateOpening.Value.Date, ReinforcedMaxCount = (int)VolumeNumericUpDown.Value }; - var vmodel = GetShop(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); + 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); } } @@ -152,7 +101,32 @@ namespace ConfectioneryView private void FormShop_Load(object sender, EventArgs e) { - LoadData(); + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var view = _logic.ReadElement(new ShopSearchModel + { + Id = _id.Value + }); + if (view != null) + { + comboBoxShop.Text = view.Name; + textBoxAddress.Text = view.Address; + textBoxDateOpening.Text = view.DateOpening.ToString(); + VolumeNumericUpDown.Value = view.ReinforcedMaxCount; + _listShops = view.ShopReinforcedies ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } } } }