From a1ac59994c839e4bf252a4217f937bf9f7f0f0e3 Mon Sep 17 00:00:00 2001 From: DavidMakarov Date: Thu, 4 Apr 2024 00:29:16 +0400 Subject: [PATCH] =?UTF-8?q?=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BD=D0=B0=D1=8F=20=D0=BB=D0=B0=D0=B11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FlowerShop/FlowerShop/FormMain.Designer.cs | 24 +- FlowerShop/FlowerShop/FormMain.cs | 16 ++ FlowerShop/FlowerShop/FormMain.resx | 2 +- .../FlowerShop/FormMakeSupply.Designer.cs | 153 +++++++++++++ FlowerShop/FlowerShop/FormMakeSupply.cs | 125 ++++++++++ FlowerShop/FlowerShop/FormMakeSupply.resx | 120 ++++++++++ FlowerShop/FlowerShop/FormShop.Designer.cs | 213 ++++++++++++++++++ FlowerShop/FlowerShop/FormShop.cs | 125 ++++++++++ FlowerShop/FlowerShop/FormShop.resx | 120 ++++++++++ FlowerShop/FlowerShop/FormShops.Designer.cs | 126 +++++++++++ FlowerShop/FlowerShop/FormShops.cs | 104 +++++++++ FlowerShop/FlowerShop/FormShops.resx | 120 ++++++++++ FlowerShop/FlowerShop/Program.cs | 5 + .../BusinessLogics/ShopLogic.cs | 158 +++++++++++++ .../BindingModels/ShopBindingModel.cs | 17 ++ .../BusinessLogicsContracts/IShopLogic.cs | 17 ++ .../SearchModels/ShopSearchModel.cs | 8 + .../StoragesContracts/IShopStorage.cs | 16 ++ .../ViewModels/ShopViewModel.cs | 21 ++ .../FlowerShopDataModels/Models/IShopModel.cs | 10 + .../DataListSingleton.cs | 2 + .../Implements/ShopStorage.cs | 109 +++++++++ .../FlowerShopListImplement/Models/Shop.cs | 50 ++++ 23 files changed, 1657 insertions(+), 4 deletions(-) create mode 100644 FlowerShop/FlowerShop/FormMakeSupply.Designer.cs create mode 100644 FlowerShop/FlowerShop/FormMakeSupply.cs create mode 100644 FlowerShop/FlowerShop/FormMakeSupply.resx create mode 100644 FlowerShop/FlowerShop/FormShop.Designer.cs create mode 100644 FlowerShop/FlowerShop/FormShop.cs create mode 100644 FlowerShop/FlowerShop/FormShop.resx create mode 100644 FlowerShop/FlowerShop/FormShops.Designer.cs create mode 100644 FlowerShop/FlowerShop/FormShops.cs create mode 100644 FlowerShop/FlowerShop/FormShops.resx create mode 100644 FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs create mode 100644 FlowerShop/FlowerShopContracts/BindingModels/ShopBindingModel.cs create mode 100644 FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 FlowerShop/FlowerShopContracts/SearchModels/ShopSearchModel.cs create mode 100644 FlowerShop/FlowerShopContracts/StoragesContracts/IShopStorage.cs create mode 100644 FlowerShop/FlowerShopContracts/ViewModels/ShopViewModel.cs create mode 100644 FlowerShop/FlowerShopDataModels/Models/IShopModel.cs create mode 100644 FlowerShop/FlowerShopListImplement/Implements/ShopStorage.cs create mode 100644 FlowerShop/FlowerShopListImplement/Models/Shop.cs diff --git a/FlowerShop/FlowerShop/FormMain.Designer.cs b/FlowerShop/FlowerShop/FormMain.Designer.cs index f8b0d15..eb2bf22 100644 --- a/FlowerShop/FlowerShop/FormMain.Designer.cs +++ b/FlowerShop/FlowerShop/FormMain.Designer.cs @@ -32,12 +32,14 @@ справочникиToolStripMenuItem = new ToolStripMenuItem(); ЦветыToolStripMenuItem = new ToolStripMenuItem(); КомпонентыToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); buttonTakeOrderInWork = new Button(); buttonOrderReady = new Button(); buttonIssuedOrder = new Button(); buttonRef = new Button(); + пополнениеМагазинаToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -53,7 +55,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { ЦветыToolStripMenuItem, КомпонентыToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { ЦветыToolStripMenuItem, КомпонентыToolStripMenuItem, магазиныToolStripMenuItem, пополнениеМагазинаToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -61,17 +63,24 @@ // ЦветыToolStripMenuItem // ЦветыToolStripMenuItem.Name = "ЦветыToolStripMenuItem"; - ЦветыToolStripMenuItem.Size = new Size(145, 22); + ЦветыToolStripMenuItem.Size = new Size(198, 22); ЦветыToolStripMenuItem.Text = "Цветы"; ЦветыToolStripMenuItem.Click += ЦветыToolStripMenuItem_Click; // // КомпонентыToolStripMenuItem // КомпонентыToolStripMenuItem.Name = "КомпонентыToolStripMenuItem"; - КомпонентыToolStripMenuItem.Size = new Size(145, 22); + КомпонентыToolStripMenuItem.Size = new Size(198, 22); КомпонентыToolStripMenuItem.Text = "Компоненты"; КомпонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(198, 22); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; + // // dataGridView // dataGridView.BackgroundColor = Color.White; @@ -132,6 +141,13 @@ buttonRef.UseVisualStyleBackColor = true; buttonRef.Click += ButtonRef_Click; // + // пополнениеМагазинаToolStripMenuItem + // + пополнениеМагазинаToolStripMenuItem.Name = "пополнениеМагазинаToolStripMenuItem"; + пополнениеМагазинаToolStripMenuItem.Size = new Size(198, 22); + пополнениеМагазинаToolStripMenuItem.Text = "Пополнение магазина"; + пополнениеМагазинаToolStripMenuItem.Click += пополнениеМагазинаToolStripMenuItem_Click; + // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); @@ -167,5 +183,7 @@ private Button buttonOrderReady; private Button buttonIssuedOrder; private Button buttonRef; + private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; } } \ No newline at end of file diff --git a/FlowerShop/FlowerShop/FormMain.cs b/FlowerShop/FlowerShop/FormMain.cs index 3b92cbb..d96670d 100644 --- a/FlowerShop/FlowerShop/FormMain.cs +++ b/FlowerShop/FlowerShop/FormMain.cs @@ -53,6 +53,22 @@ namespace FlowerShopView form.ShowDialog(); } } + private void магазиныToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + if (service is FormShops form) + { + form.ShowDialog(); + } + } + private void пополнениеМагазинаToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormMakeSupply)); + if (service is FormMakeSupply form) + { + form.ShowDialog(); + } + } private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); diff --git a/FlowerShop/FlowerShop/FormMain.resx b/FlowerShop/FlowerShop/FormMain.resx index a0623c8..5203d24 100644 --- a/FlowerShop/FlowerShop/FormMain.resx +++ b/FlowerShop/FlowerShop/FormMain.resx @@ -18,7 +18,7 @@ System.Resources.ResXResourceReader, System.Windows.Forms, ... System.Resources.ResXResourceWriter, System.Windows.Forms, ... this is my long stringthis is a comment - Blue + Blue [base64 mime encoded serialized .NET Framework object] diff --git a/FlowerShop/FlowerShop/FormMakeSupply.Designer.cs b/FlowerShop/FlowerShop/FormMakeSupply.Designer.cs new file mode 100644 index 0000000..ef47e13 --- /dev/null +++ b/FlowerShop/FlowerShop/FormMakeSupply.Designer.cs @@ -0,0 +1,153 @@ +namespace FlowerShopView +{ + partial class FormMakeSupply + { + /// + /// 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() + { + labelShop = new Label(); + comboBoxShop = new ComboBox(); + labelFlower = new Label(); + comboBoxFlower = new ComboBox(); + labelCount = new Label(); + textBoxCount = new TextBox(); + buttonSave = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // labelShop + // + labelShop.AutoSize = true; + labelShop.Location = new Point(14, 13); + labelShop.Margin = new Padding(4, 0, 4, 0); + labelShop.Name = "labelShop"; + labelShop.Size = new Size(60, 15); + labelShop.TabIndex = 2; + labelShop.Text = "Магазин:"; + // + // comboBoxShop + // + comboBoxShop.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxShop.FormattingEnabled = true; + comboBoxShop.Location = new Point(102, 10); + comboBoxShop.Margin = new Padding(4, 3, 4, 3); + comboBoxShop.Name = "comboBoxShop"; + comboBoxShop.Size = new Size(252, 23); + comboBoxShop.TabIndex = 5; + // + // labelFlower + // + labelFlower.AutoSize = true; + labelFlower.Location = new Point(14, 48); + labelFlower.Margin = new Padding(4, 0, 4, 0); + labelFlower.Name = "labelFlower"; + labelFlower.Size = new Size(80, 15); + labelFlower.TabIndex = 6; + labelFlower.Text = "Цветок:"; + // + // comboBoxFlower + // + comboBoxFlower.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxFlower.FormattingEnabled = true; + comboBoxFlower.Location = new Point(102, 44); + comboBoxFlower.Margin = new Padding(4, 3, 4, 3); + comboBoxFlower.Name = "comboBoxFlower"; + comboBoxFlower.Size = new Size(252, 23); + comboBoxFlower.TabIndex = 7; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(14, 83); + labelCount.Margin = new Padding(4, 0, 4, 0); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(78, 15); + labelCount.TabIndex = 8; + labelCount.Text = "Количество:"; + // + // textBoxCount + // + textBoxCount.Location = new Point(102, 80); + textBoxCount.Margin = new Padding(4, 3, 4, 3); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(252, 23); + textBoxCount.TabIndex = 9; + // + // buttonSave + // + buttonSave.Location = new Point(160, 112); + buttonSave.Margin = new Padding(4, 3, 4, 3); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(88, 27); + buttonSave.TabIndex = 10; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(254, 112); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 11; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormMakeShipment + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(373, 147); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxFlower); + Controls.Add(labelFlower); + Controls.Add(comboBoxShop); + Controls.Add(labelShop); + Name = "FormMakeShipment"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Пополнение магазина"; + Load += FormMakeShipment_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelShop; + private ComboBox comboBoxShop; + private Label labelFlower; + private ComboBox comboBoxFlower; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/FlowerShop/FlowerShop/FormMakeSupply.cs b/FlowerShop/FlowerShop/FormMakeSupply.cs new file mode 100644 index 0000000..292e8d6 --- /dev/null +++ b/FlowerShop/FlowerShop/FormMakeSupply.cs @@ -0,0 +1,125 @@ +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.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 FlowerShopView +{ + public partial class FormMakeSupply : Form + { + private readonly ILogger _logger; + + private readonly IFlowerLogic _logicFlower; + + private readonly IShopLogic _logicShop; + + public FormMakeSupply(ILogger logger, IFlowerLogic logicFlower, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicFlower = logicFlower; + _logicShop = logicShop; + } + + private void FormMakeShipment_Load(object sender, EventArgs e) + { + _logger.LogInformation("Flowers loading"); + try + { + var list = _logicFlower.ReadList(null); + if (list != null) + { + comboBoxFlower.DisplayMember = "FlowerName"; + comboBoxFlower.ValueMember = "Id"; + comboBoxFlower.DataSource = list; + comboBoxFlower.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Flowers loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + _logger.LogInformation("Shops loading"); + try + { + var list = _logicShop.ReadList(null); + if (list != null) + { + comboBoxShop.DisplayMember = "ShopName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = list; + comboBoxShop.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Shops loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxFlower.SelectedValue == null) + { + MessageBox.Show("Выберите цветок", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Shop replenishment"); + try + { + var iceCream = _logicFlower.ReadElement(new FlowerSearchModel + { Id = Convert.ToInt32(comboBoxFlower.SelectedValue) }); + if (iceCream == null) + { + throw new Exception("Мороженое не найдено."); + } + var operationResult = _logicShop.MakeSupply(new ShopSearchModel + { + Id = Convert.ToInt32(comboBoxShop.SelectedValue) + }, + iceCream, + Convert.ToInt32(textBoxCount.Text)); + if (!operationResult) + { + throw new Exception("Ошибка при проведении поставки."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop supply error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/FlowerShop/FlowerShop/FormMakeSupply.resx b/FlowerShop/FlowerShop/FormMakeSupply.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/FlowerShop/FlowerShop/FormMakeSupply.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/FlowerShop/FlowerShop/FormShop.Designer.cs b/FlowerShop/FlowerShop/FormShop.Designer.cs new file mode 100644 index 0000000..92f74ef --- /dev/null +++ b/FlowerShop/FlowerShop/FormShop.Designer.cs @@ -0,0 +1,213 @@ +namespace FlowerShopView +{ + partial class FormShop + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + labelName = new Label(); + textBoxName = new TextBox(); + labelAddress = new Label(); + textBoxAddress = new TextBox(); + dateTimePicker = new DateTimePicker(); + labelOpeningDate = new Label(); + groupBoxFlowers = new GroupBox(); + dataGridView = new DataGridView(); + ColumnId = new DataGridViewTextBoxColumn(); + ColumnName = new DataGridViewTextBoxColumn(); + ColumnCount = new DataGridViewTextBoxColumn(); + buttonSave = new Button(); + buttonCancel = new Button(); + groupBoxFlowers.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // labelName + // + labelName.AutoSize = true; + labelName.Location = new Point(14, 10); + labelName.Margin = new Padding(4, 0, 4, 0); + labelName.Name = "labelName"; + labelName.Size = new Size(62, 15); + labelName.TabIndex = 1; + labelName.Text = "Название:"; + // + // textBoxName + // + textBoxName.Location = new Point(92, 7); + textBoxName.Margin = new Padding(4, 3, 4, 3); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(252, 23); + textBoxName.TabIndex = 2; + // + // labelAddress + // + labelAddress.AutoSize = true; + labelAddress.Location = new Point(14, 40); + labelAddress.Margin = new Padding(4, 0, 4, 0); + labelAddress.Name = "labelAddress"; + labelAddress.Size = new Size(43, 15); + labelAddress.TabIndex = 3; + labelAddress.Text = "Адрес:"; + // + // textBoxAddress + // + textBoxAddress.Location = new Point(92, 37); + textBoxAddress.Margin = new Padding(4, 3, 4, 3); + textBoxAddress.Name = "textBoxAddress"; + textBoxAddress.Size = new Size(252, 23); + textBoxAddress.TabIndex = 4; + // + // dateTimePicker + // + dateTimePicker.Location = new Point(126, 66); + dateTimePicker.Name = "dateTimePicker"; + dateTimePicker.Size = new Size(218, 23); + dateTimePicker.TabIndex = 5; + // + // labelOpeningDate + // + labelOpeningDate.AutoSize = true; + labelOpeningDate.Location = new Point(14, 69); + labelOpeningDate.Margin = new Padding(4, 0, 4, 0); + labelOpeningDate.Name = "labelOpeningDate"; + labelOpeningDate.Size = new Size(90, 15); + labelOpeningDate.TabIndex = 6; + labelOpeningDate.Text = "Дата открытия:"; + // + // groupBoxFlowers + // + groupBoxFlowers.Controls.Add(dataGridView); + groupBoxFlowers.Location = new Point(4, 100); + groupBoxFlowers.Margin = new Padding(4, 3, 4, 3); + groupBoxFlowers.Name = "groupBoxFlowers"; + groupBoxFlowers.Padding = new Padding(4, 3, 4, 3); + groupBoxFlowers.Size = new Size(469, 288); + groupBoxFlowers.TabIndex = 7; + groupBoxFlowers.TabStop = false; + groupBoxFlowers.Text = "Цветы"; + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ControlLightLight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnName, ColumnCount }); + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(4, 19); + dataGridView.Margin = new Padding(4, 3, 4, 3); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(457, 266); + dataGridView.TabIndex = 0; + // + // ColumnId + // + ColumnId.HeaderText = "Id"; + ColumnId.Name = "ColumnId"; + ColumnId.ReadOnly = true; + ColumnId.Visible = false; + // + // ColumnName + // + ColumnName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + ColumnName.HeaderText = "Название цветка"; + ColumnName.Name = "ColumnName"; + ColumnName.ReadOnly = true; + // + // ColumnCount + // + ColumnCount.HeaderText = "Количество"; + ColumnCount.Name = "ColumnCount"; + ColumnCount.ReadOnly = true; + // + // buttonSave + // + buttonSave.Location = new Point(255, 394); + buttonSave.Margin = new Padding(4, 3, 4, 3); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(88, 27); + buttonSave.TabIndex = 8; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(359, 394); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 9; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormShop + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(478, 432); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(groupBoxFlowers); + Controls.Add(labelOpeningDate); + Controls.Add(dateTimePicker); + Controls.Add(textBoxAddress); + Controls.Add(labelAddress); + Controls.Add(textBoxName); + Controls.Add(labelName); + Name = "FormShop"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Магазин"; + Load += FormShop_Load; + groupBoxFlowers.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelName; + private TextBox textBoxName; + private Label labelAddress; + private TextBox textBoxAddress; + private DateTimePicker dateTimePicker; + private Label labelOpeningDate; + private GroupBox groupBoxFlowers; + private DataGridView dataGridView; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnName; + private DataGridViewTextBoxColumn ColumnCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/FlowerShop/FlowerShop/FormShop.cs b/FlowerShop/FlowerShop/FormShop.cs new file mode 100644 index 0000000..82c4f82 --- /dev/null +++ b/FlowerShop/FlowerShop/FormShop.cs @@ -0,0 +1,125 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace FlowerShopView +{ + public partial class FormShop : Form + { + private readonly ILogger _logger; + + private readonly IShopLogic _logic; + + private int? _id; + + private Dictionary _shopFlowers; + + public int Id { set { _id = value; } } + + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _shopFlowers = new Dictionary(); + } + + private void FormShop_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Shop loading"); + try + { + var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value }); + if (view != null) + { + textBoxName.Text = view.ShopName; + textBoxAddress.Text = view.Address; + dateTimePicker.Value = view.DateOpening; + _shopFlowers = view.Flowers ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Shop flowers loading"); + try + { + if (_shopFlowers != null) + { + dataGridView.Rows.Clear(); + foreach (var iceCream in _shopFlowers) + { + dataGridView.Rows.Add(new object[] { iceCream.Key, iceCream.Value.Item1.FlowerName, iceCream.Value.Item2 }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop flowers loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxAddress.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(dateTimePicker.Text)) + { + MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Shop saving"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxName.Text, + Address = textBoxAddress.Text, + DateOpening = dateTimePicker.Value, + Flowers = _shopFlowers + }; + 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, "Shop saving error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/FlowerShop/FlowerShop/FormShop.resx b/FlowerShop/FlowerShop/FormShop.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/FlowerShop/FlowerShop/FormShop.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/FlowerShop/FlowerShop/FormShops.Designer.cs b/FlowerShop/FlowerShop/FormShops.Designer.cs new file mode 100644 index 0000000..4df060f --- /dev/null +++ b/FlowerShop/FlowerShop/FormShops.Designer.cs @@ -0,0 +1,126 @@ +namespace FlowerShopView +{ + partial class FormShops + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + dataGridView = new DataGridView(); + buttonUpd = new Button(); + buttonDel = new Button(); + buttonEdit = new Button(); + buttonAdd = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.BackgroundColor = SystemColors.ControlLightLight; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Dock = DockStyle.Left; + dataGridView.Location = new Point(0, 0); + dataGridView.Margin = new Padding(4, 3, 4, 3); + dataGridView.MultiSelect = false; + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersVisible = false; + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.Size = new Size(408, 360); + dataGridView.TabIndex = 2; + // + // buttonUpd + // + buttonUpd.Location = new Point(432, 152); + buttonUpd.Margin = new Padding(4, 3, 4, 3); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(88, 27); + buttonUpd.TabIndex = 12; + buttonUpd.Text = "Обновить"; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += ButtonUpd_Click; + // + // buttonDel + // + buttonDel.Location = new Point(432, 105); + buttonDel.Margin = new Padding(4, 3, 4, 3); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(88, 27); + buttonDel.TabIndex = 11; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += ButtonDel_Click; + // + // buttonEdit + // + buttonEdit.Location = new Point(432, 58); + buttonEdit.Margin = new Padding(4, 3, 4, 3); + buttonEdit.Name = "buttonEdit"; + buttonEdit.Size = new Size(88, 27); + buttonEdit.TabIndex = 10; + buttonEdit.Text = "Изменить"; + buttonEdit.UseVisualStyleBackColor = true; + buttonEdit.Click += ButtonEdit_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(432, 14); + buttonAdd.Margin = new Padding(4, 3, 4, 3); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(88, 27); + buttonAdd.TabIndex = 9; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // FormShops + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(541, 360); + Controls.Add(buttonUpd); + Controls.Add(buttonDel); + Controls.Add(buttonEdit); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormShops"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Магазины"; + Load += FormShops_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonUpd; + private Button buttonDel; + private Button buttonEdit; + private Button buttonAdd; + } +} \ No newline at end of file diff --git a/FlowerShop/FlowerShop/FormShops.cs b/FlowerShop/FlowerShop/FormShops.cs new file mode 100644 index 0000000..5828e21 --- /dev/null +++ b/FlowerShop/FlowerShop/FormShops.cs @@ -0,0 +1,104 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace FlowerShopView +{ + public partial class FormShops : Form + { + private readonly ILogger _logger; + + private readonly IShopLogic _logic; + + public FormShops(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormShops_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Flowers"].Visible = false; + } + _logger.LogInformation("Shops loading"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Shops loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void ButtonEdit_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Deletion of shop"); + try + { + if (!_logic.Delete(new ShopBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Shop deletion error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ButtonUpd_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/FlowerShop/FlowerShop/FormShops.resx b/FlowerShop/FlowerShop/FormShops.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/FlowerShop/FlowerShop/FormShops.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/FlowerShop/FlowerShop/Program.cs b/FlowerShop/FlowerShop/Program.cs index d1c5842..324185b 100644 --- a/FlowerShop/FlowerShop/Program.cs +++ b/FlowerShop/FlowerShop/Program.cs @@ -36,9 +36,11 @@ namespace FlowerShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -46,6 +48,9 @@ namespace FlowerShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs b/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs new file mode 100644 index 0000000..0ee35f5 --- /dev/null +++ b/FlowerShop/FlowerShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -0,0 +1,158 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.BusinessLogicsContracts; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace FlowerShopBusinessLogic.BusinessLogics +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage) + { + _logger = logger; + _shopStorage = shopStorage; + } + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. Name:{Name}.Id:{Id}", model?.ShopName, model?.Id); + var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.ShopName, model.Id); + var element = _shopStorage.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(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool MakeSupply(ShopSearchModel shopModel, IFlowerModel flower, int count) + { + if (shopModel == null) + { + throw new ArgumentNullException(nameof(shopModel)); + } + if (flower == null) + { + throw new ArgumentNullException(nameof(flower)); + } + if (count <= 0) + { + throw new ArgumentException("Количество цветов в поставке должно быть больше нуля", nameof(count)); + } + _logger.LogInformation("MakeSupply(GetElement). ShopName: {ShopName}. Id: {Id}", shopModel.ShopName, shopModel.Id); + var shop = _shopStorage.GetElement(shopModel); + if (shop == null) + { + _logger.LogWarning("MakeSupply(GetElement). Element not found"); + return false; + } + if (shop.Flowers.ContainsKey(flower.Id)) + { + var shopFlower = shop.Flowers[flower.Id]; + shopFlower.Item2 += count; + shop.Flowers[flower.Id] = shopFlower; + _logger.LogInformation("MakeSupply. Added {count} '{flower}' to '{ShopName}' shop", count, flower.FlowerName, shop.ShopName); + } + else + { + shop.Flowers.Add(flower.Id, (flower, count)); + _logger.LogInformation("MakeSupply. Added {count} new '{flower}' to '{ShopName}' shop", count, flower.FlowerName, shop.ShopName); + } + if (_shopStorage.Update(new ShopBindingModel() + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + Flowers = shop.Flowers, + }) == null) + { + _logger.LogWarning("MakeSupply. Update operation failed"); + return false; + } + return true; + } + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Нет названия магазина ", nameof(model.ShopName)); + } + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адреса ", nameof(model.Address)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}. Address:{Address}. DateOpening:{DateOpening} Id: {Id}", model.ShopName, model.Address, model.DateOpening, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/FlowerShop/FlowerShopContracts/BindingModels/ShopBindingModel.cs b/FlowerShop/FlowerShopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..3197295 --- /dev/null +++ b/FlowerShop/FlowerShopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,17 @@ +using FlowerShopDataModels.Models; + +namespace FlowerShopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Address { get; set; } = string.Empty; + public DateTime DateOpening { get; set; } = DateTime.Now; + public Dictionary Flowers + { + get; + set; + } = new(); + } +} diff --git a/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs b/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..7a3ff14 --- /dev/null +++ b/FlowerShop/FlowerShopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,17 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; + +namespace FlowerShopContracts.BusinessLogicsContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool MakeSupply(ShopSearchModel shopModel, IFlowerModel flower, int count); + } +} diff --git a/FlowerShop/FlowerShopContracts/SearchModels/ShopSearchModel.cs b/FlowerShop/FlowerShopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..0e9440f --- /dev/null +++ b/FlowerShop/FlowerShopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,8 @@ +namespace FlowerShopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? ShopName { get; set; } + } +} diff --git a/FlowerShop/FlowerShopContracts/StoragesContracts/IShopStorage.cs b/FlowerShop/FlowerShopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..4827bd1 --- /dev/null +++ b/FlowerShop/FlowerShopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,16 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.ViewModels; + +namespace FlowerShopContracts.StoragesContracts +{ + public interface IShopStorage + { + List GetFullList(); + List GetFilteredList(ShopSearchModel model); + ShopViewModel? GetElement(ShopSearchModel model); + ShopViewModel? Insert(ShopBindingModel model); + ShopViewModel? Update(ShopBindingModel model); + ShopViewModel? Delete(ShopBindingModel model); + } +} diff --git a/FlowerShop/FlowerShopContracts/ViewModels/ShopViewModel.cs b/FlowerShop/FlowerShopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..0ff5525 --- /dev/null +++ b/FlowerShop/FlowerShopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,21 @@ +using FlowerShopDataModels.Models; +using System.ComponentModel; + +namespace FlowerShopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + [DisplayName("Название магазина")] + public string ShopName { get; set; } = string.Empty; + [DisplayName("Адрес магазина")] + public string Address { get; set; } = string.Empty; + [DisplayName("Дата открытия")] + public DateTime DateOpening { get; set; } = DateTime.Now; + public Dictionary Flowers + { + get; + set; + } = new(); + } +} diff --git a/FlowerShop/FlowerShopDataModels/Models/IShopModel.cs b/FlowerShop/FlowerShopDataModels/Models/IShopModel.cs new file mode 100644 index 0000000..1f09b18 --- /dev/null +++ b/FlowerShop/FlowerShopDataModels/Models/IShopModel.cs @@ -0,0 +1,10 @@ +namespace FlowerShopDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + string Address { get; } + Dictionary Flowers { get; } + DateTime DateOpening { get; } + } +} \ No newline at end of file diff --git a/FlowerShop/FlowerShopListImplement/DataListSingleton.cs b/FlowerShop/FlowerShopListImplement/DataListSingleton.cs index 4871a55..7b4985b 100644 --- a/FlowerShop/FlowerShopListImplement/DataListSingleton.cs +++ b/FlowerShop/FlowerShopListImplement/DataListSingleton.cs @@ -8,11 +8,13 @@ namespace FlowerShopListImplement public List Components { get; set; } public List Orders { get; set; } public List Flowers { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Flowers = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/FlowerShop/FlowerShopListImplement/Implements/ShopStorage.cs b/FlowerShop/FlowerShopListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..3f01ede --- /dev/null +++ b/FlowerShop/FlowerShopListImplement/Implements/ShopStorage.cs @@ -0,0 +1,109 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopListImplement.Models; + +namespace FlowerShopListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.ShopName)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.ShopName) && + shop.ShopName == model.ShopName) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/FlowerShop/FlowerShopListImplement/Models/Shop.cs b/FlowerShop/FlowerShopListImplement/Models/Shop.cs new file mode 100644 index 0000000..342b198 --- /dev/null +++ b/FlowerShop/FlowerShopListImplement/Models/Shop.cs @@ -0,0 +1,50 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.Models; + +namespace FlowerShopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public DateTime DateOpening { get; private set; } + public Dictionary Flowers { get; private set; } = new(); + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + Flowers = Flowers + }; + } +} -- 2.25.1