diff --git a/Confectionery/FormAddPastryInShop.cs b/Confectionery/FormAddPastryInShop.cs index e5c786b..362584b 100644 --- a/Confectionery/FormAddPastryInShop.cs +++ b/Confectionery/FormAddPastryInShop.cs @@ -1,4 +1,5 @@ -using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.ViewModels; using Microsoft.Extensions.Logging; using System; @@ -17,33 +18,84 @@ namespace ConfectioneryView public partial class FormAddPastryInShop : Form { private readonly ILogger _logger; + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; private readonly List? _listShops; private readonly List? _listPastries; public FormAddPastryInShop(ILogger logger, IShopLogic shopLogic, IPastryLogic pastryLogic) { InitializeComponent(); + _shopLogic = shopLogic; + _pastryLogic = pastryLogic; _logger = logger; _listShops = shopLogic.ReadList(null); if (_listShops != null) { - comboBoxShop.DisplayMember = "ComponentName"; + comboBoxShop.DisplayMember = "Name"; comboBoxShop.ValueMember = "Id"; comboBoxShop.DataSource = _listShops; comboBoxShop.SelectedItem = null; } - _listPastries + _listPastries = pastryLogic.ReadList(null); + if (_listPastries != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember= "Id"; + comboBoxPastry.DataSource = _listPastries; + comboBoxPastry.SelectedItem = null; + } } private void ButtonSave_Click(object sender, EventArgs e) { - + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Добавление изделия в магазин"); + try + { + var pastry = _pastryLogic.ReadElement(new() + { + Id = (int)comboBoxPastry.SelectedValue + }); + if (pastry == null) + { + throw new Exception("Не найдено изделие. Дополнительная информация в логах."); + } + var resultOperation = _shopLogic.AddPastry( + model: new() { Id = (int)comboBoxShop.SelectedValue }, + pastry: pastry, + count: (int)numericUpDownCount.Value + ); + if (!resultOperation) + { + throw new Exception("Ошибка при добавлении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void ButtonCancel_Click(object sender, EventArgs e) { - + DialogResult = DialogResult.Cancel; + Close(); } } } diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 190dc44..ca969bd 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -32,13 +32,14 @@ this.справочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.pastryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.componentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ShopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); - this.ShopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.buttonAddPastryInShop = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -77,6 +78,13 @@ this.componentToolStripMenuItem.Text = "Компоненты"; this.componentToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); // + // ShopsToolStripMenuItem + // + this.ShopsToolStripMenuItem.Name = "ShopsToolStripMenuItem"; + this.ShopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.ShopsToolStripMenuItem.Text = "Магазины"; + this.ShopsToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click); + // // dataGridView // this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -144,18 +152,22 @@ this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.ButtonRef_Click); // - // ShopsToolStripMenuItem + // buttonAddPastryInShop // - this.ShopsToolStripMenuItem.Name = "ShopsToolStripMenuItem"; - this.ShopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.ShopsToolStripMenuItem.Text = "Магазины"; - this.ShopsToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; + this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); + this.buttonAddPastryInShop.TabIndex = 7; + this.buttonAddPastryInShop.Text = "Пополнение магазина"; + this.buttonAddPastryInShop.UseVisualStyleBackColor = true; + this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(783, 380); + this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); @@ -187,5 +199,6 @@ private ToolStripMenuItem pastryToolStripMenuItem; private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; + private Button buttonAddPastryInShop; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 489ec5c..92e771a 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -30,8 +30,8 @@ namespace ConfectioneryView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - /*dataGridView.Columns["PastryName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill;*/ + dataGridView.Columns["PastryName"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation(" "); } @@ -158,7 +158,15 @@ namespace ConfectioneryView if (service is FormViewShops form) { form.ShowDialog(); - LoadData(); + } + } + + private void ButtonAddPastryInShop_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddPastryInShop)); + if (service is FormAddPastryInShop form) + { + form.ShowDialog(); } } } diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index d3d5709..645885a 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,4 +1,5 @@ using AbstractShopListImplement.Implements; +using ConfectioneryBusinessLogic; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; @@ -38,9 +39,11 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -48,6 +51,8 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file