From 5db09bc6364b133d5899fdf906c3125e8351ca02 Mon Sep 17 00:00:00 2001 From: frog24 Date: Mon, 19 Feb 2024 22:16:00 +0400 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=3D>=20=D0=91=D0=BE=D0=B3=20=D0=B5=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ComputersShop/ComputersShop.sln | 6 + .../ComputersShop/ComputersShop.csproj | 18 ++ ComputersShop/ComputersShop/Form1.Designer.cs | 39 --- ComputersShop/ComputersShop/Form1.cs | 10 - ComputersShop/ComputersShop/Form1.resx | 120 --------- .../ComputersShop/FormComponent.Designer.cs | 118 +++++++++ ComputersShop/ComputersShop/FormComponent.cs | 79 ++++++ .../ComputersShop/FormComponent.resx | 60 +++++ .../ComputersShop/FormComponents.Designer.cs | 148 +++++++++++ ComputersShop/ComputersShop/FormComponents.cs | 109 ++++++++ .../ComputersShop/FormComponents.resx | 60 +++++ .../ComputersShop/FormComputer.Designer.cs | 234 ++++++++++++++++++ ComputersShop/ComputersShop/FormComputer.cs | 217 ++++++++++++++++ ComputersShop/ComputersShop/FormComputer.resx | 69 ++++++ .../FormComputerComponent.Designer.cs | 119 +++++++++ .../ComputersShop/FormComputerComponent.cs | 96 +++++++ .../ComputersShop/FormComputerComponent.resx | 60 +++++ .../ComputersShop/FormComputers.Designer.cs | 148 +++++++++++ ComputersShop/ComputersShop/FormComputers.cs | 113 +++++++++ .../ComputersShop/FormComputers.resx | 60 +++++ .../ComputersShop/FormCreateOrder.Designer.cs | 145 +++++++++++ .../ComputersShop/FormCreateOrder.cs | 118 +++++++++ .../ComputersShop/FormCreateOrder.resx | 60 +++++ .../ComputersShop/FormMain.Designer.cs | 176 +++++++++++++ ComputersShop/ComputersShop/FormMain.cs | 157 ++++++++++++ ComputersShop/ComputersShop/FormMain.resx | 63 +++++ ComputersShop/ComputersShop/Program.cs | 39 ++- ComputersShop/ComputersShop/nlog.config | 15 ++ .../BusinessLogics/ComputerLogic.cs | 2 +- .../BusinessLogics/OrderLogic.cs | 70 +++--- .../IComponentLogic.cs | 0 .../IComputerLogic.cs | 0 .../IOrderLogic.cs | 0 .../ViewModels/ComputerViewModel.cs | 2 +- .../ViewModels/OrderViewModel.cs | 2 +- .../ComputersShopDataModels.csproj | 6 + .../ComputersShopListImplement.csproj | 14 ++ .../DataListSingleton.cs | 31 +++ .../Implements/ComponentStorage.cs | 107 ++++++++ .../Implements/ComputerStorage.cs | 107 ++++++++ .../Implements/OrderStorage.cs | 114 +++++++++ .../Models/Component.cs | 47 ++++ .../Models/Computer.cs | 50 ++++ .../Models/Order.cs | 65 +++++ 44 files changed, 3064 insertions(+), 209 deletions(-) delete mode 100644 ComputersShop/ComputersShop/Form1.Designer.cs delete mode 100644 ComputersShop/ComputersShop/Form1.cs delete mode 100644 ComputersShop/ComputersShop/Form1.resx create mode 100644 ComputersShop/ComputersShop/FormComponent.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormComponent.cs create mode 100644 ComputersShop/ComputersShop/FormComponent.resx create mode 100644 ComputersShop/ComputersShop/FormComponents.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormComponents.cs create mode 100644 ComputersShop/ComputersShop/FormComponents.resx create mode 100644 ComputersShop/ComputersShop/FormComputer.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormComputer.cs create mode 100644 ComputersShop/ComputersShop/FormComputer.resx create mode 100644 ComputersShop/ComputersShop/FormComputerComponent.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormComputerComponent.cs create mode 100644 ComputersShop/ComputersShop/FormComputerComponent.resx create mode 100644 ComputersShop/ComputersShop/FormComputers.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormComputers.cs create mode 100644 ComputersShop/ComputersShop/FormComputers.resx create mode 100644 ComputersShop/ComputersShop/FormCreateOrder.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormCreateOrder.cs create mode 100644 ComputersShop/ComputersShop/FormCreateOrder.resx create mode 100644 ComputersShop/ComputersShop/FormMain.Designer.cs create mode 100644 ComputersShop/ComputersShop/FormMain.cs create mode 100644 ComputersShop/ComputersShop/FormMain.resx create mode 100644 ComputersShop/ComputersShop/nlog.config rename ComputersShop/ComputersShopContracts/{BuisnessLogicsContracts => BusinessLogicsContracts}/IComponentLogic.cs (100%) rename ComputersShop/ComputersShopContracts/{BuisnessLogicsContracts => BusinessLogicsContracts}/IComputerLogic.cs (100%) rename ComputersShop/ComputersShopContracts/{BuisnessLogicsContracts => BusinessLogicsContracts}/IOrderLogic.cs (100%) create mode 100644 ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj create mode 100644 ComputersShop/ComputersShopListImplement/DataListSingleton.cs create mode 100644 ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs create mode 100644 ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs create mode 100644 ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs create mode 100644 ComputersShop/ComputersShopListImplement/Models/Component.cs create mode 100644 ComputersShop/ComputersShopListImplement/Models/Computer.cs create mode 100644 ComputersShop/ComputersShopListImplement/Models/Order.cs diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index eb41921..bfc313b 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopContracts", "C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopBusinessLogic", "ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj", "{93D47C23-DA56-4179-8918-9D70A1012048}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopListImplement", "ComputersShopListImplement\ComputersShopListImplement.csproj", "{8AF31165-C031-42B2-88D4-A1A89EFCDC59}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {93D47C23-DA56-4179-8918-9D70A1012048}.Debug|Any CPU.Build.0 = Debug|Any CPU {93D47C23-DA56-4179-8918-9D70A1012048}.Release|Any CPU.ActiveCfg = Release|Any CPU {93D47C23-DA56-4179-8918-9D70A1012048}.Release|Any CPU.Build.0 = Release|Any CPU + {8AF31165-C031-42B2-88D4-A1A89EFCDC59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8AF31165-C031-42B2-88D4-A1A89EFCDC59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8AF31165-C031-42B2-88D4-A1A89EFCDC59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8AF31165-C031-42B2-88D4-A1A89EFCDC59}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/ComputersShop/ComputersShop.csproj b/ComputersShop/ComputersShop/ComputersShop.csproj index b57c89e..925cc89 100644 --- a/ComputersShop/ComputersShop/ComputersShop.csproj +++ b/ComputersShop/ComputersShop/ComputersShop.csproj @@ -8,4 +8,22 @@ enable + + + + + + + + + + + + + + + Always + + + \ No newline at end of file diff --git a/ComputersShop/ComputersShop/Form1.Designer.cs b/ComputersShop/ComputersShop/Form1.Designer.cs deleted file mode 100644 index 8127802..0000000 --- a/ComputersShop/ComputersShop/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace ComputersShop -{ - 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/ComputersShop/ComputersShop/Form1.cs b/ComputersShop/ComputersShop/Form1.cs deleted file mode 100644 index 85dadf8..0000000 --- a/ComputersShop/ComputersShop/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ComputersShop -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/Form1.resx b/ComputersShop/ComputersShop/Form1.resx deleted file mode 100644 index 1af7de1..0000000 --- a/ComputersShop/ComputersShop/Form1.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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/ComputersShop/ComputersShop/FormComponent.Designer.cs b/ComputersShop/ComputersShop/FormComponent.Designer.cs new file mode 100644 index 0000000..76376ba --- /dev/null +++ b/ComputersShop/ComputersShop/FormComponent.Designer.cs @@ -0,0 +1,118 @@ +namespace ComputersShop +{ + 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() + { + labelName = new Label(); + labelCost = new Label(); + textBoxName = new TextBox(); + textBoxCost = new TextBox(); + buttonSave = new Button(); + buttonCancel = new Button(); + 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 = "Название:"; + // + // labelCost + // + labelCost.AutoSize = true; + labelCost.Location = new Point(12, 42); + labelCost.Name = "labelCost"; + labelCost.Size = new Size(38, 15); + labelCost.TabIndex = 1; + labelCost.Text = "Цена:"; + // + // textBoxName + // + textBoxName.Location = new Point(77, 6); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(295, 23); + textBoxName.TabIndex = 2; + // + // textBoxCost + // + textBoxCost.Location = new Point(77, 39); + textBoxCost.Name = "textBoxCost"; + textBoxCost.Size = new Size(295, 23); + textBoxCost.TabIndex = 3; + // + // buttonSave + // + buttonSave.Location = new Point(160, 75); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(100, 30); + buttonSave.TabIndex = 4; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(270, 75); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(100, 30); + buttonCancel.TabIndex = 5; + buttonCancel.Text = "Отменить"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormComponent + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(384, 111); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxCost); + Controls.Add(textBoxName); + Controls.Add(labelCost); + Controls.Add(labelName); + Name = "FormComponent"; + Text = "Компонент"; + Load += FormComponent_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelName; + private Label labelCost; + private TextBox textBoxName; + private TextBox textBoxCost; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComponent.cs b/ComputersShop/ComputersShop/FormComponent.cs new file mode 100644 index 0000000..863ea53 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComponent.cs @@ -0,0 +1,79 @@ +using ComputersShopContracts.BuisnessLogicsContracts; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using Microsoft.Extensions.Logging; + +namespace ComputersShop +{ + public partial class FormComponent : Form + { + public readonly ILogger _logger; + public 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(); + } + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComponent.resx b/ComputersShop/ComputersShop/FormComponent.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShop/FormComponent.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/ComputersShop/ComputersShop/FormComponents.Designer.cs b/ComputersShop/ComputersShop/FormComponents.Designer.cs new file mode 100644 index 0000000..b7c939b --- /dev/null +++ b/ComputersShop/ComputersShop/FormComponents.Designer.cs @@ -0,0 +1,148 @@ +namespace ComputersShop +{ + 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() + { + DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle(); + DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle(); + DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle(); + dataGridView = new DataGridView(); + buttonAdd = new Button(); + buttonUpdate = new Button(); + buttonDelete = new Button(); + buttonReload = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle1.BackColor = SystemColors.Control; + dataGridViewCellStyle1.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + dataGridViewCellStyle1.ForeColor = SystemColors.WindowText; + dataGridViewCellStyle1.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle1.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle1.WrapMode = DataGridViewTriState.True; + dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle2.BackColor = SystemColors.Window; + dataGridViewCellStyle2.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + dataGridViewCellStyle2.ForeColor = SystemColors.ControlText; + dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle2.WrapMode = DataGridViewTriState.False; + dataGridView.DefaultCellStyle = dataGridViewCellStyle2; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridViewCellStyle3.Alignment = DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle3.BackColor = SystemColors.Control; + dataGridViewCellStyle3.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + dataGridViewCellStyle3.ForeColor = SystemColors.WindowText; + dataGridViewCellStyle3.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle3.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle3.WrapMode = DataGridViewTriState.True; + dataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle3; + dataGridView.RowHeadersVisible = false; + dataGridView.RowTemplate.Height = 25; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(300, 361); + dataGridView.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.Location = new Point(320, 15); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(140, 30); + buttonAdd.TabIndex = 5; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_CLick; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(320, 51); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(140, 30); + buttonUpdate.TabIndex = 6; + buttonUpdate.Text = "Изменить"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += ButtonUpd_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(320, 87); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(140, 30); + buttonDelete.TabIndex = 7; + buttonDelete.Text = "Удалить"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += ButtonDel_Click; + // + // buttonReload + // + buttonReload.Location = new Point(320, 123); + buttonReload.Name = "buttonReload"; + buttonReload.Size = new Size(140, 30); + buttonReload.TabIndex = 8; + buttonReload.Text = "Обновить"; + buttonReload.UseVisualStyleBackColor = true; + buttonReload.Click += ButtonRef_Click; + // + // FormComponents + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(484, 361); + Controls.Add(buttonReload); + Controls.Add(buttonDelete); + Controls.Add(buttonUpdate); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormComponents"; + Text = "Компоненты"; + Load += FormComponents_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpdate; + private Button buttonDelete; + private Button buttonReload; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComponents.cs b/ComputersShop/ComputersShop/FormComponents.cs new file mode 100644 index 0000000..ed10bc0 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComponents.cs @@ -0,0 +1,109 @@ +using ComputersShopContracts.BuisnessLogicsContracts; +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 ComputersShop +{ + public partial class FormComponents : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + public FormComponents(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + public 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 ComputersShopContracts.BindingModels.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/ComputersShop/ComputersShop/FormComponents.resx b/ComputersShop/ComputersShop/FormComponents.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShop/FormComponents.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/ComputersShop/ComputersShop/FormComputer.Designer.cs b/ComputersShop/ComputersShop/FormComputer.Designer.cs new file mode 100644 index 0000000..cece764 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputer.Designer.cs @@ -0,0 +1,234 @@ +namespace ComputersShop +{ + partial class FormComputer + { + /// + /// 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(); + textBoxPrice = new TextBox(); + textBoxName = new TextBox(); + groupBoxComponents = new GroupBox(); + dataGridView = new DataGridView(); + ColumnId = new DataGridViewTextBoxColumn(); + ColumnComponent = new DataGridViewTextBoxColumn(); + ColumnCount = new DataGridViewTextBoxColumn(); + buttonReload = new Button(); + buttonDelete = new Button(); + buttonUpdate = new Button(); + buttonAdd = new Button(); + buttonCancel = new Button(); + buttonSave = new Button(); + groupBoxComponents.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, 42); + labelPrice.Name = "labelPrice"; + labelPrice.Size = new Size(70, 15); + labelPrice.TabIndex = 1; + labelPrice.Text = "Стоимость:"; + // + // textBoxPrice + // + textBoxPrice.Enabled = false; + textBoxPrice.Location = new Point(90, 39); + textBoxPrice.Name = "textBoxPrice"; + textBoxPrice.Size = new Size(125, 23); + textBoxPrice.TabIndex = 2; + // + // textBoxName + // + textBoxName.Location = new Point(90, 6); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(250, 23); + textBoxName.TabIndex = 3; + // + // groupBoxComponents + // + groupBoxComponents.Controls.Add(dataGridView); + groupBoxComponents.Controls.Add(buttonReload); + groupBoxComponents.Controls.Add(buttonDelete); + groupBoxComponents.Controls.Add(buttonUpdate); + groupBoxComponents.Controls.Add(buttonAdd); + groupBoxComponents.Location = new Point(12, 68); + groupBoxComponents.Name = "groupBoxComponents"; + groupBoxComponents.Size = new Size(535, 295); + groupBoxComponents.TabIndex = 4; + groupBoxComponents.TabStop = false; + groupBoxComponents.Text = "Компоненты"; + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnComponent, ColumnCount }); + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(3, 19); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.RowTemplate.Height = 25; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(377, 273); + dataGridView.TabIndex = 0; + // + // ColumnId + // + ColumnId.HeaderText = "Id"; + ColumnId.Name = "ColumnId"; + ColumnId.ReadOnly = true; + ColumnId.Visible = false; + // + // ColumnComponent + // + ColumnComponent.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + ColumnComponent.HeaderText = "Компонент"; + ColumnComponent.Name = "ColumnComponent"; + ColumnComponent.ReadOnly = true; + // + // ColumnCount + // + ColumnCount.HeaderText = "Количество"; + ColumnCount.Name = "ColumnCount"; + ColumnCount.ReadOnly = true; + // + // buttonReload + // + buttonReload.Location = new Point(389, 130); + buttonReload.Name = "buttonReload"; + buttonReload.Size = new Size(140, 30); + buttonReload.TabIndex = 12; + buttonReload.Text = "Обновить"; + buttonReload.UseVisualStyleBackColor = true; + buttonReload.Click += ButtonRef_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(389, 94); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(140, 30); + buttonDelete.TabIndex = 11; + buttonDelete.Text = "Удалить"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += ButtonDel_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(389, 58); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(140, 30); + buttonUpdate.TabIndex = 10; + buttonUpdate.Text = "Изменить"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += ButtonUpd_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(389, 22); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(140, 30); + buttonAdd.TabIndex = 9; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(447, 369); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(100, 30); + buttonCancel.TabIndex = 7; + buttonCancel.Text = "Отменить"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // buttonSave + // + buttonSave.Location = new Point(337, 369); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(100, 30); + buttonSave.TabIndex = 6; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // FormComputer + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(559, 411); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(groupBoxComponents); + Controls.Add(textBoxName); + Controls.Add(textBoxPrice); + Controls.Add(labelPrice); + Controls.Add(labelName); + Name = "FormComputer"; + Text = "Компьютер"; + Load += FormIomputer_Load; + groupBoxComponents.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelName; + private Label labelPrice; + private TextBox textBoxPrice; + private TextBox textBoxName; + private GroupBox groupBoxComponents; + private Button buttonCancel; + private Button buttonSave; + private Button buttonReload; + private Button buttonDelete; + private Button buttonUpdate; + private Button buttonAdd; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnComponent; + private DataGridViewTextBoxColumn ColumnCount; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComputer.cs b/ComputersShop/ComputersShop/FormComputer.cs new file mode 100644 index 0000000..8921317 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputer.cs @@ -0,0 +1,217 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BuisnessLogicsContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopDataModels.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 ComputersShop +{ + public partial class FormComputer : Form + { + private readonly ILogger _logger; + + private readonly IComputerLogic _logic; + + private int? _id; + + private Dictionary _computerComponents; + public int Id { set { _id = value; } } + + public FormComputer(ILogger logger, IComputerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _computerComponents = new Dictionary(); + } + + private void FormIomputer_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Computer loading"); + try + { + var view = _logic.ReadElement(new ComputerSearchModel { Id = _id.Value }); + if (view != null) + { + textBoxName.Text = view.ComputerName; + textBoxPrice.Text = view.Price.ToString(); + _computerComponents = view.ComputerComponents ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Computer loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Computer components loading"); + try + { + if (_computerComponents != null) + { + dataGridView.Rows.Clear(); + foreach (var сomputerC in _computerComponents) + { + dataGridView.Rows.Add(new object[] { сomputerC.Key, сomputerC.Value.Item1.ComponentName, сomputerC.Value.Item2 }); + } + textBoxPrice.Text = CalcPrice().ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Computer components loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComputerComponent)); + if (service is FormComputerComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Adding new component: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); + if (_computerComponents.ContainsKey(form.Id)) + { + _computerComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _computerComponents.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(FormComputerComponent)); + if (service is FormComputerComponent form) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _computerComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Component editing: {ComponentName} - {Count}", form.ComponentModel.ComponentName, form.Count); + _computerComponents[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("Deletion of component: {ComponentName} - {Count}", dataGridView.SelectedRows[0].Cells[1].Value, + dataGridView.SelectedRows[0].Cells[2].Value); + _computerComponents?.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 (_computerComponents == null || _computerComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Computer saving"); + try + { + var model = new ComputerBindingModel + { + Id = _id ?? 0, + ComputerName = textBoxName.Text, + Price = Convert.ToDouble(textBoxPrice.Text), + ComputerComponents = _computerComponents + }; + 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, "Computer saving error"); + 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 _computerComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } + } +} diff --git a/ComputersShop/ComputersShop/FormComputer.resx b/ComputersShop/ComputersShop/FormComputer.resx new file mode 100644 index 0000000..30a7e11 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputer.resx @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + True + + + True + + + True + + \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComputerComponent.Designer.cs b/ComputersShop/ComputersShop/FormComputerComponent.Designer.cs new file mode 100644 index 0000000..58cb7c4 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputerComponent.Designer.cs @@ -0,0 +1,119 @@ +namespace ComputersShop +{ + partial class FormComputerComponent + { + /// + /// 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(); + comboBoxComponent = new ComboBox(); + textBoxCount = new TextBox(); + 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, 42); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(75, 15); + labelCount.TabIndex = 1; + labelCount.Text = "Количество:"; + // + // comboBoxComponent + // + comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxComponent.FormattingEnabled = true; + comboBoxComponent.Location = new Point(102, 6); + comboBoxComponent.Name = "comboBoxComponent"; + comboBoxComponent.Size = new Size(270, 23); + comboBoxComponent.TabIndex = 2; + // + // textBoxCount + // + textBoxCount.Location = new Point(102, 39); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(270, 23); + textBoxCount.TabIndex = 3; + // + // buttonCancel + // + buttonCancel.Location = new Point(270, 75); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(100, 30); + buttonCancel.TabIndex = 6; + buttonCancel.Text = "Отменить"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // buttonSave + // + buttonSave.Location = new Point(160, 75); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(100, 30); + buttonSave.TabIndex = 7; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // FormComputerComponent + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(384, 111); + Controls.Add(buttonSave); + Controls.Add(buttonCancel); + Controls.Add(textBoxCount); + Controls.Add(comboBoxComponent); + Controls.Add(labelCount); + Controls.Add(labelComponent); + Name = "FormComputerComponent"; + Text = "Компонент компьютера"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelComponent; + private Label labelCount; + private ComboBox comboBoxComponent; + private TextBox textBoxCount; + private Button buttonCancel; + private Button buttonSave; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComputerComponent.cs b/ComputersShop/ComputersShop/FormComputerComponent.cs new file mode 100644 index 0000000..5c02cf4 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputerComponent.cs @@ -0,0 +1,96 @@ +using ComputersShopContracts.BuisnessLogicsContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +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 ComputersShop +{ + public partial class FormComputerComponent : 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 FormComputerComponent(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/ComputersShop/ComputersShop/FormComputerComponent.resx b/ComputersShop/ComputersShop/FormComputerComponent.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputerComponent.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/ComputersShop/ComputersShop/FormComputers.Designer.cs b/ComputersShop/ComputersShop/FormComputers.Designer.cs new file mode 100644 index 0000000..0d29af2 --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputers.Designer.cs @@ -0,0 +1,148 @@ +namespace ComputersShop +{ + partial class FormComputers + { + /// + /// 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() + { + DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle(); + DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle(); + DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle(); + dataGridView = new DataGridView(); + buttonAdd = new Button(); + buttonUpdate = new Button(); + buttonDelete = new Button(); + buttonReload = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle1.BackColor = SystemColors.Control; + dataGridViewCellStyle1.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + dataGridViewCellStyle1.ForeColor = SystemColors.WindowText; + dataGridViewCellStyle1.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle1.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle1.WrapMode = DataGridViewTriState.True; + dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle2.BackColor = SystemColors.Window; + dataGridViewCellStyle2.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + dataGridViewCellStyle2.ForeColor = SystemColors.ControlText; + dataGridViewCellStyle2.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle2.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle2.WrapMode = DataGridViewTriState.False; + dataGridView.DefaultCellStyle = dataGridViewCellStyle2; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridViewCellStyle3.Alignment = DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle3.BackColor = SystemColors.Control; + dataGridViewCellStyle3.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + dataGridViewCellStyle3.ForeColor = SystemColors.WindowText; + dataGridViewCellStyle3.SelectionBackColor = SystemColors.Highlight; + dataGridViewCellStyle3.SelectionForeColor = SystemColors.HighlightText; + dataGridViewCellStyle3.WrapMode = DataGridViewTriState.True; + dataGridView.RowHeadersDefaultCellStyle = dataGridViewCellStyle3; + dataGridView.RowHeadersVisible = false; + dataGridView.RowTemplate.Height = 25; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(300, 361); + dataGridView.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.Location = new Point(320, 15); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(140, 30); + buttonAdd.TabIndex = 5; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(320, 51); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(140, 30); + buttonUpdate.TabIndex = 6; + buttonUpdate.Text = "Изменить"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += ButtonUpd_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(320, 87); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(140, 30); + buttonDelete.TabIndex = 7; + buttonDelete.Text = "Удалить"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += ButtonDel_Click; + // + // buttonReload + // + buttonReload.Location = new Point(320, 123); + buttonReload.Name = "buttonReload"; + buttonReload.Size = new Size(140, 30); + buttonReload.TabIndex = 8; + buttonReload.Text = "Обновить"; + buttonReload.UseVisualStyleBackColor = true; + buttonReload.Click += ButtonRef_Click; + // + // FormComputers + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(484, 361); + Controls.Add(buttonReload); + Controls.Add(buttonDelete); + Controls.Add(buttonUpdate); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormComputers"; + Text = "Компьютеры"; + Load += FormComputers_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpdate; + private Button buttonDelete; + private Button buttonReload; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormComputers.cs b/ComputersShop/ComputersShop/FormComputers.cs new file mode 100644 index 0000000..ee56f6f --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputers.cs @@ -0,0 +1,113 @@ +using Microsoft.Extensions.Logging; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BuisnessLogicsContracts; +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 ComputersShop +{ + public partial class FormComputers : Form + { + private readonly ILogger _logger; + + private readonly IComputerLogic _logic; + + public FormComputers(ILogger logger, IComputerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormComputers_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["ComputerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["ComputerComponents"].Visible = false; + } + _logger.LogInformation("Computers loading"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Computers loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComputer)); + if (service is FormComputer 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(FormComputer)); + if (service is FormComputer 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("Deletion of computer"); + try + { + if (!_logic.Delete(new ComputerBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Computer deletion error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/ComputersShop/ComputersShop/FormComputers.resx b/ComputersShop/ComputersShop/FormComputers.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShop/FormComputers.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/ComputersShop/ComputersShop/FormCreateOrder.Designer.cs b/ComputersShop/ComputersShop/FormCreateOrder.Designer.cs new file mode 100644 index 0000000..e56f476 --- /dev/null +++ b/ComputersShop/ComputersShop/FormCreateOrder.Designer.cs @@ -0,0 +1,145 @@ +namespace ComputersShop +{ + partial class FormCreateOrder + { + /// + /// 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() + { + textBoxCount = new TextBox(); + comboBoxComputer = new ComboBox(); + labelCount = new Label(); + labelComputer = new Label(); + textBoxSum = new TextBox(); + labelSum = new Label(); + buttonSave = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // textBoxCount + // + textBoxCount.Location = new Point(102, 39); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(270, 23); + textBoxCount.TabIndex = 7; + textBoxCount.TextChanged += TextBoxCount_TextChanged; + // + // comboBoxComputer + // + comboBoxComputer.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxComputer.FormattingEnabled = true; + comboBoxComputer.Location = new Point(102, 6); + comboBoxComputer.Name = "comboBoxComputer"; + comboBoxComputer.Size = new Size(270, 23); + comboBoxComputer.TabIndex = 6; + comboBoxComputer.SelectedIndexChanged += ComboBoxComputer_SelectedIndexChanged; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(12, 42); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(75, 15); + labelCount.TabIndex = 5; + labelCount.Text = "Количество:"; + // + // labelComputer + // + labelComputer.AutoSize = true; + labelComputer.Location = new Point(12, 9); + labelComputer.Name = "labelComputer"; + labelComputer.Size = new Size(74, 15); + labelComputer.TabIndex = 4; + labelComputer.Text = "Компьютер:"; + // + // textBoxSum + // + textBoxSum.Location = new Point(102, 71); + textBoxSum.Name = "textBoxSum"; + textBoxSum.ReadOnly = true; + textBoxSum.Size = new Size(270, 23); + textBoxSum.TabIndex = 9; + // + // labelSum + // + labelSum.AutoSize = true; + labelSum.Location = new Point(12, 74); + labelSum.Name = "labelSum"; + labelSum.Size = new Size(48, 15); + labelSum.TabIndex = 8; + labelSum.Text = "Сумма:"; + // + // buttonSave + // + buttonSave.Location = new Point(160, 120); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(100, 30); + buttonSave.TabIndex = 11; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(270, 120); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(100, 30); + buttonCancel.TabIndex = 10; + buttonCancel.Text = "Отменить"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormCreateOrder + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(384, 161); + Controls.Add(buttonSave); + Controls.Add(buttonCancel); + Controls.Add(textBoxSum); + Controls.Add(labelSum); + Controls.Add(textBoxCount); + Controls.Add(comboBoxComputer); + Controls.Add(labelCount); + Controls.Add(labelComputer); + Name = "FormCreateOrder"; + Text = "Заказ"; + Load += FormCreateOrder_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBoxCount; + private ComboBox comboBoxComputer; + private Label labelCount; + private Label labelComputer; + private TextBox textBoxSum; + private Label labelSum; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormCreateOrder.cs b/ComputersShop/ComputersShop/FormCreateOrder.cs new file mode 100644 index 0000000..a9c0e9b --- /dev/null +++ b/ComputersShop/ComputersShop/FormCreateOrder.cs @@ -0,0 +1,118 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BuisnessLogicsContracts; +using Microsoft.Extensions.Logging; +using ComputersShopContracts.SearchModels; + + +namespace ComputersShop +{ + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + + private readonly IComputerLogic _logicC; + + private readonly IOrderLogic _logicO; + + public FormCreateOrder(ILogger logger, IComputerLogic logicC, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicC = logicC; + _logicO = logicO; + } + + private void FormCreateOrder_Load(object sender, EventArgs e) + { + _logger.LogInformation("Loading computer for order"); + try + { + var computerList = _logicC.ReadList(null); + if (computerList != null) + { + comboBoxComputer.DataSource = computerList; + comboBoxComputer.DisplayMember = "ComputerName"; + comboBoxComputer.ValueMember = "Id"; + comboBoxComputer.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Error during loading computer for order"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void CalcSum() + { + if (comboBoxComputer.SelectedValue != null && !string.IsNullOrEmpty(textBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(comboBoxComputer.SelectedValue); + var product = _logicC.ReadElement(new ComputerSearchModel { Id = id }); + int count = Convert.ToInt32(textBoxCount.Text); + textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); + _logger.LogInformation("Calculation of order sum"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Calculation of order sum error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void TextBoxCount_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + + private void ComboBoxComputer_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxComputer.SelectedValue == null) + { + MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Order creation"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + ComputerId = Convert.ToInt32(comboBoxComputer.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text), + Sum = Convert.ToDouble(textBoxSum.Text) + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании заказа. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Order creation error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/ComputersShop/ComputersShop/FormCreateOrder.resx b/ComputersShop/ComputersShop/FormCreateOrder.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShop/FormCreateOrder.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/ComputersShop/ComputersShop/FormMain.Designer.cs b/ComputersShop/ComputersShop/FormMain.Designer.cs new file mode 100644 index 0000000..72ca113 --- /dev/null +++ b/ComputersShop/ComputersShop/FormMain.Designer.cs @@ -0,0 +1,176 @@ +namespace ComputersShop +{ + partial class FormMain + { + /// + /// 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() + { + menuStrip = new MenuStrip(); + справочникиToolStripMenuItem = new ToolStripMenuItem(); + компонентыToolStripMenuItem = new ToolStripMenuItem(); + комьютерыToolStripMenuItem = new ToolStripMenuItem(); + dataGridView = new DataGridView(); + buttonCreateOrder = new Button(); + buttonTakeOrderInWork = new Button(); + buttonOrderReady = new Button(); + buttonIssuedOrder = new Button(); + buttonUpdate = new Button(); + menuStrip.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // menuStrip + // + menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem }); + menuStrip.Location = new Point(0, 0); + menuStrip.Name = "menuStrip"; + menuStrip.Size = new Size(984, 24); + menuStrip.TabIndex = 0; + menuStrip.Text = "menuStrip1"; + // + // справочникиToolStripMenuItem + // + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, комьютерыToolStripMenuItem }); + справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; + справочникиToolStripMenuItem.Size = new Size(94, 20); + справочникиToolStripMenuItem.Text = "Справочники"; + // + // компонентыToolStripMenuItem + // + компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; + компонентыToolStripMenuItem.Size = new Size(145, 22); + компонентыToolStripMenuItem.Text = "Компоненты"; + компонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click; + // + // комьютерыToolStripMenuItem + // + комьютерыToolStripMenuItem.Name = "комьютерыToolStripMenuItem"; + комьютерыToolStripMenuItem.Size = new Size(145, 22); + комьютерыToolStripMenuItem.Text = "Комьютеры"; + комьютерыToolStripMenuItem.Click += КомпьютерыToolStripMenuItem_Click; + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ButtonHighlight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(0, 27); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.RowTemplate.Height = 25; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(780, 425); + dataGridView.TabIndex = 1; + // + // buttonCreateOrder + // + buttonCreateOrder.Location = new Point(792, 27); + buttonCreateOrder.Name = "buttonCreateOrder"; + buttonCreateOrder.Size = new Size(180, 35); + buttonCreateOrder.TabIndex = 2; + buttonCreateOrder.Text = "Создать заказ"; + buttonCreateOrder.UseVisualStyleBackColor = true; + buttonCreateOrder.Click += ButtonCreateOrder_Click; + // + // buttonTakeOrderInWork + // + buttonTakeOrderInWork.Location = new Point(792, 87); + buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; + buttonTakeOrderInWork.Size = new Size(180, 35); + buttonTakeOrderInWork.TabIndex = 3; + buttonTakeOrderInWork.Text = "Отправить на выполнение"; + buttonTakeOrderInWork.UseVisualStyleBackColor = true; + buttonTakeOrderInWork.Click += ButtonTakeOrderInWork_Click; + // + // buttonOrderReady + // + buttonOrderReady.Location = new Point(792, 147); + buttonOrderReady.Name = "buttonOrderReady"; + buttonOrderReady.Size = new Size(180, 35); + buttonOrderReady.TabIndex = 4; + buttonOrderReady.Text = "Заказ готов"; + buttonOrderReady.UseVisualStyleBackColor = true; + buttonOrderReady.Click += ButtonOrderReady_Click; + // + // buttonIssuedOrder + // + buttonIssuedOrder.Location = new Point(792, 207); + buttonIssuedOrder.Name = "buttonIssuedOrder"; + buttonIssuedOrder.Size = new Size(180, 35); + buttonIssuedOrder.TabIndex = 5; + buttonIssuedOrder.Text = "Заказ выдан"; + buttonIssuedOrder.UseVisualStyleBackColor = true; + buttonIssuedOrder.Click += ButtonIssuedOrder_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(792, 267); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(180, 35); + buttonUpdate.TabIndex = 6; + buttonUpdate.Text = "Обновить список"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += ButtonUpd_Click; + // + // FormMain + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(984, 450); + Controls.Add(buttonUpdate); + Controls.Add(buttonIssuedOrder); + Controls.Add(buttonOrderReady); + Controls.Add(buttonTakeOrderInWork); + Controls.Add(buttonCreateOrder); + Controls.Add(dataGridView); + Controls.Add(menuStrip); + MainMenuStrip = menuStrip; + Name = "FormMain"; + Text = "Компьютерный магазин"; + menuStrip.ResumeLayout(false); + menuStrip.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private MenuStrip menuStrip; + private ToolStripMenuItem справочникиToolStripMenuItem; + private ToolStripMenuItem компонентыToolStripMenuItem; + private ToolStripMenuItem комьютерыToolStripMenuItem; + private DataGridView dataGridView; + private Button buttonCreateOrder; + private Button buttonTakeOrderInWork; + private Button buttonOrderReady; + private Button buttonIssuedOrder; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShop/FormMain.cs b/ComputersShop/ComputersShop/FormMain.cs new file mode 100644 index 0000000..e945a5e --- /dev/null +++ b/ComputersShop/ComputersShop/FormMain.cs @@ -0,0 +1,157 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BuisnessLogicsContracts; +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 ComputersShop +{ + public partial class FormMain : Form + { + private readonly ILogger _logger; + + private readonly IOrderLogic _orderLogic; + + public FormMain(ILogger logger, IOrderLogic orderLogic) + { + InitializeComponent(); + _logger = logger; + _orderLogic = orderLogic; + } + + private void FormMain_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _orderLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["ComputerId"].Visible = false; + dataGridView.Columns["ComputerName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Orders loading"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Orders loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void КомпонентыToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComponents)); + if (service is FormComponents form) + { + form.ShowDialog(); + } + } + + private void КомпьютерыToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormComputers)); + if (service is FormComputers form) + { + form.ShowDialog(); + } + } + + private void ButtonCreateOrder_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); + if (service is FormCreateOrder form) + { + form.ShowDialog(); + LoadData(); + } + } + + private void ButtonTakeOrderInWork_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Order №{id}. Status changes to 'В работе'", id); + try + { + var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error taking an order to work"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void ButtonOrderReady_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Order №{id}. Status changes to 'Готов'", id); + try + { + var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Order readiness marking error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void ButtonIssuedOrder_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Order №{id}. Status changes to 'Выдан'", id); + try + { + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + _logger.LogInformation("Order №{id} issued", id); + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Order issue marking error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void ButtonUpd_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/ComputersShop/ComputersShop/FormMain.resx b/ComputersShop/ComputersShop/FormMain.resx new file mode 100644 index 0000000..81a9e3d --- /dev/null +++ b/ComputersShop/ComputersShop/FormMain.resx @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + \ No newline at end of file diff --git a/ComputersShop/ComputersShop/Program.cs b/ComputersShop/ComputersShop/Program.cs index 6394a0e..7facdfa 100644 --- a/ComputersShop/ComputersShop/Program.cs +++ b/ComputersShop/ComputersShop/Program.cs @@ -1,7 +1,18 @@ +using ComputersShopBusinessLogic.BusinessLogics; +using ComputersShopContracts.BuisnessLogicsContracts; +using ComputersShopContracts.StoragesContracts; +using ComputersShopListImplement.Implements; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using System; + namespace ComputersShop { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -11,7 +22,33 @@ namespace ComputersShop // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + Application.Run(_serviceProvider.GetRequiredService()); + } + 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/ComputersShop/ComputersShop/nlog.config b/ComputersShop/ComputersShop/nlog.config new file mode 100644 index 0000000..85797a7 --- /dev/null +++ b/ComputersShop/ComputersShop/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs index 1f8ff51..4e09052 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ComputerLogic.cs @@ -92,7 +92,7 @@ namespace ComputersShopBusinessLogic.BusinessLogics } if (string.IsNullOrEmpty(model.ComputerName)) { - throw new ArgumentNullException("Нет названия компонента", nameof(model.ComputerName)); + throw new ArgumentNullException("Нет названия компьютера", nameof(model.ComputerName)); } if (model.Price <= 0) { diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs index 5585456..ae3d09c 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -50,56 +50,52 @@ namespace ComputersShopBusinessLogic.BusinessLogics } return true; } - public bool TakeOrderInWork(OrderBindingModel model) + public bool ChangeStatus(OrderBindingModel model, OrderStatus newStatus) { - CheckModel(model); - if (model.Status != OrderStatus.Принят) + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (viewModel == null) { - _logger.LogWarning("Wrong order status. Update operation failed"); + throw new ArgumentNullException(nameof(viewModel)); + } + if (viewModel.Status + 1 != newStatus) + { + _logger.LogWarning("Status change operation failed"); return false; } - model.Status = OrderStatus.Выполняется; + + model.Status = newStatus; + model.ComputerId = viewModel.ComputerId; + model.Count = viewModel.Count; + model.Sum = viewModel.Sum; + model.DateCreate = viewModel.DateCreate; + if (model.Status == OrderStatus.Готов) + { + model.DateImplement = DateTime.Now; + } + else + { + model.DateImplement = viewModel.DateImplement; + } + CheckModel(model, false); if (_orderStorage.Update(model) == null) { - _logger.LogWarning("Update operation failed"); + _logger.LogWarning("Status change operation failed"); return false; } return true; } + public bool TakeOrderInWork(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } public bool FinishOrder(OrderBindingModel model) { - CheckModel(model); - if (model.Status != OrderStatus.Выполняется) - { - _logger.LogWarning("Wrong order status. Update operation failed"); - return false; - } - model.Status = OrderStatus.Готов; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; + return ChangeStatus(model, OrderStatus.Готов); } - public bool DeliveryOrder(OrderBindingModel model) { - CheckModel(model); - if (model.Status != OrderStatus.Готов) - { - _logger.LogWarning("Wrong order status. Update operation failed"); - return false; - } - model.Status = OrderStatus.Выдан; - if (_orderStorage.Update(model) == null) - { - _logger.LogWarning("Update operation failed"); - return false; - } - return true; + return ChangeStatus(model, OrderStatus.Выдан); } - // нужен ли в данном случае withParams?? private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) @@ -118,11 +114,12 @@ namespace ComputersShopBusinessLogic.BusinessLogics { throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count)); } - if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate) + if (model.DateImplement != null && model.DateImplement < model.DateCreate) { - throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} должна быть позже даты его создания {model.DateCreate}"); + throw new ArithmeticException("Дата завершения не может быть раньше даты начала"); } _logger.LogInformation("Order. Id:{Id}. Sum:{Sum}. ComputerId:{ComputerId}", model.Id, model.Sum, model.ComputerId); + /* var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id @@ -131,6 +128,7 @@ namespace ComputersShopBusinessLogic.BusinessLogics { throw new InvalidOperationException("Заказ с таким номером уже есть"); } + */ } } } diff --git a/ComputersShop/ComputersShopContracts/BuisnessLogicsContracts/IComponentLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IComponentLogic.cs similarity index 100% rename from ComputersShop/ComputersShopContracts/BuisnessLogicsContracts/IComponentLogic.cs rename to ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IComponentLogic.cs diff --git a/ComputersShop/ComputersShopContracts/BuisnessLogicsContracts/IComputerLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IComputerLogic.cs similarity index 100% rename from ComputersShop/ComputersShopContracts/BuisnessLogicsContracts/IComputerLogic.cs rename to ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IComputerLogic.cs diff --git a/ComputersShop/ComputersShopContracts/BuisnessLogicsContracts/IOrderLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IOrderLogic.cs similarity index 100% rename from ComputersShop/ComputersShopContracts/BuisnessLogicsContracts/IOrderLogic.cs rename to ComputersShop/ComputersShopContracts/BusinessLogicsContracts/IOrderLogic.cs diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs index 6e35fda..e9b9bfb 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs @@ -12,7 +12,7 @@ namespace ComputersShopContracts.ViewModels { public int Id { get; set; } - [DisplayName("Название изделия")] + [DisplayName("Название компьютера")] public string ComputerName { get; set; } = string.Empty; [DisplayName("Цена")] diff --git a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs index 21522a9..e1be573 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs @@ -16,7 +16,7 @@ namespace ComputersShopContracts.ViewModels public int ComputerId { get; set; } - [DisplayName("Изделие")] + [DisplayName("Компьютер")] public string ComputerName { get; set; } = string.Empty; [DisplayName("Количество")] diff --git a/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj b/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj index 132c02c..276e564 100644 --- a/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj +++ b/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj @@ -6,4 +6,10 @@ enable + + + Never + + + diff --git a/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj new file mode 100644 index 0000000..8eb14fb --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/ComputersShop/ComputersShopListImplement/DataListSingleton.cs b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..58dabf4 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs @@ -0,0 +1,31 @@ +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Computers { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Computers = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..9db8ce0 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,107 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.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 GetFiltredList(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; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs new file mode 100644 index 0000000..19e9903 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs @@ -0,0 +1,107 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Implements +{ + public class ComputerStorage: IComputerStorage + { + private readonly DataListSingleton _source; + public ComputerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var computer in _source.Computers) + { + result.Add(computer.GetViewModel); + } + return result; + } + public List GetFiltredList(ComputerSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComputerName)) + { + return result; + } + foreach (var computer in _source.Computers) + { + if (computer.ComputerName.Contains(model.ComputerName)) + { + result.Add(computer.GetViewModel); + } + } + return result; + } + public ComputerViewModel? GetElement(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName) && !model.Id.HasValue) + { + return null; + } + foreach (var computer in _source.Computers) + { + if ((!string.IsNullOrEmpty(model.ComputerName) + && computer.ComputerName == model.ComputerName) + || (model.Id.HasValue && computer.Id == model.Id)) + { + return computer.GetViewModel; + } + } + return null; + } + public ComputerViewModel? Insert(ComputerBindingModel model) + { + model.Id = 1; + foreach (var computer in _source.Computers) + { + if (model.Id <= computer.Id) + { + model.Id = computer.Id + 1; + } + } + var newComputer = Computer.Create(model); + if (newComputer == null) + { + return null; + } + _source.Computers.Add(newComputer); + return newComputer.GetViewModel; + } + public ComputerViewModel? Update(ComputerBindingModel model) + { + foreach (var computer in _source.Computers) + { + if (computer.Id == model.Id) + { + computer.Update(model); + return computer.GetViewModel; + } + } + return null; + } + public ComputerViewModel? Delete(ComputerBindingModel model) + { + for (int i = 0; i < _source.Computers.Count; ++i) + { + if (_source.Computers[i].Id == model.Id) + { + var element = _source.Computers[i]; + _source.Computers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..c0e5094 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,114 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.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(AddComputerName(order.GetViewModel)); + } + return result; + } + public List GetFiltredList(OrderSearchModel model) + { + var result = new List(); + if (model == null || !model.Id.HasValue) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(AddComputerName(order.GetViewModel)); + } + } + return result; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + return AddComputerName(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 AddComputerName(newOrder.GetViewModel); + } + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return AddComputerName(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 AddComputerName(element.GetViewModel); + } + } + return null; + } + private OrderViewModel AddComputerName(OrderViewModel model) + { + var selectedComputer = _source.Computers.Find(computer => computer.Id == model.ComputerId); + if (selectedComputer != null) + { + model.ComputerName = selectedComputer.ComputerName; + } + return model; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Component.cs b/ComputersShop/ComputersShopListImplement/Models/Component.cs new file mode 100644 index 0000000..20e6051 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Component.cs @@ -0,0 +1,47 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.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, + }; + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Computer.cs b/ComputersShop/ComputersShopListImplement/Models/Computer.cs new file mode 100644 index 0000000..76dc61a --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Computer.cs @@ -0,0 +1,50 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Computer: IComputerModel + { + public int Id { get; private set; } + public string ComputerName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary ComputerComponents { get; private set; } = new Dictionary(); + public static Computer? Create(ComputerBindingModel? model) + { + if (model == null) + { + return null; + } + return new Computer() + { + Id = model.Id, + ComputerName = model.ComputerName, + Price = model.Price, + ComputerComponents = model.ComputerComponents + }; + } + public void Update(ComputerBindingModel? model) + { + if (model == null) + { + return; + } + ComputerName = model.ComputerName; + Price = model.Price; + ComputerComponents = model.ComputerComponents; + } + public ComputerViewModel GetViewModel => new() + { + Id = Id, + ComputerName = ComputerName, + Price = Price, + ComputerComponents = ComputerComponents + }; + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Order.cs b/ComputersShop/ComputersShopListImplement/Models/Order.cs new file mode 100644 index 0000000..0696681 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Order.cs @@ -0,0 +1,65 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Order: IOrderModel + { + public int Id { get; private set; } + public int ComputerId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set;} + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ComputerId = model.ComputerId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + ComputerId = model.ComputerId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ComputerId = ComputerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +}