From 7914c6d4b0721bdf7140291d84bb0343d322b065 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 28 Feb 2024 08:47:11 +0400 Subject: [PATCH 1/6] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshop/FormMain.Designer.cs | 23 ++- .../CarpentryWorkshop/FormMain.cs | 18 ++ .../CarpentryWorkshop/Program.cs | 7 + .../CarpentryWorkshop/ShopForm.Designer.cs | 187 ++++++++++++++++++ .../CarpentryWorkshop/ShopForm.cs | 114 +++++++++++ .../CarpentryWorkshop/ShopForm.resx | 120 +++++++++++ .../CarpentryWorkshop/ShopsForm.Designer.cs | 117 +++++++++++ .../CarpentryWorkshop/ShopsForm.cs | 108 ++++++++++ .../CarpentryWorkshop/ShopsForm.resx | 120 +++++++++++ .../CarpentryWorkshop/SupplyForm.Designer.cs | 105 ++++++++++ .../CarpentryWorkshop/SupplyForm.cs | 151 ++++++++++++++ .../CarpentryWorkshop/SupplyForm.resx | 120 +++++++++++ .../BusinessLogics/ShopLogic.cs | 128 ++++++++++++ .../BindingModels/ShopBindingModel.cs | 13 ++ .../BusinessLogicsContracts/IShopLogic.cs | 17 ++ .../SearchModels/ShopSearchModel.cs | 8 + .../StoragesContracts/IShopStorage.cs | 23 +++ .../ViewModels/ShopViewModel.cs | 17 ++ .../Models/IShopModel.cs | 10 + .../DataListSingleton.cs | 2 + .../Implements/ShopStorage.cs | 135 +++++++++++++ .../Models/Shop.cs | 55 ++++++ 22 files changed, 1596 insertions(+), 2 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/ShopForm.resx create mode 100644 CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/ShopsForm.resx create mode 100644 CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx create mode 100644 CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs index 08dbbfe..453a582 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs @@ -32,6 +32,8 @@ справочникиToolStripMenuItem = new ToolStripMenuItem(); КомпонентыToolStripMenuItem = new ToolStripMenuItem(); ИзделияToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); + поставкиToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); ButtonCreateOrder = new Button(); ButtonTakeOrderInWork = new Button(); @@ -47,13 +49,14 @@ menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Size = new Size(896, 24); + menuStrip1.Padding = new Padding(5, 2, 0, 2); + menuStrip1.Size = new Size(1134, 24); menuStrip1.TabIndex = 0; menuStrip1.Text = "menuStrip1"; // // справочники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 = "Справочники"; @@ -72,6 +75,20 @@ ИзделияToolStripMenuItem.Text = "Изделия"; ИзделияToolStripMenuItem.Click += ИзделияToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(180, 22); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; + // + // поставкиToolStripMenuItem + // + поставкиToolStripMenuItem.Name = "поставкиToolStripMenuItem"; + поставкиToolStripMenuItem.Size = new Size(180, 22); + поставкиToolStripMenuItem.Text = "Поставки"; + поставкиToolStripMenuItem.Click += поставкиToolStripMenuItem_Click; + // // dataGridView // dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; @@ -174,5 +191,7 @@ private Button ButtonRef; private ToolStripMenuItem КомпонентыToolStripMenuItem; private ToolStripMenuItem ИзделияToolStripMenuItem; + private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem поставкиToolStripMenuItem; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs index 73d980e..cfc3fdc 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs @@ -151,5 +151,23 @@ namespace CarpentryWorkshopView { LoadData(); } + + private void магазиныToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(ShopsForm)); + if (service is ShopsForm form) + { + form.ShowDialog(); + } + } + + private void поставкиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(SupplyForm)); + if (service is SupplyForm form) + { + form.ShowDialog(); + } + } } } diff --git a/CarpentryWorkshop/CarpentryWorkshop/Program.cs b/CarpentryWorkshop/CarpentryWorkshop/Program.cs index a1741f7..021c482 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Program.cs @@ -39,9 +39,13 @@ namespace CarpentryWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -49,6 +53,9 @@ namespace CarpentryWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs new file mode 100644 index 0000000..9a265ac --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs @@ -0,0 +1,187 @@ +namespace CarpentryWorkshopView +{ + partial class ShopForm + { + /// + /// 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() + { + DateTimePicker = new DateTimePicker(); + NameTextBox = new TextBox(); + AddressTextBox = new TextBox(); + NameLabel = new Label(); + AdressLabel = new Label(); + DateLabel = new Label(); + SaveButton = new Button(); + CancelButton = new Button(); + DataGridView = new DataGridView(); + Column1 = new DataGridViewTextBoxColumn(); + Название = new DataGridViewTextBoxColumn(); + Цена = new DataGridViewTextBoxColumn(); + Количество = new DataGridViewTextBoxColumn(); + ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + SuspendLayout(); + // + // DateTimePicker + // + DateTimePicker.Location = new Point(85, 70); + DateTimePicker.Name = "DateTimePicker"; + DateTimePicker.Size = new Size(318, 23); + DateTimePicker.TabIndex = 0; + // + // NameTextBox + // + NameTextBox.Location = new Point(85, 12); + NameTextBox.Name = "NameTextBox"; + NameTextBox.Size = new Size(318, 23); + NameTextBox.TabIndex = 1; + // + // AddressTextBox + // + AddressTextBox.Location = new Point(85, 41); + AddressTextBox.Name = "AddressTextBox"; + AddressTextBox.Size = new Size(318, 23); + AddressTextBox.TabIndex = 2; + // + // NameLabel + // + NameLabel.AutoSize = true; + NameLabel.Location = new Point(12, 15); + NameLabel.Name = "NameLabel"; + NameLabel.Size = new Size(59, 15); + NameLabel.TabIndex = 3; + NameLabel.Text = "Название"; + // + // AdressLabel + // + AdressLabel.AutoSize = true; + AdressLabel.Location = new Point(12, 44); + AdressLabel.Name = "AdressLabel"; + AdressLabel.Size = new Size(40, 15); + AdressLabel.TabIndex = 4; + AdressLabel.Text = "Адрес"; + // + // DateLabel + // + DateLabel.AutoSize = true; + DateLabel.Location = new Point(12, 76); + DateLabel.Name = "DateLabel"; + DateLabel.Size = new Size(32, 15); + DateLabel.TabIndex = 5; + DateLabel.Text = "Дата"; + // + // SaveButton + // + SaveButton.Location = new Point(247, 255); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 6; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(328, 255); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 7; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += CancelButton_Click; + // + // DataGridView + // + DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + DataGridView.Columns.AddRange(new DataGridViewColumn[] { Column1, Название, Цена, Количество }); + DataGridView.Location = new Point(12, 99); + DataGridView.Name = "DataGridView"; + DataGridView.Size = new Size(391, 150); + DataGridView.TabIndex = 8; + // + // Column1 + // + Column1.HeaderText = "Column1"; + Column1.Name = "Column1"; + Column1.Visible = false; + // + // Название + // + Название.HeaderText = "Название"; + Название.Name = "Название"; + Название.ReadOnly = true; + Название.Width = 150; + // + // Цена + // + Цена.HeaderText = "Цена"; + Цена.Name = "Цена"; + Цена.ReadOnly = true; + // + // Количество + // + Количество.HeaderText = "Количество"; + Количество.Name = "Количество"; + Количество.ReadOnly = true; + // + // ShopForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(411, 285); + Controls.Add(DataGridView); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(DateLabel); + Controls.Add(AdressLabel); + Controls.Add(NameLabel); + Controls.Add(AddressTextBox); + Controls.Add(NameTextBox); + Controls.Add(DateTimePicker); + Name = "ShopForm"; + Text = "ShopForm"; + Load += ShopForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private DateTimePicker DateTimePicker; + private TextBox NameTextBox; + private TextBox AddressTextBox; + private Label NameLabel; + private Label AdressLabel; + private Label DateLabel; + private Button SaveButton; + private Button CancelButton; + private DataGridView DataGridView; + private DataGridViewTextBoxColumn Column1; + private DataGridViewTextBoxColumn Название; + private DataGridViewTextBoxColumn Цена; + private DataGridViewTextBoxColumn Количество; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs b/CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs new file mode 100644 index 0000000..954210b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs @@ -0,0 +1,114 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopView +{ + public partial class ShopForm : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public int? _id; + private Dictionary _woods; + public ShopForm(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void ShopForm_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var shop = _logic.ReadElement(new ShopSearchModel { Id = _id }); + if (shop != null) + { + NameTextBox.Text = shop.ShopName; + AddressTextBox.Text = shop.Address; + DateTimePicker.Text = shop.DateOpen.ToString(); + _woods = shop.ShopWoods; + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка товаров магазина"); + try + { + if (_woods != null) + { + foreach (var wood in _woods) + { + DataGridView.Rows.Add(new object[] { wood.Key, wood.Value.Item1.WoodName, wood.Value.Item1.Price, wood.Value.Item2 }); + } + } + } + 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 SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(NameTextBox.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(AddressTextBox.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = NameTextBox.Text, + Address = AddressTextBox.Text, + DateOpen = DateTimePicker.Value.Date, + ShopWoods = _woods + }; + 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); + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.resx b/CarpentryWorkshop/CarpentryWorkshop/ShopForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/ShopForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs new file mode 100644 index 0000000..ef41ce2 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs @@ -0,0 +1,117 @@ +namespace CarpentryWorkshopView +{ + partial class ShopsForm + { + /// + /// 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(); + RefreshButton = new Button(); + DeleteButton = 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(393, 374); + DataGridView.TabIndex = 0; + // + // AddButton + // + AddButton.Location = new Point(411, 12); + AddButton.Name = "AddButton"; + AddButton.Size = new Size(114, 23); + AddButton.TabIndex = 1; + AddButton.Text = "Добавить"; + AddButton.UseVisualStyleBackColor = true; + AddButton.Click += AddButton_Click; + // + // UpdateButton + // + UpdateButton.Location = new Point(411, 41); + UpdateButton.Name = "UpdateButton"; + UpdateButton.Size = new Size(114, 23); + UpdateButton.TabIndex = 2; + UpdateButton.Text = "Изменить"; + UpdateButton.UseVisualStyleBackColor = true; + UpdateButton.Click += UpdateButton_Click; + // + // RefreshButton + // + RefreshButton.Location = new Point(411, 70); + RefreshButton.Name = "RefreshButton"; + RefreshButton.Size = new Size(114, 23); + RefreshButton.TabIndex = 3; + RefreshButton.Text = "Обновить"; + RefreshButton.UseVisualStyleBackColor = true; + RefreshButton.Click += RefreshButton_Click; + // + // DeleteButton + // + DeleteButton.Location = new Point(411, 99); + DeleteButton.Name = "DeleteButton"; + DeleteButton.Size = new Size(114, 23); + DeleteButton.TabIndex = 4; + DeleteButton.Text = "Удалить"; + DeleteButton.UseVisualStyleBackColor = true; + DeleteButton.Click += DeleteButton_Click; + // + // ShopsForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(539, 403); + Controls.Add(DeleteButton); + Controls.Add(RefreshButton); + Controls.Add(UpdateButton); + Controls.Add(AddButton); + Controls.Add(DataGridView); + Name = "ShopsForm"; + Text = "ShopsForm"; + Load += ShopsForm_Load; + ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView DataGridView; + private Button AddButton; + private Button UpdateButton; + private Button RefreshButton; + private Button DeleteButton; + private DataGridViewTextBoxColumn Column1; + private DataGridViewTextBoxColumn Column2; + private DataGridViewTextBoxColumn Column3; + private DataGridViewTextBoxColumn Column4; + private DataGridViewTextBoxColumn Column5; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs b/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs new file mode 100644 index 0000000..10ac590 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs @@ -0,0 +1,108 @@ +using CarpentryWorkshop; +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopView +{ + public partial class ShopsForm : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public ShopsForm(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + 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["Address"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + DataGridView.Columns["DateOpen"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + DataGridView.Columns["ShopWoods"].Visible = false; + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ShopsForm_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void AddButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(ShopForm)); + if (service is ShopForm 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(ShopForm)); + if (service is ShopForm 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 RefreshButton_Click(object sender, EventArgs e) + { + 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 ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.resx b/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs new file mode 100644 index 0000000..44383fc --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs @@ -0,0 +1,105 @@ +namespace CarpentryWorkshopView +{ + partial class SupplyForm + { + /// + /// 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() + { + ShopComboBox = new ComboBox(); + WoodComboBox = new ComboBox(); + CountTextBox = new TextBox(); + SaveButton = new Button(); + CancelButton = new Button(); + SuspendLayout(); + // + // ShopComboBox + // + ShopComboBox.FormattingEnabled = true; + ShopComboBox.Location = new Point(12, 12); + ShopComboBox.Name = "ShopComboBox"; + ShopComboBox.Size = new Size(331, 23); + ShopComboBox.TabIndex = 0; + // + // WoodComboBox + // + WoodComboBox.FormattingEnabled = true; + WoodComboBox.Location = new Point(12, 41); + WoodComboBox.Name = "WoodComboBox"; + WoodComboBox.Size = new Size(331, 23); + WoodComboBox.TabIndex = 1; + // + // CountTextBox + // + CountTextBox.Location = new Point(12, 70); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(331, 23); + CountTextBox.TabIndex = 2; + // + // SaveButton + // + SaveButton.Location = new Point(187, 99); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 3; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(268, 99); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 4; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + CancelButton.Click += CancelButton_Click; + // + // SupplyForm + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(355, 136); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(CountTextBox); + Controls.Add(WoodComboBox); + Controls.Add(ShopComboBox); + Name = "SupplyForm"; + Text = "SupplyForm"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox ShopComboBox; + private ComboBox WoodComboBox; + private TextBox CountTextBox; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs b/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs new file mode 100644 index 0000000..ca539be --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs @@ -0,0 +1,151 @@ +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.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 CarpentryWorkshopView +{ + public partial class SupplyForm : Form + { + private readonly List? _woodList; + private readonly List? _shopsList; + IShopLogic _shopLogic; + IWoodLogic _woodLogic; + public int ShopId + { + get + { + return + Convert.ToInt32(ShopComboBox.SelectedValue); + } + set + { + ShopComboBox.SelectedValue = value; + } + } + public int WoodId + { + get + { + return + Convert.ToInt32(WoodComboBox.SelectedValue); + } + set + { + WoodComboBox.SelectedValue = value; + } + } + + public IWoodModel? WoodModel + { + get + { + if (_woodList == null) + { + return null; + } + foreach (var elem in _woodList) + { + if (elem.Id == WoodId) + { + return elem; + } + } + return null; + } + } + + + public int Count + { + get { return Convert.ToInt32(CountTextBox.Text); } + set + { CountTextBox.Text = value.ToString(); } + } + public SupplyForm(IWoodLogic woodLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _woodLogic = woodLogic; + _woodList = woodLogic.ReadList(null); + _shopsList = shopLogic.ReadList(null); + if (_woodList != null) + { + WoodComboBox.DisplayMember = "WoodName"; + WoodComboBox.ValueMember = "Id"; + WoodComboBox.DataSource = _woodList; + WoodComboBox.SelectedItem = null; + } + if (_shopsList != null) + { + ShopComboBox.DisplayMember = "ShopName"; + ShopComboBox.ValueMember = "Id"; + ShopComboBox.DataSource = _shopsList; + ShopComboBox.SelectedItem = null; + } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (WoodComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (ShopComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + int count = Convert.ToInt32(CountTextBox.Text); + + bool res = _shopLogic.MakeSupply( + new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) }, + _woodLogic.ReadElement(new() { Id = Convert.ToInt32(WoodComboBox.SelectedValue) }), + count + ); + + if (!res) + { + throw new Exception("Ошибка при пополнении. Дополнительная информация в логах"); + } + + MessageBox.Show("Пополнение прошло успешно"); + DialogResult = DialogResult.OK; + Close(); + + } + catch (Exception err) + { + MessageBox.Show("Ошибка пополнения"); + return; + } + } + + private void CancelButton_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx b/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs new file mode 100644 index 0000000..4d50c2f --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -0,0 +1,128 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopBusinessLogic.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. ShopName:{Name}. Id:{ Id}", model?.Name, 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 bool MakeSupply(ShopSearchModel model, IWoodModel wood, int count) + { + if (model == null) + return false; + return _shopStorage.SupplyWood(model, wood, count); + } + + public ShopViewModel ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.Name, 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; + } + + 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.ShopName)); + } + if (model.DateOpen == null) + { + throw new ArgumentNullException("Нет даты открытия магазина", + nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}. DateOpen:{DateOpen}. Id: { Id}", model.ShopName, model.Address, model.DateOpen, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + Name = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..feed4c9 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,13 @@ +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } + public string Address { get; set; } + public DateTime DateOpen { get; set; } + public Dictionary ShopWoods { get; set; } = new(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..df5d729 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,17 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.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 model, IWoodModel wood, int count); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..1398bb7 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,8 @@ +namespace CarpentryWorkshopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..f5a59d6 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,23 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopContracts.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); + bool SupplyWood(ShopSearchModel model, IWoodModel wood, int Count); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..d469d57 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,17 @@ +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel; + +namespace CarpentryWorkshopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public int Id { get; set; } + [DisplayName("Название магазина")] + public string ShopName { get; set; } + [DisplayName("Адрес магазина")] + public string Address { get; set; } + [DisplayName("Дата открытия")] + public DateTime DateOpen { get; set; } + public Dictionary ShopWoods { get; set; } = new(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs new file mode 100644 index 0000000..19e6d4c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs @@ -0,0 +1,10 @@ +namespace CarpentryWorkshopDataModels.Models +{ + public interface IShopModel : IId + { + string ShopName { get; } + string Address { get; } + DateTime DateOpen { get; } + Dictionary ShopWoods { get; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs index 9813b2a..1d3857e 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs @@ -8,11 +8,13 @@ namespace CarpentryWorkshopListImplement public List Components { get; set; } public List Orders { get; set; } public List Woods { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Woods = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..df27e7a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs @@ -0,0 +1,135 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.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.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.Name)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.ShopName == model.Name) || + (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; + } + + public bool SupplyWood(ShopSearchModel model, IWoodModel wood, int count) + { + if (model == null) + throw new ArgumentNullException(nameof(model)); + if (wood == null) + throw new ArgumentNullException(nameof(wood)); + if (count <= 0) + throw new ArgumentNullException("Количество должно быть положительным числом"); + + ShopViewModel curModel = GetElement(model); + if (curModel == null) + throw new ArgumentNullException(nameof(curModel)); + if (curModel.ShopWoods.TryGetValue(wood.Id, out var pair)) + { + curModel.ShopWoods[wood.Id] = (pair.Item1, pair.Item2 + count); + } + else + { + curModel.ShopWoods.Add(wood.Id, (wood, count)); + } + Update(new() + { + Id = curModel.Id, + ShopName = curModel.ShopName, + DateOpen = curModel.DateOpen, + Address = curModel.Address, + ShopWoods = curModel.ShopWoods, + }); + return true; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs new file mode 100644 index 0000000..23cae3b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs @@ -0,0 +1,55 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CarpentryWorkshopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } + public string Address { get; private set; } + public DateTime DateOpen { get; private set; } + public Dictionary ShopWoods { 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, + DateOpen = model.DateOpen, + ShopWoods = new() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + ShopWoods = model.ShopWoods; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopWoods = ShopWoods + }; + } +} From f7e4f386baa919a13ae3343ba2df1d0f80ae8a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sat, 2 Mar 2024 15:52:23 +0400 Subject: [PATCH 2/6] =?UTF-8?q?=D0=B7=D0=B0=D0=B1=D1=8B=D1=82=D1=8B=D0=B9?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82=201=20=D1=83=D1=81?= =?UTF-8?q?=D0=BB=D0=BE=D0=B6=D0=BD=D1=91=D0=BD=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshop/FormMain.cs | 8 +-- ...pForm.Designer.cs => FormShop.Designer.cs} | 2 +- .../{ShopForm.cs => FormShop.cs} | 4 +- .../{ShopForm.resx => FormShop.resx} | 0 ...Form.Designer.cs => FormShops.Designer.cs} | 2 +- .../{ShopsForm.cs => FormShops.cs} | 12 ++-- .../{ShopsForm.resx => FormShops.resx} | 0 ...orm.Designer.cs => FormSupply.Designer.cs} | 10 ++- .../{SupplyForm.cs => FormSupply.cs} | 18 ++--- .../{SupplyForm.resx => FormSupply.resx} | 50 +++++++------- .../CarpentryWorkshop/Program.cs | 6 +- .../BusinessLogics/ShopLogic.cs | 65 +++++++++++++------ .../BindingModels/SupplyBindingModel .cs | 11 ++++ .../BusinessLogicsContracts/IShopLogic.cs | 2 +- .../SearchModels/ShopSearchModel.cs | 2 +- .../StoragesContracts/IShopStorage.cs | 1 - .../Models/ISupplyModel.cs | 9 +++ .../Implements/ShopStorage.cs | 14 ++-- .../Models/Shop.cs | 4 +- 19 files changed, 135 insertions(+), 85 deletions(-) rename CarpentryWorkshop/CarpentryWorkshop/{ShopForm.Designer.cs => FormShop.Designer.cs} (99%) rename CarpentryWorkshop/CarpentryWorkshop/{ShopForm.cs => FormShop.cs} (97%) rename CarpentryWorkshop/CarpentryWorkshop/{ShopForm.resx => FormShop.resx} (100%) rename CarpentryWorkshop/CarpentryWorkshop/{ShopsForm.Designer.cs => FormShops.Designer.cs} (99%) rename CarpentryWorkshop/CarpentryWorkshop/{ShopsForm.cs => FormShops.cs} (94%) rename CarpentryWorkshop/CarpentryWorkshop/{ShopsForm.resx => FormShops.resx} (100%) rename CarpentryWorkshop/CarpentryWorkshop/{SupplyForm.Designer.cs => FormSupply.Designer.cs} (91%) rename CarpentryWorkshop/CarpentryWorkshop/{SupplyForm.cs => FormSupply.cs} (88%) rename CarpentryWorkshop/CarpentryWorkshop/{SupplyForm.resx => FormSupply.resx} (93%) create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/SupplyBindingModel .cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopDataModels/Models/ISupplyModel.cs diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs index cfc3fdc..3a3039d 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs @@ -154,8 +154,8 @@ namespace CarpentryWorkshopView private void магазиныToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(ShopsForm)); - if (service is ShopsForm form) + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + if (service is FormShops form) { form.ShowDialog(); } @@ -163,8 +163,8 @@ namespace CarpentryWorkshopView private void поставкиToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(SupplyForm)); - if (service is SupplyForm form) + var service = Program.ServiceProvider?.GetService(typeof(FormSupply)); + if (service is FormSupply form) { form.ShowDialog(); } diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs similarity index 99% rename from CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs index 9a265ac..a11fedc 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs @@ -1,6 +1,6 @@ namespace CarpentryWorkshopView { - partial class ShopForm + partial class FormShop { /// /// Required designer variable. diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs similarity index 97% rename from CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs rename to CarpentryWorkshop/CarpentryWorkshop/FormShop.cs index 954210b..0076e41 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs @@ -6,13 +6,13 @@ using Microsoft.Extensions.Logging; namespace CarpentryWorkshopView { - public partial class ShopForm : Form + public partial class FormShop : Form { private readonly ILogger _logger; private readonly IShopLogic _logic; public int? _id; private Dictionary _woods; - public ShopForm(ILogger logger, IShopLogic logic) + public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopForm.resx b/CarpentryWorkshop/CarpentryWorkshop/FormShop.resx similarity index 100% rename from CarpentryWorkshop/CarpentryWorkshop/ShopForm.resx rename to CarpentryWorkshop/CarpentryWorkshop/FormShop.resx diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs similarity index 99% rename from CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs index ef41ce2..ec39f29 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs @@ -1,6 +1,6 @@ namespace CarpentryWorkshopView { - partial class ShopsForm + partial class FormShops { /// /// Required designer variable. diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShops.cs similarity index 94% rename from CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs rename to CarpentryWorkshop/CarpentryWorkshop/FormShops.cs index 10ac590..cbe1cd6 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShops.cs @@ -5,11 +5,11 @@ using Microsoft.Extensions.Logging; namespace CarpentryWorkshopView { - public partial class ShopsForm : Form + public partial class FormShops : Form { private readonly ILogger _logger; private readonly IShopLogic _logic; - public ShopsForm(ILogger logger, IShopLogic logic) + public FormShops(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; @@ -45,8 +45,8 @@ namespace CarpentryWorkshopView private void AddButton_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(ShopForm)); - if (service is ShopForm form) + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) { if (form.ShowDialog() == DialogResult.OK) { @@ -59,8 +59,8 @@ namespace CarpentryWorkshopView { if (DataGridView.SelectedRows.Count == 1) { - var service = Program.ServiceProvider?.GetService(typeof(ShopForm)); - if (service is ShopForm form) + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + if (service is FormShop form) { var tmp = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); form._id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); diff --git a/CarpentryWorkshop/CarpentryWorkshop/ShopsForm.resx b/CarpentryWorkshop/CarpentryWorkshop/FormShops.resx similarity index 100% rename from CarpentryWorkshop/CarpentryWorkshop/ShopsForm.resx rename to CarpentryWorkshop/CarpentryWorkshop/FormShops.resx diff --git a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.Designer.cs similarity index 91% rename from CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs rename to CarpentryWorkshop/CarpentryWorkshop/FormSupply.Designer.cs index 44383fc..5f858ea 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.Designer.cs @@ -1,6 +1,6 @@ namespace CarpentryWorkshopView { - partial class SupplyForm + partial class FormSupply { /// /// Required designer variable. @@ -42,6 +42,8 @@ ShopComboBox.Name = "ShopComboBox"; ShopComboBox.Size = new Size(331, 23); ShopComboBox.TabIndex = 0; + ShopComboBox.DropDownStyle = ComboBoxStyle.DropDownList; + ShopComboBox.FormattingEnabled = true; // // WoodComboBox // @@ -50,6 +52,8 @@ WoodComboBox.Name = "WoodComboBox"; WoodComboBox.Size = new Size(331, 23); WoodComboBox.TabIndex = 1; + WoodComboBox.DropDownStyle = ComboBoxStyle.DropDownList; + WoodComboBox.FormattingEnabled = true; // // CountTextBox // @@ -78,7 +82,7 @@ CancelButton.UseVisualStyleBackColor = true; CancelButton.Click += CancelButton_Click; // - // SupplyForm + // FormSupply // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; @@ -88,7 +92,7 @@ Controls.Add(CountTextBox); Controls.Add(WoodComboBox); Controls.Add(ShopComboBox); - Name = "SupplyForm"; + Name = "FormSupply"; Text = "SupplyForm"; ResumeLayout(false); PerformLayout(); diff --git a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs similarity index 88% rename from CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs rename to CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs index ca539be..ba71e4b 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs @@ -1,4 +1,5 @@ -using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopContracts.ViewModels; using CarpentryWorkshopDataModels.Models; @@ -14,7 +15,7 @@ using System.Windows.Forms; namespace CarpentryWorkshopView { - public partial class SupplyForm : Form + public partial class FormSupply : Form { private readonly List? _woodList; private readonly List? _shopsList; @@ -71,7 +72,7 @@ namespace CarpentryWorkshopView set { CountTextBox.Text = value.ToString(); } } - public SupplyForm(IWoodLogic woodLogic, IShopLogic shopLogic) + public FormSupply(IWoodLogic woodLogic, IShopLogic shopLogic) { InitializeComponent(); _shopLogic = shopLogic; @@ -119,11 +120,12 @@ namespace CarpentryWorkshopView { int count = Convert.ToInt32(CountTextBox.Text); - bool res = _shopLogic.MakeSupply( - new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) }, - _woodLogic.ReadElement(new() { Id = Convert.ToInt32(WoodComboBox.SelectedValue) }), - count - ); + bool res = _shopLogic.MakeSupply(new SupplyBindingModel + { + ShopId = Convert.ToInt32(ShopComboBox.SelectedValue), + WoodId = Convert.ToInt32(WoodComboBox.SelectedValue), + Count = Convert.ToInt32(CountTextBox.Text) + }); if (!res) { diff --git a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.resx similarity index 93% rename from CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx rename to CarpentryWorkshop/CarpentryWorkshop/FormSupply.resx index 1af7de1..af32865 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/SupplyForm.resx +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.resx @@ -1,17 +1,17 @@  - diff --git a/CarpentryWorkshop/CarpentryWorkshop/Program.cs b/CarpentryWorkshop/CarpentryWorkshop/Program.cs index 021c482..56b3301 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Program.cs @@ -53,9 +53,9 @@ namespace CarpentryWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); - services.AddTransient(); - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs index 4d50c2f..dbd73e0 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -12,18 +12,19 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IShopStorage _shopStorage; + private readonly IWoodStorage _woodStorage; - public ShopLogic(ILogger logger, IShopStorage shopStorage) + public ShopLogic(ILogger logger, IShopStorage shopStorage, IWoodStorage woodStorage) { _logger = logger; _shopStorage = shopStorage; + _woodStorage = woodStorage; } public List ReadList(ShopSearchModel model) { - _logger.LogInformation("ReadList. ShopName:{Name}. Id:{ Id}", model?.Name, model?.Id); - var list = model == null ? _shopStorage.GetFullList() : - _shopStorage.GetFilteredList(model); + _logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id}", model?.ShopName, model?.Id); + var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); @@ -31,14 +32,45 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; - } - public bool MakeSupply(ShopSearchModel model, IWoodModel wood, int count) + public bool MakeSupply(SupplyBindingModel model) { if (model == null) - return false; - return _shopStorage.SupplyWood(model, wood, count); + { + throw new ArgumentNullException(nameof(model)); + } + if (model.Count <= 0) + { + throw new ArgumentException("Количество изделий должно быть больше 0"); + } + var shop = _shopStorage.GetElement(new ShopSearchModel + { + Id = model.ShopId + }); + if (shop == null) + { + throw new ArgumentException("Магазина не существует"); + } + if (shop.ShopWoods.ContainsKey(model.WoodId)) + { + var oldValue = shop.ShopWoods[model.WoodId]; + oldValue.Item2 += model.Count; + shop.ShopWoods[model.WoodId] = oldValue; + } + else + { + var wood = _woodStorage.GetElement(new WoodSearchModel + { + Id = model.WoodId + }); + if (wood == null) + { + throw new ArgumentException($"Поставка: Товар с id:{model.WoodId} не найденн"); + } + shop.ShopWoods.Add(model.WoodId, (wood, model.Count)); + } + return true; } public ShopViewModel ReadElement(ShopSearchModel model) @@ -47,7 +79,7 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { throw new ArgumentNullException(nameof(model)); } - _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id); + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id); var element = _shopStorage.GetElement(model); if (element == null) { @@ -101,23 +133,18 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { return; } - if (string.IsNullOrEmpty(model.ShopName)) - { - throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); - } if (string.IsNullOrEmpty(model.Address)) { - throw new ArgumentNullException("Нет адресса магазина", nameof(model.ShopName)); + throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(model.Address)); } - if (model.DateOpen == null) + if (string.IsNullOrEmpty(model.ShopName)) { - throw new ArgumentNullException("Нет даты открытия магазина", - nameof(model.ShopName)); + throw new ArgumentException("Название магазина должно быть заполнено", nameof(model.ShopName)); } - _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}. DateOpen:{DateOpen}. Id: { Id}", model.ShopName, model.Address, model.DateOpen, model.Id); + _logger.LogInformation("Shop. ShopName:{ShopName}.Adres:{Adres}.OpeningDate:{OpeningDate}.Id:{ Id}", model.ShopName, model.Address, model.DateOpen, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { - Name = model.ShopName + ShopName = model.ShopName }); if (element != null && element.Id != model.Id) { diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/SupplyBindingModel .cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/SupplyBindingModel .cs new file mode 100644 index 0000000..d824da7 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/SupplyBindingModel .cs @@ -0,0 +1,11 @@ +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.BindingModels +{ + public class SupplyBindingModel : ISupplyModel + { + public int ShopId { get; set; } + public int WoodId { get; set; } + public int Count { get; set; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs index df5d729..5840f0f 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -12,6 +12,6 @@ namespace CarpentryWorkshopContracts.BusinessLogicsContracts bool Create(ShopBindingModel model); bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); - bool MakeSupply(ShopSearchModel model, IWoodModel wood, int count); + bool MakeSupply(SupplyBindingModel model); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs index 1398bb7..71d0ee2 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/ShopSearchModel.cs @@ -3,6 +3,6 @@ public class ShopSearchModel { public int? Id { get; set; } - public string? Name { get; set; } + public string? ShopName { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs index f5a59d6..465cef8 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -18,6 +18,5 @@ namespace CarpentryWorkshopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); - bool SupplyWood(ShopSearchModel model, IWoodModel wood, int Count); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/ISupplyModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/ISupplyModel.cs new file mode 100644 index 0000000..8d0e414 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/ISupplyModel.cs @@ -0,0 +1,9 @@ +namespace CarpentryWorkshopDataModels.Models +{ + public interface ISupplyModel + { + int ShopId { get; } + int WoodId { get; } + int Count { get; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs index df27e7a..5f6c4c4 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs @@ -27,13 +27,13 @@ namespace CarpentryWorkshopListImplement.Implements model) { var result = new List(); - if (string.IsNullOrEmpty(model.Name)) + if (string.IsNullOrEmpty(model.ShopName)) { return result; } foreach (var shop in _source.Shops) { - if (shop.ShopName.Contains(model.Name)) + if (shop.ShopName.Contains(model.ShopName)) { result.Add(shop.GetViewModel); } @@ -42,14 +42,14 @@ namespace CarpentryWorkshopListImplement.Implements } public ShopViewModel? GetElement(ShopSearchModel model) { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) { return null; } foreach (var shop in _source.Shops) { - if ((!string.IsNullOrEmpty(model.Name) && - shop.ShopName == model.Name) || + if ((!string.IsNullOrEmpty(model.ShopName) && + shop.ShopName == model.ShopName) || (model.Id.HasValue && shop.Id == model.Id)) { return shop.GetViewModel; @@ -101,7 +101,7 @@ namespace CarpentryWorkshopListImplement.Implements return null; } - public bool SupplyWood(ShopSearchModel model, IWoodModel wood, int count) + /* public bool SupplyWood(ShopSearchModel model, IWoodModel wood, int count) { if (model == null) throw new ArgumentNullException(nameof(model)); @@ -130,6 +130,6 @@ namespace CarpentryWorkshopListImplement.Implements ShopWoods = curModel.ShopWoods, }); return true; - } + } */ } } diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs index 23cae3b..1b9426b 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs @@ -26,8 +26,7 @@ namespace CarpentryWorkshopListImplement.Models Id = model.Id, ShopName = model.ShopName, Address = model.Address, - DateOpen = model.DateOpen, - ShopWoods = new() + DateOpen = model.DateOpen }; } @@ -40,7 +39,6 @@ namespace CarpentryWorkshopListImplement.Models ShopName = model.ShopName; Address = model.Address; DateOpen = model.DateOpen; - ShopWoods = model.ShopWoods; } public ShopViewModel GetViewModel => new() From 987f1174b272168d1976b4b4a13171fda67476da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sat, 2 Mar 2024 15:56:29 +0400 Subject: [PATCH 3/6] =?UTF-8?q?=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/ShopStorage.cs | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs index 5f6c4c4..fc8c47b 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs @@ -100,36 +100,5 @@ namespace CarpentryWorkshopListImplement.Implements } return null; } - - /* public bool SupplyWood(ShopSearchModel model, IWoodModel wood, int count) - { - if (model == null) - throw new ArgumentNullException(nameof(model)); - if (wood == null) - throw new ArgumentNullException(nameof(wood)); - if (count <= 0) - throw new ArgumentNullException("Количество должно быть положительным числом"); - - ShopViewModel curModel = GetElement(model); - if (curModel == null) - throw new ArgumentNullException(nameof(curModel)); - if (curModel.ShopWoods.TryGetValue(wood.Id, out var pair)) - { - curModel.ShopWoods[wood.Id] = (pair.Item1, pair.Item2 + count); - } - else - { - curModel.ShopWoods.Add(wood.Id, (wood, count)); - } - Update(new() - { - Id = curModel.Id, - ShopName = curModel.ShopName, - DateOpen = curModel.DateOpen, - Address = curModel.Address, - ShopWoods = curModel.ShopWoods, - }); - return true; - } */ } } From 9448236d9350900b0d910825e0b9fc32e20eac2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 24 Mar 2024 16:18:45 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/OrderLogic.cs | 20 ++- .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + .../SearchModels/SupplySearchModel.cs | 8 + .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 2 + .../Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 6 + .../Implements/ShopStorage.cs | 148 ++++++++++++++++++ .../Models/Shop.cs | 101 ++++++++++++ 10 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 66a3eee..9ac7bf2 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -12,11 +12,13 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopStorage _shopStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) { _logger = logger; _orderStorage = orderStorage; + _shopStorage = shopStorage; } public List? ReadList(OrderSearchModel? model) @@ -78,6 +80,22 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics public bool DeliveryOrder(OrderBindingModel model) { + var order = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id, + }); + if (order == null) + { + throw new ArgumentNullException(nameof(order)); + } + if (!_shopStorage.RestockingShops(new SupplyBindingModel + { + WoodId = order.WoodId, + Count = order.Count + })) + { + throw new ArgumentException("Недостаточно места"); + } return ChangeStatus(model, OrderStatus.Выдан); } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs index feed4c9..1ca3279 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/ShopBindingModel.cs @@ -9,5 +9,6 @@ namespace CarpentryWorkshopContracts.BindingModels public string Address { get; set; } public DateTime DateOpen { get; set; } public Dictionary ShopWoods { get; set; } = new(); + public int WoodMaxCount { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs index 5840f0f..97fbb5a 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -13,5 +13,6 @@ namespace CarpentryWorkshopContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool MakeSupply(SupplyBindingModel model); + bool Sale(SupplySearchModel model); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs new file mode 100644 index 0000000..27b4eeb --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/SupplySearchModel.cs @@ -0,0 +1,8 @@ +namespace CarpentryWorkshopContracts.SearchModels +{ + public class SupplySearchModel + { + public int? WoodId { get; set; } + public int? Count { get; set; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs index 465cef8..3638058 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -18,5 +18,7 @@ namespace CarpentryWorkshopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sale(SupplySearchModel model); + bool RestockingShops(SupplyBindingModel model); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs index d469d57..ec8eed1 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/ShopViewModel.cs @@ -13,5 +13,7 @@ namespace CarpentryWorkshopContracts.ViewModels [DisplayName("Дата открытия")] public DateTime DateOpen { get; set; } public Dictionary ShopWoods { get; set; } = new(); + [DisplayName("Вместимость")] + public int WoodMaxCount { get; set; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs index 19e6d4c..b6a8824 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IShopModel.cs @@ -6,5 +6,6 @@ string Address { get; } DateTime DateOpen { get; } Dictionary ShopWoods { get; } + public int WoodMaxCount { get; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs index 3d9819c..87c458e 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace CarpentryWorkshopFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string WoodFileName = "Wood.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Woods { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -26,6 +28,8 @@ namespace CarpentryWorkshopFileImplement "Woods", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, + "Shops", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => @@ -34,6 +38,8 @@ namespace CarpentryWorkshopFileImplement Wood.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => + Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..dddfeb2 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,148 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopFileImplement.Models; + +namespace CarpentryWorkshopFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveShops(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveShops(); + return shop.GetViewModel; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop != null) + { + source.Shops.Remove(shop); + source.SaveShops(); + return shop.GetViewModel; + } + return null; + } + + public bool Sale(SupplySearchModel model) + { + if (model == null || !model.WoodId.HasValue || !model.Count.HasValue) + return false; + int remainingSpace = source.Shops.Select(x => x.Woods.ContainsKey(model.WoodId.Value) ? x.Woods[model.WoodId.Value] : 0).Sum(); + if (remainingSpace < model.Count) + { + return false; + } + var shops = source.Shops.Where(x => x.Woods.ContainsKey(model.WoodId.Value)).OrderByDescending(x => x.Woods[model.WoodId.Value]).ToList(); + foreach (var shop in shops) + { + int residue = model.Count.Value - shop.Woods[model.WoodId.Value]; + if (residue > 0) + { + shop.Woods.Remove(model.WoodId.Value); + shop.Woods.Update(); + model.Count = residue; + } + else + { + if (residue == 0) + { + shop.Woods.Remove(model.WoodId.Value); + } + else + { + shop.Woods[model.WoodId.Value] = -residue; + } + shop.Woods.Update(); + source.SaveShops(); + return true; + } + } + source.SaveShops(); + return false; + } + + public bool RestockingShops(SupplyBindingModel model) + { + if (model == null || source.Shops.Select(x => x.WoodMaxCount - x.ShopWoods.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + { + return false; + } + foreach (Shop shop in source.Shops) + { + int free_places = shop.WoodMaxCount - shop.ShopWoods.Select(x => x.Value.Item2).Sum(); + if (free_places <= 0) + continue; + free_places = Math.Min(free_places, model.Count); + model.Count -= free_places; + if (shop.Woods.ContainsKey(model.WoodId)) + { + shop.Woods[model.WoodId] += free_places; + } + else + { + shop.Woods.Add(model.WoodId, free_places); + } + shop.Woods.Update(); + if (model.Count == 0) + { + source.SaveShops(); + return true; + } + } + return false; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs new file mode 100644 index 0000000..cf1c4ae --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs @@ -0,0 +1,101 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace CarpentryWorkshopFileImplement.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 DateOpen { get; private set; } + public Dictionary Woods { get; private set; } = new(); + private Dictionary? _shopWoods = null; + + public Dictionary ShopWoods + { + get + { + if (_shopWoods == null) + { + var source = DataFileSingleton.GetInstance(); + _shopWoods = Woods.ToDictionary(x => x.Key, y => ((source.Woods.FirstOrDefault(z => z.Id == y.Key) as IWoodModel)!, y.Value)); + } + return _shopWoods; + } + } + + public int WoodMaxCount { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + Woods = model.ShopWoods.ToDictionary(x => x.Key, x => x.Value.Item2), + WoodMaxCount = model.WoodMaxCount + }; + } + + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Address = element.Element("Address")!.Value, + DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value), + Woods = element.Element("ShopWoods")!.Elements("ShopWood")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)), + WoodMaxCount = Convert.ToInt32(element.Element("WoodMaxCount")!.Value) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + WoodMaxCount = model.WoodMaxCount; + Woods = model.ShopWoods.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopWoods = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopWoods = ShopWoods, + WoodMaxCount = WoodMaxCount + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpen", DateOpen.ToString()), + new XElement("ShopWoods", Woods.Select( + x => new XElement("ShopWood", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()), + new XElement("WoodMaxCount", WoodMaxCount.ToString()) + ); + } +} From 831820a67718647305aae70f624cf878f70f9493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Wed, 24 Apr 2024 09:15:18 +0400 Subject: [PATCH 5/6] =?UTF-8?q?=D0=BA=D0=B0=D0=BA=D0=BE=D0=B9-=D1=82=D0=BE?= =?UTF-8?q?=20=D0=BA=D0=BE=D1=81=D1=8F=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshop/FormMain.Designer.cs | 10 +- .../CarpentryWorkshop/FormMain.cs | 8 ++ .../CarpentryWorkshop/FormSell.Designer.cs | 117 +++++++++++++++++ .../CarpentryWorkshop/FormSell.cs | 115 +++++++++++++++++ .../CarpentryWorkshop/FormSell.resx | 120 ++++++++++++++++++ .../CarpentryWorkshop/FormShop.Designer.cs | 50 ++++++-- .../CarpentryWorkshop/FormShop.cs | 4 +- .../CarpentryWorkshop/FormSupply.cs | 17 +-- .../CarpentryWorkshop/Program.cs | 1 + .../BusinessLogics/OrderLogic.cs | 100 ++++++++++++--- .../BusinessLogics/ShopLogic.cs | 91 +++++++------ .../BusinessLogicsContracts/IShopLogic.cs | 4 +- .../StoragesContracts/IShopStorage.cs | 3 +- .../Implements/OrderStorage.cs | 12 +- .../Implements/ShopStorage.cs | 89 +++++-------- .../Models/Shop.cs | 9 +- .../Implements/ShopStorage.cs | 45 +++++++ .../Models/Shop.cs | 5 +- 18 files changed, 651 insertions(+), 149 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshop/FormSell.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/FormSell.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/FormSell.resx diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs index 453a582..758713b 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs @@ -32,6 +32,7 @@ справочникиToolStripMenuItem = new ToolStripMenuItem(); КомпонентыToolStripMenuItem = new ToolStripMenuItem(); ИзделияToolStripMenuItem = new ToolStripMenuItem(); + продажиToolStripMenuItem = new ToolStripMenuItem(); магазиныToolStripMenuItem = new ToolStripMenuItem(); поставкиToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); @@ -56,7 +57,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыToolStripMenuItem, ИзделияToolStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { КомпонентыToolStripMenuItem, ИзделияToolStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem, продажиToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -89,6 +90,12 @@ поставкиToolStripMenuItem.Text = "Поставки"; поставкиToolStripMenuItem.Click += поставкиToolStripMenuItem_Click; // + // продажиToolStripMenuItem + // + продажиToolStripMenuItem.Name = "продажиToolStripMenuItem"; + продажиToolStripMenuItem.Size = new Size(180, 22); + продажиToolStripMenuItem.Text = "Продажи"; + // // dataGridView // dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; @@ -193,5 +200,6 @@ private ToolStripMenuItem ИзделияToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem поставкиToolStripMenuItem; + private ToolStripMenuItem продажиToolStripMenuItem; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs index 3a3039d..9976417 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs @@ -169,5 +169,13 @@ namespace CarpentryWorkshopView form.ShowDialog(); } } + private void продажиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSell)); + if (service is FormSell form) + { + form.ShowDialog(); + } + } } } diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormSell.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormSell.Designer.cs new file mode 100644 index 0000000..945e17b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSell.Designer.cs @@ -0,0 +1,117 @@ +namespace CarpentryWorkshopView +{ + partial class FormSell + { + /// + /// 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() + { + WoodComboBox = new ComboBox(); + WoodLabel = new Label(); + CountLabel = new Label(); + CountTextBox = new TextBox(); + SaveButton = new Button(); + CancelButton = new Button(); + SuspendLayout(); + // + // WoodComboBox + // + WoodComboBox.FormattingEnabled = true; + WoodComboBox.Location = new Point(99, 12); + WoodComboBox.Name = "WoodComboBox"; + WoodComboBox.Size = new Size(121, 23); + WoodComboBox.TabIndex = 0; + // + // WoodLabel + // + WoodLabel.AutoSize = true; + WoodLabel.Location = new Point(12, 20); + WoodLabel.Name = "WoodLabel"; + WoodLabel.Size = new Size(81, 15); + WoodLabel.TabIndex = 1; + WoodLabel.Text = "Изделие"; + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(21, 49); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(72, 15); + CountLabel.TabIndex = 2; + CountLabel.Text = "Количество"; + // + // CountTextBox + // + CountTextBox.Location = new Point(99, 41); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(121, 23); + CountTextBox.TabIndex = 3; + // + // SaveButton + // + SaveButton.Location = new Point(64, 73); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(75, 23); + SaveButton.TabIndex = 4; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // CancelButton + // + CancelButton.Location = new Point(145, 73); + CancelButton.Name = "CancelButton"; + CancelButton.Size = new Size(75, 23); + CancelButton.TabIndex = 5; + CancelButton.Text = "Отмена"; + CancelButton.UseVisualStyleBackColor = true; + // + // FormSell + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(224, 108); + Controls.Add(CancelButton); + Controls.Add(SaveButton); + Controls.Add(CountTextBox); + Controls.Add(CountLabel); + Controls.Add(WoodLabel); + Controls.Add(WoodComboBox); + Name = "FormSell"; + Text = "Форма продажи"; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private ComboBox WoodComboBox; + private Label WoodLabel; + private Label CountLabel; + private TextBox CountTextBox; + private Button SaveButton; + private Button CancelButton; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormSell.cs b/CarpentryWorkshop/CarpentryWorkshop/FormSell.cs new file mode 100644 index 0000000..89c3d3d --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSell.cs @@ -0,0 +1,115 @@ +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.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 CarpentryWorkshopView +{ + public partial class FormSell : Form + { + private readonly List? _woodList; + IShopLogic _shopLogic; + IWoodLogic _woodLogic; + public FormSell(IWoodLogic woodLogic, IShopLogic shopLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _woodLogic = woodLogic; + _woodList = woodLogic.ReadList(null); + if (_woodList != null) + { + WoodComboBox.DisplayMember = "WoodName"; + WoodComboBox.ValueMember = "Id"; + WoodComboBox.DataSource = _woodList; + WoodComboBox.SelectedItem = null; + } + } + public int WoodId + { + get + { + return Convert.ToInt32(WoodComboBox.SelectedValue); + } + set + { + WoodComboBox.SelectedValue = value; + } + } + + public IWoodModel? WoodModel + { + get + { + if (_woodList == null) + { + return null; + } + foreach (var elem in _woodList) + { + if (elem.Id == WoodId) + { + return elem; + } + } + return null; + } + } + + + public int Count + { + get { return Convert.ToInt32(CountTextBox.Text); } + set + { CountTextBox.Text = value.ToString(); } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(CountTextBox.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (WoodComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + int count = Convert.ToInt32(CountTextBox.Text); + + bool res = _shopLogic.MakeSell( + _woodLogic.ReadElement(new() { Id = Convert.ToInt32(WoodComboBox.SelectedValue) }), + count + ); + + if (!res) + { + throw new Exception("Ошибка при продаже."); + } + + MessageBox.Show("Продажа прошла успешно"); + DialogResult = DialogResult.OK; + Close(); + + } + catch (Exception err) + { + MessageBox.Show("Ошибка продажи"); + return; + } + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormSell.resx b/CarpentryWorkshop/CarpentryWorkshop/FormSell.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSell.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs index a11fedc..cc496e7 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs @@ -41,7 +41,10 @@ Название = new DataGridViewTextBoxColumn(); Цена = new DataGridViewTextBoxColumn(); Количество = new DataGridViewTextBoxColumn(); + CapacityUpDown = new NumericUpDown(); + CapacityLabel = new Label(); ((System.ComponentModel.ISupportInitialize)DataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)CapacityUpDown).BeginInit(); SuspendLayout(); // // DateTimePicker @@ -94,7 +97,7 @@ // // SaveButton // - SaveButton.Location = new Point(247, 255); + SaveButton.Location = new Point(247, 284); SaveButton.Name = "SaveButton"; SaveButton.Size = new Size(75, 23); SaveButton.TabIndex = 6; @@ -104,7 +107,7 @@ // // CancelButton // - CancelButton.Location = new Point(328, 255); + CancelButton.Location = new Point(328, 284); CancelButton.Name = "CancelButton"; CancelButton.Size = new Size(75, 23); CancelButton.TabIndex = 7; @@ -116,20 +119,24 @@ // DataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; DataGridView.Columns.AddRange(new DataGridViewColumn[] { Column1, Название, Цена, Количество }); - DataGridView.Location = new Point(12, 99); + DataGridView.Location = new Point(12, 128); DataGridView.Name = "DataGridView"; + DataGridView.RowHeadersWidth = 51; DataGridView.Size = new Size(391, 150); DataGridView.TabIndex = 8; // // Column1 // Column1.HeaderText = "Column1"; + Column1.MinimumWidth = 6; Column1.Name = "Column1"; Column1.Visible = false; + Column1.Width = 125; // // Название // Название.HeaderText = "Название"; + Название.MinimumWidth = 6; Название.Name = "Название"; Название.ReadOnly = true; Название.Width = 150; @@ -137,20 +144,43 @@ // Цена // Цена.HeaderText = "Цена"; + Цена.MinimumWidth = 6; Цена.Name = "Цена"; Цена.ReadOnly = true; + Цена.Width = 125; // // Количество // Количество.HeaderText = "Количество"; + Количество.MinimumWidth = 6; Количество.Name = "Количество"; Количество.ReadOnly = true; + Количество.Width = 125; // - // ShopForm + // CapacityUpDown + // + CapacityUpDown.Location = new Point(98, 99); + CapacityUpDown.Maximum = new decimal(new int[] { 10000, 0, 0, 0 }); + CapacityUpDown.Name = "CapacityUpDown"; + CapacityUpDown.Size = new Size(120, 23); + CapacityUpDown.TabIndex = 9; + // + // CapacityLabel + // + CapacityLabel.AutoSize = true; + CapacityLabel.Location = new Point(12, 107); + CapacityLabel.Name = "CapacityLabel"; + CapacityLabel.Size = new Size(80, 15); + CapacityLabel.TabIndex = 10; + CapacityLabel.Text = "Вместимость"; + // + // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(411, 285); + ClientSize = new Size(411, 315); + Controls.Add(CapacityLabel); + Controls.Add(CapacityUpDown); Controls.Add(DataGridView); Controls.Add(CancelButton); Controls.Add(SaveButton); @@ -160,14 +190,16 @@ Controls.Add(AddressTextBox); Controls.Add(NameTextBox); Controls.Add(DateTimePicker); - Name = "ShopForm"; - Text = "ShopForm"; - Load += ShopForm_Load; + Name = "FormShop"; + Text = "Форма магазина"; + Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)CapacityUpDown).EndInit(); ResumeLayout(false); PerformLayout(); } + #endregion private DateTimePicker DateTimePicker; @@ -183,5 +215,7 @@ private DataGridViewTextBoxColumn Название; private DataGridViewTextBoxColumn Цена; private DataGridViewTextBoxColumn Количество; + private NumericUpDown CapacityUpDown; + private Label CapacityLabel; } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs index 0076e41..2ca553e 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs @@ -19,7 +19,7 @@ namespace CarpentryWorkshopView _logic = logic; } - private void ShopForm_Load(object sender, EventArgs e) + private void FormShop_Load(object sender, EventArgs e) { if (_id.HasValue) { @@ -32,6 +32,7 @@ namespace CarpentryWorkshopView NameTextBox.Text = shop.ShopName; AddressTextBox.Text = shop.Address; DateTimePicker.Text = shop.DateOpen.ToString(); + CapacityUpDown.Value = shop.WoodMaxCount; _woods = shop.ShopWoods; } LoadData(); @@ -93,6 +94,7 @@ namespace CarpentryWorkshopView ShopName = NameTextBox.Text, Address = AddressTextBox.Text, DateOpen = DateTimePicker.Value.Date, + WoodMaxCount = Convert.ToInt32(CapacityUpDown.Value), ShopWoods = _woods }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs index ba71e4b..8297b97 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormSupply.cs @@ -25,8 +25,7 @@ namespace CarpentryWorkshopView { get { - return - Convert.ToInt32(ShopComboBox.SelectedValue); + return Convert.ToInt32(ShopComboBox.SelectedValue); } set { @@ -37,8 +36,7 @@ namespace CarpentryWorkshopView { get { - return - Convert.ToInt32(WoodComboBox.SelectedValue); + return Convert.ToInt32(WoodComboBox.SelectedValue); } set { @@ -120,12 +118,11 @@ namespace CarpentryWorkshopView { int count = Convert.ToInt32(CountTextBox.Text); - bool res = _shopLogic.MakeSupply(new SupplyBindingModel - { - ShopId = Convert.ToInt32(ShopComboBox.SelectedValue), - WoodId = Convert.ToInt32(WoodComboBox.SelectedValue), - Count = Convert.ToInt32(CountTextBox.Text) - }); + bool res = _shopLogic.MakeSupply( + new ShopSearchModel() { Id = Convert.ToInt32(ShopComboBox.SelectedValue) }, + _woodLogic.ReadElement(new() { Id = Convert.ToInt32(WoodComboBox.SelectedValue) }), + count + ); if (!res) { diff --git a/CarpentryWorkshop/CarpentryWorkshop/Program.cs b/CarpentryWorkshop/CarpentryWorkshop/Program.cs index 88fe7a4..9b7966a 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Program.cs @@ -56,6 +56,7 @@ namespace CarpentryWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 9ac7bf2..f9da371 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -4,6 +4,7 @@ using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopContracts.StoragesContracts; using CarpentryWorkshopContracts.ViewModels; using CarpentryWorkshopDataModels.Enums; +using CarpentryWorkshopDataModels.Models; using Microsoft.Extensions.Logging; namespace CarpentryWorkshopBusinessLogic.BusinessLogics @@ -13,12 +14,17 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; private readonly IShopStorage _shopStorage; + private readonly IShopLogic _shopLogic; + private readonly IWoodStorage _woodStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopStorage shopStorage) + public OrderLogic(IOrderStorage orderStorage, IShopStorage shopStorage, + IShopLogic shopLogic, IWoodStorage woodStorage, ILogger logger) { _logger = logger; _orderStorage = orderStorage; _shopStorage = shopStorage; + _shopLogic = shopLogic; + _woodStorage = woodStorage; } public List? ReadList(OrderSearchModel? model) @@ -62,6 +68,20 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics _logger.LogWarning("Status change operation failed"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); } + if (element.Status == OrderStatus.Готов) + { + var wood = _woodStorage.GetElement(new WoodSearchModel() { Id = model.WoodId }); + if (wood == null) + { + _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Document not found."); + return false; + } + if (CheckSupply(wood, model.Count) == false) + { + _logger.LogWarning("Status update to " + status.ToString() + " operation failed. Shop supply error."); + return false; + } + } model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; _orderStorage.Update(model); @@ -80,27 +100,69 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics public bool DeliveryOrder(OrderBindingModel model) { - var order = _orderStorage.GetElement(new OrderSearchModel - { - Id = model.Id, - }); - if (order == null) - { - throw new ArgumentNullException(nameof(order)); - } - if (!_shopStorage.RestockingShops(new SupplyBindingModel - { - WoodId = order.WoodId, - Count = order.Count - })) - { - throw new ArgumentException("Недостаточно места"); - } return ChangeStatus(model, OrderStatus.Выдан); } - private void CheckModel(OrderBindingModel model, bool withParams = -true) + public bool CheckSupply(IWoodModel wood, int count) + { + if (count <= 0) + { + _logger.LogWarning("Check then supply operation error. Wood count < 0."); + return false; + } + + int sumCapacity = 0; + int sumCount = 0; + sumCapacity = _shopStorage.GetFullList().Select(x => x.WoodMaxCount).Sum(); + sumCount = _shopStorage.GetFullList().Select(x => x.ShopWoods.Select(y => y.Value.Item2).Sum()).Sum(); + int freeSpace = sumCapacity - sumCount; + + if (freeSpace - count < 0) + { + _logger.LogWarning("Check then supply operation error. There's no place for new Wood in shops."); + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + freeSpace = shop.WoodMaxCount; + foreach (var doc in shop.ShopWoods) + { + freeSpace -= doc.Value.Item2; + } + if (freeSpace == 0) + { + continue; + } + if (freeSpace - count >= 0) + { + if (_shopLogic.MakeSupply(new() { Id = shop.Id }, wood, count)) + count = 0; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (freeSpace - count < 0) + { + if (_shopLogic.MakeSupply(new() { Id = shop.Id }, wood, freeSpace)) + count -= freeSpace; + else + { + _logger.LogWarning("Supply error"); + return false; + } + } + if (count <= 0) + { + return true; + } + } + return false; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs index dbd73e0..1499e2c 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -12,18 +12,16 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IShopStorage _shopStorage; - private readonly IWoodStorage _woodStorage; - public ShopLogic(ILogger logger, IShopStorage shopStorage, IWoodStorage woodStorage) + public ShopLogic(ILogger logger, IShopStorage shopStorage) { _logger = logger; _shopStorage = shopStorage; - _woodStorage = woodStorage; } public List ReadList(ShopSearchModel model) { - _logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id}", model?.ShopName, model?.Id); + _logger.LogInformation("ReadList. ShopName:{Name}. Id:{ Id}", model?.ShopName, model?.Id); var list = model == null ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); if (list == null) { @@ -32,47 +30,58 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; + } - public bool MakeSupply(SupplyBindingModel model) + public bool MakeSupply(ShopSearchModel model, IWoodModel wood, int count) { + if (model == null) - { throw new ArgumentNullException(nameof(model)); - } - if (model.Count <= 0) + if (wood == null) + throw new ArgumentNullException(nameof(wood)); + if (count <= 0) + throw new ArgumentNullException("Количество должно быть положительным числом"); + + ShopViewModel curModel = _shopStorage.GetElement(model); + if (curModel == null) + throw new ArgumentNullException(nameof(curModel)); + var countItems = curModel.ShopWoods.Select(x => x.Value.Item2).Sum(); + if (curModel.WoodMaxCount - countItems >= count) { - throw new ArgumentException("Количество изделий должно быть больше 0"); - } - var shop = _shopStorage.GetElement(new ShopSearchModel - { - Id = model.ShopId - }); - if (shop == null) - { - throw new ArgumentException("Магазина не существует"); - } - if (shop.ShopWoods.ContainsKey(model.WoodId)) - { - var oldValue = shop.ShopWoods[model.WoodId]; - oldValue.Item2 += model.Count; - shop.ShopWoods[model.WoodId] = oldValue; + if (curModel.ShopWoods.TryGetValue(wood.Id, out var sameDocument)) + { + curModel.ShopWoods[wood.Id] = (wood, sameDocument.Item2 + count); + _logger.LogInformation("Same wood found by supply. Added {0} of {1} in {2} shop", count, wood.WoodName, curModel.ShopName); + } + else + { + curModel.ShopWoods[wood.Id] = (wood, count); + _logger.LogInformation("New wood added by supply. Added {0} of {1} in {2} shop", count, wood.WoodName, curModel.ShopName); + } + _shopStorage.Update(new() + { + Id = curModel.Id, + ShopName = curModel.ShopName, + Address = curModel.Address, + DateOpen = curModel.DateOpen, + ShopWoods = curModel.ShopWoods, + WoodMaxCount = curModel.WoodMaxCount + }); } else { - var wood = _woodStorage.GetElement(new WoodSearchModel - { - Id = model.WoodId - }); - if (wood == null) - { - throw new ArgumentException($"Поставка: Товар с id:{model.WoodId} не найденн"); - } - shop.ShopWoods.Add(model.WoodId, (wood, model.Count)); + _logger.LogWarning("Required shop is overflowed"); + return false; } return true; } + public bool MakeSell(IWoodModel wood, int count) + { + return _shopStorage.SellWoods(wood, count); + } + public ShopViewModel ReadElement(ShopSearchModel model) { if (model == null) @@ -133,15 +142,20 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { return; } - if (string.IsNullOrEmpty(model.Address)) - { - throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(model.Address)); - } if (string.IsNullOrEmpty(model.ShopName)) { - throw new ArgumentException("Название магазина должно быть заполнено", nameof(model.ShopName)); + throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); } - _logger.LogInformation("Shop. ShopName:{ShopName}.Adres:{Adres}.OpeningDate:{OpeningDate}.Id:{ Id}", model.ShopName, model.Address, model.DateOpen, model.Id); + if (string.IsNullOrEmpty(model.Address)) + { + throw new ArgumentNullException("Нет адресса магазина", nameof(model.ShopName)); + } + if (model.DateOpen == null) + { + throw new ArgumentNullException("Нет даты открытия магазина", nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Address:{Address}. DateOpen:{DateOpen}. Id: { Id}", + model.ShopName, model.Address, model.DateOpen, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { ShopName = model.ShopName @@ -151,5 +165,6 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics throw new InvalidOperationException("Магазин с таким названием уже есть"); } } + } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs index 97fbb5a..f223e12 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IShopLogic.cs @@ -12,7 +12,7 @@ namespace CarpentryWorkshopContracts.BusinessLogicsContracts bool Create(ShopBindingModel model); bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); - bool MakeSupply(SupplyBindingModel model); - bool Sale(SupplySearchModel model); + bool MakeSupply(ShopSearchModel model, IWoodModel wood, int count); + bool MakeSell(IWoodModel wood, int count); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs index 3638058..2ff6ee3 100644 --- a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IShopStorage.cs @@ -18,7 +18,6 @@ namespace CarpentryWorkshopContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); - bool Sale(SupplySearchModel model); - bool RestockingShops(SupplyBindingModel model); + public bool SellWoods(IWoodModel model, int count); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs index 2befeac..533e58b 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/OrderStorage.cs @@ -78,14 +78,10 @@ namespace CarpentryWorkshopFileImplement.Implements { if (model == null) return null; - foreach (var wood in source.Woods) - { - if (wood.Id == model.WoodId) - { - model.WoodName = wood.WoodName; - break; - } - } + var wood = source.Woods.FirstOrDefault(x => (x.Id == model.WoodId)); + if (wood == null) + return model; + model.WoodName = wood.WoodName; return model; } } diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs index dddfeb2..6733450 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopContracts.StoragesContracts; using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; using CarpentryWorkshopFileImplement.Models; namespace CarpentryWorkshopFileImplement.Implements @@ -76,73 +77,51 @@ namespace CarpentryWorkshopFileImplement.Implements return null; } - public bool Sale(SupplySearchModel model) + public bool CheckAvailability(int woodId, int count) { - if (model == null || !model.WoodId.HasValue || !model.Count.HasValue) - return false; - int remainingSpace = source.Shops.Select(x => x.Woods.ContainsKey(model.WoodId.Value) ? x.Woods[model.WoodId.Value] : 0).Sum(); - if (remainingSpace < model.Count) - { - return false; - } - var shops = source.Shops.Where(x => x.Woods.ContainsKey(model.WoodId.Value)).OrderByDescending(x => x.Woods[model.WoodId.Value]).ToList(); - foreach (var shop in shops) - { - int residue = model.Count.Value - shop.Woods[model.WoodId.Value]; - if (residue > 0) - { - shop.Woods.Remove(model.WoodId.Value); - shop.Woods.Update(); - model.Count = residue; - } - else - { - if (residue == 0) - { - shop.Woods.Remove(model.WoodId.Value); - } - else - { - shop.Woods[model.WoodId.Value] = -residue; - } - shop.Woods.Update(); - source.SaveShops(); - return true; - } - } - source.SaveShops(); - return false; + int minus = source.Shops.Select(x => x.ShopWoods.Select(y => (y.Value.Item1.Id == woodId ? y.Value.Item2 : 0)).Sum()).Sum(); + count -= minus; + return count <= 0; } - public bool RestockingShops(SupplyBindingModel model) + public bool SellWoods(IWoodModel model, int count) { - if (model == null || source.Shops.Select(x => x.WoodMaxCount - x.ShopWoods.Select(y => y.Value.Item2).Sum()).Sum() < model.Count) + var wood = source.Woods.FirstOrDefault(x => x.Id == model.Id); + + if (wood == null || !CheckAvailability(wood.Id, count)) { return false; } - foreach (Shop shop in source.Shops) + + for (int i = 0; i < source.Shops.Count; i++) { - int free_places = shop.WoodMaxCount - shop.ShopWoods.Select(x => x.Value.Item2).Sum(); - if (free_places <= 0) - continue; - free_places = Math.Min(free_places, model.Count); - model.Count -= free_places; - if (shop.Woods.ContainsKey(model.WoodId)) + var shop = source.Shops[i]; + var woods = shop.ShopWoods; + + foreach (var wod in woods.Where(x => x.Value.Item1.Id == wood.Id)) { - shop.Woods[model.WoodId] += free_places; + var min = Math.Min(wod.Value.Item2, count); + woods[wod.Value.Item1.Id] = (wod.Value.Item1, wod.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } } - else + + shop.Update(new ShopBindingModel { - shop.Woods.Add(model.WoodId, free_places); - } - shop.Woods.Update(); - if (model.Count == 0) - { - source.SaveShops(); - return true; - } + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpen = shop.DateOpen, + WoodMaxCount = shop.WoodMaxCount, + ShopWoods = woods + }); } - return false; + source.SaveShops(); + return true; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs index cf1c4ae..f396abb 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs @@ -93,9 +93,10 @@ namespace CarpentryWorkshopFileImplement.Models new XElement("ShopName", ShopName), new XElement("Address", Address), new XElement("DateOpen", DateOpen.ToString()), - new XElement("ShopWoods", Woods.Select( - x => new XElement("ShopWood", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()), - new XElement("WoodMaxCount", WoodMaxCount.ToString()) - ); + new XElement("WoodMaxCount", WoodMaxCount), + new XElement("ShopWoods", Woods + .Select(x => new XElement("ShopWood", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } } diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs index fc8c47b..586314c 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/ShopStorage.cs @@ -100,5 +100,50 @@ namespace CarpentryWorkshopListImplement.Implements } return null; } + public bool CheckAvailability(int woodId, int count) + { + int minus = _source.Shops.Select(x => x.ShopWoods.Select(y => (y.Value.Item1.Id == woodId ? y.Value.Item2 : 0)).Sum()).Sum(); + count -= minus; + return count <= 0; + } + + public bool SellWoods(IWoodModel model, int count) + { + var wood = _source.Woods.FirstOrDefault(x => x.Id == model.Id); + + if (wood == null || !CheckAvailability(wood.Id, count)) + { + return false; + } + + for (int i = 0; i < _source.Shops.Count; i++) + { + var shop = _source.Shops[i]; + var woods = shop.ShopWoods; + + foreach (var wod in woods.Where(x => x.Value.Item1.Id == wood.Id)) + { + var min = Math.Min(wod.Value.Item2, count); + woods[wod.Value.Item1.Id] = (wod.Value.Item1, wod.Value.Item2 - min); + count -= min; + + if (count <= 0) + { + break; + } + } + + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpen = shop.DateOpen, + WoodMaxCount = shop.WoodMaxCount, + ShopWoods = woods + }); + } + return true; + } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs index 1b9426b..c14e7b6 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/Shop.cs @@ -15,6 +15,7 @@ namespace CarpentryWorkshopListImplement.Models public string ShopName { get; private set; } public string Address { get; private set; } public DateTime DateOpen { get; private set; } + public int WoodMaxCount { get; private set; } public Dictionary ShopWoods { get; private set; } = new(); public static Shop? Create(ShopBindingModel model) @@ -26,7 +27,8 @@ namespace CarpentryWorkshopListImplement.Models Id = model.Id, ShopName = model.ShopName, Address = model.Address, - DateOpen = model.DateOpen + DateOpen = model.DateOpen, + ShopWoods = new() }; } @@ -39,6 +41,7 @@ namespace CarpentryWorkshopListImplement.Models ShopName = model.ShopName; Address = model.Address; DateOpen = model.DateOpen; + ShopWoods = model.ShopWoods; } public ShopViewModel GetViewModel => new() From df6465ba4f316e5056430ff5aeb598a2d3c6b600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=93=D0=B0=D0=BB=D0=B8=D0=BD=D0=B0=20=D0=A4=D0=B5=D0=B4?= =?UTF-8?q?=D0=BE=D1=80=D0=B5=D0=BD=D0=BA=D0=BE?= Date: Sun, 28 Apr 2024 00:54:51 +0400 Subject: [PATCH 6/6] =?UTF-8?q?=D1=81=D0=BB=D0=B5=D0=BF=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B7=D0=B0=D1=85=D0=BE=D0=B4=D0=B8=D1=82=20=D0=B2=20=D0=B1?= =?UTF-8?q?=D0=B0=D1=80=20=D0=B8=20=D0=B3=D0=BE=D0=B2=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=82:=20=20=D0=B2=D1=81=D0=B5=D0=BC=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=B2=D0=B5=D1=82=20=D0=BA=D0=BE=D0=B3=D0=BE=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B2=D0=B8=D0=B4=D0=B5=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CarpentryWorkshop/FormMain.Designer.cs | 1 + .../CarpentryWorkshop/FormMain.cs | 4 +- .../CarpentryWorkshop/FormShop.Designer.cs | 4 +- .../CarpentryWorkshop/FormShop.cs | 83 +++++++----- .../CarpentryWorkshop/FormShops.Designer.cs | 14 +- .../CarpentryWorkshop/FormShops.cs | 48 ++++--- .../FormWoodSale.Designer.cs | 127 ++++++++++++++++++ .../CarpentryWorkshop/FormWoodSale.cs | 96 +++++++++++++ .../CarpentryWorkshop/FormWoodSale.resx | 120 +++++++++++++++++ .../CarpentryWorkshop/Program.cs | 2 +- .../Models/Shop.cs | 29 ++-- 11 files changed, 444 insertions(+), 84 deletions(-) create mode 100644 CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.Designer.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.cs create mode 100644 CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.resx diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs index 758713b..e50250d 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.Designer.cs @@ -95,6 +95,7 @@ продажиToolStripMenuItem.Name = "продажиToolStripMenuItem"; продажиToolStripMenuItem.Size = new Size(180, 22); продажиToolStripMenuItem.Text = "Продажи"; + продажиToolStripMenuItem.Click += продажиToolStripMenuItem_Click; // // dataGridView // diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs index 9976417..e7ad7c8 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormMain.cs @@ -171,8 +171,8 @@ namespace CarpentryWorkshopView } private void продажиToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormSell)); - if (service is FormSell form) + var service = Program.ServiceProvider?.GetService(typeof(FormWoodSale)); + if (service is FormWoodSale form) { form.ShowDialog(); } diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs index cc496e7..5a8b4ca 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShop.Designer.cs @@ -103,7 +103,7 @@ SaveButton.TabIndex = 6; SaveButton.Text = "Сохранить"; SaveButton.UseVisualStyleBackColor = true; - SaveButton.Click += SaveButton_Click; + SaveButton.Click += ButtonSave_Click; // // CancelButton // @@ -113,7 +113,7 @@ CancelButton.TabIndex = 7; CancelButton.Text = "Отмена"; CancelButton.UseVisualStyleBackColor = true; - CancelButton.Click += CancelButton_Click; + CancelButton.Click += ButtonCancel_Click; // // DataGridView // diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs index 2ca553e..0de17b6 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShop.cs @@ -3,77 +3,78 @@ using CarpentryWorkshopContracts.BusinessLogicsContracts; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopDataModels.Models; using Microsoft.Extensions.Logging; +using System.Windows.Forms; namespace CarpentryWorkshopView { public partial class FormShop : Form { private readonly ILogger _logger; + private readonly IShopLogic _logic; - public int? _id; - private Dictionary _woods; + + private int? _id; + + private Dictionary _shopWoods; + + public int Id { set { _id = value; } } + public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; + _shopWoods = new Dictionary(); } private void FormShop_Load(object sender, EventArgs e) { if (_id.HasValue) { - _logger.LogInformation("Загрузка магазина"); + _logger.LogInformation("Shop loading"); try { - var shop = _logic.ReadElement(new ShopSearchModel { Id = _id }); - if (shop != null) + var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value }); + if (view != null) { - NameTextBox.Text = shop.ShopName; - AddressTextBox.Text = shop.Address; - DateTimePicker.Text = shop.DateOpen.ToString(); - CapacityUpDown.Value = shop.WoodMaxCount; - _woods = shop.ShopWoods; + NameTextBox.Text = view.ShopName; + AddressTextBox.Text = view.Address; + DateTimePicker.Value = view.DateOpen; + CapacityUpDown.Text = view.WoodMaxCount.ToString(); + _shopWoods = view.ShopWoods ?? new Dictionary(); + LoadData(); } - LoadData(); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки магазина"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + _logger.LogError(ex, "Shop loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void LoadData() { - _logger.LogInformation("Загрузка товаров магазина"); + _logger.LogInformation("Shop ice creams loading"); try { - if (_woods != null) + if (_shopWoods != null) { - foreach (var wood in _woods) + DataGridView.Rows.Clear(); + foreach (var wood in _shopWoods) { - DataGridView.Rows.Add(new object[] { wood.Key, wood.Value.Item1.WoodName, wood.Value.Item1.Price, wood.Value.Item2 }); + DataGridView.Rows.Add(new object[] { wood.Key, wood.Value.Item1.WoodName, wood.Value.Item2 }); } } } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки изделий магазина"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + _logger.LogError(ex, "Shop ice creams loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void CancelButton_Click(object sender, EventArgs e) - { - DialogResult = DialogResult.Cancel; - Close(); - } - - private void SaveButton_Click(object sender, EventArgs e) + private void ButtonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(NameTextBox.Text)) { @@ -85,7 +86,17 @@ namespace CarpentryWorkshopView MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _logger.LogInformation("Сохранение магазина"); + if (string.IsNullOrEmpty(DateTimePicker.Text)) + { + MessageBox.Show("Заполните дату", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(CapacityUpDown.Text)) + { + MessageBox.Show("Заполните максимальное количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Shop saving"); try { var model = new ShopBindingModel @@ -93,9 +104,9 @@ namespace CarpentryWorkshopView Id = _id ?? 0, ShopName = NameTextBox.Text, Address = AddressTextBox.Text, - DateOpen = DateTimePicker.Value.Date, - WoodMaxCount = Convert.ToInt32(CapacityUpDown.Value), - ShopWoods = _woods + DateOpen = DateTimePicker.Value, + WoodMaxCount = Convert.ToInt32(CapacityUpDown.Text), + ShopWoods = _shopWoods }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) @@ -108,9 +119,15 @@ namespace CarpentryWorkshopView } catch (Exception ex) { - _logger.LogError(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/CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs index ec39f29..b1ec242 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShops.Designer.cs @@ -52,7 +52,7 @@ AddButton.TabIndex = 1; AddButton.Text = "Добавить"; AddButton.UseVisualStyleBackColor = true; - AddButton.Click += AddButton_Click; + AddButton.Click += ButtonAdd_Click; // // UpdateButton // @@ -62,7 +62,7 @@ UpdateButton.TabIndex = 2; UpdateButton.Text = "Изменить"; UpdateButton.UseVisualStyleBackColor = true; - UpdateButton.Click += UpdateButton_Click; + UpdateButton.Click += ButtonEdit_Click; // // RefreshButton // @@ -72,7 +72,7 @@ RefreshButton.TabIndex = 3; RefreshButton.Text = "Обновить"; RefreshButton.UseVisualStyleBackColor = true; - RefreshButton.Click += RefreshButton_Click; + RefreshButton.Click += ButtonUpd_Click; // // DeleteButton // @@ -82,7 +82,7 @@ DeleteButton.TabIndex = 4; DeleteButton.Text = "Удалить"; DeleteButton.UseVisualStyleBackColor = true; - DeleteButton.Click += DeleteButton_Click; + DeleteButton.Click += ButtonDel_Click; // // ShopsForm // @@ -94,9 +94,9 @@ Controls.Add(UpdateButton); Controls.Add(AddButton); Controls.Add(DataGridView); - Name = "ShopsForm"; - Text = "ShopsForm"; - Load += ShopsForm_Load; + Name = "FormShops"; + Text = "FormShops"; + Load += FormShops_Load; ((System.ComponentModel.ISupportInitialize)DataGridView).EndInit(); ResumeLayout(false); } diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormShops.cs b/CarpentryWorkshop/CarpentryWorkshop/FormShops.cs index cbe1cd6..76ae5c2 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/FormShops.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/FormShops.cs @@ -2,19 +2,28 @@ using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; using Microsoft.Extensions.Logging; +using System.Windows.Forms; namespace CarpentryWorkshopView { 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 @@ -25,25 +34,18 @@ namespace CarpentryWorkshopView DataGridView.DataSource = list; DataGridView.Columns["Id"].Visible = false; DataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - DataGridView.Columns["Address"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - DataGridView.Columns["DateOpen"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; DataGridView.Columns["ShopWoods"].Visible = false; } - _logger.LogInformation("Загрузка магазинов"); + _logger.LogInformation("Shops loading"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки магазинов"); + _logger.LogError(ex, "Shops loading error"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - private void ShopsForm_Load(object sender, EventArgs e) - { - LoadData(); - } - - private void AddButton_Click(object sender, EventArgs e) + private void ButtonAdd_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormShop)); if (service is FormShop form) @@ -55,15 +57,14 @@ namespace CarpentryWorkshopView } } - private void UpdateButton_Click(object sender, EventArgs e) + 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) { - var tmp = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); - form._id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); + form.Id = Convert.ToInt32(DataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { LoadData(); @@ -72,25 +73,17 @@ namespace CarpentryWorkshopView } } - private void RefreshButton_Click(object sender, EventArgs e) - { - LoadData(); - } - - private void DeleteButton_Click(object sender, EventArgs e) + 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("Удаление магазина"); + _logger.LogInformation("Deletion of shop"); try { - if (!_logic.Delete(new ShopBindingModel - { - Id = id - })) + if (!_logic.Delete(new ShopBindingModel { Id = id })) { throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); } @@ -98,11 +91,16 @@ namespace CarpentryWorkshopView } catch (Exception ex) { - _logger.LogError(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/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.Designer.cs b/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.Designer.cs new file mode 100644 index 0000000..df9ee7a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.Designer.cs @@ -0,0 +1,127 @@ +namespace CarpentryWorkshopView +{ + partial class FormWoodSale + { + /// + /// 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() + { + buttonCancel = new Button(); + buttonSale = new Button(); + textBoxCount = new TextBox(); + labelCount = new Label(); + comboBoxWood = new ComboBox(); + labelWood = new Label(); + SuspendLayout(); + // + // buttonCancel + // + buttonCancel.Location = new Point(253, 83); + buttonCancel.Margin = new Padding(4, 3, 4, 3); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(88, 27); + buttonCancel.TabIndex = 17; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // buttonSale + // + buttonSale.Location = new Point(159, 83); + buttonSale.Margin = new Padding(4, 3, 4, 3); + buttonSale.Name = "buttonSale"; + buttonSale.Size = new Size(88, 27); + buttonSale.TabIndex = 16; + buttonSale.Text = "Продать"; + buttonSale.UseVisualStyleBackColor = true; + buttonSale.Click += ButtonSale_Click; + // + // textBoxCount + // + textBoxCount.Location = new Point(101, 51); + textBoxCount.Margin = new Padding(4, 3, 4, 3); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(252, 23); + textBoxCount.TabIndex = 15; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(13, 54); + labelCount.Margin = new Padding(4, 0, 4, 0); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(78, 15); + labelCount.TabIndex = 14; + labelCount.Text = "Количество :"; + // + // comboBoxWood + // + comboBoxWood.DropDownStyle = ComboBoxStyle.DropDownList; + comboBoxWood.FormattingEnabled = true; + comboBoxWood.Location = new Point(101, 15); + comboBoxWood.Margin = new Padding(4, 3, 4, 3); + comboBoxWood.Name = "comboBoxWood"; + comboBoxWood.Size = new Size(252, 23); + comboBoxWood.TabIndex = 13; + // + // labelWood + // + labelWood.AutoSize = true; + labelWood.Location = new Point(13, 19); + labelWood.Margin = new Padding(4, 0, 4, 0); + labelWood.Name = "labelWood"; + labelWood.Size = new Size(80, 15); + labelWood.TabIndex = 12; + labelWood.Text = "Изделие :"; + // + // FormWoodSale + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(373, 123); + Controls.Add(buttonCancel); + Controls.Add(buttonSale); + Controls.Add(textBoxCount); + Controls.Add(labelCount); + Controls.Add(comboBoxWood); + Controls.Add(labelWood); + Name = "FormWoodSale"; + StartPosition = FormStartPosition.CenterScreen; + Text = "Продажа изделий"; + Load += FormWoodSale_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Button buttonCancel; + private Button buttonSale; + private TextBox textBoxCount; + private Label labelCount; + private ComboBox comboBoxWood; + private Label labelWood; + } +} \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.cs b/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.cs new file mode 100644 index 0000000..3d7352c --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.cs @@ -0,0 +1,96 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.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 CarpentryWorkshopView +{ + public partial class FormWoodSale : Form + { + private readonly ILogger _logger; + + private readonly IWoodLogic _logicWood; + + private readonly IShopLogic _logicShop; + + public FormWoodSale(ILogger logger, IWoodLogic logicWood, IShopLogic logicShop) + { + InitializeComponent(); + _logger = logger; + _logicWood = logicWood; + _logicShop = logicShop; + } + + private void FormWoodSale_Load(object sender, EventArgs e) + { + _logger.LogInformation("Ice creams loading"); + try + { + var list = _logicWood.ReadList(null); + if (list != null) + { + comboBoxWood.DisplayMember = "WoodName"; + comboBoxWood.ValueMember = "Id"; + comboBoxWood.DataSource = list; + comboBoxWood.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ice creams loading error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonSale_Click(object sender, EventArgs e) + { + if (comboBoxWood.SelectedValue == null) + { + MessageBox.Show("Выберите мороженое", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxCount.Text)) + { + MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Ice cream sale"); + try + { + var operationResult = _logicShop.MakeSell( + new WoodBindingModel + { + Id = Convert.ToInt32(comboBoxWood.SelectedValue) + }, + 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, "Wood sale error"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.resx b/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshop/FormWoodSale.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshop/Program.cs b/CarpentryWorkshop/CarpentryWorkshop/Program.cs index 9b7966a..7989730 100644 --- a/CarpentryWorkshop/CarpentryWorkshop/Program.cs +++ b/CarpentryWorkshop/CarpentryWorkshop/Program.cs @@ -56,7 +56,7 @@ namespace CarpentryWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs index f396abb..6a19218 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/Shop.cs @@ -21,7 +21,8 @@ namespace CarpentryWorkshopFileImplement.Models if (_shopWoods == null) { var source = DataFileSingleton.GetInstance(); - _shopWoods = Woods.ToDictionary(x => x.Key, y => ((source.Woods.FirstOrDefault(z => z.Id == y.Key) as IWoodModel)!, y.Value)); + _shopWoods = Woods.ToDictionary(x => x.Key, + y => ((source.Woods.FirstOrDefault(z => z.Id == y.Key) as IWoodModel)!, y.Value)); } return _shopWoods; } @@ -52,15 +53,15 @@ namespace CarpentryWorkshopFileImplement.Models { return null; } - return new() + return new Shop() { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ShopName = element.Element("ShopName")!.Value, Address = element.Element("Address")!.Value, DateOpen = Convert.ToDateTime(element.Element("DateOpen")!.Value), - Woods = element.Element("ShopWoods")!.Elements("ShopWood")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), - x => Convert.ToInt32(x.Element("Value")?.Value)), - WoodMaxCount = Convert.ToInt32(element.Element("WoodMaxCount")!.Value) + WoodMaxCount = Convert.ToInt32(element.Element("WoodMaxCount")!.Value), + Woods = element.Element("ShopWoods")!.Elements("ShopWood") + .ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } @@ -89,14 +90,14 @@ namespace CarpentryWorkshopFileImplement.Models }; public XElement GetXElement => new("Shop", - new XAttribute("Id", Id), - new XElement("ShopName", ShopName), - new XElement("Address", Address), - new XElement("DateOpen", DateOpen.ToString()), - new XElement("WoodMaxCount", WoodMaxCount), - new XElement("ShopWoods", Woods - .Select(x => new XElement("ShopWood", - new XElement("Key", x.Key), - new XElement("Value", x.Value))).ToArray())); + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpen", DateOpen.ToString()), + new XElement("WoodMaxCount", WoodMaxCount.ToString()), + new XElement("ShopWoods", + Woods.Select(x => new XElement("ShopWood", + new XElement("Key", x.Key), + new XElement("Value", x.Value))).ToArray())); } }