From c7fe80cd4a892cb726632f4a3de6ef406f388815 Mon Sep 17 00:00:00 2001 From: user Date: Sun, 30 Apr 2023 02:34:19 +0400 Subject: [PATCH] lab1 --- GiftShop/GiftShop.sln | 49 ++++ GiftShop/GiftShop/FormComponent.Designer.cs | 118 +++++++++ GiftShop/GiftShop/FormComponent.cs | 96 ++++++++ GiftShop/GiftShop/FormComponent.resx | 60 +++++ GiftShop/GiftShop/FormComponents.Designer.cs | 116 +++++++++ GiftShop/GiftShop/FormComponents.cs | 116 +++++++++ GiftShop/GiftShop/FormComponents.resx | 60 +++++ GiftShop/GiftShop/FormCreateOrder.Designer.cs | 146 +++++++++++ GiftShop/GiftShop/FormCreateOrder.cs | 131 ++++++++++ GiftShop/GiftShop/FormCreateOrder.resx | 60 +++++ GiftShop/GiftShop/FormGift.Designer.cs | 233 ++++++++++++++++++ GiftShop/GiftShop/FormGift.cs | 217 ++++++++++++++++ GiftShop/GiftShop/FormGift.resx | 69 ++++++ .../GiftShop/FormGiftComponent.Designer.cs | 120 +++++++++ GiftShop/GiftShop/FormGiftComponent.cs | 90 +++++++ GiftShop/GiftShop/FormGiftComponent.resx | 60 +++++ GiftShop/GiftShop/FormGifts.Designer.cs | 116 +++++++++ GiftShop/GiftShop/FormGifts.cs | 115 +++++++++ GiftShop/GiftShop/FormGifts.resx | 60 +++++ GiftShop/GiftShop/FormMain.Designer.cs | 177 +++++++++++++ GiftShop/GiftShop/FormMain.cs | 178 +++++++++++++ GiftShop/GiftShop/FormMain.resx | 63 +++++ GiftShop/GiftShop/GiftShopView.csproj | 24 ++ GiftShop/GiftShop/Program.cs | 48 ++++ .../BusinessLogics/ComponentLogic.cs | 115 +++++++++ .../BusinessLogics/GiftLogic.cs | 142 +++++++++++ .../BusinessLogics/OrderLogic.cs | 135 ++++++++++ .../GiftShopBusinessLogic.csproj | 20 ++ .../BindingModels/ComponentBindingModel.cs | 16 ++ .../BindingModels/GiftBindingModel.cs | 22 ++ .../BindingModels/OrderBindingModel.cs | 22 ++ .../IComponentLogic.cs | 20 ++ .../BusinessLogicsContracts/IGiftLogic.cs | 21 ++ .../BusinessLogicsContracts/IOrderLogic.cs | 20 ++ .../GiftShopContracts.csproj | 20 ++ .../SearchModels/ComponentSearchModel.cs | 14 ++ .../SearchModels/GiftSearchModel.cs | 14 ++ .../SearchModels/OrderSearchModel.cs | 13 + .../StoragesContracts/IComponentStorage.cs | 21 ++ .../StoragesContracts/IGiftStorage.cs | 21 ++ .../StoragesContracts/IOrderStorage.cs | 21 ++ .../ViewModels/ComponentViewModel.cs | 19 ++ .../ViewModels/GiftViewModel.cs | 24 ++ .../ViewModels/OrderViewModel.cs | 30 +++ .../GiftShopDataModels/Enums/OrderStatus.cs | 17 ++ .../GiftShopDataModels.csproj | 16 ++ GiftShop/GiftShopDataModels/IId.cs | 13 + .../Models/IComponentModel.cs | 14 ++ .../GiftShopDataModels/Models/IGiftModel.cs | 15 ++ .../GiftShopDataModels/Models/IOrderModel.cs | 19 ++ .../DataListSingleton.cs | 32 +++ .../GiftShopListImplement.csproj | 21 ++ .../Implements/ComponentStorage.cs | 102 ++++++++ .../Implements/GiftStorage.cs | 126 ++++++++++ .../Implements/OrderStorage.cs | 124 ++++++++++ .../GiftShopListImplement/Models/Component.cs | 46 ++++ GiftShop/GiftShopListImplement/Models/Gift.cs | 56 +++++ .../GiftShopListImplement/Models/Order.cs | 71 ++++++ 58 files changed, 3924 insertions(+) create mode 100644 GiftShop/GiftShop.sln create mode 100644 GiftShop/GiftShop/FormComponent.Designer.cs create mode 100644 GiftShop/GiftShop/FormComponent.cs create mode 100644 GiftShop/GiftShop/FormComponent.resx create mode 100644 GiftShop/GiftShop/FormComponents.Designer.cs create mode 100644 GiftShop/GiftShop/FormComponents.cs create mode 100644 GiftShop/GiftShop/FormComponents.resx create mode 100644 GiftShop/GiftShop/FormCreateOrder.Designer.cs create mode 100644 GiftShop/GiftShop/FormCreateOrder.cs create mode 100644 GiftShop/GiftShop/FormCreateOrder.resx create mode 100644 GiftShop/GiftShop/FormGift.Designer.cs create mode 100644 GiftShop/GiftShop/FormGift.cs create mode 100644 GiftShop/GiftShop/FormGift.resx create mode 100644 GiftShop/GiftShop/FormGiftComponent.Designer.cs create mode 100644 GiftShop/GiftShop/FormGiftComponent.cs create mode 100644 GiftShop/GiftShop/FormGiftComponent.resx create mode 100644 GiftShop/GiftShop/FormGifts.Designer.cs create mode 100644 GiftShop/GiftShop/FormGifts.cs create mode 100644 GiftShop/GiftShop/FormGifts.resx create mode 100644 GiftShop/GiftShop/FormMain.Designer.cs create mode 100644 GiftShop/GiftShop/FormMain.cs create mode 100644 GiftShop/GiftShop/FormMain.resx create mode 100644 GiftShop/GiftShop/GiftShopView.csproj create mode 100644 GiftShop/GiftShop/Program.cs create mode 100644 GiftShop/GiftShopBusinessLogic/BusinessLogics/ComponentLogic.cs create mode 100644 GiftShop/GiftShopBusinessLogic/BusinessLogics/GiftLogic.cs create mode 100644 GiftShop/GiftShopBusinessLogic/BusinessLogics/OrderLogic.cs create mode 100644 GiftShop/GiftShopBusinessLogic/GiftShopBusinessLogic.csproj create mode 100644 GiftShop/GiftShopContracts/BindingModels/ComponentBindingModel.cs create mode 100644 GiftShop/GiftShopContracts/BindingModels/GiftBindingModel.cs create mode 100644 GiftShop/GiftShopContracts/BindingModels/OrderBindingModel.cs create mode 100644 GiftShop/GiftShopContracts/BusinessLogicsContracts/IComponentLogic.cs create mode 100644 GiftShop/GiftShopContracts/BusinessLogicsContracts/IGiftLogic.cs create mode 100644 GiftShop/GiftShopContracts/BusinessLogicsContracts/IOrderLogic.cs create mode 100644 GiftShop/GiftShopContracts/GiftShopContracts.csproj create mode 100644 GiftShop/GiftShopContracts/SearchModels/ComponentSearchModel.cs create mode 100644 GiftShop/GiftShopContracts/SearchModels/GiftSearchModel.cs create mode 100644 GiftShop/GiftShopContracts/SearchModels/OrderSearchModel.cs create mode 100644 GiftShop/GiftShopContracts/StoragesContracts/IComponentStorage.cs create mode 100644 GiftShop/GiftShopContracts/StoragesContracts/IGiftStorage.cs create mode 100644 GiftShop/GiftShopContracts/StoragesContracts/IOrderStorage.cs create mode 100644 GiftShop/GiftShopContracts/ViewModels/ComponentViewModel.cs create mode 100644 GiftShop/GiftShopContracts/ViewModels/GiftViewModel.cs create mode 100644 GiftShop/GiftShopContracts/ViewModels/OrderViewModel.cs create mode 100644 GiftShop/GiftShopDataModels/Enums/OrderStatus.cs create mode 100644 GiftShop/GiftShopDataModels/GiftShopDataModels.csproj create mode 100644 GiftShop/GiftShopDataModels/IId.cs create mode 100644 GiftShop/GiftShopDataModels/Models/IComponentModel.cs create mode 100644 GiftShop/GiftShopDataModels/Models/IGiftModel.cs create mode 100644 GiftShop/GiftShopDataModels/Models/IOrderModel.cs create mode 100644 GiftShop/GiftShopListImplement/DataListSingleton.cs create mode 100644 GiftShop/GiftShopListImplement/GiftShopListImplement.csproj create mode 100644 GiftShop/GiftShopListImplement/Implements/ComponentStorage.cs create mode 100644 GiftShop/GiftShopListImplement/Implements/GiftStorage.cs create mode 100644 GiftShop/GiftShopListImplement/Implements/OrderStorage.cs create mode 100644 GiftShop/GiftShopListImplement/Models/Component.cs create mode 100644 GiftShop/GiftShopListImplement/Models/Gift.cs create mode 100644 GiftShop/GiftShopListImplement/Models/Order.cs diff --git a/GiftShop/GiftShop.sln b/GiftShop/GiftShop.sln new file mode 100644 index 0000000..bfc9d81 --- /dev/null +++ b/GiftShop/GiftShop.sln @@ -0,0 +1,49 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32825.248 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GiftShopView", "GiftShop\GiftShopView.csproj", "{F68ABBA7-2115-4B52-B15B-F666AD4BD30B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopDataModels", "GiftShopDataModels\GiftShopDataModels.csproj", "{89ACA130-8ABF-41BD-8F44-3291FD808D19}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopContracts", "GiftShopContracts\GiftShopContracts.csproj", "{9CB906D3-DED4-4093-838B-4B1B90F5E55A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopBusinessLogic", "GiftShopBusinessLogic\GiftShopBusinessLogic.csproj", "{68BC50EF-6A06-4F3C-8DF8-3B6B6B3AD2AD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GiftShopListImplement", "GiftShopListImplement\GiftShopListImplement.csproj", "{D3518048-9FEF-41E6-A2A7-80C9BF4A3D04}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F68ABBA7-2115-4B52-B15B-F666AD4BD30B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F68ABBA7-2115-4B52-B15B-F666AD4BD30B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F68ABBA7-2115-4B52-B15B-F666AD4BD30B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F68ABBA7-2115-4B52-B15B-F666AD4BD30B}.Release|Any CPU.Build.0 = Release|Any CPU + {89ACA130-8ABF-41BD-8F44-3291FD808D19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89ACA130-8ABF-41BD-8F44-3291FD808D19}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89ACA130-8ABF-41BD-8F44-3291FD808D19}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89ACA130-8ABF-41BD-8F44-3291FD808D19}.Release|Any CPU.Build.0 = Release|Any CPU + {9CB906D3-DED4-4093-838B-4B1B90F5E55A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9CB906D3-DED4-4093-838B-4B1B90F5E55A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9CB906D3-DED4-4093-838B-4B1B90F5E55A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9CB906D3-DED4-4093-838B-4B1B90F5E55A}.Release|Any CPU.Build.0 = Release|Any CPU + {68BC50EF-6A06-4F3C-8DF8-3B6B6B3AD2AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {68BC50EF-6A06-4F3C-8DF8-3B6B6B3AD2AD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {68BC50EF-6A06-4F3C-8DF8-3B6B6B3AD2AD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {68BC50EF-6A06-4F3C-8DF8-3B6B6B3AD2AD}.Release|Any CPU.Build.0 = Release|Any CPU + {D3518048-9FEF-41E6-A2A7-80C9BF4A3D04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D3518048-9FEF-41E6-A2A7-80C9BF4A3D04}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D3518048-9FEF-41E6-A2A7-80C9BF4A3D04}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D3518048-9FEF-41E6-A2A7-80C9BF4A3D04}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {33944AAE-BC0E-4ABE-BA92-309F92B24243} + EndGlobalSection +EndGlobal diff --git a/GiftShop/GiftShop/FormComponent.Designer.cs b/GiftShop/GiftShop/FormComponent.Designer.cs new file mode 100644 index 0000000..a09bcb7 --- /dev/null +++ b/GiftShop/GiftShop/FormComponent.Designer.cs @@ -0,0 +1,118 @@ +namespace GiftShopView +{ + partial class FormComponent + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.labelName = new System.Windows.Forms.Label(); + this.labelCost = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxCost = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(61, 130); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(123, 45); + this.buttonSave.TabIndex = 0; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(282, 130); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(124, 45); + this.buttonCancel.TabIndex = 1; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(61, 25); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(77, 20); + this.labelName.TabIndex = 2; + this.labelName.Text = "Название"; + // + // labelCost + // + this.labelCost.AutoSize = true; + this.labelCost.Location = new System.Drawing.Point(61, 83); + this.labelCost.Name = "labelCost"; + this.labelCost.Size = new System.Drawing.Size(83, 20); + this.labelCost.TabIndex = 3; + this.labelCost.Text = "Стоимость"; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(186, 22); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(220, 27); + this.textBoxName.TabIndex = 4; + // + // textBoxCost + // + this.textBoxCost.Location = new System.Drawing.Point(186, 80); + this.textBoxCost.Name = "textBoxCost"; + this.textBoxCost.Size = new System.Drawing.Size(220, 27); + this.textBoxCost.TabIndex = 5; + // + // FormComponent + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(483, 205); + this.Controls.Add(this.textBoxCost); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelCost); + this.Controls.Add(this.labelName); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Name = "FormComponent"; + this.Text = "Компонент"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Button buttonSave; + private Button buttonCancel; + private Label labelName; + private Label labelCost; + private TextBox textBoxName; + private TextBox textBoxCost; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormComponent.cs b/GiftShop/GiftShop/FormComponent.cs new file mode 100644 index 0000000..3635b11 --- /dev/null +++ b/GiftShop/GiftShop/FormComponent.cs @@ -0,0 +1,96 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.SearchModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace GiftShopView +{ + public partial class FormComponent : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormComponent(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void FormComponent_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation(" "); + var view = _logic.ReadElement(new ComponentSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ComponentName; + textBoxCost.Text = view.Cost.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, " "); + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show(" ", "", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation(" "); + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + ComponentName = textBoxName.Text, + Cost = Convert.ToDouble(textBoxCost.Text) + }; + var operationResult = _id.HasValue ? _logic.Update(model) : + _logic.Create(model); + if (!operationResult) + { + throw new Exception(" . ."); + } + MessageBox.Show(" ", "", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, " "); + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } + +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormComponent.resx b/GiftShop/GiftShop/FormComponent.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/GiftShop/GiftShop/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/GiftShop/GiftShop/FormComponents.Designer.cs b/GiftShop/GiftShop/FormComponents.Designer.cs new file mode 100644 index 0000000..335459e --- /dev/null +++ b/GiftShop/GiftShop/FormComponents.Designer.cs @@ -0,0 +1,116 @@ +namespace GiftShopView +{ + 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() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Control; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(382, 456); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(421, 92); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(94, 29); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(421, 167); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(94, 29); + this.buttonUpdate.TabIndex = 2; + this.buttonUpdate.Text = "Изменить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(422, 237); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(94, 29); + this.buttonDel.TabIndex = 3; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(422, 304); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(94, 29); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormComponents + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(528, 489); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormComponents"; + this.Text = "Компоненты"; + this.Load += new System.EventHandler(this.FormComponents_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpdate; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormComponents.cs b/GiftShop/GiftShop/FormComponents.cs new file mode 100644 index 0000000..ee645f5 --- /dev/null +++ b/GiftShop/GiftShop/FormComponents.cs @@ -0,0 +1,116 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +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 GiftShopView +{ + public partial class FormComponents : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + public FormComponents(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void FormComponents_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ComponentName"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = + Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = + Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + form.Id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление компонента"); + try + { + if (!_logic.Delete(new ComponentBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении.Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } + +} diff --git a/GiftShop/GiftShop/FormComponents.resx b/GiftShop/GiftShop/FormComponents.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/GiftShop/GiftShop/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/GiftShop/GiftShop/FormCreateOrder.Designer.cs b/GiftShop/GiftShop/FormCreateOrder.Designer.cs new file mode 100644 index 0000000..f937ee5 --- /dev/null +++ b/GiftShop/GiftShop/FormCreateOrder.Designer.cs @@ -0,0 +1,146 @@ +namespace GiftShopView +{ + 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() + { + this.labelGift = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.labelSum = new System.Windows.Forms.Label(); + this.comboBoxGift = new System.Windows.Forms.ComboBox(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.textBoxSum = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelGift + // + this.labelGift.AutoSize = true; + this.labelGift.Location = new System.Drawing.Point(34, 44); + this.labelGift.Name = "labelGift"; + this.labelGift.Size = new System.Drawing.Size(68, 20); + this.labelGift.TabIndex = 0; + this.labelGift.Text = "Изделие"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(34, 112); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(90, 20); + this.labelCount.TabIndex = 1; + this.labelCount.Text = "Количество"; + // + // labelSum + // + this.labelSum.AutoSize = true; + this.labelSum.Location = new System.Drawing.Point(34, 171); + this.labelSum.Name = "labelSum"; + this.labelSum.Size = new System.Drawing.Size(55, 20); + this.labelSum.TabIndex = 2; + this.labelSum.Text = "Сумма"; + // + // comboBoxGift + // + this.comboBoxGift.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxGift.FormattingEnabled = true; + this.comboBoxGift.Location = new System.Drawing.Point(141, 41); + this.comboBoxGift.Name = "comboBoxGift"; + this.comboBoxGift.Size = new System.Drawing.Size(369, 28); + this.comboBoxGift.TabIndex = 3; + this.comboBoxGift.SelectedIndexChanged += new System.EventHandler(this.ComboBoxProduct_SelectedIndexChanged); + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(141, 109); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(369, 27); + this.textBoxCount.TabIndex = 4; + this.textBoxCount.TextChanged += new System.EventHandler(this.TextBoxCount_TextChanged); + // + // textBoxSum + // + this.textBoxSum.Location = new System.Drawing.Point(141, 171); + this.textBoxSum.Name = "textBoxSum"; + this.textBoxSum.ReadOnly = true; + this.textBoxSum.Size = new System.Drawing.Size(369, 27); + this.textBoxSum.TabIndex = 5; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(82, 232); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(110, 58); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(366, 232); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(110, 58); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormCreateOrder + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(539, 308); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxSum); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxGift); + this.Controls.Add(this.labelSum); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelGift); + this.Name = "FormCreateOrder"; + this.Text = "Заказ"; + this.Load += new System.EventHandler(this.FormCreateOrder_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelGift; + private Label labelCount; + private Label labelSum; + private ComboBox comboBoxGift; + private TextBox textBoxCount; + private TextBox textBoxSum; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormCreateOrder.cs b/GiftShop/GiftShop/FormCreateOrder.cs new file mode 100644 index 0000000..a039249 --- /dev/null +++ b/GiftShop/GiftShop/FormCreateOrder.cs @@ -0,0 +1,131 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.SearchModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace GiftShopView +{ + public partial class FormCreateOrder : Form + { + private readonly ILogger _logger; + private readonly IGiftLogic _logicP; + private readonly IOrderLogic _logicO; + public FormCreateOrder(ILogger logger, IGiftLogic logicP, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicO = logicO; + } + private void FormCreateOrder_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка изделий для заказа"); + LoadData(); + } + private void LoadData() + { + _logger.LogInformation("Загрузка изделий для заказа"); + try + { + var list = _logicP.ReadList(null); + if (list != null) + { + comboBoxGift.DisplayMember = "GiftName"; + comboBoxGift.ValueMember = "ID"; + comboBoxGift.DataSource = list; + comboBoxGift.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка изделий"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void CalcSum() + { + if (comboBoxGift.SelectedValue != null && + !string.IsNullOrEmpty(textBoxCount.Text)) + { + try + { + int id = Convert.ToInt32(comboBoxGift.SelectedValue); + var product = _logicP.ReadElement(new GiftSearchModel + { + Id = id + }); + int count = Convert.ToInt32(textBoxCount.Text); + textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), + 2).ToString(); + _logger.LogInformation("Расчет суммы заказа"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка расчета суммы заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void TextBoxCount_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + private void ComboBoxProduct_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 (comboBoxGift.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + GiftId = Convert.ToInt32(comboBoxGift.SelectedValue), + GiftName = comboBoxGift.Text, + 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, "Ошибка создания заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/GiftShop/GiftShop/FormCreateOrder.resx b/GiftShop/GiftShop/FormCreateOrder.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/GiftShop/GiftShop/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/GiftShop/GiftShop/FormGift.Designer.cs b/GiftShop/GiftShop/FormGift.Designer.cs new file mode 100644 index 0000000..54aa1a2 --- /dev/null +++ b/GiftShop/GiftShop/FormGift.Designer.cs @@ -0,0 +1,233 @@ +namespace GiftShopView +{ + partial class FormGift + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.labelName = new System.Windows.Forms.Label(); + this.labelPrice = new System.Windows.Forms.Label(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxPrice = new System.Windows.Forms.TextBox(); + this.groupBox = new System.Windows.Forms.GroupBox(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonRef = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.ID = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Component = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.groupBox.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // labelName + // + this.labelName.AutoSize = true; + this.labelName.Location = new System.Drawing.Point(86, 18); + this.labelName.Name = "labelName"; + this.labelName.Size = new System.Drawing.Size(77, 20); + this.labelName.TabIndex = 0; + this.labelName.Text = "Название"; + // + // labelPrice + // + this.labelPrice.AutoSize = true; + this.labelPrice.Location = new System.Drawing.Point(86, 74); + this.labelPrice.Name = "labelPrice"; + this.labelPrice.Size = new System.Drawing.Size(83, 20); + this.labelPrice.TabIndex = 1; + this.labelPrice.Text = "Стоимость"; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(185, 18); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(253, 27); + this.textBoxName.TabIndex = 2; + // + // textBoxPrice + // + this.textBoxPrice.Location = new System.Drawing.Point(185, 74); + this.textBoxPrice.Name = "textBoxPrice"; + this.textBoxPrice.ReadOnly = true; + this.textBoxPrice.Size = new System.Drawing.Size(253, 27); + this.textBoxPrice.TabIndex = 3; + // + // groupBox + // + this.groupBox.Controls.Add(this.dataGridView); + this.groupBox.Controls.Add(this.buttonRef); + this.groupBox.Controls.Add(this.buttonDel); + this.groupBox.Controls.Add(this.buttonUpdate); + this.groupBox.Controls.Add(this.buttonAdd); + this.groupBox.Location = new System.Drawing.Point(12, 121); + this.groupBox.Name = "groupBox"; + this.groupBox.Size = new System.Drawing.Size(581, 272); + this.groupBox.TabIndex = 4; + this.groupBox.TabStop = false; + this.groupBox.Text = "Компонент"; + // + // dataGridView + // + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Control; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.ID, + this.Component, + this.Count}); + this.dataGridView.Location = new System.Drawing.Point(12, 25); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(371, 241); + this.dataGridView.TabIndex = 5; + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(401, 221); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(155, 29); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(401, 162); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(155, 29); + this.buttonDel.TabIndex = 3; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(401, 106); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(155, 29); + this.buttonUpdate.TabIndex = 2; + this.buttonUpdate.Text = "Изменить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(401, 52); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(155, 29); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(344, 409); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(94, 29); + this.buttonSave.TabIndex = 5; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(474, 409); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(94, 29); + this.buttonCancel.TabIndex = 6; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // ID + // + this.ID.HeaderText = "ID"; + this.ID.MinimumWidth = 6; + this.ID.Name = "ID"; + this.ID.Visible = false; + this.ID.Width = 125; + // + // Component + // + this.Component.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.Component.HeaderText = "Компонент"; + this.Component.MinimumWidth = 6; + this.Component.Name = "Component"; + // + // Count + // + this.Count.HeaderText = "Количество"; + this.Count.MinimumWidth = 6; + this.Count.Name = "Count"; + this.Count.Width = 125; + // + // FormGift + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(614, 450); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.groupBox); + this.Controls.Add(this.textBoxPrice); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.labelPrice); + this.Controls.Add(this.labelName); + this.Name = "FormGift"; + this.Text = "Изделие"; + this.groupBox.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelName; + private Label labelPrice; + private TextBox textBoxName; + private TextBox textBoxPrice; + private GroupBox groupBox; + private Button buttonRef; + private Button buttonDel; + private Button buttonUpdate; + private Button buttonAdd; + private Button buttonSave; + private Button buttonCancel; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn ID; + private DataGridViewTextBoxColumn Component; + private DataGridViewTextBoxColumn Count; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormGift.cs b/GiftShop/GiftShop/FormGift.cs new file mode 100644 index 0000000..f7f37cc --- /dev/null +++ b/GiftShop/GiftShop/FormGift.cs @@ -0,0 +1,217 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.SearchModels; +using GiftShopDataModels.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 GiftShopView +{ + public partial class FormGift : Form + { + private readonly ILogger _logger; + private readonly IGiftLogic _logic; + private int? _id; + private Dictionary _giftComponents; + public int Id { set { _id = value; } } + public FormGift(ILogger logger, IGiftLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _giftComponents = new Dictionary(); + } + private void FormGift_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка изделия"); + try + { + var view = _logic.ReadElement(new GiftSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.GiftName; + textBoxPrice.Text = view.Price.ToString(); + _giftComponents = view.GiftComponents ?? new + Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void LoadData() + { + _logger.LogInformation("Загрузка компонент изделия"); + try + { + if (_giftComponents != null) + { + dataGridView.Rows.Clear(); + foreach (var pc in _giftComponents) + { + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); + } + textBoxPrice.Text = CalcPrice().ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонент изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormGiftComponent)); + if (service is FormGiftComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Добавление нового компонента:{ ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + if (_giftComponents.ContainsKey(form.Id)) + { + _giftComponents[form.Id] = (form.ComponentModel, form.Count); + } + else + { + _giftComponents.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(FormGiftComponent)); + if (service is FormGiftComponent form) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _giftComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Изменение компонента:{ ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + _giftComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); + } + } + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + try + { + _logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); + _giftComponents?.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 (_giftComponents == null || _giftComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение изделия"); + try + { + var model = new GiftBindingModel + { + Id = _id ?? 0, + GiftName = textBoxName.Text, + Price = Convert.ToDouble(textBoxPrice.Text), + GiftComponents = _giftComponents + }; + var operationResult = _id.HasValue ? _logic.Update(model) : + _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + private double CalcPrice() + { + double price = 0; + foreach (var elem in _giftComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } + } +} + diff --git a/GiftShop/GiftShop/FormGift.resx b/GiftShop/GiftShop/FormGift.resx new file mode 100644 index 0000000..58a78b5 --- /dev/null +++ b/GiftShop/GiftShop/FormGift.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/GiftShop/GiftShop/FormGiftComponent.Designer.cs b/GiftShop/GiftShop/FormGiftComponent.Designer.cs new file mode 100644 index 0000000..709b7b2 --- /dev/null +++ b/GiftShop/GiftShop/FormGiftComponent.Designer.cs @@ -0,0 +1,120 @@ +namespace GiftShopView +{ + partial class FormGiftComponent + { + /// + /// 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.labelComponent = new System.Windows.Forms.Label(); + this.labelCount = new System.Windows.Forms.Label(); + this.comboBoxComponent = new System.Windows.Forms.ComboBox(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelComponent + // + this.labelComponent.AutoSize = true; + this.labelComponent.Location = new System.Drawing.Point(23, 35); + this.labelComponent.Name = "labelComponent"; + this.labelComponent.Size = new System.Drawing.Size(88, 20); + this.labelComponent.TabIndex = 0; + this.labelComponent.Text = "Компонент"; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(21, 85); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(90, 20); + this.labelCount.TabIndex = 1; + this.labelCount.Text = "Количество"; + // + // comboBoxComponent + // + this.comboBoxComponent.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.comboBoxComponent.FormattingEnabled = true; + this.comboBoxComponent.Location = new System.Drawing.Point(128, 35); + this.comboBoxComponent.Name = "comboBoxComponent"; + this.comboBoxComponent.Size = new System.Drawing.Size(265, 28); + this.comboBoxComponent.TabIndex = 2; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(128, 82); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(265, 27); + this.textBoxCount.TabIndex = 3; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(34, 130); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(114, 48); + this.buttonSave.TabIndex = 4; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(302, 130); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(114, 48); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormGiftComponent + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(461, 190); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.comboBoxComponent); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.labelComponent); + this.Name = "FormGiftComponent"; + this.Text = "Компонент изделия"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelComponent; + private Label labelCount; + private ComboBox comboBoxComponent; + private TextBox textBoxCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormGiftComponent.cs b/GiftShop/GiftShop/FormGiftComponent.cs new file mode 100644 index 0000000..317ef4a --- /dev/null +++ b/GiftShop/GiftShop/FormGiftComponent.cs @@ -0,0 +1,90 @@ +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.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 GiftShopView +{ + public partial class FormGiftComponent : 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 FormGiftComponent(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/GiftShop/GiftShop/FormGiftComponent.resx b/GiftShop/GiftShop/FormGiftComponent.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/GiftShop/GiftShop/FormGiftComponent.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/GiftShop/GiftShop/FormGifts.Designer.cs b/GiftShop/GiftShop/FormGifts.Designer.cs new file mode 100644 index 0000000..95914a9 --- /dev/null +++ b/GiftShop/GiftShop/FormGifts.Designer.cs @@ -0,0 +1,116 @@ +namespace GiftShopView +{ + partial class FormGifts + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Control; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(444, 380); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(520, 40); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(170, 29); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(520, 115); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(170, 29); + this.buttonUpdate.TabIndex = 2; + this.buttonUpdate.Text = "Изменить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(520, 201); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(170, 29); + this.buttonDel.TabIndex = 3; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(520, 287); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(170, 29); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormGifts + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(709, 407); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormGifts"; + this.Text = "Изделия"; + this.Load += new System.EventHandler(this.FormGifts_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpdate; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormGifts.cs b/GiftShop/GiftShop/FormGifts.cs new file mode 100644 index 0000000..06e5023 --- /dev/null +++ b/GiftShop/GiftShop/FormGifts.cs @@ -0,0 +1,115 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +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 GiftShopView +{ + public partial class FormGifts : Form + { + private readonly ILogger _logger; + private readonly IGiftLogic _logic; + public FormGifts(ILogger logger, IGiftLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormGifts_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["GiftName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["GiftComponents"].Visible = false; + } + _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(FormGift)); + if (service is FormGift 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(FormGift)); + if (service is FormGift 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 GiftBindingModel + { + 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/GiftShop/GiftShop/FormGifts.resx b/GiftShop/GiftShop/FormGifts.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/GiftShop/GiftShop/FormGifts.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/GiftShop/GiftShop/FormMain.Designer.cs b/GiftShop/GiftShop/FormMain.Designer.cs new file mode 100644 index 0000000..349e793 --- /dev/null +++ b/GiftShop/GiftShop/FormMain.Designer.cs @@ -0,0 +1,177 @@ +namespace GiftShopView +{ + 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() + { + this.menuStrip1 = new System.Windows.Forms.MenuStrip(); + this.справочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.компонентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonCreateOrder = new System.Windows.Forms.Button(); + this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); + this.buttonOrderReady = new System.Windows.Forms.Button(); + this.buttonIssuedOrder = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + this.menuStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // menuStrip1 + // + this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); + this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.справочникиToolStripMenuItem}); + this.menuStrip1.Location = new System.Drawing.Point(0, 0); + this.menuStrip1.Name = "menuStrip1"; + this.menuStrip1.Size = new System.Drawing.Size(1367, 28); + this.menuStrip1.TabIndex = 0; + this.menuStrip1.Text = "menuStrip1"; + // + // справочникиToolStripMenuItem + // + this.справочникиToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.компонентыToolStripMenuItem, + this.изделияToolStripMenuItem}); + this.справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; + this.справочникиToolStripMenuItem.Size = new System.Drawing.Size(117, 24); + this.справочникиToolStripMenuItem.Text = "Справочники"; + // + // компонентыToolStripMenuItem + // + this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(182, 26); + this.компонентыToolStripMenuItem.Text = "Компоненты"; + this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); + // + // изделияToolStripMenuItem + // + this.изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; + this.изделияToolStripMenuItem.Size = new System.Drawing.Size(182, 26); + this.изделияToolStripMenuItem.Text = "Изделия"; + this.изделияToolStripMenuItem.Click += new System.EventHandler(this.ИзделияToolStripMenuItem_Click); + // + // dataGridView + // + this.dataGridView.BackgroundColor = System.Drawing.SystemColors.Control; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 41); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 51; + this.dataGridView.RowTemplate.Height = 29; + this.dataGridView.Size = new System.Drawing.Size(1123, 423); + this.dataGridView.TabIndex = 1; + // + // buttonCreateOrder + // + this.buttonCreateOrder.Location = new System.Drawing.Point(1155, 67); + this.buttonCreateOrder.Name = "buttonCreateOrder"; + this.buttonCreateOrder.Size = new System.Drawing.Size(189, 29); + this.buttonCreateOrder.TabIndex = 2; + this.buttonCreateOrder.Text = "Создать заказ"; + this.buttonCreateOrder.UseVisualStyleBackColor = true; + this.buttonCreateOrder.Click += new System.EventHandler(this.ButtonCreateOrder_Click); + // + // buttonTakeOrderInWork + // + this.buttonTakeOrderInWork.Location = new System.Drawing.Point(1155, 136); + this.buttonTakeOrderInWork.Name = "buttonTakeOrderInWork"; + this.buttonTakeOrderInWork.Size = new System.Drawing.Size(189, 29); + this.buttonTakeOrderInWork.TabIndex = 3; + this.buttonTakeOrderInWork.Text = "Отдать на выполнение"; + this.buttonTakeOrderInWork.UseVisualStyleBackColor = true; + this.buttonTakeOrderInWork.Click += new System.EventHandler(this.ButtonTakeOrderInWork_Click); + // + // buttonOrderReady + // + this.buttonOrderReady.Location = new System.Drawing.Point(1155, 211); + this.buttonOrderReady.Name = "buttonOrderReady"; + this.buttonOrderReady.Size = new System.Drawing.Size(189, 29); + this.buttonOrderReady.TabIndex = 4; + this.buttonOrderReady.Text = "Заказ готов"; + this.buttonOrderReady.UseVisualStyleBackColor = true; + this.buttonOrderReady.Click += new System.EventHandler(this.ButtonOrderReady_Click); + // + // buttonIssuedOrder + // + this.buttonIssuedOrder.Location = new System.Drawing.Point(1155, 290); + this.buttonIssuedOrder.Name = "buttonIssuedOrder"; + this.buttonIssuedOrder.Size = new System.Drawing.Size(189, 29); + this.buttonIssuedOrder.TabIndex = 5; + this.buttonIssuedOrder.Text = "Заказ выдан"; + this.buttonIssuedOrder.UseVisualStyleBackColor = true; + this.buttonIssuedOrder.Click += new System.EventHandler(this.ButtonIssuedOrder_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(1155, 356); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(189, 29); + this.buttonRef.TabIndex = 6; + this.buttonRef.Text = "Обновить список"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(1367, 471); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonIssuedOrder); + this.Controls.Add(this.buttonOrderReady); + this.Controls.Add(this.buttonTakeOrderInWork); + this.Controls.Add(this.buttonCreateOrder); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.menuStrip1); + this.MainMenuStrip = this.menuStrip1; + this.Name = "FormMain"; + this.Text = "Магазин подарков"; + this.Load += new System.EventHandler(this.FormMain_Load); + this.menuStrip1.ResumeLayout(false); + this.menuStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private MenuStrip menuStrip1; + 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 buttonRef; + } +} \ No newline at end of file diff --git a/GiftShop/GiftShop/FormMain.cs b/GiftShop/GiftShop/FormMain.cs new file mode 100644 index 0000000..aed5cf8 --- /dev/null +++ b/GiftShop/GiftShop/FormMain.cs @@ -0,0 +1,178 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopDataModels.Enums; +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 GiftShopView +{ + 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() + { + _logger.LogInformation("Загрузка заказов"); + try + { + var list = _orderLogic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["GiftId"].Visible = false; + dataGridView.Columns["GiftName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка заказов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки заказов"); + 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(FormGifts)); + + if (service is FormGifts 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("Заказ №{id}. Меняется статус на 'В работе'", id); + try + { + var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = id, + GiftId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["GiftId"].Value), + GiftName = dataGridView.SelectedRows[0].Cells["GiftName"].Value.ToString(), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка передачи заказа в работу"); + 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("Заказ №{id}. Меняется статус на 'Готов'", id); + try + { + var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel + { + Id = id, + GiftId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["GiftId"].Value), + GiftName = dataGridView.SelectedRows[0].Cells["GiftName"].Value.ToString(), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа"); + 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("Заказ №{id}. Меняется статус на 'Выдан'", id); + try + { + var operationResult = _orderLogic.FinishOrder(new OrderBindingModel + { + Id = id, + GiftId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["GiftId"].Value), + GiftName = dataGridView.SelectedRows[0].Cells["GiftName"].Value.ToString(), + Status = Enum.Parse(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()), + Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value), + Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), + DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), + }); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + _logger.LogInformation("Заказ №{id} выдан", id); + 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/GiftShop/GiftShop/FormMain.resx b/GiftShop/GiftShop/FormMain.resx new file mode 100644 index 0000000..938108a --- /dev/null +++ b/GiftShop/GiftShop/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/GiftShop/GiftShop/GiftShopView.csproj b/GiftShop/GiftShop/GiftShopView.csproj new file mode 100644 index 0000000..b70bb37 --- /dev/null +++ b/GiftShop/GiftShop/GiftShopView.csproj @@ -0,0 +1,24 @@ + + + + WinExe + net6.0-windows + enable + true + enable + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/GiftShop/GiftShop/Program.cs b/GiftShop/GiftShop/Program.cs new file mode 100644 index 0000000..d031057 --- /dev/null +++ b/GiftShop/GiftShop/Program.cs @@ -0,0 +1,48 @@ + +using Microsoft.Extensions.DependencyInjection; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.StoragesContracts; +using GiftShopListImplement.Implements; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using GiftShopBusinessLogic.BusinessLogics; + +namespace GiftShopView +{ + internal static class Program + { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; + [STAThread] + static void Main() + { + ApplicationConfiguration.Initialize(); + 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(); + } + } +} diff --git a/GiftShop/GiftShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/GiftShop/GiftShopBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..1dd3088 --- /dev/null +++ b/GiftShop/GiftShopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,115 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using NLog; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ILogger = Microsoft.Extensions.Logging.ILogger; + +namespace GiftShopBusinessLogic.BusinessLogics +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + public List? ReadList(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{ Id}", model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}. Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия компонента", + nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id: { Id} ", model.ComponentName, model.Cost, model.Id); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/GiftShop/GiftShopBusinessLogic/BusinessLogics/GiftLogic.cs b/GiftShop/GiftShopBusinessLogic/BusinessLogics/GiftLogic.cs new file mode 100644 index 0000000..0136d16 --- /dev/null +++ b/GiftShop/GiftShopBusinessLogic/BusinessLogics/GiftLogic.cs @@ -0,0 +1,142 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopBusinessLogic.BusinessLogics +{ + public class GiftLogic: IGiftLogic + { + private readonly ILogger _logger; + + private readonly IGiftStorage _giftStorage; + + public GiftLogic(ILogger logger, IGiftStorage giftStorage) + { + _logger = logger; + _giftStorage = giftStorage; + } + + public bool Create(GiftBindingModel model) + { + CheckModel(model); + + if (_giftStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Delete(GiftBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_giftStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + + return true; + } + + public GiftViewModel? ReadElement(GiftSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. GiftName:{GiftName}.Id:{ Id}", model.GiftName, model.Id); + + var element = _giftStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + + return element; + } + + public List? ReadList(GiftSearchModel? model) + { + _logger.LogInformation("ReadList. GiftName:{GiftName}.Id:{ Id}", model?.GiftName, model?.Id); + + var list = model == null ? _giftStorage.GetFullList() : _giftStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public bool Update(GiftBindingModel model) + { + CheckModel(model); + + if (_giftStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + private void CheckModel(GiftBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (!withParams) + { + return; + } + + if (string.IsNullOrEmpty(model.GiftName)) + { + throw new ArgumentNullException("Нет названия изделия", nameof(model.GiftName)); + } + + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price)); + } + + _logger.LogInformation("Gift. GiftName:{GiftName}.Price:{ Cost}. Id: { Id}", model.GiftName, model.Price, model.Id); + + var element = _giftStorage.GetElement(new GiftSearchModel + { + GiftName = model.GiftName + }); + + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Изделие с таким названием уже есть"); + } + } + } +} diff --git a/GiftShop/GiftShopBusinessLogic/BusinessLogics/OrderLogic.cs b/GiftShop/GiftShopBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..a743dcd --- /dev/null +++ b/GiftShop/GiftShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,135 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.BusinessLogicsContracts; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + + model.Status = OrderStatus.Принят; + + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) + { + CheckModel(model); + + if (model.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + + model.Status = newStatus; + + if (model.Status == OrderStatus.Выдан) + model.DateImplement = DateTime.Now; + + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выполняется); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Готов); + } + + public bool FinishOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выдан); + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. OrderId:{Id}", model?.Id); + + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (!withParams) + { + return; + } + + if (model.GiftId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.GiftId)); + } + + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); + } + + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + + _logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. EngineId: { EngineId}", model.Id, model.Sum, model.GiftId); + } + } +} diff --git a/GiftShop/GiftShopBusinessLogic/GiftShopBusinessLogic.csproj b/GiftShop/GiftShopBusinessLogic/GiftShopBusinessLogic.csproj new file mode 100644 index 0000000..1c060b5 --- /dev/null +++ b/GiftShop/GiftShopBusinessLogic/GiftShopBusinessLogic.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + diff --git a/GiftShop/GiftShopContracts/BindingModels/ComponentBindingModel.cs b/GiftShop/GiftShopContracts/BindingModels/ComponentBindingModel.cs new file mode 100644 index 0000000..40ba26c --- /dev/null +++ b/GiftShop/GiftShopContracts/BindingModels/ComponentBindingModel.cs @@ -0,0 +1,16 @@ +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.BindingModels +{ + public class ComponentBindingModel : IComponentModel + { + public int Id { get; set; } + public string ComponentName { get; set; } = string.Empty; + public double Cost { get; set; } + } +} diff --git a/GiftShop/GiftShopContracts/BindingModels/GiftBindingModel.cs b/GiftShop/GiftShopContracts/BindingModels/GiftBindingModel.cs new file mode 100644 index 0000000..3b71f06 --- /dev/null +++ b/GiftShop/GiftShopContracts/BindingModels/GiftBindingModel.cs @@ -0,0 +1,22 @@ +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.BindingModels +{ + public class GiftBindingModel : IGiftModel + { + public int Id { get; set; } + public string GiftName { get; set; } = string.Empty; + public double Price { get; set; } + public Dictionary GiftComponents + { + get; + set; + } = new(); + } + +} diff --git a/GiftShop/GiftShopContracts/BindingModels/OrderBindingModel.cs b/GiftShop/GiftShopContracts/BindingModels/OrderBindingModel.cs new file mode 100644 index 0000000..ffcced2 --- /dev/null +++ b/GiftShop/GiftShopContracts/BindingModels/OrderBindingModel.cs @@ -0,0 +1,22 @@ +using GiftShopDataModels.Enums; +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.BindingModels +{ + public class OrderBindingModel : IOrderModel + { + public int Id { get; set; } + public int GiftId { get; set; } + public string GiftName { get; set; } = string.Empty; + public int Count { get; set; } + public double Sum { get; set; } + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + } +} diff --git a/GiftShop/GiftShopContracts/BusinessLogicsContracts/IComponentLogic.cs b/GiftShop/GiftShopContracts/BusinessLogicsContracts/IComponentLogic.cs new file mode 100644 index 0000000..31b83eb --- /dev/null +++ b/GiftShop/GiftShopContracts/BusinessLogicsContracts/IComponentLogic.cs @@ -0,0 +1,20 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.BusinessLogicsContracts +{ + public interface IComponentLogic + { + List? ReadList(ComponentSearchModel? model); + ComponentViewModel? ReadElement(ComponentSearchModel model); + bool Create(ComponentBindingModel model); + bool Update(ComponentBindingModel model); + bool Delete(ComponentBindingModel model); + } +} diff --git a/GiftShop/GiftShopContracts/BusinessLogicsContracts/IGiftLogic.cs b/GiftShop/GiftShopContracts/BusinessLogicsContracts/IGiftLogic.cs new file mode 100644 index 0000000..a58cfd5 --- /dev/null +++ b/GiftShop/GiftShopContracts/BusinessLogicsContracts/IGiftLogic.cs @@ -0,0 +1,21 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.BusinessLogicsContracts +{ + public interface IGiftLogic + { + List? ReadList(GiftSearchModel? model); + GiftViewModel? ReadElement(GiftSearchModel model); + bool Create(GiftBindingModel model); + bool Update(GiftBindingModel model); + bool Delete(GiftBindingModel model); + } + +} diff --git a/GiftShop/GiftShopContracts/BusinessLogicsContracts/IOrderLogic.cs b/GiftShop/GiftShopContracts/BusinessLogicsContracts/IOrderLogic.cs new file mode 100644 index 0000000..c342b61 --- /dev/null +++ b/GiftShop/GiftShopContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -0,0 +1,20 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.BusinessLogicsContracts +{ + public interface IOrderLogic + { + List? ReadList(OrderSearchModel? model); + bool CreateOrder(OrderBindingModel model); + bool TakeOrderInWork(OrderBindingModel model); + bool FinishOrder(OrderBindingModel model); + bool DeliveryOrder(OrderBindingModel model); + } +} diff --git a/GiftShop/GiftShopContracts/GiftShopContracts.csproj b/GiftShop/GiftShopContracts/GiftShopContracts.csproj new file mode 100644 index 0000000..5d9addc --- /dev/null +++ b/GiftShop/GiftShopContracts/GiftShopContracts.csproj @@ -0,0 +1,20 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + diff --git a/GiftShop/GiftShopContracts/SearchModels/ComponentSearchModel.cs b/GiftShop/GiftShopContracts/SearchModels/ComponentSearchModel.cs new file mode 100644 index 0000000..253a3e6 --- /dev/null +++ b/GiftShop/GiftShopContracts/SearchModels/ComponentSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.SearchModels +{ + public class ComponentSearchModel + { + public int? Id { get; set; } + public string? ComponentName { get; set; } + } +} diff --git a/GiftShop/GiftShopContracts/SearchModels/GiftSearchModel.cs b/GiftShop/GiftShopContracts/SearchModels/GiftSearchModel.cs new file mode 100644 index 0000000..d912c9a --- /dev/null +++ b/GiftShop/GiftShopContracts/SearchModels/GiftSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.SearchModels +{ + public class GiftSearchModel + { + public int? Id { get; set; } + public string? GiftName { get; set; } + } +} diff --git a/GiftShop/GiftShopContracts/SearchModels/OrderSearchModel.cs b/GiftShop/GiftShopContracts/SearchModels/OrderSearchModel.cs new file mode 100644 index 0000000..9a3294b --- /dev/null +++ b/GiftShop/GiftShopContracts/SearchModels/OrderSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.SearchModels +{ + public class OrderSearchModel + { + public int? Id { get; set; } + } +} diff --git a/GiftShop/GiftShopContracts/StoragesContracts/IComponentStorage.cs b/GiftShop/GiftShopContracts/StoragesContracts/IComponentStorage.cs new file mode 100644 index 0000000..4a46def --- /dev/null +++ b/GiftShop/GiftShopContracts/StoragesContracts/IComponentStorage.cs @@ -0,0 +1,21 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.StoragesContracts +{ + public interface IComponentStorage + { + List GetFullList(); + List GetFilteredList(ComponentSearchModel model); + ComponentViewModel? GetElement(ComponentSearchModel model); + ComponentViewModel? Insert(ComponentBindingModel model); + ComponentViewModel? Update(ComponentBindingModel model); + ComponentViewModel? Delete(ComponentBindingModel model); + } +} diff --git a/GiftShop/GiftShopContracts/StoragesContracts/IGiftStorage.cs b/GiftShop/GiftShopContracts/StoragesContracts/IGiftStorage.cs new file mode 100644 index 0000000..2426f03 --- /dev/null +++ b/GiftShop/GiftShopContracts/StoragesContracts/IGiftStorage.cs @@ -0,0 +1,21 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.StoragesContracts +{ + public interface IGiftStorage + { + List GetFullList(); + List GetFilteredList(GiftSearchModel model); + GiftViewModel? GetElement(GiftSearchModel model); + GiftViewModel? Insert(GiftBindingModel model); + GiftViewModel? Update(GiftBindingModel model); + GiftViewModel? Delete(GiftBindingModel model); + } +} diff --git a/GiftShop/GiftShopContracts/StoragesContracts/IOrderStorage.cs b/GiftShop/GiftShopContracts/StoragesContracts/IOrderStorage.cs new file mode 100644 index 0000000..7885ff7 --- /dev/null +++ b/GiftShop/GiftShopContracts/StoragesContracts/IOrderStorage.cs @@ -0,0 +1,21 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.StoragesContracts +{ + public interface IOrderStorage + { + List GetFullList(); + List GetFilteredList(OrderSearchModel model); + OrderViewModel? GetElement(OrderSearchModel model); + OrderViewModel? Insert(OrderBindingModel model); + OrderViewModel? Update(OrderBindingModel model); + OrderViewModel? Delete(OrderBindingModel model); + } +} diff --git a/GiftShop/GiftShopContracts/ViewModels/ComponentViewModel.cs b/GiftShop/GiftShopContracts/ViewModels/ComponentViewModel.cs new file mode 100644 index 0000000..148b21a --- /dev/null +++ b/GiftShop/GiftShopContracts/ViewModels/ComponentViewModel.cs @@ -0,0 +1,19 @@ +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.ViewModels +{ + public class ComponentViewModel : IComponentModel + { + public int Id { get; set; } + [DisplayName("Название компонента")] + public string ComponentName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Cost { get; set; } + } +} diff --git a/GiftShop/GiftShopContracts/ViewModels/GiftViewModel.cs b/GiftShop/GiftShopContracts/ViewModels/GiftViewModel.cs new file mode 100644 index 0000000..2f27ee1 --- /dev/null +++ b/GiftShop/GiftShopContracts/ViewModels/GiftViewModel.cs @@ -0,0 +1,24 @@ +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.ViewModels +{ + public class GiftViewModel : IGiftModel + { + public int Id { get; set; } + [DisplayName("Название изделия")] + public string GiftName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Price { get; set; } + public Dictionary GiftComponents + { + get; + set; + } = new(); + } +} diff --git a/GiftShop/GiftShopContracts/ViewModels/OrderViewModel.cs b/GiftShop/GiftShopContracts/ViewModels/OrderViewModel.cs new file mode 100644 index 0000000..86eb896 --- /dev/null +++ b/GiftShop/GiftShopContracts/ViewModels/OrderViewModel.cs @@ -0,0 +1,30 @@ +using GiftShopDataModels.Enums; +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopContracts.ViewModels +{ + public class OrderViewModel : IOrderModel + { + [DisplayName("Номер")] + public int Id { get; set; } + public int GiftId { get; set; } + [DisplayName("Изделие")] + public string GiftName { get; set; } = string.Empty; + [DisplayName("Количество")] + public int Count { get; set; } + [DisplayName("Сумма")] + public double Sum { get; set; } + [DisplayName("Статус")] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [DisplayName("Дата создания")] + public DateTime DateCreate { get; set; } = DateTime.Now; + [DisplayName("Дата выполнения")] + public DateTime? DateImplement { get; set; } + } +} diff --git a/GiftShop/GiftShopDataModels/Enums/OrderStatus.cs b/GiftShop/GiftShopDataModels/Enums/OrderStatus.cs new file mode 100644 index 0000000..8e1a6fc --- /dev/null +++ b/GiftShop/GiftShopDataModels/Enums/OrderStatus.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDataModels.Enums +{ + public enum OrderStatus + { + Неизвестен = -1, + Принят = 0, + Выполняется = 1, + Готов = 2, + Выдан = 3 + } +} diff --git a/GiftShop/GiftShopDataModels/GiftShopDataModels.csproj b/GiftShop/GiftShopDataModels/GiftShopDataModels.csproj new file mode 100644 index 0000000..ea7b0c2 --- /dev/null +++ b/GiftShop/GiftShopDataModels/GiftShopDataModels.csproj @@ -0,0 +1,16 @@ + + + + net6.0 + enable + enable + + + + + + + + + + diff --git a/GiftShop/GiftShopDataModels/IId.cs b/GiftShop/GiftShopDataModels/IId.cs new file mode 100644 index 0000000..adaaa8d --- /dev/null +++ b/GiftShop/GiftShopDataModels/IId.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDataModels +{ + public interface IId + { + int Id { get; } + } +} diff --git a/GiftShop/GiftShopDataModels/Models/IComponentModel.cs b/GiftShop/GiftShopDataModels/Models/IComponentModel.cs new file mode 100644 index 0000000..41afed2 --- /dev/null +++ b/GiftShop/GiftShopDataModels/Models/IComponentModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDataModels.Models +{ + public interface IComponentModel: IId + { + string ComponentName { get; } + double Cost { get; } + } +} diff --git a/GiftShop/GiftShopDataModels/Models/IGiftModel.cs b/GiftShop/GiftShopDataModels/Models/IGiftModel.cs new file mode 100644 index 0000000..f8cfaa3 --- /dev/null +++ b/GiftShop/GiftShopDataModels/Models/IGiftModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDataModels.Models +{ + public interface IGiftModel: IId + { + string GiftName { get; } + double Price { get; } + Dictionary GiftComponents { get; } + } +} diff --git a/GiftShop/GiftShopDataModels/Models/IOrderModel.cs b/GiftShop/GiftShopDataModels/Models/IOrderModel.cs new file mode 100644 index 0000000..10dcf38 --- /dev/null +++ b/GiftShop/GiftShopDataModels/Models/IOrderModel.cs @@ -0,0 +1,19 @@ +using GiftShopDataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopDataModels.Models +{ + public interface IOrderModel : IId + { + int GiftId { get; } + int Count { get; } + double Sum { get; } + OrderStatus Status { get; } + DateTime DateCreate { get; } + DateTime? DateImplement { get; } + } +} diff --git a/GiftShop/GiftShopListImplement/DataListSingleton.cs b/GiftShop/GiftShopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..4b172ec --- /dev/null +++ b/GiftShop/GiftShopListImplement/DataListSingleton.cs @@ -0,0 +1,32 @@ +using GiftShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Gifts { get; set; } + + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Gifts = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} diff --git a/GiftShop/GiftShopListImplement/GiftShopListImplement.csproj b/GiftShop/GiftShopListImplement/GiftShopListImplement.csproj new file mode 100644 index 0000000..6b5c598 --- /dev/null +++ b/GiftShop/GiftShopListImplement/GiftShopListImplement.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/GiftShop/GiftShopListImplement/Implements/ComponentStorage.cs b/GiftShop/GiftShopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..55d2d2a --- /dev/null +++ b/GiftShop/GiftShopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,102 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopListImplement.Models; + +namespace GiftShopListImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataListSingleton _source; + public ComponentStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Components) + { + result.Add(component.GetViewModel); + } + return result; + } + public List GetFilteredList(ComponentSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComponentName)) + { + return result; + } + foreach (var component in _source.Components) + { + if (component.ComponentName.Contains(model.ComponentName)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Components) + { + if ((!string.IsNullOrEmpty(model.ComponentName) && + component.ComponentName == model.ComponentName) || + (model.Id.HasValue && component.Id == model.Id)) + { + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Components) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + _source.Components.Add(newComponent); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + foreach (var component in _source.Components) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + for (int i = 0; i < _source.Components.Count; ++i) + { + if (_source.Components[i].Id == model.Id) + { + var element = _source.Components[i]; + _source.Components.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/GiftShop/GiftShopListImplement/Implements/GiftStorage.cs b/GiftShop/GiftShopListImplement/Implements/GiftStorage.cs new file mode 100644 index 0000000..a7df519 --- /dev/null +++ b/GiftShop/GiftShopListImplement/Implements/GiftStorage.cs @@ -0,0 +1,126 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopListImplement.Implements +{ + public class GiftStorage : IGiftStorage + { + private readonly DataListSingleton _source; + + public GiftStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public GiftViewModel? Delete(GiftBindingModel model) + { + for (int i = 0; i < _source.Gifts.Count; ++i) + { + if (_source.Gifts[i].Id == model.Id) + { + var element = _source.Gifts[i]; + _source.Gifts.RemoveAt(i); + return element.GetViewModel; + } + } + + return null; + } + + public GiftViewModel? GetElement(GiftSearchModel model) + { + if (string.IsNullOrEmpty(model.GiftName) && !model.Id.HasValue) + { + return null; + } + + foreach (var package in _source.Gifts) + { + if ((!string.IsNullOrEmpty(model.GiftName) && package.GiftName == model.GiftName) || (model.Id.HasValue && package.Id == model.Id)) + { + return package.GetViewModel; + } + } + + return null; + } + + public List GetFilteredList(GiftSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.GiftName)) + { + return result; + } + + foreach (var package in _source.Gifts) + { + if (package.GiftName.Contains(model.GiftName)) + { + result.Add(package.GetViewModel); + } + } + + return result; + } + + public List GetFullList() + { + var result = new List(); + + foreach (var package in _source.Gifts) + { + result.Add(package.GetViewModel); + } + + return result; + } + + public GiftViewModel? Insert(GiftBindingModel model) + { + model.Id = 1; + + foreach (var package in _source.Gifts) + { + if (model.Id <= package.Id) + { + model.Id = package.Id + 1; + } + } + + var newGift = Gift.Create(model); + + if (newGift == null) + { + return null; + } + + _source.Gifts.Add(newGift); + + return newGift.GetViewModel; + } + + public GiftViewModel? Update(GiftBindingModel model) + { + foreach (var package in _source.Gifts) + { + if (package.Id == model.Id) + { + package.Update(model); + return package.GetViewModel; + } + } + + return null; + } + } +} diff --git a/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs b/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1f2d3d0 --- /dev/null +++ b/GiftShop/GiftShopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,124 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.SearchModels; +using GiftShopContracts.StoragesContracts; +using GiftShopContracts.ViewModels; +using GiftShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return element.GetViewModel; + } + } + + return null; + } + + 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 order.GetViewModel; + } + } + + return null; + } + + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + + if (!model.Id.HasValue) + { + return result; + } + + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + result.Add(order.GetViewModel); + } + } + + return result; + } + + public List GetFullList() + { + var result = new List(); + + foreach (var order in _source.Orders) + { + result.Add(order.GetViewModel); + } + + return result; + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + _source.Orders.Add(newOrder); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return order.GetViewModel; + } + } + + return null; + } + } +} diff --git a/GiftShop/GiftShopListImplement/Models/Component.cs b/GiftShop/GiftShopListImplement/Models/Component.cs new file mode 100644 index 0000000..d06dd6e --- /dev/null +++ b/GiftShop/GiftShopListImplement/Models/Component.cs @@ -0,0 +1,46 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopListImplement.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/GiftShop/GiftShopListImplement/Models/Gift.cs b/GiftShop/GiftShopListImplement/Models/Gift.cs new file mode 100644 index 0000000..821d2d6 --- /dev/null +++ b/GiftShop/GiftShopListImplement/Models/Gift.cs @@ -0,0 +1,56 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopListImplement.Models +{ + public class Gift : IGiftModel + { + public string GiftName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public Dictionary GiftComponents { get; private set; } = new Dictionary(); + + public int Id { get; private set; } + + public static Gift? Create(GiftBindingModel? model) + { + if (model == null) + { + return null; + } + return new Gift() + { + Id = model.Id, + GiftName = model.GiftName, + Price = model.Price, + GiftComponents = model.GiftComponents + }; + } + + public void Update(GiftBindingModel? model) + { + if (model == null) + { + return; + } + GiftName = model.GiftName; + Price = model.Price; + GiftComponents = model.GiftComponents; + } + + public GiftViewModel GetViewModel => new() + { + Id = Id, + GiftName = GiftName, + Price = Price, + GiftComponents = GiftComponents + }; + } +} diff --git a/GiftShop/GiftShopListImplement/Models/Order.cs b/GiftShop/GiftShopListImplement/Models/Order.cs new file mode 100644 index 0000000..a15047b --- /dev/null +++ b/GiftShop/GiftShopListImplement/Models/Order.cs @@ -0,0 +1,71 @@ +using GiftShopContracts.BindingModels; +using GiftShopContracts.ViewModels; +using GiftShopDataModels.Enums; +using GiftShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GiftShopListImplement.Models +{ + public class Order : IOrderModel + { + public int GiftId { get; private set; } + + public string GiftName { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + GiftId = model.GiftId, + GiftName = model.GiftName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + GiftId = GiftId, + GiftName = GiftName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +}