diff --git a/CarpentryWorkshop/CarpentryWorkshop.sln b/CarpentryWorkshop/CarpentryWorkshop.sln index 907fc20..8003ba3 100644 --- a/CarpentryWorkshop/CarpentryWorkshop.sln +++ b/CarpentryWorkshop/CarpentryWorkshop.sln @@ -3,7 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.7.34024.191 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshop", "CarpentryWorkshop\CarpentryWorkshop.csproj", "{98E796E8-D77E-4A5D-BB64-1C4029C51360}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarpentryWorkshopView", "CarpentryWorkshop\CarpentryWorkshopView.csproj", "{98E796E8-D77E-4A5D-BB64-1C4029C51360}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopDataModels", "CarpentryWorkshopDataModels\CarpentryWorkshopDataModels.csproj", "{E4AD7D91-C944-486B-891A-32D402EE6AD8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopContracts", "CarpentryWorkshopContracts\CarpentryWorkshopContracts.csproj", "{E1F5F636-7BA7-49CA-AF95-D36D1C55FD0E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopListImplement", "CarpentryWorkshopListImplement\CarpentryWorkshopListImplement.csproj", "{8778E6ED-03D0-4073-B61E-8ED7FF2DC420}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarpentryWorkshopBusinessLogic", "CarpentryWorkshopBusinessLogic\CarpentryWorkshopBusinessLogic.csproj", "{4D7EFA75-B5DC-4287-A50D-73405118D4A0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +23,22 @@ Global {98E796E8-D77E-4A5D-BB64-1C4029C51360}.Debug|Any CPU.Build.0 = Debug|Any CPU {98E796E8-D77E-4A5D-BB64-1C4029C51360}.Release|Any CPU.ActiveCfg = Release|Any CPU {98E796E8-D77E-4A5D-BB64-1C4029C51360}.Release|Any CPU.Build.0 = Release|Any CPU + {E4AD7D91-C944-486B-891A-32D402EE6AD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E4AD7D91-C944-486B-891A-32D402EE6AD8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E4AD7D91-C944-486B-891A-32D402EE6AD8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E4AD7D91-C944-486B-891A-32D402EE6AD8}.Release|Any CPU.Build.0 = Release|Any CPU + {E1F5F636-7BA7-49CA-AF95-D36D1C55FD0E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E1F5F636-7BA7-49CA-AF95-D36D1C55FD0E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E1F5F636-7BA7-49CA-AF95-D36D1C55FD0E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E1F5F636-7BA7-49CA-AF95-D36D1C55FD0E}.Release|Any CPU.Build.0 = Release|Any CPU + {8778E6ED-03D0-4073-B61E-8ED7FF2DC420}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8778E6ED-03D0-4073-B61E-8ED7FF2DC420}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8778E6ED-03D0-4073-B61E-8ED7FF2DC420}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8778E6ED-03D0-4073-B61E-8ED7FF2DC420}.Release|Any CPU.Build.0 = Release|Any CPU + {4D7EFA75-B5DC-4287-A50D-73405118D4A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4D7EFA75-B5DC-4287-A50D-73405118D4A0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4D7EFA75-B5DC-4287-A50D-73405118D4A0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4D7EFA75-B5DC-4287-A50D-73405118D4A0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CarpentryWorkshop/CarpentryWorkshop/CarpentryWorkshopView.csproj b/CarpentryWorkshop/CarpentryWorkshop/CarpentryWorkshopView.csproj new file mode 100644 index 0000000..1bf66cf --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/CarpentryWorkshopView.csproj @@ -0,0 +1,33 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + + + + + Always + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/Form1.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/Form1.Designer.cs deleted file mode 100644 index 22e0d5a..0000000 --- a/CarpentryWorkshop/CarpentryWorkshop/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace CarpentryWorkshop -{ - partial class Form1 - { - /// - /// 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.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; - } - - #endregion - } -} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/Form1.cs b/CarpentryWorkshop/CarpentryWorkshop/Form1.cs deleted file mode 100644 index c75964d..0000000 --- a/CarpentryWorkshop/CarpentryWorkshop/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace CarpentryWorkshop -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormComponent.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormComponent.Designer.cs new file mode 100644 index 0000000..41265c4 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormComponent.Designer.cs @@ -0,0 +1,127 @@ +namespace CarpentryWorkshopView +{ + partial class FormComponent + { + /// + /// 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.buttonCancel = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.labelName = new System.Windows.Forms.Label(); + this.textBoxCost = new System.Windows.Forms.TextBox(); + this.labelCost = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(257, 71); + this.buttonCancel.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(88, 27); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(162, 71); + this.buttonSave.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(88, 27); + this.buttonSave.TabIndex = 4; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(91, 7); + this.textBoxName.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(252, 23); + this.textBoxName.TabIndex = 1; + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(14, 10); + this.labelName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(62, 15); + this.labelName.TabIndex = 0; + this.labelName.Text = "Название:"; + // + // textBoxCost + // + this.textBoxCost.Location = new System.Drawing.Point(91, 36); + this.textBoxCost.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.textBoxCost.Name = "textBoxCost"; + this.textBoxCost.Size = new System.Drawing.Size(129, 23); + this.textBoxCost.TabIndex = 3; + // + // labelCost + // + this.labelCost.AutoSize = true; + this.labelCost.Location = new System.Drawing.Point(14, 39); + this.labelCost.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); + this.labelCost.Name = "labelCost"; + this.labelCost.Size = new System.Drawing.Size(38, 15); + this.labelCost.TabIndex = 2; + this.labelCost.Text = "Цена:"; + // + // FormComponent + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(360, 110); + this.Controls.Add(this.textBoxCost); + this.Controls.Add(this.labelCost); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelName); + this.Margin = new System.Windows.Forms.Padding(4, 3, 4, 3); + this.Name = "FormComponent"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Компонент"; + this.Load += new System.EventHandler(this.FormComponent_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.Button buttonCancel; + private System.Windows.Forms.Button buttonSave; + private System.Windows.Forms.TextBox textBoxName; + private System.Windows.Forms.Label labelName; + private TextBox textBoxCost; + private Label labelCost; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormComponent.cs b/CarpentryWorkshop/CarpentryWorkshop/FormComponent.cs new file mode 100644 index 0000000..4fe6856 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormComponent.cs @@ -0,0 +1,85 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopView +{ + public partial class FormComponent : Form + { + private readonly ILogger _logger; + + private readonly IComponentLogic _logic; + + private int? _id; + + public int Id { set { _id = value; } } + + public FormComponent(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormComponent_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение компонента"); + var view = _logic.ReadElement(new ComponentSearchModel { Id = _id.Value }); + if (view != null) + { + textBoxName.Text = view.ComponentName; + textBoxCost.Text = view.Cost.ToString(); + } + } + 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; + } + _logger.LogInformation("Сохранение компонента"); + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + ComponentName = textBoxName.Text, + Cost = Convert.ToDouble(textBoxCost.Text) + }; + 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/CarpentryWorkshop/CarpentryWorkshop/Form1.resx b/CarpentryWorkshop/CarpentryWorkshop/FormComponent.resx similarity index 100% rename from CarpentryWorkshop/CarpentryWorkshop/Form1.resx rename to CarpentryWorkshop/CarpentryWorkshop/FormComponent.resx diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormComponents.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormComponents.Designer.cs new file mode 100644 index 0000000..659028e --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormComponents.Designer.cs @@ -0,0 +1,120 @@ +namespace CarpentryWorkshopView +{ + partial class FormComponents + { + /// + /// 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() + { + dataGridView = new DataGridView(); + buttonAdd = new Button(); + buttonUpd = new Button(); + buttonDel = new Button(); + buttonRef = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ControlLightLight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(350, 360); + dataGridView.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.Location = new Point(410, 32); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(75, 23); + buttonAdd.TabIndex = 1; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonUpd + // + buttonUpd.Location = new Point(410, 70); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(75, 23); + buttonUpd.TabIndex = 2; + buttonUpd.Text = "Изменить"; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += buttonUpd_Click; + // + // buttonDel + // + buttonDel.Location = new Point(410, 111); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(75, 23); + buttonDel.TabIndex = 3; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; + // + // buttonRef + // + buttonRef.Location = new Point(410, 152); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(75, 23); + buttonRef.TabIndex = 4; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += buttonRef_Click; + // + // FormComponents + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(541, 360); + Controls.Add(buttonRef); + Controls.Add(buttonDel); + Controls.Add(buttonUpd); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Margin = new Padding(4, 3, 4, 3); + Name = "FormComponents"; + Text = "Компоненты"; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + 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/CarpentryWorkshop/CarpentryWorkshop/FormComponents.cs b/CarpentryWorkshop/CarpentryWorkshop/FormComponents.cs new file mode 100644 index 0000000..98cca33 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormComponents.cs @@ -0,0 +1,105 @@ +using CarpentryWorkshop; +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopView +{ + public partial class FormComponents : Form + { + private readonly ILogger _logger; + + private readonly IComponentLogic _logic; + + public FormComponents(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormComponents_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["ComponentName"].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(FormComponent)); + if (service is FormComponent 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(FormComponent)); + if (service is FormComponent 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 ComponentBindingModel { 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/CarpentryWorkshop/CarpentryWorkshop/FormComponents.resx b/CarpentryWorkshop/CarpentryWorkshop/FormComponents.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormComponents.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/CarpentryWorkshop/CarpentryWorkshop/FormWood.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormWood.Designer.cs new file mode 100644 index 0000000..c039c4b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWood.Designer.cs @@ -0,0 +1,224 @@ +namespace CarpentryWorkshopView +{ + partial class FormWood + { + /// + /// 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() + { + labelName = new Label(); + labelPrice = new Label(); + textBoxName = new TextBox(); + textBoxPrice = new TextBox(); + groupBox1 = new GroupBox(); + dataGridView = new DataGridView(); + ButtonRef = new Button(); + ButtonDel = new Button(); + ButtonUpd = new Button(); + ButtonAdd = new Button(); + ButtonSave = new Button(); + ButtonCancel = new Button(); + ID = new DataGridViewTextBoxColumn(); + ComponentName = new DataGridViewTextBoxColumn(); + Count = new DataGridViewTextBoxColumn(); + groupBox1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // labelName + // + labelName.AutoSize = true; + labelName.Location = new Point(12, 9); + labelName.Name = "labelName"; + labelName.Size = new Size(62, 15); + labelName.TabIndex = 0; + labelName.Text = "Название:"; + // + // labelPrice + // + labelPrice.AutoSize = true; + labelPrice.Location = new Point(12, 41); + labelPrice.Name = "labelPrice"; + labelPrice.Size = new Size(70, 15); + labelPrice.TabIndex = 1; + labelPrice.Text = "Стоимость:"; + // + // textBoxName + // + textBoxName.Location = new Point(94, 6); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(247, 23); + textBoxName.TabIndex = 2; + // + // textBoxPrice + // + textBoxPrice.Location = new Point(94, 38); + textBoxPrice.Name = "textBoxPrice"; + textBoxPrice.Size = new Size(100, 23); + textBoxPrice.TabIndex = 3; + // + // groupBox1 + // + groupBox1.Controls.Add(dataGridView); + groupBox1.Controls.Add(ButtonRef); + groupBox1.Controls.Add(ButtonDel); + groupBox1.Controls.Add(ButtonUpd); + groupBox1.Controls.Add(ButtonAdd); + groupBox1.Location = new Point(12, 67); + groupBox1.Name = "groupBox1"; + groupBox1.Size = new Size(510, 338); + groupBox1.TabIndex = 4; + groupBox1.TabStop = false; + groupBox1.Text = "Компонент"; + // + // dataGridView + // + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ID, ComponentName, Count }); + dataGridView.Location = new Point(0, 21); + dataGridView.Name = "dataGridView"; + dataGridView.RowTemplate.Height = 25; + dataGridView.Size = new Size(394, 311); + dataGridView.TabIndex = 5; + // + // ButtonRef + // + ButtonRef.Location = new Point(400, 169); + ButtonRef.Name = "ButtonRef"; + ButtonRef.Size = new Size(106, 28); + ButtonRef.TabIndex = 4; + ButtonRef.Text = "Обновить"; + ButtonRef.UseVisualStyleBackColor = true; + ButtonRef.Click += ButtonRef_Click; + // + // ButtonDel + // + ButtonDel.Location = new Point(400, 135); + ButtonDel.Name = "ButtonDel"; + ButtonDel.Size = new Size(106, 28); + ButtonDel.TabIndex = 3; + ButtonDel.Text = "Удалить"; + ButtonDel.UseVisualStyleBackColor = true; + ButtonDel.Click += ButtonDel_Click; + // + // ButtonUpd + // + ButtonUpd.Location = new Point(400, 101); + ButtonUpd.Name = "ButtonUpd"; + ButtonUpd.Size = new Size(106, 28); + ButtonUpd.TabIndex = 2; + ButtonUpd.Text = "Изменить"; + ButtonUpd.UseVisualStyleBackColor = true; + ButtonUpd.Click += ButtonUpd_Click; + // + // ButtonAdd + // + ButtonAdd.Location = new Point(400, 67); + ButtonAdd.Name = "ButtonAdd"; + ButtonAdd.Size = new Size(106, 28); + ButtonAdd.TabIndex = 1; + ButtonAdd.Text = "Добавить"; + ButtonAdd.UseVisualStyleBackColor = true; + ButtonAdd.Click += ButtonAdd_Click; + // + // ButtonSave + // + ButtonSave.Location = new Point(354, 415); + ButtonSave.Name = "ButtonSave"; + ButtonSave.Size = new Size(75, 23); + ButtonSave.TabIndex = 5; + ButtonSave.Text = "Сохранить"; + ButtonSave.UseVisualStyleBackColor = true; + ButtonSave.Click += ButtonSave_Click; + // + // ButtonCancel + // + ButtonCancel.Location = new Point(435, 415); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(75, 23); + ButtonCancel.TabIndex = 6; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // ID + // + ID.HeaderText = "Id"; + ID.Name = "ID"; + ID.Visible = false; + ID.Width = 250; + // + // ComponentName + // + ComponentName.HeaderText = "Компонент"; + ComponentName.Name = "ComponentName"; + ComponentName.Width = 250; + // + // Count + // + Count.HeaderText = "Количество"; + Count.Name = "Count"; + // + // FormWood + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(536, 450); + Controls.Add(ButtonCancel); + Controls.Add(ButtonSave); + Controls.Add(groupBox1); + Controls.Add(textBoxPrice); + Controls.Add(textBoxName); + Controls.Add(labelPrice); + Controls.Add(labelName); + Name = "FormWood"; + Text = "Создание набора"; + Load += FormWood_Load; + groupBox1.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelName; + private Label labelPrice; + private TextBox textBoxName; + private TextBox textBoxPrice; + private GroupBox groupBox1; + private DataGridView dataGridView; + private Button ButtonRef; + private Button ButtonDel; + private Button ButtonUpd; + private Button ButtonAdd; + private Button ButtonSave; + private Button ButtonCancel; + private DataGridViewTextBoxColumn ID; + private DataGridViewTextBoxColumn ComponentName; + private DataGridViewTextBoxColumn Count; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormWood.cs b/CarpentryWorkshop/CarpentryWorkshop/FormWood.cs new file mode 100644 index 0000000..4facb1c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWood.cs @@ -0,0 +1,202 @@ +using CarpentryWorkshop; +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopDataModels.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 CarpentryWorkshopView +{ + public partial class FormWood : Form + { + private readonly ILogger _logger; + private readonly IWoodLogic _logic; + private int? _id; + private Dictionary _woodComponents; + public int Id { set { _id = value; } } + public FormWood(ILogger logger, IWoodLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _woodComponents = new Dictionary(); + } + private void FormWood_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка изделия"); + try + { + var view = _logic.ReadElement(new WoodSearchModel { Id = _id.Value }); + if (view != null) + { + textBoxName.Text = view.WoodName; + textBoxPrice.Text = view.Price.ToString(); + _woodComponents = view.WoodComponents ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void LoadData() + { + _logger.LogInformation("Загрузка компонент изделия"); + try + { + if (_woodComponents != null) + { + dataGridView.Rows.Clear(); + foreach (var pc in _woodComponents) + { + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); + } + textBoxPrice.Text = CalcPrice().ToString(); + } + } + 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(FormWoodComponent)); + if (service is FormWoodComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Добавление нового компонента: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); + if (_woodComponents.ContainsKey(form.Id)) + { + _woodComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _woodComponents.Add(form.Id, (form.ComponentModel, form.Count)); + } + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormWoodComponent)); + if (service is FormWoodComponent form) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _woodComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Изменение компонента: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); + _woodComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); + } + } + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + _logger.LogInformation("Удаление компонента: {ComponentName} - {Count}", dataGridView.SelectedRows[0].Cells[1].Value); _woodComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPrice.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (_woodComponents == null || _woodComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение изделия"); + try + { + var model = new WoodBindingModel + { + Id = _id ?? 0, + WoodName = textBoxName.Text, + Price = Convert.ToDouble(textBoxPrice.Text), + WoodComponents = _woodComponents + }; + 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(); + } + private double CalcPrice() + { + double price = 0; + foreach (var elem in _woodComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormWood.resx b/CarpentryWorkshop/CarpentryWorkshop/FormWood.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWood.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.Designer.cs new file mode 100644 index 0000000..e06c6ad --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.Designer.cs @@ -0,0 +1,119 @@ +namespace CarpentryWorkshopView +{ + partial class FormWoodComponent + { + /// + /// 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() + { + labelComponent = new Label(); + labelCount = new Label(); + textBoxCount = new TextBox(); + comboBoxComponent = new ComboBox(); + buttonCancel = new Button(); + buttonSave = new Button(); + SuspendLayout(); + // + // labelComponent + // + labelComponent.AutoSize = true; + labelComponent.Location = new Point(12, 9); + labelComponent.Name = "labelComponent"; + labelComponent.Size = new Size(72, 15); + labelComponent.TabIndex = 0; + labelComponent.Text = "Компонент:"; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(12, 37); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(75, 15); + labelCount.TabIndex = 1; + labelCount.Text = "Количество:"; + // + // textBoxCount + // + textBoxCount.Location = new Point(90, 34); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(235, 23); + textBoxCount.TabIndex = 3; + // + // comboBoxComponent + // + comboBoxComponent.BackColor = SystemColors.Window; + comboBoxComponent.FormattingEnabled = true; + comboBoxComponent.Location = new Point(90, 6); + comboBoxComponent.Name = "comboBoxComponent"; + comboBoxComponent.Size = new Size(235, 23); + comboBoxComponent.TabIndex = 4; + // + // buttonCancel + // + buttonCancel.Location = new Point(248, 63); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(75, 23); + buttonCancel.TabIndex = 5; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // buttonSave + // + buttonSave.Location = new Point(167, 63); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(75, 23); + buttonSave.TabIndex = 6; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // FormWoodComponent + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(337, 97); + Controls.Add(buttonSave); + Controls.Add(buttonCancel); + Controls.Add(comboBoxComponent); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(labelComponent); + Name = "FormWoodComponent"; + Text = "Компонент изделия"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelComponent; + private Label labelCount; + private TextBox textBoxCount; + private ComboBox comboBoxComponent; + private Button buttonCancel; + private Button buttonSave; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.cs b/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.cs new file mode 100644 index 0000000..a9af5f1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.cs @@ -0,0 +1,63 @@ +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopView +{ + public partial class FormWoodComponent : Form + { + private readonly List? _list; + public int Id { get { return Convert.ToInt32(comboBoxComponent.SelectedValue); } set { comboBoxComponent.SelectedValue = value; } } + public IComponentModel? ComponentModel + { + get + { + if (_list == null) + { + return null; + } + foreach (var elem in _list) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + public int Count { get { return Convert.ToInt32(textBoxCount.Text); } set { textBoxCount.Text = value.ToString(); } } + public FormWoodComponent(IComponentLogic logic) + { + InitializeComponent(); + _list = logic.ReadList(null); + if (_list != null) + { + comboBoxComponent.DisplayMember = "ComponentName"; + comboBoxComponent.ValueMember = "Id"; + comboBoxComponent.DataSource = _list; + comboBoxComponent.SelectedItem = null; + } + } + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxComponent.SelectedValue == null) + { + MessageBox.Show("Выберите компонент", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.resx b/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWoodComponent.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/CarpentryWorkshop/CarpentryWorkshop/Program.cs b/CarpentryWorkshop/CarpentryWorkshop/Program.cs index 57440dd..837f262 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Program.cs @@ -1,17 +1,51 @@ +using CarpentryWorkshopBusinessLogic.BusinessLogics; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopListImplement.Implements; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; + namespace CarpentryWorkshop { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; + /// /// The main entry point for the application. /// [STAThread] static void Main() { + // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + // Application.Run(new Form1()); + } + + private static void ConfigureServices(ServiceCollection services) + { + services.AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/nlog.config b/CarpentryWorkshop/CarpentryWorkshop/nlog.config new file mode 100644 index 0000000..85797a7 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..497ae97 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,107 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopBusinessLogic.BusinessLogics +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + public List? ReadList(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{ Id}", model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}. Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия компонента", nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..1166d01 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,130 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Enums; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public bool CreateOrder(OrderBindingModel model) + { + _orderStorage.Insert(new OrderBindingModel + { + WoodId = model.WoodId, + WoodName = model.WoodName, + Count = model.Count, + Sum = model.Sum, + Status = OrderStatus.Принят, + DateCreate = DateTime.Now + }); ; + return true; + } + public bool TakeOrderInWork(OrderBindingModel model) + { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (order == null) + { + throw new Exception("Элемент не найден"); + } + if (order.Status.ToString() != "Принят") + { + throw new Exception("Заказ не в статусе \"Принят\""); + } + _orderStorage.Update(new OrderBindingModel + { + Id = order.Id, + WoodId = order.WoodId, + WoodName = order.WoodName, + Count = order.Count, + Sum = order.Sum, + DateCreate = order.DateCreate, + + Status = OrderStatus.Выполняется + }); + return true; + } + public bool FinishOrder(OrderBindingModel model) + { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (order == null) + { + throw new Exception("Не найден заказ"); + } + if (order.Status.ToString() != "Выполняется") + { + throw new Exception("Заказ не в статусе \"Выполняется\""); + } + _orderStorage.Update(new OrderBindingModel + { + Id = order.Id, + WoodId = order.WoodId, + WoodName = order.WoodName, + Count = order.Count, + Sum = order.Sum, + DateCreate = order.DateCreate, + DateImplement = DateTime.Now, + Status = OrderStatus.Готов + }); + return true; + } + public bool DeliveryOrder(OrderBindingModel model) + { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (order == null) + { + throw new Exception("Не найден заказ"); + } + + if (order.Status.ToString() != "Готов") + { + throw new Exception("Заказ не в статусе \"Готов\""); + } + + _orderStorage.Update(new OrderBindingModel + { + Id = order.Id, + WoodId = order.WoodId, + WoodName = order.WoodName, + Count = order.Count, + Sum = order.Sum, + DateCreate = order.DateCreate, + DateImplement = order.DateImplement, + Status = OrderStatus.Выдан + }); + return true; + + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/WoodLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/WoodLogic.cs new file mode 100644 index 0000000..54b08dc --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/WoodLogic.cs @@ -0,0 +1,107 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopBusinessLogic.BusinessLogics +{ + public class WoodLogic : IWoodLogic + { + private readonly ILogger _logger; + private readonly IWoodStorage _woodStorage; + public WoodLogic(ILogger logger, IWoodStorage woodStorage) + { + _logger = logger; + _woodStorage = woodStorage; + } + public List? ReadList(WoodSearchModel? model) + { + _logger.LogInformation("ReadList. WoodName:{WoodName}. Id:{ Id}", model?.WoodName, model?.Id); + var list = model == null ? _woodStorage.GetFullList() : _woodStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public WoodViewModel? ReadElement(WoodSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. WoodName:{WoodName}. Id:{ Id}", model.WoodName, model.Id); + var element = _woodStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(WoodBindingModel model) + { + CheckModel(model); + if (_woodStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(WoodBindingModel model) + { + CheckModel(model); + if (_woodStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(WoodBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_woodStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(WoodBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.WoodName)) + { + throw new ArgumentNullException("Нет названия компонента", nameof(model.WoodName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Wood. WoodName:{WoodName}. Cost:{ Cost}. Id: { Id}", model.WoodName, model.Price, model.Id); + var element = _woodStorage.GetElement(new WoodSearchModel + { + WoodName = model.WoodName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj new file mode 100644 index 0000000..055d91f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ComponentBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ComponentBindingModel.cs new file mode 100644 index 0000000..faf4e34 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ComponentBindingModel.cs @@ -0,0 +1,11 @@ +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.BindingModels +{ + public class ComponentBindingModel : IComponentModel + { + public int Id { get; set; } + public string ComponentName { get; set; } = string.Empty; + public double Cost { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel.cs new file mode 100644 index 0000000..b814e83 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/OrderBindingModel.cs @@ -0,0 +1,17 @@ +using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.BindingModels +{ + public class OrderBindingModel : IOrderModel + { + public int Id { get; set; } + public int WoodId { get; set; } + public string WoodName { get; set; } = string.Empty; + public int Count { get; set; } + public double Sum { get; set; } + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/WoodBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/WoodBindingModel.cs new file mode 100644 index 0000000..4f4b1af --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/WoodBindingModel.cs @@ -0,0 +1,16 @@ +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.BindingModels +{ + public class WoodBindingModel : IWoodModel + { + public int Id { get; set; } + public string WoodName { get; set; } = string.Empty; + public double Price { get; set; } + public Dictionary WoodComponents + { + get; + set; + } = new(); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IComponentLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IComponentLogic.cs new file mode 100644 index 0000000..d36006f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IComponentLogic.cs @@ -0,0 +1,15 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.BusinessLogicsContracts +{ + public interface IComponentLogic + { + List? ReadList(ComponentSearchModel? model); + ComponentViewModel? ReadElement(ComponentSearchModel model); + bool Create(ComponentBindingModel model); + bool Update(ComponentBindingModel model); + bool Delete(ComponentBindingModel model); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IOrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IOrderLogic.cs new file mode 100644 index 0000000..07a7c17 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -0,0 +1,15 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.BusinessLogicsContracts +{ + public interface IOrderLogic + { + List? ReadList(OrderSearchModel? model); + bool CreateOrder(OrderBindingModel model); + bool TakeOrderInWork(OrderBindingModel model); + bool FinishOrder(OrderBindingModel model); + bool DeliveryOrder(OrderBindingModel model); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IWoodLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IWoodLogic.cs new file mode 100644 index 0000000..2b2bb25 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IWoodLogic.cs @@ -0,0 +1,15 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.BusinessLogicsContracts +{ + public interface IWoodLogic + { + List? ReadList(WoodSearchModel? model); + WoodViewModel? ReadElement(WoodSearchModel model); + bool Create(WoodBindingModel model); + bool Update(WoodBindingModel model); + bool Delete(WoodBindingModel model); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj b/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj new file mode 100644 index 0000000..6ef3d42 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/CarpentryWorkshopContracts.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ComponentSearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ComponentSearchModel.cs new file mode 100644 index 0000000..14c1448 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ComponentSearchModel.cs @@ -0,0 +1,8 @@ +namespace CarpentryWorkshopContracts.SearchModels +{ + public class ComponentSearchModel + { + public int? Id { get; set; } + public string? ComponentName { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/OrderSearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/OrderSearchModel.cs new file mode 100644 index 0000000..4ea399e --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/OrderSearchModel.cs @@ -0,0 +1,7 @@ +namespace CarpentryWorkshopContracts.SearchModels +{ + public class OrderSearchModel + { + public int? Id { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/WoodSearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/WoodSearchModel.cs new file mode 100644 index 0000000..ada738d --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/WoodSearchModel.cs @@ -0,0 +1,8 @@ +namespace CarpentryWorkshopContracts.SearchModels +{ + public class WoodSearchModel + { + public int? Id { get; set; } + public string? WoodName { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IComponentStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IComponentStorage.cs new file mode 100644 index 0000000..c24a37b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IComponentStorage.cs @@ -0,0 +1,16 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.StoragesContracts +{ + public interface IComponentStorage + { + List GetFullList(); + List GetFilteredList(ComponentSearchModel model); + ComponentViewModel? GetElement(ComponentSearchModel model); + ComponentViewModel? Insert(ComponentBindingModel model); + ComponentViewModel? Update(ComponentBindingModel model); + ComponentViewModel? Delete(ComponentBindingModel model); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IOrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IOrderStorage.cs new file mode 100644 index 0000000..50c291e --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IOrderStorage.cs @@ -0,0 +1,16 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.StoragesContracts +{ + public interface IOrderStorage + { + List GetFullList(); + List GetFilteredList(OrderSearchModel model); + OrderViewModel? GetElement(OrderSearchModel model); + OrderViewModel? Insert(OrderBindingModel model); + OrderViewModel? Update(OrderBindingModel model); + OrderViewModel? Delete(OrderBindingModel model); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IWoodStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IWoodStorage.cs new file mode 100644 index 0000000..d2e3f41 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IWoodStorage.cs @@ -0,0 +1,16 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.StoragesContracts +{ + public interface IWoodStorage + { + List GetFullList(); + List GetFilteredList(WoodSearchModel model); + WoodViewModel? GetElement(WoodSearchModel model); + WoodViewModel? Insert(WoodBindingModel model); + WoodViewModel? Update(WoodBindingModel model); + WoodViewModel? Delete(WoodBindingModel model); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs new file mode 100644 index 0000000..6e7b31c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ComponentViewModel.cs @@ -0,0 +1,14 @@ +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel; + +namespace CarpentryWorkshopContracts.ViewModels +{ + public class ComponentViewModel : IComponentModel + { + public int Id { get; set; } + [DisplayName("Название компонента")] + public string ComponentName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Cost { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs new file mode 100644 index 0000000..64235bc --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/OrderViewModel.cs @@ -0,0 +1,25 @@ +using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel; + +namespace CarpentryWorkshopContracts.ViewModels +{ + public class OrderViewModel : IOrderModel + { + [DisplayName("Номер")] + public int Id { get; set; } + public int WoodId { get; set; } + [DisplayName("Изделие")] + public string WoodName { get; set; } = string.Empty; + [DisplayName("Количество")] + public int Count { get; set; } + [DisplayName("Сумма")] + public double Sum { get; set; } + [DisplayName("Статус")] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [DisplayName("Дата создания")] + public DateTime DateCreate { get; set; } = DateTime.Now; + [DisplayName("Дата выполнения")] + public DateTime? DateImplement { get; set; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs new file mode 100644 index 0000000..aef07a4 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/WoodViewModel.cs @@ -0,0 +1,19 @@ +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel; + +namespace CarpentryWorkshopContracts.ViewModels +{ + public class WoodViewModel : IWoodModel + { + public int Id { get; set; } + [DisplayName("Название изделия")] + public string WoodName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Price { get; set; } + public Dictionary WoodComponents + { + get; + set; + } = new(); + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/CarpentryWorkshopDataModels.csproj b/CarpentryWorkshop/CarpentryWorkshopDataModels/CarpentryWorkshopDataModels.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/CarpentryWorkshopDataModels.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Enums/OrderStatus.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Enums/OrderStatus.cs new file mode 100644 index 0000000..b87de40 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Enums/OrderStatus.cs @@ -0,0 +1,15 @@ +namespace CarpentryWorkshopDataModels.Enums +{ + public enum OrderStatus + { + Неизвестен = -1, + + Принят = 0, + + Выполняется = 1, + + Готов = 2, + + Выдан = 3 + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/IId.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/IId.cs new file mode 100644 index 0000000..86b5c6d --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/IId.cs @@ -0,0 +1,7 @@ +namespace CarpentryWorkshopDataModels +{ + public interface IId + { + int Id { get; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IComponentModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IComponentModel.cs new file mode 100644 index 0000000..ede69d8 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IComponentModel.cs @@ -0,0 +1,9 @@ +namespace CarpentryWorkshopDataModels.Models +{ + public interface IComponentModel : IId + { + string ComponentName { get; } + + double Cost { get; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs new file mode 100644 index 0000000..4312a8f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IOrderModel.cs @@ -0,0 +1,19 @@ +using CarpentryWorkshopDataModels.Enums; + +namespace CarpentryWorkshopDataModels.Models +{ + public interface IOrderModel : IId + { + int WoodId { get; } + + int Count { get; } + + double Sum { get; } + + OrderStatus Status { get; } + + DateTime DateCreate { get; } + + DateTime? DateImplement { get; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IWoodModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IWoodModel.cs new file mode 100644 index 0000000..3fc19d8 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IWoodModel.cs @@ -0,0 +1,9 @@ +namespace CarpentryWorkshopDataModels.Models +{ + public interface IWoodModel : IId + { + string WoodName { get; } + double Price { get; } + Dictionary WoodComponents { get; } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj b/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj new file mode 100644 index 0000000..319d159 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/CarpentryWorkshopListImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..9813b2a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs @@ -0,0 +1,26 @@ +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Woods { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Woods = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ComponentStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..6cccd91 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,101 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataListSingleton _source; + public ComponentStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Components) + { + result.Add(component.GetViewModel); + } + return result; + } + public List GetFilteredList(ComponentSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComponentName)) + { + return result; + } + foreach (var component in _source.Components) + { + if (component.ComponentName.Contains(model.ComponentName)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Components) + { + if ((!string.IsNullOrEmpty(model.ComponentName) && component.ComponentName == model.ComponentName) || + (model.Id.HasValue && component.Id == model.Id)) + { + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Components) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + _source.Components.Add(newComponent); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + foreach (var component in _source.Components) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + for (int i = 0; i < _source.Components.Count; ++i) + { + if (_source.Components[i].Id == model.Id) + { + var element = _source.Components[i]; + _source.Components.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/OrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b7c740f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,101 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(order.GetViewModel); + } + return result; + } + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (model.Id == null) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id.Equals(model.Id)) + { + result.Add(order.GetViewModel); + } + } + return result; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (model.Id == null && !model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if ((model.Id != null && order.Id == model.Id) || + (model.Id.HasValue && order.Id == model.Id)) + { + return order.GetViewModel; + } + } + return null; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return order.GetViewModel; + } + } + return null; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/WoodStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/WoodStorage.cs new file mode 100644 index 0000000..3c0c06c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/WoodStorage.cs @@ -0,0 +1,101 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.Implements +{ + public class WoodStorage : IWoodStorage + { + private readonly DataListSingleton _source; + public WoodStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var wood in _source.Woods) + { + result.Add(wood.GetViewModel); + } + return result; + } + public List GetFilteredList(WoodSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.WoodName)) + { + return result; + } + foreach (var wood in _source.Woods) + { + if (wood.WoodName.Contains(model.WoodName)) + { + result.Add(wood.GetViewModel); + } + } + return result; + } + public WoodViewModel? GetElement(WoodSearchModel model) + { + if (string.IsNullOrEmpty(model.WoodName) && !model.Id.HasValue) + { + return null; + } + foreach (var wood in _source.Woods) + { + if ((!string.IsNullOrEmpty(model.WoodName) && wood.WoodName == model.WoodName) || + (model.Id.HasValue && wood.Id == model.Id)) + { + return wood.GetViewModel; + } + } + return null; + } + public WoodViewModel? Insert(WoodBindingModel model) + { + model.Id = 1; + foreach (var wood in _source.Woods) + { + if (model.Id <= wood.Id) + { + model.Id = wood.Id + 1; + } + } + var newWood = Wood.Create(model); + if (newWood == null) + { + return null; + } + _source.Woods.Add(newWood); + return newWood.GetViewModel; + } + public WoodViewModel? Update(WoodBindingModel model) + { + foreach (var wood in _source.Woods) + { + if (wood.Id == model.Id) + { + wood.Update(model); + return wood.GetViewModel; + } + } + return null; + } + public WoodViewModel? Delete(WoodBindingModel model) + { + for (int i = 0; i < _source.Woods.Count; ++i) + { + if (_source.Woods[i].Id == model.Id) + { + var element = _source.Woods[i]; + _source.Woods.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Component.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Component.cs new file mode 100644 index 0000000..8dce082 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Component.cs @@ -0,0 +1,41 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopListImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel? model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel? model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Order.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Order.cs new file mode 100644 index 0000000..acd2e75 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Order.cs @@ -0,0 +1,58 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopListImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; set; } + public int WoodId { get; set; } + public int Count { get; set; } + public double Sum { get; set; } + public OrderStatus Status { get; set; } + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + WoodId = model.WoodId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + WoodId = model.WoodId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + WoodId = WoodId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Wood.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Wood.cs new file mode 100644 index 0000000..ade5ca4 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Wood.cs @@ -0,0 +1,45 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopListImplement.Models +{ + public class Wood : IWoodModel + { + public int Id { get; private set; } + public string WoodName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary WoodComponents { get; private set; } = new Dictionary(); + public static Wood? Create(WoodBindingModel? model) + { + if (model == null) + { + return null; + } + return new Wood() + { + Id = model.Id, + WoodName = model.WoodName, + Price = model.Price, + WoodComponents = model.WoodComponents + }; + } + public void Update(WoodBindingModel? model) + { + if (model == null) + { + return; + } + WoodName = model.WoodName; + Price = model.Price; + WoodComponents = model.WoodComponents; + } + public WoodViewModel GetViewModel => new() + { + Id = Id, + WoodName = WoodName, + Price = Price, + WoodComponents = WoodComponents + }; + } +} \ No newline at end of file