From 4e6da8e751468c1415f98cfe64dec15298ac8b37 Mon Sep 17 00:00:00 2001 From: user Date: Tue, 28 Feb 2023 13:48:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D1=83?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D1=91=D0=BD=D0=BD=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormAddManufacture.Designer.cs | 151 +++++++++++++ .../BlacksmithWorkshop/FormAddManufacture.cs | 128 +++++++++++ .../FormAddManufacture.resx | 60 +++++ .../BlacksmithWorkshop/FormMain.Designer.cs | 66 ++++-- .../BlacksmithWorkshop/FormMain.cs | 22 ++ .../BlacksmithWorkshop/FormShop.Designer.cs | 209 ++++++++++++++++++ .../BlacksmithWorkshop/FormShop.cs | 131 +++++++++++ .../BlacksmithWorkshop/FormShop.resx | 60 +++++ .../BlacksmithWorkshop/FormShops.Designer.cs | 121 ++++++++++ .../BlacksmithWorkshop/FormShops.cs | 117 ++++++++++ .../BlacksmithWorkshop/FormShops.resx | 60 +++++ .../BlacksmithWorkshop/Program.cs | 5 + .../BusinessLogic/ShopLogic.cs | 109 ++++++++- 13 files changed, 1215 insertions(+), 24 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.resx create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShops.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShops.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormShops.resx diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.Designer.cs new file mode 100644 index 0000000..69f6439 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.Designer.cs @@ -0,0 +1,151 @@ +namespace BlacksmithWorkshop +{ + partial class FormAddManufacture + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.labelIceCream = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.labelShop = new System.Windows.Forms.Label(); + this.comboBoxManufacture = new System.Windows.Forms.ComboBox(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.comboBoxShop = new System.Windows.Forms.ComboBox(); + this.SuspendLayout(); + // + // labelIceCream + // + this.labelIceCream.AutoSize = true; + this.labelIceCream.Location = new System.Drawing.Point(28, 58); + this.labelIceCream.Name = "labelIceCream"; + this.labelIceCream.Size = new System.Drawing.Size(56, 15); + this.labelIceCream.TabIndex = 0; + this.labelIceCream.Text = "Изделие:"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(28, 91); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(75, 15); + this.labelCount.TabIndex = 1; + this.labelCount.Text = "Количество:"; + // + // labelShop + // + this.labelShop.AutoSize = true; + this.labelShop.Location = new System.Drawing.Point(28, 23); + this.labelShop.Name = "labelShop"; + this.labelShop.Size = new System.Drawing.Size(57, 15); + this.labelShop.TabIndex = 2; + this.labelShop.Text = "Магазин:"; + // + // comboBoxManufacture + // + this.comboBoxManufacture.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxManufacture.FormattingEnabled = true; + this.comboBoxManufacture.Location = new System.Drawing.Point(149, 55); + this.comboBoxManufacture.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxManufacture.Name = "comboBoxManufacture"; + this.comboBoxManufacture.Size = new System.Drawing.Size(230, 23); + this.comboBoxManufacture.TabIndex = 3; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(149, 88); + this.textBoxCount.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(230, 23); + this.textBoxCount.TabIndex = 4; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(209, 123); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(82, 22); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(297, 123); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(82, 22); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // comboBoxShop + // + this.comboBoxShop.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxShop.FormattingEnabled = true; + this.comboBoxShop.Location = new System.Drawing.Point(149, 23); + this.comboBoxShop.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.comboBoxShop.Name = "comboBoxShop"; + this.comboBoxShop.Size = new System.Drawing.Size(230, 23); + this.comboBoxShop.TabIndex = 8; + // + // FormAddManufacture + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(394, 159); + this.Controls.Add(this.comboBoxShop); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxManufacture); + this.Controls.Add(this.labelShop); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelIceCream); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormAddManufacture"; + this.Text = "Пополнение магазина"; + this.Load += new System.EventHandler(this.FormAddManufacture_Load_1); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelIceCream; + private Label labelCount; + private Label labelShop; + private ComboBox comboBoxManufacture; + private TextBox textBoxCount; + private Button buttonSave; + private Button buttonCancel; + private ComboBox comboBoxShop; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.cs new file mode 100644 index 0000000..1016285 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.cs @@ -0,0 +1,128 @@ +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BlacksmithWorkshop +{ + public partial class FormAddManufacture : Form + { + private readonly ILogger _logger; + private readonly IManufactureLogic _logicI; + private readonly IShopLogic _logicS; + + public FormAddManufacture(ILogger logger, IManufactureLogic logicI, IShopLogic logicS) + { + InitializeComponent(); + _logger = logger; + _logicI = logicI; + _logicS = logicS; + } + + private void FormAddManufacture_Load_1(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка списка изделий для пополнения"); + + try + { + var list = _logicI.ReadList(null); + + if (list != null) + { + comboBoxManufacture.DisplayMember = "ManufactureName"; + comboBoxManufacture.ValueMember = "Id"; + comboBoxManufacture.DataSource = list; + comboBoxManufacture.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка изделий"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + _logger.LogInformation("Загрузка списка магазинов для пополнения"); + try + { + var list = _logicS.ReadList(null); + + if (list != null) + { + comboBoxShop.DisplayMember = "ShopName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = list; + comboBoxShop.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (comboBoxManufacture.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Пополнение магазина"); + + try + { + var operationResult = _logicS.AddManufacture(new ShopSearchModel + { + Id = Convert.ToInt32(comboBoxShop.SelectedValue) + }, + + _logicI.ReadElement(new ManufactureSearchModel() + { + Id = Convert.ToInt32(comboBoxManufacture.SelectedValue) + })!, Convert.ToInt32(textBoxCount.Text)); + + if (!operationResult) + { + 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/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.resx b/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormAddManufacture.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs index 018e907..6248df9 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs @@ -38,6 +38,8 @@ this.toolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.workPieceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.manufactureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.shopToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.addManufactureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); @@ -45,18 +47,20 @@ // dataGridView // this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.dataGridView.Location = new System.Drawing.Point(12, 36); + this.dataGridView.Location = new System.Drawing.Point(10, 27); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 51; this.dataGridView.RowTemplate.Height = 29; - this.dataGridView.Size = new System.Drawing.Size(937, 402); + this.dataGridView.Size = new System.Drawing.Size(820, 302); this.dataGridView.TabIndex = 0; // // buttonCreateOrder // - this.buttonCreateOrder.Location = new System.Drawing.Point(1014, 66); + this.buttonCreateOrder.Location = new System.Drawing.Point(887, 50); + this.buttonCreateOrder.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonCreateOrder.Name = "buttonCreateOrder"; - this.buttonCreateOrder.Size = new System.Drawing.Size(235, 29); + this.buttonCreateOrder.Size = new System.Drawing.Size(206, 22); this.buttonCreateOrder.TabIndex = 1; this.buttonCreateOrder.Text = "Создать заказ"; this.buttonCreateOrder.UseVisualStyleBackColor = true; @@ -64,9 +68,10 @@ // // buttonTakeOrderInWork // - this.buttonTakeOrderInWork.Location = new System.Drawing.Point(1014, 143); + this.buttonTakeOrderInWork.Location = new System.Drawing.Point(887, 107); + this.buttonTakeOrderInWork.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; - this.buttonTakeOrderInWork.Size = new System.Drawing.Size(235, 29); + this.buttonTakeOrderInWork.Size = new System.Drawing.Size(206, 22); this.buttonTakeOrderInWork.TabIndex = 2; this.buttonTakeOrderInWork.Text = "Отдать на выполнение"; this.buttonTakeOrderInWork.UseVisualStyleBackColor = true; @@ -74,9 +79,10 @@ // // buttonOrderReady // - this.buttonOrderReady.Location = new System.Drawing.Point(1014, 220); + this.buttonOrderReady.Location = new System.Drawing.Point(887, 165); + this.buttonOrderReady.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonOrderReady.Name = "buttonOrderReady"; - this.buttonOrderReady.Size = new System.Drawing.Size(235, 29); + this.buttonOrderReady.Size = new System.Drawing.Size(206, 22); this.buttonOrderReady.TabIndex = 3; this.buttonOrderReady.Text = "Заказ готов"; this.buttonOrderReady.UseVisualStyleBackColor = true; @@ -84,9 +90,10 @@ // // buttonIssuedOrder // - this.buttonIssuedOrder.Location = new System.Drawing.Point(1014, 296); + this.buttonIssuedOrder.Location = new System.Drawing.Point(887, 222); + this.buttonIssuedOrder.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonIssuedOrder.Name = "buttonIssuedOrder"; - this.buttonIssuedOrder.Size = new System.Drawing.Size(235, 29); + this.buttonIssuedOrder.Size = new System.Drawing.Size(206, 22); this.buttonIssuedOrder.TabIndex = 4; this.buttonIssuedOrder.Text = "Заказ выдан"; this.buttonIssuedOrder.UseVisualStyleBackColor = true; @@ -94,9 +101,10 @@ // // buttonRef // - this.buttonRef.Location = new System.Drawing.Point(1014, 369); + this.buttonRef.Location = new System.Drawing.Point(887, 277); + this.buttonRef.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.buttonRef.Name = "buttonRef"; - this.buttonRef.Size = new System.Drawing.Size(235, 29); + this.buttonRef.Size = new System.Drawing.Size(206, 22); this.buttonRef.TabIndex = 5; this.buttonRef.Text = "Обновить"; this.buttonRef.UseVisualStyleBackColor = true; @@ -109,7 +117,8 @@ this.toolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Size = new System.Drawing.Size(1297, 28); + this.menuStrip1.Padding = new System.Windows.Forms.Padding(5, 2, 0, 2); + this.menuStrip1.Size = new System.Drawing.Size(1135, 24); this.menuStrip1.TabIndex = 6; this.menuStrip1.Text = "menuStrip1"; // @@ -117,30 +126,46 @@ // this.toolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.workPieceToolStripMenuItem, - this.manufactureToolStripMenuItem}); + this.manufactureToolStripMenuItem, + this.shopToolStripMenuItem, + this.addManufactureToolStripMenuItem}); this.toolStripMenuItem.Name = "toolStripMenuItem"; - this.toolStripMenuItem.Size = new System.Drawing.Size(117, 24); + this.toolStripMenuItem.Size = new System.Drawing.Size(94, 20); this.toolStripMenuItem.Text = "Справочники"; // // workPieceToolStripMenuItem // this.workPieceToolStripMenuItem.Name = "workPieceToolStripMenuItem"; - this.workPieceToolStripMenuItem.Size = new System.Drawing.Size(162, 26); + this.workPieceToolStripMenuItem.Size = new System.Drawing.Size(198, 22); this.workPieceToolStripMenuItem.Text = "Заготовки"; this.workPieceToolStripMenuItem.Click += new System.EventHandler(this.WorkPieceToolStripMenuItem_Click); // // manufactureToolStripMenuItem // this.manufactureToolStripMenuItem.Name = "manufactureToolStripMenuItem"; - this.manufactureToolStripMenuItem.Size = new System.Drawing.Size(162, 26); + this.manufactureToolStripMenuItem.Size = new System.Drawing.Size(198, 22); this.manufactureToolStripMenuItem.Text = "Изделия"; this.manufactureToolStripMenuItem.Click += new System.EventHandler(this.ManufactureToolStripMenuItem_Click); // + // shopToolStripMenuItem + // + this.shopToolStripMenuItem.Name = "shopToolStripMenuItem"; + this.shopToolStripMenuItem.Size = new System.Drawing.Size(198, 22); + this.shopToolStripMenuItem.Text = "Магазины"; + this.shopToolStripMenuItem.Click += new System.EventHandler(this.ShopToolStripMenuItem_Click); + // + // addManufactureToolStripMenuItem + // + this.addManufactureToolStripMenuItem.Name = "addManufactureToolStripMenuItem"; + this.addManufactureToolStripMenuItem.Size = new System.Drawing.Size(198, 22); + this.addManufactureToolStripMenuItem.Text = "Пополнение магазина"; + this.addManufactureToolStripMenuItem.Click += new System.EventHandler(this.AddManufactureToolStripMenuItem_Click); + // // FormMain // - this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1297, 450); + this.ClientSize = new System.Drawing.Size(1135, 338); this.Controls.Add(this.buttonRef); this.Controls.Add(this.buttonIssuedOrder); this.Controls.Add(this.buttonOrderReady); @@ -149,6 +174,7 @@ this.Controls.Add(this.dataGridView); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.Name = "FormMain"; this.Text = "Кузнечная мастерская"; this.Load += new System.EventHandler(this.FormMain_Load); @@ -172,5 +198,7 @@ private ToolStripMenuItem toolStripMenuItem; private ToolStripMenuItem workPieceToolStripMenuItem; private ToolStripMenuItem manufactureToolStripMenuItem; + private ToolStripMenuItem shopToolStripMenuItem; + private ToolStripMenuItem addManufactureToolStripMenuItem; } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs index a8faa65..aac79d3 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs @@ -181,5 +181,27 @@ namespace BlacksmithWorkshop { LoadData(); } + + private void ShopToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + + if (service is FormShops form) + { + form.ShowDialog(); + } + } + + + private void AddManufactureToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddManufacture)); + + if (service is FormAddManufacture form) + { + form.ShowDialog(); + LoadData(); + } + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs new file mode 100644 index 0000000..24ee0ca --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.Designer.cs @@ -0,0 +1,209 @@ +namespace BlacksmithWorkshop +{ + partial class FormShop + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.labelName = new System.Windows.Forms.Label(); + this.labelAddress = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxAddress = new System.Windows.Forms.TextBox(); + this.groupBoxIceCreams = new System.Windows.Forms.GroupBox(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.labelDate = new System.Windows.Forms.Label(); + this.dateTimePickerDate = new System.Windows.Forms.DateTimePicker(); + this.ID = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnManufactureName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.groupBoxIceCreams.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(18, 10); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(62, 15); + this.labelName.TabIndex = 0; + this.labelName.Text = "Название:"; + // + // labelAddress + // + this.labelAddress.AutoSize = true; + this.labelAddress.Location = new System.Drawing.Point(266, 9); + this.labelAddress.Name = "labelAddress"; + this.labelAddress.Size = new System.Drawing.Size(43, 15); + this.labelAddress.TabIndex = 1; + this.labelAddress.Text = "Адрес:"; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(86, 7); + this.textBoxName.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(138, 23); + this.textBoxName.TabIndex = 2; + // + // textBoxAddress + // + this.textBoxAddress.Location = new System.Drawing.Point(315, 6); + this.textBoxAddress.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.textBoxAddress.Name = "textBoxAddress"; + this.textBoxAddress.Size = new System.Drawing.Size(138, 23); + this.textBoxAddress.TabIndex = 3; + // + // groupBoxIceCreams + // + this.groupBoxIceCreams.Controls.Add(this.dataGridView); + this.groupBoxIceCreams.Location = new System.Drawing.Point(12, 62); + this.groupBoxIceCreams.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.groupBoxIceCreams.Name = "groupBoxIceCreams"; + this.groupBoxIceCreams.Padding = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.groupBoxIceCreams.Size = new System.Drawing.Size(720, 206); + this.groupBoxIceCreams.TabIndex = 4; + this.groupBoxIceCreams.TabStop = false; + this.groupBoxIceCreams.Text = "Изделия"; + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.ID, + this.ColumnManufactureName, + this.ColumnCount}); + this.dataGridView.Location = new System.Drawing.Point(5, 20); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(709, 182); + this.dataGridView.TabIndex = 0; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(411, 286); + this.buttonSave.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(136, 22); + this.buttonSave.TabIndex = 5; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(579, 286); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(136, 22); + this.buttonCancel.TabIndex = 6; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // labelDate + // + this.labelDate.AutoSize = true; + this.labelDate.Location = new System.Drawing.Point(492, 9); + this.labelDate.Name = "labelDate"; + this.labelDate.Size = new System.Drawing.Size(90, 15); + this.labelDate.TabIndex = 7; + this.labelDate.Text = "Дата открытия:"; + // + // dateTimePickerDate + // + this.dateTimePickerDate.Location = new System.Drawing.Point(588, 7); + this.dateTimePickerDate.Name = "dateTimePickerDate"; + this.dateTimePickerDate.Size = new System.Drawing.Size(127, 23); + this.dateTimePickerDate.TabIndex = 9; + // + // ID + // + this.ID.HeaderText = "ID"; + this.ID.Name = "ID"; + this.ID.Visible = false; + // + // ColumnManufactureName + // + this.ColumnManufactureName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.ColumnManufactureName.HeaderText = "Изделие"; + this.ColumnManufactureName.MinimumWidth = 6; + this.ColumnManufactureName.Name = "ColumnManufactureName"; + this.ColumnManufactureName.Resizable = System.Windows.Forms.DataGridViewTriState.True; + // + // ColumnCount + // + this.ColumnCount.HeaderText = "Количество"; + this.ColumnCount.MinimumWidth = 6; + this.ColumnCount.Name = "ColumnCount"; + this.ColumnCount.Width = 125; + // + // FormShop + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(744, 319); + this.Controls.Add(this.dateTimePickerDate); + this.Controls.Add(this.labelDate); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.groupBoxIceCreams); + this.Controls.Add(this.textBoxAddress); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelAddress); + this.Controls.Add(this.labelName); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormShop"; + this.Text = "Магазин"; + this.Load += new System.EventHandler(this.FormShop_Load); + this.groupBoxIceCreams.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelName; + private Label labelAddress; + private TextBox textBoxName; + private TextBox textBoxAddress; + private GroupBox groupBoxIceCreams; + private DataGridView dataGridView; + private Button buttonSave; + private Button buttonCancel; + private Label labelDate; + private DateTimePicker dateTimePickerDate; + private DataGridViewTextBoxColumn ID; + private DataGridViewTextBoxColumn ColumnManufactureName; + private DataGridViewTextBoxColumn ColumnCount; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs new file mode 100644 index 0000000..dc28673 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.cs @@ -0,0 +1,131 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BlacksmithWorkshop +{ + public partial class FormShop : Form + { + private readonly ILogger _logger; + + private readonly IShopLogic _logic; + + private int? _id; + + private Dictionary _shopManufactures; + + public int Id { set { _id = value; } } + + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _shopManufactures = new Dictionary(); + } + + private void FormShop_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var view = _logic.ReadElement(new ShopSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ShopName; + textBoxAddress.Text = view.Address.ToString(); + dateTimePickerDate.Value = view.DateOpen; + _shopManufactures = view.Manufactures ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка изделий в магазине"); + try + { + if (_shopManufactures != null) + { + dataGridView.Rows.Clear(); + foreach (var element in _shopManufactures) + { + dataGridView.Rows.Add(new object[] { element.Key, element.Value.Item1.ManufactureName, element.Value.Item2 }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделий в магазине"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxAddress.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxName.Text, + Address = textBoxAddress.Text, + DateOpen = dateTimePickerDate.Value.Date + }; + 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, "Ошибка сохранения магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShop.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.Designer.cs new file mode 100644 index 0000000..fb8cbf3 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.Designer.cs @@ -0,0 +1,121 @@ +namespace BlacksmithWorkshop +{ + partial class FormShops + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(10, 9); + this.dataGridView.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(444, 320); + this.dataGridView.TabIndex = 1; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(465, 37); + this.buttonAdd.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(133, 22); + this.buttonAdd.TabIndex = 2; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonUpd + // + this.buttonUpd.Location = new System.Drawing.Point(465, 119); + this.buttonUpd.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(133, 22); + this.buttonUpd.TabIndex = 3; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(465, 197); + this.buttonDel.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(133, 22); + this.buttonDel.TabIndex = 4; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(465, 274); + this.buttonRef.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(133, 22); + this.buttonRef.TabIndex = 5; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormShops + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(610, 338); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormShops"; + this.Text = "Магазины"; + this.Load += new System.EventHandler(this.FormShops_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpd; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.cs new file mode 100644 index 0000000..6cc577b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.cs @@ -0,0 +1,117 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BlacksmithWorkshop +{ + public partial class FormShops : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + + public FormShops(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormShops_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Manufactures"].Visible = false; + dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление магазина"); + try + { + if (!_logic.Delete(new ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления мороженого"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.resx b/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormShops.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index f0537ce..99ba26a 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -39,10 +39,12 @@ namespace BlacksmithWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -51,6 +53,9 @@ namespace BlacksmithWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs index 123d366..313d497 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/ShopLogic.cs @@ -13,7 +13,7 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopBusinessLogic.BusinessLogic { - internal class ShopLogic : IShopLogic + public class ShopLogic : IShopLogic { private readonly ILogger _logger; private readonly IShopStorage _shopStorage; @@ -65,22 +65,121 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic public bool AddManufacture(ShopSearchModel model, IManufactureModel manufacture, int count) { - throw new NotImplementedException(); + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (count <= 0) + { + throw new ArgumentNullException("Количество добавляемого мороженого должно быть больше 0", nameof(count)); + } + + _logger.LogInformation("AddIceCream. ShopName:{ShopName}. Id: {Id}", model?.ShopName, model?.Id); + var shop = _shopStorage.GetElement(model); + + if (shop == null) + { + _logger.LogWarning("Add Manufacture operation failed"); + + return false; + } + + if (!shop.Manufactures.ContainsKey(manufacture.Id)) + { + shop.Manufactures[manufacture.Id] = (manufacture, count); + } + else + { + shop.Manufactures[manufacture.Id] = (manufacture, shop.Manufactures[manufacture.Id].Item2 + count); + } + + _shopStorage.Update(new ShopBindingModel() + { + ShopName = shop.ShopName, + Address = shop.Address, + DateOpen = shop.DateOpen, + Manufactures = shop.Manufactures + }); + + return true; } public bool Create(ShopBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + + return false; + } + + return true; } public bool Update(ShopBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + + return false; + } + + return true; } public bool Delete(ShopBindingModel model) { - throw new NotImplementedException(); + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (!withParams) + { + return; + } + + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Отсутствует названия магазина", nameof(model.ShopName)); + } + + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Отсутствует адреса магазина", nameof(model.Address)); + } + + _logger.LogInformation("Shop. ShopName:{ShopName}. Address:{Address}. Id: {Id}", model.ShopName, model.Address, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } } } }