diff --git a/IceCreamShop/IceCreamShop.sln b/IceCreamShop/IceCreamShop.sln index bef78df..70f6641 100644 --- a/IceCreamShop/IceCreamShop.sln +++ b/IceCreamShop/IceCreamShop.sln @@ -3,7 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34309.116 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShop", "IceCreamShop\IceCreamShop.csproj", "{CD111C92-FD71-4F7E-A7E7-03C273B634E6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IceCreamShop", "IceCreamShop\IceCreamShop.csproj", "{CD111C92-FD71-4F7E-A7E7-03C273B634E6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopDataModels", "IceCreamShopDataModels\IceCreamShopDataModels.csproj", "{0979632B-BB70-441E-9109-C52DBA5A85B2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopContracts", "IceCreamShopContracts\IceCreamShopContracts.csproj", "{8D344A7A-951D-496B-BC8A-DF18A06B1DF0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopBusinessLogic", "IceCreamShopBusinessLogic\IceCreamShopBusinessLogic.csproj", "{6E71A941-D9B0-47F8-A180-78D544754C4F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IceCreamShopListImplement", "IceCreamShopListImplement\IceCreamShopListImplement.csproj", "{566BA4F5-7316-4747-A745-FCA934EBA9E0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +23,22 @@ Global {CD111C92-FD71-4F7E-A7E7-03C273B634E6}.Debug|Any CPU.Build.0 = Debug|Any CPU {CD111C92-FD71-4F7E-A7E7-03C273B634E6}.Release|Any CPU.ActiveCfg = Release|Any CPU {CD111C92-FD71-4F7E-A7E7-03C273B634E6}.Release|Any CPU.Build.0 = Release|Any CPU + {0979632B-BB70-441E-9109-C52DBA5A85B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0979632B-BB70-441E-9109-C52DBA5A85B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0979632B-BB70-441E-9109-C52DBA5A85B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0979632B-BB70-441E-9109-C52DBA5A85B2}.Release|Any CPU.Build.0 = Release|Any CPU + {8D344A7A-951D-496B-BC8A-DF18A06B1DF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8D344A7A-951D-496B-BC8A-DF18A06B1DF0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8D344A7A-951D-496B-BC8A-DF18A06B1DF0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8D344A7A-951D-496B-BC8A-DF18A06B1DF0}.Release|Any CPU.Build.0 = Release|Any CPU + {6E71A941-D9B0-47F8-A180-78D544754C4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6E71A941-D9B0-47F8-A180-78D544754C4F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6E71A941-D9B0-47F8-A180-78D544754C4F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6E71A941-D9B0-47F8-A180-78D544754C4F}.Release|Any CPU.Build.0 = Release|Any CPU + {566BA4F5-7316-4747-A745-FCA934EBA9E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {566BA4F5-7316-4747-A745-FCA934EBA9E0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {566BA4F5-7316-4747-A745-FCA934EBA9E0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {566BA4F5-7316-4747-A745-FCA934EBA9E0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IceCreamShop/IceCreamShop/ComponentForm.Designer.cs b/IceCreamShop/IceCreamShop/ComponentForm.Designer.cs new file mode 100644 index 0000000..52c0a82 --- /dev/null +++ b/IceCreamShop/IceCreamShop/ComponentForm.Designer.cs @@ -0,0 +1,121 @@ +namespace IceCreamShop +{ + partial class ComponentForm + { + /// + /// 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() + { + NameTextBox = new TextBox(); + CostTextBox = new TextBox(); + NameLabel = new Label(); + CostLabel = new Label(); + CancelButton = new Button(); + SaveButton = new Button(); + SuspendLayout(); + // + // NameTextBox + // + NameTextBox.Location = new Point(80, 10); + NameTextBox.Name = "NameTextBox"; + NameTextBox.Size = new Size(243, 23); + NameTextBox.TabIndex = 0; + // + // CostTextBox + // + CostTextBox.Location = new Point(80, 42); + CostTextBox.Name = "CostTextBox"; + CostTextBox.Size = new Size(119, 23); + CostTextBox.TabIndex = 1; + // + // NameLabel + // + NameLabel.AutoSize = true; + NameLabel.Location = new Point(7, 20); + NameLabel.Name = "NameLabel"; + NameLabel.Size = new Size(59, 15); + NameLabel.TabIndex = 2; + NameLabel.Text = "Название"; + // + // CostLabel + // + CostLabel.AutoSize = true; + CostLabel.Location = new Point(7, 49); + CostLabel.Name = "CostLabel"; + CostLabel.Size = new Size(35, 15); + CostLabel.TabIndex = 3; + CostLabel.Text = "Цена"; + // + // CancelButton + // + CancelButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + CancelButton.Location = new Point(247, 68); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 4; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += CancelButton_Click; + // + // SaveButton + // + SaveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + SaveButton.Location = new Point(165, 68); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 5; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // ComponentForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + AutoSizeMode = AutoSizeMode.GrowAndShrink; + ClientSize = new Size(333, 97); + Controls.Add(SaveButton); + Controls.Add(CancelButton); + Controls.Add(CostLabel); + Controls.Add(NameLabel); + Controls.Add(CostTextBox); + Controls.Add(NameTextBox); + Name = "ComponentForm"; + Text = "ComponentForm"; + Load += ComponentForm_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox NameTextBox; + private TextBox CostTextBox; + private Label NameLabel; + private Label CostLabel; + private Button CancelButton; + private Button SaveButton; + } +} diff --git a/IceCreamShop/IceCreamShop/ComponentForm.cs b/IceCreamShop/IceCreamShop/ComponentForm.cs new file mode 100644 index 0000000..f27c098 --- /dev/null +++ b/IceCreamShop/IceCreamShop/ComponentForm.cs @@ -0,0 +1,91 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using Microsoft.Extensions.Logging; +using System.Windows.Forms; + +namespace IceCreamShop +{ + public partial class ComponentForm : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public ComponentForm(ILogger logger, IComponentLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void ComponentForm_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation(" "); + var view = _logic.ReadElement(new ComponentSearchModel + { + Id = + _id.Value + }); + if (view != null) + { + NameTextBox.Text = view.ComponentName; + CostTextBox.Text = view.Cost.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, " "); + MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(NameTextBox.Text)) + { + MessageBox.Show(" ", "", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation(" "); + try + { + var model = new ComponentBindingModel + { + Id = _id ?? 0, + ComponentName = NameTextBox.Text, + Cost = Convert.ToDouble(CostTextBox.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 CancelButton_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/IceCreamShop/IceCreamShop/Form1.resx b/IceCreamShop/IceCreamShop/ComponentForm.resx similarity index 93% rename from IceCreamShop/IceCreamShop/Form1.resx rename to IceCreamShop/IceCreamShop/ComponentForm.resx index 1af7de1..af32865 100644 --- a/IceCreamShop/IceCreamShop/Form1.resx +++ b/IceCreamShop/IceCreamShop/ComponentForm.resx @@ -1,17 +1,17 @@  - diff --git a/IceCreamShop/IceCreamShop/ComponentsForm.Designer.cs b/IceCreamShop/IceCreamShop/ComponentsForm.Designer.cs new file mode 100644 index 0000000..f2c8139 --- /dev/null +++ b/IceCreamShop/IceCreamShop/ComponentsForm.Designer.cs @@ -0,0 +1,112 @@ +namespace IceCreamShop +{ + partial class ComponentsForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + DataGridView = new DataGridView(); + AddButton = new Button(); + ChangeButton = new Button(); + DeleteButton = new Button(); + RefreshButton = new Button(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Location = new Point(12, 12); + DataGridView.Name = "DataGridView"; + DataGridView.Size = new Size(369, 350); + DataGridView.TabIndex = 0; + // + // AddButton + // + AddButton.Location = new Point(387, 12); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(126, 23); + AddButton.TabIndex = 1; + AddButton.Text = "Добавить"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += AddButton_Click; + // + // ChangeButton + // + ChangeButton.Location = new Point(387, 41); + ChangeButton.Name = "ChangeButton"; + ChangeButton.Size = new Size(126, 23); + ChangeButton.TabIndex = 2; + ChangeButton.Text = "Изменить"; + ChangeButton.UseVisualStyleBackColor = true; + ChangeButton.Click += ChangeButton_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(387, 70); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(126, 23); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // RefreshButton + // + RefreshButton.Location = new Point(387, 99); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(126, 23); + RefreshButton.TabIndex = 4; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // ComponentsForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(525, 374); + Controls.Add(RefreshButton); + Controls.Add(DeleteButton); + Controls.Add(ChangeButton); + Controls.Add(AddButton); + Controls.Add(DataGridView); + Name = "ComponentsForm"; + Text = "ComponentsForm"; + Load += ComponentsForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView DataGridView; + private Button AddButton; + private Button ChangeButton; + private Button DeleteButton; + private Button RefreshButton; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/ComponentsForm.cs b/IceCreamShop/IceCreamShop/ComponentsForm.cs new file mode 100644 index 0000000..23a9c16 --- /dev/null +++ b/IceCreamShop/IceCreamShop/ComponentsForm.cs @@ -0,0 +1,121 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.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 IceCreamShop +{ + public partial class ComponentsForm : Form + { + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + public ComponentsForm(ILogger logger, IComponentLogic + logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void ComponentsForm_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 AddButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(ComponentForm)); + if (service is ComponentForm form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ChangeButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = + Program.ServiceProvider?.GetService(typeof(ComponentForm)); + if (service is ComponentForm form) + { + form.Id = + Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + } + + private void DeleteButton_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 RefreshButton_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/IceCreamShop/IceCreamShop/ComponentsForm.resx b/IceCreamShop/IceCreamShop/ComponentsForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShop/ComponentsForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/Form1.Designer.cs b/IceCreamShop/IceCreamShop/Form1.Designer.cs deleted file mode 100644 index 797e3e7..0000000 --- a/IceCreamShop/IceCreamShop/Form1.Designer.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace IceCreamShop -{ - partial class Form1 - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; - } - - #endregion - } -} diff --git a/IceCreamShop/IceCreamShop/Form1.cs b/IceCreamShop/IceCreamShop/Form1.cs deleted file mode 100644 index a016747..0000000 --- a/IceCreamShop/IceCreamShop/Form1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace IceCreamShop -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - } -} diff --git a/IceCreamShop/IceCreamShop/IceCreamComponentForm.Designer.cs b/IceCreamShop/IceCreamShop/IceCreamComponentForm.Designer.cs new file mode 100644 index 0000000..7406dd4 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamComponentForm.Designer.cs @@ -0,0 +1,118 @@ +namespace IceCreamShop +{ + partial class IceCreamComponentForm + { + /// + /// 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() + { + ComponentLabel = new Label(); + CountLabel = new Label(); + CountTextBox = new TextBox(); + ComboBox = new ComboBox(); + SaveButton = new Button(); + CancelButton = new Button(); + SuspendLayout(); + // + // ComponentLabel + // + ComponentLabel.AutoSize = true; + ComponentLabel.Location = new Point(12, 9); + ComponentLabel.Name = "ComponentLabel"; + ComponentLabel.Size = new Size(69, 15); + ComponentLabel.TabIndex = 0; + ComponentLabel.Text = "Компонент"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(12, 40); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(72, 15); + CountLabel.TabIndex = 1; + CountLabel.Text = "Количество"; + // + // CountTextBox + // + CountTextBox.Location = new Point(87, 37); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(292, 23); + CountTextBox.TabIndex = 2; + // + // ComboBox + // + ComboBox.FormattingEnabled = true; + ComboBox.Location = new Point(87, 6); + ComboBox.Name = "ComboBox"; + ComboBox.Size = new Size(292, 23); + ComboBox.TabIndex = 3; + // + // SaveButton + // + SaveButton.Location = new Point(218, 66); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 4; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += ButtonSave_Click; + // + // CancelButton + // + CancelButton.Location = new Point(299, 66); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 5; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += ButtonCancel_Click; + // + // IceCreamComponentForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(391, 96); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(ComboBox); + Controls.Add(CountTextBox); + Controls.Add(CountLabel); + Controls.Add(ComponentLabel); + Name = "IceCreamComponentForm"; + Text = "IceCreamComponentForm"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label ComponentLabel; + private Label CountLabel; + private TextBox CountTextBox; + private ComboBox ComboBox; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/IceCreamComponentForm.cs b/IceCreamShop/IceCreamShop/IceCreamComponentForm.cs new file mode 100644 index 0000000..0dfb993 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamComponentForm.cs @@ -0,0 +1,90 @@ +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.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 IceCreamShop +{ + public partial class IceCreamComponentForm : Form + { + private readonly List? _list; + public int Id + { + get + { + return + Convert.ToInt32(ComboBox.SelectedValue); + } + set + { + ComboBox.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(CountTextBox.Text); } + set + { CountTextBox.Text = value.ToString(); } + } + public IceCreamComponentForm(IComponentLogic logic) + { + InitializeComponent(); + _list = logic.ReadList(null); + if (_list != null) + { + ComboBox.DisplayMember = "ComponentName"; + ComboBox.ValueMember = "Id"; + ComboBox.DataSource = _list; + ComboBox.SelectedItem = null; + } + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (ComboBox.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/IceCreamShop/IceCreamShop/IceCreamComponentForm.resx b/IceCreamShop/IceCreamShop/IceCreamComponentForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamComponentForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/IceCreamForm.Designer.cs b/IceCreamShop/IceCreamShop/IceCreamForm.Designer.cs new file mode 100644 index 0000000..c799428 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamForm.Designer.cs @@ -0,0 +1,227 @@ +namespace IceCreamShop +{ + partial class IceCreamForm + { + /// + /// 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() + { + NameLabel = new Label(); + PriceLabel = new Label(); + NameTextBox = new TextBox(); + PriceTextBox = new TextBox(); + ComponentsGroupBox = new GroupBox(); + RefreshButton = new Button(); + DeleteButton = new Button(); + UpdateButton = new Button(); + AddButton = new Button(); + DataGridView = new DataGridView(); + IdColumn = new DataGridViewTextBoxColumn(); + ComponentNameColumn = new DataGridViewTextBoxColumn(); + CountColumn = new DataGridViewTextBoxColumn(); + SaveButton = new Button(); + CancelButton = new Button(); + ComponentsGroupBox.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // NameLabel + // + NameLabel.AutoSize = true; + NameLabel.Location = new Point(12, 9); + NameLabel.Name = "NameLabel"; + NameLabel.Size = new Size(59, 15); + NameLabel.TabIndex = 0; + NameLabel.Text = "Название"; + // + // PriceLabel + // + PriceLabel.AutoSize = true; + PriceLabel.Location = new Point(12, 35); + PriceLabel.Name = "PriceLabel"; + PriceLabel.Size = new Size(35, 15); + PriceLabel.TabIndex = 1; + PriceLabel.Text = "Цена"; + // + // NameTextBox + // + NameTextBox.Location = new Point(77, 6); + NameTextBox.Name = "NameTextBox"; + NameTextBox.Size = new Size(237, 23); + NameTextBox.TabIndex = 2; + // + // PriceTextBox + // + PriceTextBox.Location = new Point(77, 32); + PriceTextBox.Name = "PriceTextBox"; + PriceTextBox.ReadOnly = true; + PriceTextBox.Size = new Size(100, 23); + PriceTextBox.TabIndex = 3; + // + // ComponentsGroupBox + // + ComponentsGroupBox.Controls.Add(RefreshButton); + ComponentsGroupBox.Controls.Add(DeleteButton); + ComponentsGroupBox.Controls.Add(UpdateButton); + ComponentsGroupBox.Controls.Add(AddButton); + ComponentsGroupBox.Controls.Add(DataGridView); + ComponentsGroupBox.Location = new Point(12, 64); + ComponentsGroupBox.Name = "ComponentsGroupBox"; + ComponentsGroupBox.Size = new Size(582, 287); + ComponentsGroupBox.TabIndex = 4; + ComponentsGroupBox.TabStop = false; + ComponentsGroupBox.Text = "Компоненты"; + // + // RefreshButton + // + RefreshButton.Location = new Point(481, 109); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(75, 23); + RefreshButton.TabIndex = 4; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(481, 80); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(75, 23); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // UpdateButton + // + UpdateButton.Location = new Point(481, 51); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(75, 23); + UpdateButton.TabIndex = 2; + UpdateButton.Text = "Изменить"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += UpdateButton_Click; + // + // AddButton + // + AddButton.Location = new Point(481, 22); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(75, 23); + AddButton.TabIndex = 1; + AddButton.Text = "Добавить"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += AddButton_Click; + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Columns.AddRange(new DataGridViewColumn[] { IdColumn, ComponentNameColumn, CountColumn }); + DataGridView.Location = new Point(6, 22); + DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; + DataGridView.Size = new Size(442, 259); + DataGridView.TabIndex = 0; + // + // IdColumn + // + IdColumn.HeaderText = "Column1"; + IdColumn.MinimumWidth = 6; + IdColumn.Name = "IdColumn"; + IdColumn.Visible = false; + IdColumn.Width = 6; + // + // ComponentNameColumn + // + ComponentNameColumn.HeaderText = "Компонент"; + ComponentNameColumn.MinimumWidth = 6; + ComponentNameColumn.Name = "ComponentNameColumn"; + ComponentNameColumn.Width = 300; + // + // CountColumn + // + CountColumn.HeaderText = "Количество"; + CountColumn.MinimumWidth = 6; + CountColumn.Name = "CountColumn"; + CountColumn.Width = 125; + // + // SaveButton + // + SaveButton.Location = new Point(424, 363); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 5; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(505, 363); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 6; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + // + // IceCreamForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(606, 398); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(ComponentsGroupBox); + Controls.Add(PriceTextBox); + Controls.Add(NameTextBox); + Controls.Add(PriceLabel); + Controls.Add(NameLabel); + Name = "IceCreamForm"; + Text = "IceCreamForm"; + Load += IceCreamForm_Load; + ComponentsGroupBox.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label NameLabel; + private Label PriceLabel; + private TextBox NameTextBox; + private TextBox PriceTextBox; + private GroupBox ComponentsGroupBox; + private Button RefreshButton; + private Button DeleteButton; + private Button UpdateButton; + private Button AddButton; + private DataGridView DataGridView; + private DataGridViewTextBoxColumn IdColumn; + private DataGridViewTextBoxColumn ComponentNameColumn; + private DataGridViewTextBoxColumn CountColumn; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/IceCreamForm.cs b/IceCreamShop/IceCreamShop/IceCreamForm.cs new file mode 100644 index 0000000..5591902 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamForm.cs @@ -0,0 +1,235 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopDataModels.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 IceCreamShop +{ + public partial class IceCreamForm : Form + { + private readonly ILogger _logger; + private readonly IIceCreamLogic _logic; + private int? _id; + private Dictionary _iceCreamComponents; + public int Id { set { _id = value; } } + + public IceCreamForm(ILogger logger, IIceCreamLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _iceCreamComponents = new Dictionary(); + } + + private void IceCreamForm_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка изделия"); + try + { + var view = _logic.ReadElement(new IceCreamSearchModel + { + Id = + _id.Value + }); + if (view != null) + { + NameTextBox.Text = view.IceCreamName; + PriceTextBox.Text = view.Price.ToString(); + _iceCreamComponents = view.IceCreamComponents ?? new + Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка компонент изделия"); + try + { + if (_iceCreamComponents != null) + { + DataGridView.Rows.Clear(); + foreach (var pc in _iceCreamComponents) + { + DataGridView.Rows.Add(new object[] { pc.Key, +pc.Value.Item1.ComponentName, pc.Value.Item2 }); + } + PriceTextBox.Text = CalcPrice().ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонент изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void AddButton_Click(object sender, EventArgs e) + { + var service = +Program.ServiceProvider?.GetService(typeof(IceCreamComponentForm)); + if (service is IceCreamComponentForm form) + { + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Добавление нового компонента:{ ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); + if (_iceCreamComponents.ContainsKey(form.Id)) + { + _iceCreamComponents[form.Id] = (form.ComponentModel, + form.Count); + } + else + { + _iceCreamComponents.Add(form.Id, (form.ComponentModel, + form.Count)); + } + LoadData(); + } + } + } + + private void UpdateButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(IceCreamComponentForm)); + if (service is IceCreamComponentForm form) + { + int id = + Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0].Value); + form.Id = id; + form.Count = _iceCreamComponents[id].Item2; + if (form.ShowDialog() == DialogResult.OK) + { + if (form.ComponentModel == null) + { + return; + } + _logger.LogInformation("Изменение компонента:{ ComponentName}- { Count}", form.ComponentModel.ComponentName, form.Count); + _iceCreamComponents[form.Id] = (form.ComponentModel, form.Count); + LoadData(); + } + } + } + + } + + private void DeleteButton_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); + _iceCreamComponents?.Remove(Convert.ToInt32(DataGridView.SelectedRows[0].Cells[0]. + Value)); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + } + + private void RefreshButton_Click(object sender, EventArgs e) + { + LoadData(); + + } + + private void SaveButton_Click(object sender, EventArgs e) + { + + if (string.IsNullOrEmpty(NameTextBox.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(PriceTextBox.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } + if (_iceCreamComponents == null || _iceCreamComponents.Count == 0) + { + MessageBox.Show("Заполните компоненты", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение изделия"); + try + { + var model = new IceCreamBindingModel + { + Id = _id ?? 0, + IceCreamName = NameTextBox.Text, + Price = Convert.ToDouble(PriceTextBox.Text), + IceCreamComponents = _iceCreamComponents + }; + 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 _iceCreamComponents) + { + price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2); + } + return Math.Round(price * 1.1, 2); + } + } +} diff --git a/IceCreamShop/IceCreamShop/IceCreamForm.resx b/IceCreamShop/IceCreamShop/IceCreamForm.resx new file mode 100644 index 0000000..1902b19 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamForm.resx @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + True + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/IceCreamShop.csproj b/IceCreamShop/IceCreamShop/IceCreamShop.csproj index 663fdb8..c0a5632 100644 --- a/IceCreamShop/IceCreamShop/IceCreamShop.csproj +++ b/IceCreamShop/IceCreamShop/IceCreamShop.csproj @@ -8,4 +8,15 @@ enable + + + + + + + + + + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/IceCreamsForm.Designer.cs b/IceCreamShop/IceCreamShop/IceCreamsForm.Designer.cs new file mode 100644 index 0000000..4bb5d0d --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamsForm.Designer.cs @@ -0,0 +1,113 @@ +namespace IceCreamShop +{ + partial class IceCreamsForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + DataGridView = new DataGridView(); + AddButton = new Button(); + UpdateButton = new Button(); + DeleteButton = new Button(); + RefreshButton = new Button(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Location = new Point(12, 12); + DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; + DataGridView.Size = new Size(379, 426); + DataGridView.TabIndex = 0; + // + // AddButton + // + AddButton.Location = new Point(397, 12); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(148, 29); + AddButton.TabIndex = 1; + AddButton.Text = "Добавить"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += AddButton_Click; + // + // UpdateButton + // + UpdateButton.Location = new Point(397, 47); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(148, 29); + UpdateButton.TabIndex = 2; + UpdateButton.Text = "Изменить"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += UpdateButton_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(397, 82); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(148, 29); + DeleteButton.TabIndex = 3; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // RefreshButton + // + RefreshButton.Location = new Point(397, 117); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(148, 29); + RefreshButton.TabIndex = 4; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // IceCreamsForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(557, 450); + Controls.Add(RefreshButton); + Controls.Add(DeleteButton); + Controls.Add(UpdateButton); + Controls.Add(AddButton); + Controls.Add(DataGridView); + Name = "IceCreamsForm"; + Text = "IceCreamsForm"; + Load += IceCreamsForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView DataGridView; + private Button AddButton; + private Button UpdateButton; + private Button DeleteButton; + private Button RefreshButton; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/IceCreamsForm.cs b/IceCreamShop/IceCreamShop/IceCreamsForm.cs new file mode 100644 index 0000000..a27ea73 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamsForm.cs @@ -0,0 +1,116 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.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 IceCreamShop +{ + public partial class IceCreamsForm : Form + { + private readonly ILogger _logger; + private readonly IIceCreamLogic _logic; + public IceCreamsForm(ILogger logger, IIceCreamLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void IceCreamsForm_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["IceCreamName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + DataGridView.Columns["IceCreamComponents"].Visible = false; + } + _logger.LogInformation("Загрузка мороженого"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки мороженого"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void AddButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(IceCreamForm)); + if (service is IceCreamForm form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void UpdateButton_Click(object sender, EventArgs e) + { + if (DataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(IceCreamForm)); + if (service is IceCreamForm form) + { + var tmp = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void DeleteButton_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 IceCreamBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления мороженого"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void RefreshButton_Click(object sender, EventArgs e) + { + LoadData(); + + } + } +} diff --git a/IceCreamShop/IceCreamShop/IceCreamsForm.resx b/IceCreamShop/IceCreamShop/IceCreamsForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/IceCreamShop/IceCreamShop/IceCreamsForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/MainForm.Designer.cs b/IceCreamShop/IceCreamShop/MainForm.Designer.cs new file mode 100644 index 0000000..2b62d4f --- /dev/null +++ b/IceCreamShop/IceCreamShop/MainForm.Designer.cs @@ -0,0 +1,171 @@ +namespace IceCreamShop +{ + partial class MainForm + { + /// + /// 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() + { + menuStrip1 = new MenuStrip(); + ToolStripMenu = new ToolStripMenuItem(); + КомпонентыStripMenuItem = new ToolStripMenuItem(); + МороженноеStripMenuItem = new ToolStripMenuItem(); + DataGridView = new DataGridView(); + CreateOrderButton = new Button(); + TakeInWorkButton = new Button(); + ReadyButton = new Button(); + IssuedButton = new Button(); + RefreshButton = new Button(); + menuStrip1.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { ToolStripMenu }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(1296, 28); + menuStrip1.TabIndex = 0; + menuStrip1.Text = "menuStrip1"; + // + // ToolStripMenu + // + ToolStripMenu.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыStripMenuItem, МороженноеStripMenuItem }); + ToolStripMenu.Name = "ToolStripMenu"; + ToolStripMenu.Size = new Size(117, 24); + ToolStripMenu.Text = "Справочники"; + // + // КомпонентыStripMenuItem + // + КомпонентыStripMenuItem.Name = "КомпонентыStripMenuItem"; + КомпонентыStripMenuItem.Size = new Size(186, 26); + КомпонентыStripMenuItem.Text = "Компоненты"; + КомпонентыStripMenuItem.Click += КомпонентыStripMenuItem_Click; + // + // МороженноеStripMenuItem + // + МороженноеStripMenuItem.Name = "МороженноеStripMenuItem"; + МороженноеStripMenuItem.Size = new Size(186, 26); + МороженноеStripMenuItem.Text = "Мороженное"; + МороженноеStripMenuItem.Click += МороженноеStripMenuItem_Click; + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Location = new Point(12, 31); + DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; + DataGridView.Size = new Size(1007, 407); + DataGridView.TabIndex = 1; + // + // CreateOrderButton + // + CreateOrderButton.Location = new Point(1025, 31); + CreateOrderButton.Name = "CreateOrderButton"; + CreateOrderButton.Size = new Size(259, 29); + CreateOrderButton.TabIndex = 2; + CreateOrderButton.Text = "Создать заказ"; + CreateOrderButton.UseVisualStyleBackColor = true; + CreateOrderButton.Click += CreateOrderButton_Click; + // + // TakeInWorkButton + // + TakeInWorkButton.Location = new Point(1025, 66); + TakeInWorkButton.Name = "TakeInWorkButton"; + TakeInWorkButton.Size = new Size(259, 29); + TakeInWorkButton.TabIndex = 3; + TakeInWorkButton.Text = "Отдать заказ в работу"; + TakeInWorkButton.UseVisualStyleBackColor = true; + TakeInWorkButton.Click += TakeInWorkButton_Click; + // + // ReadyButton + // + ReadyButton.Location = new Point(1025, 101); + ReadyButton.Name = "ReadyButton"; + ReadyButton.Size = new Size(259, 29); + ReadyButton.TabIndex = 4; + ReadyButton.Text = "Заказ готов"; + ReadyButton.UseVisualStyleBackColor = true; + ReadyButton.Click += ReadyButton_Click; + // + // IssuedButton + // + IssuedButton.Location = new Point(1025, 136); + IssuedButton.Name = "IssuedButton"; + IssuedButton.Size = new Size(259, 29); + IssuedButton.TabIndex = 5; + IssuedButton.Text = "Заказ выдан"; + IssuedButton.UseVisualStyleBackColor = true; + IssuedButton.Click += IssuedButton_Click; + // + // RefreshButton + // + RefreshButton.Location = new Point(1025, 171); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(259, 29); + RefreshButton.TabIndex = 6; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // MainForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1296, 450); + Controls.Add(RefreshButton); + Controls.Add(IssuedButton); + Controls.Add(ReadyButton); + Controls.Add(TakeInWorkButton); + Controls.Add(CreateOrderButton); + Controls.Add(DataGridView); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; + Name = "MainForm"; + Text = "MainForm"; + Load += MainForm_Load; + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private MenuStrip menuStrip1; + private ToolStripMenuItem ToolStripMenu; + private ToolStripMenuItem КомпонентыStripMenuItem; + private ToolStripMenuItem МороженноеStripMenuItem; + private DataGridView DataGridView; + private Button CreateOrderButton; + private Button TakeInWorkButton; + private Button ReadyButton; + private Button IssuedButton; + private Button RefreshButton; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/MainForm.cs b/IceCreamShop/IceCreamShop/MainForm.cs new file mode 100644 index 0000000..600e794 --- /dev/null +++ b/IceCreamShop/IceCreamShop/MainForm.cs @@ -0,0 +1,184 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopDataModels; +using IceCreamShopDataModels.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 IceCreamShop +{ + public partial class MainForm : Form + { + private readonly ILogger _logger; + private readonly IOrderLogic _orderLogic; + public MainForm(ILogger logger, IOrderLogic orderLogic) + { + InitializeComponent(); + _logger = logger; + _orderLogic = orderLogic; + } + + private void КомпонентыStripMenuItem_Click(object sender, EventArgs e) + { + var service = +Program.ServiceProvider?.GetService(typeof(ComponentsForm)); + if (service is ComponentsForm form) + { + form.ShowDialog(); + } + + } + + private void MainForm_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["IceCreamId"].Visible = false; + DataGridView.Columns["IceCreamName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка заказов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки заказов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void МороженноеStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(IceCreamsForm)); + if (service is IceCreamsForm form) + { + form.ShowDialog(); + } + } + + private void CreateOrderButton_Click(object sender, EventArgs e) + { + var service = +Program.ServiceProvider?.GetService(typeof(OrderForm)); + if (service is OrderForm form) + { + form.ShowDialog(); + LoadData(); + } + } + + private OrderBindingModel CreateBindingModel(int id, bool isDone = false) + { + return new OrderBindingModel + { + Id = id, + IceCreamId = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["IceCreamId"].Value), + 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()), + }; + } + + private void TakeInWorkButton_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(CreateBindingModel(id)); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка передачи заказа в работу"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + } + + private void ReadyButton_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(CreateBindingModel(id)); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка отметки о готовности заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void IssuedButton_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(CreateBindingModel(id)); + 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 RefreshButton_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/IceCreamShop/IceCreamShop/MainForm.resx b/IceCreamShop/IceCreamShop/MainForm.resx new file mode 100644 index 0000000..a0623c8 --- /dev/null +++ b/IceCreamShop/IceCreamShop/MainForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/IceCreamShop/IceCreamShop/OrderForm.Designer.cs b/IceCreamShop/IceCreamShop/OrderForm.Designer.cs new file mode 100644 index 0000000..78d58c6 --- /dev/null +++ b/IceCreamShop/IceCreamShop/OrderForm.Designer.cs @@ -0,0 +1,145 @@ +namespace IceCreamShop +{ + partial class OrderForm + { + /// + /// 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() + { + IceCreamComboBox = new ComboBox(); + CountTextBox = new TextBox(); + SumTextBox = new TextBox(); + SaveButton = new Button(); + CancelButton = new Button(); + IceCreamLabel = new Label(); + CountLabel = new Label(); + SumLabel = new Label(); + SuspendLayout(); + // + // IceCreamComboBox + // + IceCreamComboBox.FormattingEnabled = true; + IceCreamComboBox.Location = new Point(103, 12); + IceCreamComboBox.Name = "IceCreamComboBox"; + IceCreamComboBox.Size = new Size(232, 23); + IceCreamComboBox.TabIndex = 0; + IceCreamComboBox.SelectedIndexChanged += IceCreamComboBox_SelectedIndexChanged; + // + // CountTextBox + // + CountTextBox.Location = new Point(103, 41); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(232, 23); + CountTextBox.TabIndex = 1; + CountTextBox.TextChanged += CountTextBox_TextChanged; + // + // SumTextBox + // + SumTextBox.Location = new Point(103, 70); + SumTextBox.Name = "SumTextBox"; + SumTextBox.ReadOnly = true; + SumTextBox.Size = new Size(232, 23); + SumTextBox.TabIndex = 2; + SumTextBox.TextChanged += SumTextBox_TextChanged; + // + // SaveButton + // + SaveButton.Location = new Point(173, 99); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(81, 23); + SaveButton.TabIndex = 3; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(260, 99); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 4; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += CancelButton_Click; + // + // IceCreamLabel + // + IceCreamLabel.AutoSize = true; + IceCreamLabel.Location = new Point(12, 15); + IceCreamLabel.Name = "IceCreamLabel"; + IceCreamLabel.Size = new Size(81, 15); + IceCreamLabel.TabIndex = 5; + IceCreamLabel.Text = "Мороженное"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(12, 44); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(72, 15); + CountLabel.TabIndex = 6; + CountLabel.Text = "Количество"; + // + // SumLabel + // + SumLabel.AutoSize = true; + SumLabel.Location = new Point(12, 73); + SumLabel.Name = "SumLabel"; + SumLabel.Size = new Size(45, 15); + SumLabel.TabIndex = 7; + SumLabel.Text = "Сумма"; + // + // OrderForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(348, 128); + Controls.Add(SumLabel); + Controls.Add(CountLabel); + Controls.Add(IceCreamLabel); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(SumTextBox); + Controls.Add(CountTextBox); + Controls.Add(IceCreamComboBox); + Name = "OrderForm"; + Text = "OrderForm"; + Load += OrderForm_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox IceCreamComboBox; + private TextBox CountTextBox; + private TextBox SumTextBox; + private Button SaveButton; + private Button CancelButton; + private Label IceCreamLabel; + private Label CountLabel; + private Label SumLabel; + } +} \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/OrderForm.cs b/IceCreamShop/IceCreamShop/OrderForm.cs new file mode 100644 index 0000000..5ba03c4 --- /dev/null +++ b/IceCreamShop/IceCreamShop/OrderForm.cs @@ -0,0 +1,143 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.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 IceCreamShop +{ + public partial class OrderForm : Form + { + private readonly ILogger _logger; + private readonly IIceCreamLogic _logicI; + private readonly IOrderLogic _logicO; + public OrderForm(ILogger logger, IIceCreamLogic +logicI, IOrderLogic logicO) + { + InitializeComponent(); + _logger = logger; + _logicI = logicI; + _logicO = logicO; + } + + private void OrderForm_Load(object sender, EventArgs e) + { + _logger.LogInformation("Загрузка мороженного для заказа"); + try + { + var list = _logicI.ReadList(null); + if (list != null) + { + IceCreamComboBox.DisplayMember = "IceCreamName"; + IceCreamComboBox.ValueMember = "Id"; + IceCreamComboBox.DataSource = list; + IceCreamComboBox.SelectedItem = null; + } + _logger.LogInformation("Мороженное загружено"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки мороженного"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + + } + + private void CalcSum() + { + if (IceCreamComboBox.SelectedValue != null && + !string.IsNullOrEmpty(CountTextBox.Text)) + { + try + { + int id = Convert.ToInt32(IceCreamComboBox.SelectedValue); + var iceCream = _logicI.ReadElement(new IceCreamSearchModel + { + Id + = id + }); + int count = Convert.ToInt32(CountTextBox.Text); + SumTextBox.Text = Math.Round(count * (iceCream?.Price ?? 0), + 2).ToString(); + _logger.LogInformation("Расчет суммы заказа"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка расчета суммы заказа"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void CountTextBox_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + + private void SumTextBox_TextChanged(object sender, EventArgs e) + { + CalcSum(); + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (IceCreamComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите мороженное", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание заказа"); + try + { + var operationResult = _logicO.CreateOrder(new OrderBindingModel + { + IceCreamId = Convert.ToInt32(IceCreamComboBox.SelectedValue), + Count = Convert.ToInt32(CountTextBox.Text), + Sum = Convert.ToDouble(SumTextBox.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 CancelButton_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void IceCreamComboBox_SelectedIndexChanged(object sender, EventArgs e) + { + CalcSum(); + } + } +} diff --git a/IceCreamShop/IceCreamShop/OrderForm.resx b/IceCreamShop/IceCreamShop/OrderForm.resx new file mode 100644 index 0000000..46a2fc0 --- /dev/null +++ b/IceCreamShop/IceCreamShop/OrderForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + \ No newline at end of file diff --git a/IceCreamShop/IceCreamShop/Program.cs b/IceCreamShop/IceCreamShop/Program.cs index 6275694..f670921 100644 --- a/IceCreamShop/IceCreamShop/Program.cs +++ b/IceCreamShop/IceCreamShop/Program.cs @@ -1,7 +1,17 @@ +using IceCreamShopBusinessLogic.BusinessLogic; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopListImplement.Implements; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; + namespace IceCreamShop { internal static class Program { + private static ServiceProvider? _serviceProvider; + public static ServiceProvider? ServiceProvider => _serviceProvider; /// /// The main entry point for the application. /// @@ -11,7 +21,31 @@ namespace IceCreamShop // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new Form1()); + var services = new ServiceCollection(); + ConfigureServices(services); + _serviceProvider = services.BuildServiceProvider(); + Application.Run(_serviceProvider.GetRequiredService()); + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } -} \ No newline at end of file +} diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ComponentLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..1f263b9 --- /dev/null +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/ComponentLogic.cs @@ -0,0 +1,119 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace IceCreamShopBusinessLogic.BusinessLogic +{ + 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/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/IceCreamLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/IceCreamLogic.cs new file mode 100644 index 0000000..bf1c3f8 --- /dev/null +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/IceCreamLogic.cs @@ -0,0 +1,123 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopBusinessLogic.BusinessLogic +{ + public class IceCreamLogic : IIceCreamLogic + { + private readonly ILogger _logger; + private readonly IIceCreamStorage _iceCreamStorage; + public IceCreamLogic(ILogger logger, IIceCreamStorage iceCreamStorage) + { + _logger = logger; + _iceCreamStorage = iceCreamStorage; + } + + public List? ReadList(IceCreamSearchModel? model) + { + _logger.LogInformation("ReadList. IceCreamName:{IceCreamName}. Id:{ Id}", model?.IceCreamName, model?.Id); + var list = model == null ? _iceCreamStorage.GetFullList() : +_iceCreamStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public IceCreamViewModel? ReadElement(IceCreamSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. IceCreamName:{IceCreamName}.Id:{ Id}", model.IceCreamName, model.Id); + var element = _iceCreamStorage.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(IceCreamBindingModel model) + { + CheckModel(model); + if (_iceCreamStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(IceCreamBindingModel model) + { + CheckModel(model); + if (_iceCreamStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(IceCreamBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_iceCreamStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(IceCreamBindingModel model, bool withParams = +true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.IceCreamName)) + { + throw new ArgumentNullException("Нет названия мороженного", + nameof(model.IceCreamName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена мороженного должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("IceCream. IceCream:{IceCream}. Price:{ Price }. Id: { Id}", model.IceCreamName, model.Price, model.Id); + var element = _iceCreamStorage.GetElement(new IceCreamSearchModel + { + IceCreamName = model.IceCreamName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + + + + + } +} diff --git a/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..ef27b2a --- /dev/null +++ b/IceCreamShop/IceCreamShopBusinessLogic/BusinessLogic/OrderLogic.cs @@ -0,0 +1,112 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.BusinessLogicsContracts; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels; +using IceCreamShopDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopBusinessLogic.BusinessLogic +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : + _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) return false; + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool ChangeStatus(OrderBindingModel model, OrderStatus status) + { + CheckModel(model); + var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (element.Status != status - 1) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); + } + model.Status = status; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + _orderStorage.Update(model); + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } + + public bool FinishOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Готов); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return ChangeStatus(model, OrderStatus.Выдан); + } + + private void CheckModel(OrderBindingModel model, bool withParams = +true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); + } + _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); + } + } +} diff --git a/IceCreamShop/IceCreamShopBusinessLogic/IceCreamShopBusinessLogic.csproj b/IceCreamShop/IceCreamShopBusinessLogic/IceCreamShopBusinessLogic.csproj new file mode 100644 index 0000000..d24b47e --- /dev/null +++ b/IceCreamShop/IceCreamShopBusinessLogic/IceCreamShopBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/ComponentBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/ComponentBindingModel.cs new file mode 100644 index 0000000..463e00c --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/ComponentBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopDataModels.Models; + +namespace IceCreamShopContracts.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/IceCreamShop/IceCreamShopContracts/BindingModels/IceCreamBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/IceCreamBindingModel.cs new file mode 100644 index 0000000..416fd2d --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/IceCreamBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopDataModels.Models; + +namespace IceCreamShopContracts.BindingModels +{ + public class IceCreamBindingModel : IIceCreamModel + { + public int Id { get; set; } + public string IceCreamName { get; set; } = string.Empty; + public double Price { get; set; } + public Dictionary IceCreamComponents { get; set; } = new(); + + } +} diff --git a/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs b/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs new file mode 100644 index 0000000..25da1b5 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BindingModels/OrderBindingModel.cs @@ -0,0 +1,22 @@ +using IceCreamShopDataModels; +using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.BindingModels +{ + public class OrderBindingModel : IOrderModel + { + public int Id { get; set; } + public int IceCreamId { get; set; } + 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/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IComponentLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IComponentLogic.cs new file mode 100644 index 0000000..26aa837 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IComponentLogic.cs @@ -0,0 +1,20 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.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/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IIceCreamLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IIceCreamLogic.cs new file mode 100644 index 0000000..29ccb72 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IIceCreamLogic.cs @@ -0,0 +1,21 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.BusinessLogicsContracts +{ + public interface IIceCreamLogic + { + List? ReadList(IceCreamSearchModel? model); + IceCreamViewModel? ReadElement(IceCreamSearchModel model); + bool Create(IceCreamBindingModel model); + bool Update(IceCreamBindingModel model); + bool Delete(IceCreamBindingModel model); + + } +} diff --git a/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IOrderLogic.cs b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IOrderLogic.cs new file mode 100644 index 0000000..5611d0a --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -0,0 +1,20 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.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/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj b/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj new file mode 100644 index 0000000..4e76083 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/IceCreamShopContracts.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/IceCreamShop/IceCreamShopContracts/SearchModels/ComponentSearchModel.cs b/IceCreamShop/IceCreamShopContracts/SearchModels/ComponentSearchModel.cs new file mode 100644 index 0000000..61586b3 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/SearchModels/ComponentSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.SearchModels +{ + public class ComponentSearchModel + { + public int? Id { get; set; } + public string? ComponentName { get; set; } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/SearchModels/IceCreamSearchModel.cs b/IceCreamShop/IceCreamShopContracts/SearchModels/IceCreamSearchModel.cs new file mode 100644 index 0000000..34c34cc --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/SearchModels/IceCreamSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.SearchModels +{ + public class IceCreamSearchModel + { + public int? Id { get; set; } + public string? IceCreamName { get; set; } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/SearchModels/OrderSearchModel.cs b/IceCreamShop/IceCreamShopContracts/SearchModels/OrderSearchModel.cs new file mode 100644 index 0000000..23043f3 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/SearchModels/OrderSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.SearchModels +{ + public class OrderSearchModel + { + public int? Id { get; set; } + } +} diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IComponentStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IComponentStorage.cs new file mode 100644 index 0000000..0d00b39 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IComponentStorage.cs @@ -0,0 +1,21 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.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/IceCreamShop/IceCreamShopContracts/StoragesContracts/IIceCreamStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IIceCreamStorage.cs new file mode 100644 index 0000000..2121114 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IIceCreamStorage.cs @@ -0,0 +1,21 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.StoragesContracts +{ + public interface IIceCreamStorage + { + List GetFullList(); + List GetFilteredList(IceCreamSearchModel model); + IceCreamViewModel? GetElement(IceCreamSearchModel model); + IceCreamViewModel? Insert(IceCreamBindingModel model); + IceCreamViewModel? Update(IceCreamBindingModel model); + IceCreamViewModel? Delete(IceCreamBindingModel model); + } +} diff --git a/IceCreamShop/IceCreamShopContracts/StoragesContracts/IOrderStorage.cs b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IOrderStorage.cs new file mode 100644 index 0000000..b0d218c --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/StoragesContracts/IOrderStorage.cs @@ -0,0 +1,21 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.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/IceCreamShop/IceCreamShopContracts/ViewModels/ComponentViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/ComponentViewModel.cs new file mode 100644 index 0000000..4d9c2d7 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/ComponentViewModel.cs @@ -0,0 +1,20 @@ +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.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/IceCreamShop/IceCreamShopContracts/ViewModels/IceCreamViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/IceCreamViewModel.cs new file mode 100644 index 0000000..6843681 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/IceCreamViewModel.cs @@ -0,0 +1,25 @@ +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.ViewModels +{ + public class IceCreamViewModel : IIceCreamModel + { + public int Id { get; set; } + [DisplayName("Название мороженного")] + public string IceCreamName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Price { get; set; } + public Dictionary IceCreamComponents + { + get; + set; + } = new(); + + } +} diff --git a/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs b/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs new file mode 100644 index 0000000..b308f56 --- /dev/null +++ b/IceCreamShop/IceCreamShopContracts/ViewModels/OrderViewModel.cs @@ -0,0 +1,31 @@ +using IceCreamShopDataModels; +using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopContracts.ViewModels +{ + public class OrderViewModel : IOrderModel + { + [DisplayName("Номер")] + public int Id { get; set; } + public int IceCreamId { get; set; } + [DisplayName("Мороженное")] + public string IceCreamName { 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/IceCreamShop/IceCreamShopDataModels/IComponentModel.cs b/IceCreamShop/IceCreamShopDataModels/IComponentModel.cs new file mode 100644 index 0000000..243391e --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/IComponentModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDataModels.Models +{ + public interface IComponentModel : IId + { + string ComponentName { get; } + + double Cost { get; } + } +} diff --git a/IceCreamShop/IceCreamShopDataModels/IIceCreamModel.cs b/IceCreamShop/IceCreamShopDataModels/IIceCreamModel.cs new file mode 100644 index 0000000..919411e --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/IIceCreamModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDataModels.Models +{ + public interface IIceCreamModel : IId + { + string IceCreamName { get; } + + double Price { get; } + + Dictionary IceCreamComponents { get; } + } +} diff --git a/IceCreamShop/IceCreamShopDataModels/IId.cs b/IceCreamShop/IceCreamShopDataModels/IId.cs new file mode 100644 index 0000000..6664644 --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/IId.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDataModels +{ + public interface IId + { + int Id { get; } + } +} diff --git a/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs b/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs new file mode 100644 index 0000000..4f2a710 --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/IOrderModel.cs @@ -0,0 +1,19 @@ +using IceCreamShopDataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopDataModels.Models +{ + public interface IOrderModel : IId + { + int IceCreamId { get; } + int Count { get; } + double Sum { get; } + OrderStatus Status { get; } + DateTime DateCreate { get; } + DateTime? DateImplement { get; } + } +} diff --git a/IceCreamShop/IceCreamShopDataModels/IceCreamShopDataModels.csproj b/IceCreamShop/IceCreamShopDataModels/IceCreamShopDataModels.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/IceCreamShopDataModels.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/IceCreamShop/IceCreamShopDataModels/OrderStatus.cs b/IceCreamShop/IceCreamShopDataModels/OrderStatus.cs new file mode 100644 index 0000000..53a6a73 --- /dev/null +++ b/IceCreamShop/IceCreamShopDataModels/OrderStatus.cs @@ -0,0 +1,15 @@ +namespace IceCreamShopDataModels.Enums +{ + public enum OrderStatus + { + Неизвестен = -1, + + Принят = 0, + + Выполняется = 1, + + Готов = 2, + + Выдан = 3 + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..56da6b9 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/DataListSingleton.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using IceCreamShopListImplement.Models; + +namespace IceCreamShopListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List IceCreams { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + IceCreams = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } + +} diff --git a/IceCreamShop/IceCreamShopListImplement/IceCreamShopListImplement.csproj b/IceCreamShop/IceCreamShopListImplement/IceCreamShopListImplement.csproj new file mode 100644 index 0000000..5e48dc2 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/IceCreamShopListImplement.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/ComponentStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..2b2e9a0 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,109 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.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/IceCreamShop/IceCreamShopListImplement/Implements/IceCreamStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/IceCreamStorage.cs new file mode 100644 index 0000000..6e00114 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/IceCreamStorage.cs @@ -0,0 +1,108 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.Implements +{ + public class IceCreamStorage : IIceCreamStorage + { + private readonly DataListSingleton _source; + public IceCreamStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var iceCream in _source.IceCreams) + { + result.Add(iceCream.GetViewModel); + } + return result; + } + public List GetFilteredList(IceCreamSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.IceCreamName)) + { + return result; + } + foreach (var iceCream in _source.IceCreams) + { + if (iceCream.IceCreamName.Contains(model.IceCreamName)) + { + result.Add(iceCream.GetViewModel); + } + } + return result; + } + public IceCreamViewModel? GetElement(IceCreamSearchModel model) + { + if (string.IsNullOrEmpty(model.IceCreamName) && !model.Id.HasValue) + { + return null; + } + foreach (var iceCream in _source.IceCreams) + { + if ((!string.IsNullOrEmpty(model.IceCreamName) && + iceCream.IceCreamName == model.IceCreamName) || + (model.Id.HasValue && iceCream.Id == model.Id)) + { + return iceCream.GetViewModel; + } + } + return null; + } + public IceCreamViewModel? Insert(IceCreamBindingModel model) + { + model.Id = 1; + foreach (var iceCream in _source.IceCreams) + { + if (model.Id <= iceCream.Id) + { + model.Id = iceCream.Id + 1; + } + } + var newIceCream = IceCream.Create(model); + if (newIceCream == null) + { + return null; + } + _source.IceCreams.Add(newIceCream); + return newIceCream.GetViewModel; + } + public IceCreamViewModel? Update(IceCreamBindingModel model) + { + foreach (var iceCream in _source.IceCreams) + { + if (iceCream.Id == model.Id) + { + iceCream.Update(model); + return iceCream.GetViewModel; + } + } + return null; + } + public IceCreamViewModel? Delete(IceCreamBindingModel model) + { + for (int i = 0; i < _source.IceCreams.Count; ++i) + { + if (_source.IceCreams[i].Id == model.Id) + { + var element = _source.IceCreams[i]; + _source.IceCreams.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Implements/OrderStorage.cs b/IceCreamShop/IceCreamShopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..d10184b --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,120 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.SearchModels; +using IceCreamShopContracts.StoragesContracts; +using IceCreamShopContracts.ViewModels; +using IceCreamShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(AccessIceCreamStorage(order.GetViewModel)); + } + return result; + } + public List GetFilteredList(OrderSearchModel + model) + { + var result = new List(); + if (!model.Id.HasValue) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(AccessIceCreamStorage(order.GetViewModel)); + } + } + return result; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var order in _source.Orders) + { + if (model.Id.HasValue && order.Id == model.Id) + { + return order.GetViewModel; + } + } + return null; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var order in _source.Orders) + { + if (model.Id <= order.Id) + { + model.Id = order.Id + 1; + } + } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + order.Update(model); + return order.GetViewModel; + } + } + return null; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public OrderViewModel AccessIceCreamStorage(OrderViewModel model) + { + foreach (var iceCream in _source.IceCreams) + { + if (iceCream.Id == model.IceCreamId) + { + model.IceCreamName = iceCream.IceCreamName; + break; + } + } + return model; + } + } + +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Component.cs b/IceCreamShop/IceCreamShopListImplement/Models/Component.cs new file mode 100644 index 0000000..50964c9 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/Component.cs @@ -0,0 +1,46 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.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/IceCreamShop/IceCreamShopListImplement/Models/IceCream.cs b/IceCreamShop/IceCreamShopListImplement/Models/IceCream.cs new file mode 100644 index 0000000..4664690 --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/IceCream.cs @@ -0,0 +1,55 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.Models +{ + public class IceCream : IIceCreamModel + { + public int Id { get; private set; } + public string IceCreamName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary IceCreamComponents + { + get; + private set; + } = new Dictionary(); + public static IceCream? Create(IceCreamBindingModel? model) + { + if (model == null) + { + return null; + } + return new IceCream() + { + Id = model.Id, + IceCreamName = model.IceCreamName, + Price = model.Price, + IceCreamComponents = model.IceCreamComponents + }; + } + public void Update(IceCreamBindingModel? model) + { + if (model == null) + { + return; + } + IceCreamName = model.IceCreamName; + Price = model.Price; + IceCreamComponents = model.IceCreamComponents; + } + public IceCreamViewModel GetViewModel => new() + { + Id = Id, + IceCreamName = IceCreamName, + Price = Price, + IceCreamComponents = IceCreamComponents + }; + + } +} diff --git a/IceCreamShop/IceCreamShopListImplement/Models/Order.cs b/IceCreamShop/IceCreamShopListImplement/Models/Order.cs new file mode 100644 index 0000000..2444fac --- /dev/null +++ b/IceCreamShop/IceCreamShopListImplement/Models/Order.cs @@ -0,0 +1,69 @@ +using IceCreamShopContracts.BindingModels; +using IceCreamShopContracts.ViewModels; +using IceCreamShopDataModels; +using IceCreamShopDataModels.Enums; +using IceCreamShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace IceCreamShopListImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int IceCreamId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + IceCreamId = model.IceCreamId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate =model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Id = model.Id; + IceCreamId = model.IceCreamId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + IceCreamId = IceCreamId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + + } +}