From a0d620afd52a3b8019c83768e4f72ca59fa2aebf Mon Sep 17 00:00:00 2001 From: Alina Batylkina Date: Tue, 13 Jun 2023 23:06:43 +0400 Subject: [PATCH] =?UTF-8?q?2=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6=D0=BD=D0=B5=D0=BD=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FurnitureAssembly/FormAddToShop.cs | 65 ++++---- .../FurnitureAssembly/FormSell.Designer.cs | 119 +++++++++++++++ .../FurnitureAssembly/FormSell.cs | 113 ++++++++++++++ .../FurnitureAssembly/FormSell.resx | 60 ++++++++ .../FurnitureAssembly/FormShop.Designer.cs | 79 +++++++--- .../FurnitureAssembly/FormShop.cs | 10 +- .../FurnitureAssembly/FormShops.cs | 1 - .../FurnitureAssembly/FurnitureAssembly.sln | 6 + .../FurnitureAssemblyView.csproj | 5 +- .../FurnitureAssembly/MainForm.Designer.cs | 23 ++- .../FurnitureAssembly/MainForm.cs | 56 ++----- .../FurnitureAssembly/Program.cs | 7 +- .../BusinessLogics/FurnitureLogic.cs | 7 +- .../BusinessLogics/OrderLogic.cs | 13 +- .../BusinessLogics/ShopLogic.cs | 60 +++++++- .../FurnitureAssemblyBusinessLogic.csproj | 6 +- .../BindingModels/ShopBindingModel.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + .../FurnitureAssemblyContracts.csproj | 4 + .../StoragesContracts/IShopStorage.cs | 1 + .../ViewModels/ShopViewModel.cs | 2 + .../FurnitureAssemblyDataModels.csproj | 6 + ...ls.sln => FurnitureAssemblyDataModels.sln} | 0 .../Models/IShopModel.cs | 1 + .../DataFileSingleton.cs | 7 +- .../FurnitureAssemblyFileImplement.csproj | 4 +- .../Implements/ComponentStorage.cs | 4 +- .../Implements/FurnitureStorage.cs | 4 +- .../Implements/OrderStorage.cs | 30 ++-- .../Implements/ShopStorage.cs | 143 ++++++++++++++++++ .../Models/Component.cs | 2 +- .../Models/Furniture.cs | 2 +- .../Models/Order.cs | 13 +- .../Models/Shop.cs | 120 +++++++++++++++ .../FurnitureAssemblyListImplement.csproj | 6 +- .../Implements/ShopStorage.cs | 4 + .../Models/Shop.cs | 4 + 37 files changed, 836 insertions(+), 154 deletions(-) create mode 100644 FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs create mode 100644 FurnitureAssembly/FurnitureAssembly/FormSell.cs create mode 100644 FurnitureAssembly/FurnitureAssembly/FormSell.resx rename FurnitureAssembly/FurnitureAssemblyDataModels/{AbstractFurnitureAssemblyDataModels.sln => FurnitureAssemblyDataModels.sln} (100%) create mode 100644 FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ShopStorage.cs create mode 100644 FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Shop.cs diff --git a/FurnitureAssembly/FurnitureAssembly/FormAddToShop.cs b/FurnitureAssembly/FurnitureAssembly/FormAddToShop.cs index 2930d1f..cbe6df8 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormAddToShop.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormAddToShop.cs @@ -12,6 +12,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Microsoft.Extensions.Logging; namespace FurnitureAssembly { @@ -20,6 +21,9 @@ namespace FurnitureAssembly private readonly List? _listShops; private readonly List? _listFurnitures; + private IShopLogic _shopLogic; + private ILogger _logger; + public int Id { @@ -32,24 +36,7 @@ namespace FurnitureAssembly comboBoxShop.SelectedValue = value; } } - public IShopModel? ShopModel - { - get - { - if (_listShops == null) - { - return null; - } - foreach (var elem in _listShops) - { - if (elem.Id == Id) - { - return elem; - } - } - return null; - } - } + public int FurnitureId { @@ -62,24 +49,6 @@ namespace FurnitureAssembly comboBoxFurniture.SelectedValue = value; } } - public IFurnitureModel? FurnitureModel - { - get - { - if (_listFurnitures == null) - { - return null; - } - foreach (var elem in _listFurnitures) - { - if (elem.Id == Id) - { - return elem; - } - } - return null; - } - } public int Count { @@ -87,9 +56,11 @@ namespace FurnitureAssembly set { textBoxCount.Text = value.ToString(); } } - public FormAddToShop(IShopLogic shopLogic, IfurnitureLogic furnitureLogic) + public FormAddToShop(IShopLogic shopLogic, IfurnitureLogic furnitureLogic, ILogger logger) { - InitializeComponent(); + InitializeComponent(); + _logger = logger; + _shopLogic = shopLogic; _listShops = shopLogic.ReadList(null); _listFurnitures = furnitureLogic.ReadList(null); if (_listShops != null) @@ -134,7 +105,23 @@ namespace FurnitureAssembly MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - + + try + { + _logger.LogInformation("Добавление в магазин с id = {ShopName} изделия: id = { FurnitureName} - { Count}", Id, FurnitureId, Count); + var operationResult = _shopLogic.AddFurniture(new ShopBindingModel { Id = Id }, new FurnitureBindingModel { Id = FurnitureId }, Count); + if (!operationResult) + { + throw new Exception("Ошибка при пополнении магазина. Дополнительная информация в логах."); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при пополнении магазина c id = " + Id); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } diff --git a/FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs new file mode 100644 index 0000000..008d40e --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormSell.Designer.cs @@ -0,0 +1,119 @@ +namespace FurnitureAssemblyView +{ + 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() + { + this.labelFurniture = new System.Windows.Forms.Label(); + this.comboBoxToSell = new System.Windows.Forms.ComboBox(); + this.labelCount = new System.Windows.Forms.Label(); + this.textBoxCount = new System.Windows.Forms.TextBox(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // labelFurniture + // + this.labelFurniture.AutoSize = true; + this.labelFurniture.Location = new System.Drawing.Point(38, 53); + this.labelFurniture.Name = "labelFurniture"; + this.labelFurniture.Size = new System.Drawing.Size(80, 25); + this.labelFurniture.TabIndex = 0; + this.labelFurniture.Text = "Изделие"; + // + // comboBoxToSell + // + this.comboBoxToSell.FormattingEnabled = true; + this.comboBoxToSell.Location = new System.Drawing.Point(174, 50); + this.comboBoxToSell.Name = "comboBoxToSell"; + this.comboBoxToSell.Size = new System.Drawing.Size(182, 33); + this.comboBoxToSell.TabIndex = 1; + // + // labelCount + // + this.labelCount.AutoSize = true; + this.labelCount.Location = new System.Drawing.Point(43, 133); + this.labelCount.Name = "labelCount"; + this.labelCount.Size = new System.Drawing.Size(107, 25); + this.labelCount.TabIndex = 2; + this.labelCount.Text = "Количество"; + // + // textBoxCount + // + this.textBoxCount.Location = new System.Drawing.Point(178, 131); + this.textBoxCount.Name = "textBoxCount"; + this.textBoxCount.Size = new System.Drawing.Size(150, 31); + this.textBoxCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(38, 225); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(112, 34); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(216, 225); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(112, 34); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + // + // FormSell + // + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(463, 313); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.textBoxCount); + this.Controls.Add(this.labelCount); + this.Controls.Add(this.comboBoxToSell); + this.Controls.Add(this.labelFurniture); + this.Name = "FormSell"; + this.Text = "Продажа мебели"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label labelFurniture; + private ComboBox comboBoxToSell; + private Label labelCount; + private TextBox textBoxCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormSell.cs b/FurnitureAssembly/FurnitureAssembly/FormSell.cs new file mode 100644 index 0000000..6611f33 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormSell.cs @@ -0,0 +1,113 @@ +using FurnitureAssemblyDataModels.Models; +using FurnitureAssemblyContracts.BusinessLogicsContracts; +using FurnitureAssemblyContracts.ViewModels; +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 FurnitureAssemblyView +{ + public partial class FormSell : Form + { + private readonly IShopLogic _shopLogic; + private readonly List? _listFurnitures; + + public int Id + { + get + { + return Convert.ToInt32(comboBoxToSell.SelectedValue); + } + set + { + comboBoxToSell.SelectedValue = value; + } + } + + public IFurnitureModel? FurnitureModel + { + get + { + if (_listFurnitures == null) + { + return null; + } + foreach (var elem in _listFurnitures) + { + if (elem.Id == Id) + { + return elem; + } + } + return null; + } + } + + public int Count + { + get + { + return Convert.ToInt32(textBoxCount.Text); + } + set + { + textBoxCount.Text = value.ToString(); + } + } + public FormSell(IShopLogic shopLogic, IfurnitureLogic furnitureLogic) + { + InitializeComponent(); + + _shopLogic = shopLogic; + + _listFurnitures = furnitureLogic.ReadList(null); + if (_listFurnitures != null) + { + comboBoxToSell.DisplayMember = "FurnitureName"; + comboBoxToSell.ValueMember = "Id"; + comboBoxToSell.DataSource = _listFurnitures; + comboBoxToSell.SelectedItem = null; + } + } + + private void buttonSell_Click(object sender, EventArgs e) + { + if (FurnitureModel == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (Count <= 0) + { + MessageBox.Show("Укажите количество изделия", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (!_shopLogic.Sell(new() { Id = FurnitureModel.Id }, Count)) + { + MessageBox.Show("Не удалось продать изделие " + FurnitureModel.FurnitureName, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + MessageBox.Show("Продажа прошла успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/FurnitureAssembly/FurnitureAssembly/FormSell.resx b/FurnitureAssembly/FurnitureAssembly/FormSell.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/FurnitureAssembly/FurnitureAssembly/FormSell.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs b/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs index d7dc3a8..3f2ba42 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormShop.Designer.cs @@ -40,62 +40,71 @@ this.FurnitureId = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.FurnitureName = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Count = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.labelMaxCount = new System.Windows.Forms.Label(); + this.textBoxMaxCount = new System.Windows.Forms.TextBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // labelShopName // this.labelShopName.AutoSize = true; - this.labelShopName.Location = new System.Drawing.Point(37, 45); + this.labelShopName.Location = new System.Drawing.Point(53, 75); + this.labelShopName.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.labelShopName.Name = "labelShopName"; - this.labelShopName.Size = new System.Drawing.Size(113, 15); + this.labelShopName.Size = new System.Drawing.Size(170, 25); this.labelShopName.TabIndex = 0; this.labelShopName.Text = "Название магазина"; // // textBoxName // - this.textBoxName.Location = new System.Drawing.Point(168, 42); + this.textBoxName.Location = new System.Drawing.Point(240, 70); + this.textBoxName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.textBoxName.Name = "textBoxName"; - this.textBoxName.Size = new System.Drawing.Size(282, 23); + this.textBoxName.Size = new System.Drawing.Size(401, 31); this.textBoxName.TabIndex = 1; // // textBoxAddress // - this.textBoxAddress.Location = new System.Drawing.Point(168, 83); + this.textBoxAddress.Location = new System.Drawing.Point(240, 138); + this.textBoxAddress.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.textBoxAddress.Name = "textBoxAddress"; - this.textBoxAddress.Size = new System.Drawing.Size(282, 23); + this.textBoxAddress.Size = new System.Drawing.Size(401, 31); this.textBoxAddress.TabIndex = 3; // // labelAddress // this.labelAddress.AutoSize = true; - this.labelAddress.Location = new System.Drawing.Point(37, 86); + this.labelAddress.Location = new System.Drawing.Point(53, 143); + this.labelAddress.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.labelAddress.Name = "labelAddress"; - this.labelAddress.Size = new System.Drawing.Size(40, 15); + this.labelAddress.Size = new System.Drawing.Size(62, 25); this.labelAddress.TabIndex = 2; this.labelAddress.Text = "Адрес"; // // labelDate // this.labelDate.AutoSize = true; - this.labelDate.Location = new System.Drawing.Point(37, 128); + this.labelDate.Location = new System.Drawing.Point(53, 213); + this.labelDate.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.labelDate.Name = "labelDate"; - this.labelDate.Size = new System.Drawing.Size(87, 15); + this.labelDate.Size = new System.Drawing.Size(131, 25); this.labelDate.TabIndex = 4; this.labelDate.Text = "Дата открытия"; // // dateTimePicker // - this.dateTimePicker.Location = new System.Drawing.Point(168, 122); + this.dateTimePicker.Location = new System.Drawing.Point(240, 203); + this.dateTimePicker.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.dateTimePicker.Name = "dateTimePicker"; - this.dateTimePicker.Size = new System.Drawing.Size(282, 23); + this.dateTimePicker.Size = new System.Drawing.Size(401, 31); this.dateTimePicker.TabIndex = 6; // // ButtonSave // - this.ButtonSave.Location = new System.Drawing.Point(382, 490); + this.ButtonSave.Location = new System.Drawing.Point(546, 817); + this.ButtonSave.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.ButtonSave.Name = "ButtonSave"; - this.ButtonSave.Size = new System.Drawing.Size(99, 28); + this.ButtonSave.Size = new System.Drawing.Size(141, 47); this.ButtonSave.TabIndex = 7; this.ButtonSave.Text = "Сохранить"; this.ButtonSave.UseVisualStyleBackColor = true; @@ -103,9 +112,10 @@ // // ButtonCancel // - this.ButtonCancel.Location = new System.Drawing.Point(509, 490); + this.ButtonCancel.Location = new System.Drawing.Point(727, 817); + this.ButtonCancel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.ButtonCancel.Name = "ButtonCancel"; - this.ButtonCancel.Size = new System.Drawing.Size(99, 28); + this.ButtonCancel.Size = new System.Drawing.Size(141, 47); this.ButtonCancel.TabIndex = 8; this.ButtonCancel.Text = "Отмена"; this.ButtonCancel.UseVisualStyleBackColor = true; @@ -118,33 +128,59 @@ this.FurnitureId, this.FurnitureName, this.Count}); - this.dataGridView1.Location = new System.Drawing.Point(37, 177); + this.dataGridView1.Location = new System.Drawing.Point(53, 371); + this.dataGridView1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.dataGridView1.Name = "dataGridView1"; + this.dataGridView1.RowHeadersWidth = 62; this.dataGridView1.RowTemplate.Height = 25; - this.dataGridView1.Size = new System.Drawing.Size(571, 289); + this.dataGridView1.Size = new System.Drawing.Size(816, 406); this.dataGridView1.TabIndex = 9; // // FurnitureId // this.FurnitureId.HeaderText = "Id"; + this.FurnitureId.MinimumWidth = 8; this.FurnitureId.Name = "FurnitureId"; this.FurnitureId.Visible = false; + this.FurnitureId.Width = 150; // // FurnitureName // this.FurnitureName.HeaderText = "Название"; + this.FurnitureName.MinimumWidth = 8; this.FurnitureName.Name = "FurnitureName"; + this.FurnitureName.Width = 150; // // Count // this.Count.HeaderText = "Количество"; + this.Count.MinimumWidth = 8; this.Count.Name = "Count"; + this.Count.Width = 150; + // + // labelMaxCount + // + this.labelMaxCount.AutoSize = true; + this.labelMaxCount.Location = new System.Drawing.Point(58, 275); + this.labelMaxCount.Name = "labelMaxCount"; + this.labelMaxCount.Size = new System.Drawing.Size(117, 25); + this.labelMaxCount.TabIndex = 10; + this.labelMaxCount.Text = "Вместимость"; + // + // textBoxMaxCount + // + this.textBoxMaxCount.Location = new System.Drawing.Point(242, 267); + this.textBoxMaxCount.Name = "textBoxMaxCount"; + this.textBoxMaxCount.Size = new System.Drawing.Size(399, 31); + this.textBoxMaxCount.TabIndex = 11; // // FormShop // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(631, 530); + this.ClientSize = new System.Drawing.Size(901, 883); + this.Controls.Add(this.textBoxMaxCount); + this.Controls.Add(this.labelMaxCount); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.ButtonCancel); this.Controls.Add(this.ButtonSave); @@ -154,6 +190,7 @@ this.Controls.Add(this.labelAddress); this.Controls.Add(this.textBoxName); this.Controls.Add(this.labelShopName); + this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.Name = "FormShop"; this.Text = "Магазин"; this.Load += new System.EventHandler(this.FormShop_Load); @@ -177,5 +214,7 @@ private DataGridViewTextBoxColumn FurnitureId; private DataGridViewTextBoxColumn FurnitureName; private DataGridViewTextBoxColumn Count; + private Label labelMaxCount; + private TextBox textBoxMaxCount; } } \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/FormShop.cs b/FurnitureAssembly/FurnitureAssembly/FormShop.cs index 1d10d32..2cdf642 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormShop.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormShop.cs @@ -45,6 +45,12 @@ namespace FurnitureAssembly MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (Convert.ToInt32(textBoxMaxCount.Text) <= 0) + { + MessageBox.Show("Некорректная вместимость", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Сохранение магазина"); try { @@ -54,7 +60,8 @@ namespace FurnitureAssembly ShopName = textBoxName.Text, Address = textBoxAddress.Text, DateOpening = dateTimePicker.Value, - Furnitures = _shopFurnitures + MaxCount = Convert.ToInt32(textBoxMaxCount.Text) + }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) @@ -96,6 +103,7 @@ namespace FurnitureAssembly textBoxName.Text = view.ShopName; textBoxAddress.Text = view.Address.ToString(); dateTimePicker.Value = view.DateOpening; + textBoxMaxCount.Text = view.MaxCount.ToString(); _shopFurnitures = view.Furnitures ?? new Dictionary(); LoadData(); diff --git a/FurnitureAssembly/FurnitureAssembly/FormShops.cs b/FurnitureAssembly/FurnitureAssembly/FormShops.cs index 35fd60d..9837794 100644 --- a/FurnitureAssembly/FurnitureAssembly/FormShops.cs +++ b/FurnitureAssembly/FurnitureAssembly/FormShops.cs @@ -43,7 +43,6 @@ namespace FurnitureAssembly { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["Furnitures"].Visible = false; dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка магазинов"); diff --git a/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.sln b/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.sln index bbabb70..9ea702a 100644 --- a/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.sln +++ b/FurnitureAssembly/FurnitureAssembly/FurnitureAssembly.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyBusinessLo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FurnitureAssemblyListImplement", "..\FurnitureAssemblyListImplement\FurnitureAssemblyListImplement.csproj", "{2A965D1B-CA9F-4268-A157-8A7D539FBDD2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FurnitureAssemblyFileImplement", "..\FurnitureAssemblyFileImplement\FurnitureAssemblyFileImplement.csproj", "{6889AE21-61E2-4CAE-88FA-1D28125640BE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {2A965D1B-CA9F-4268-A157-8A7D539FBDD2}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A965D1B-CA9F-4268-A157-8A7D539FBDD2}.Release|Any CPU.ActiveCfg = Release|Any CPU {2A965D1B-CA9F-4268-A157-8A7D539FBDD2}.Release|Any CPU.Build.0 = Release|Any CPU + {6889AE21-61E2-4CAE-88FA-1D28125640BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6889AE21-61E2-4CAE-88FA-1D28125640BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6889AE21-61E2-4CAE-88FA-1D28125640BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6889AE21-61E2-4CAE-88FA-1D28125640BE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj b/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj index bc87d38..1e24889 100644 --- a/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj +++ b/FurnitureAssembly/FurnitureAssembly/FurnitureAssemblyView.csproj @@ -10,10 +10,9 @@ - - + + - diff --git a/FurnitureAssembly/FurnitureAssembly/MainForm.Designer.cs b/FurnitureAssembly/FurnitureAssembly/MainForm.Designer.cs index fd97dc8..4b3c449 100644 --- a/FurnitureAssembly/FurnitureAssembly/MainForm.Designer.cs +++ b/FurnitureAssembly/FurnitureAssembly/MainForm.Designer.cs @@ -40,6 +40,7 @@ this.изделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.магазиныToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.пополнениеМагазинаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.buttonSell = new System.Windows.Forms.Button(); buttonReady = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.menuStrip.SuspendLayout(); @@ -62,7 +63,7 @@ this.dataGridView.Name = "dataGridView"; this.dataGridView.RowHeadersWidth = 62; this.dataGridView.RowTemplate.Height = 33; - this.dataGridView.Size = new System.Drawing.Size(1227, 386); + this.dataGridView.Size = new System.Drawing.Size(1227, 441); this.dataGridView.TabIndex = 0; // // buttonCreate @@ -130,21 +131,21 @@ // компонентыToolStripMenuItem // this.компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.компонентыToolStripMenuItem.Size = new System.Drawing.Size(218, 34); this.компонентыToolStripMenuItem.Text = "Компоненты"; this.компонентыToolStripMenuItem.Click += new System.EventHandler(this.компонентыToolStripMenuItem_Click); // // изделияToolStripMenuItem // this.изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - this.изделияToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.изделияToolStripMenuItem.Size = new System.Drawing.Size(218, 34); this.изделияToolStripMenuItem.Text = "Изделия"; this.изделияToolStripMenuItem.Click += new System.EventHandler(this.изделияToolStripMenuItem_Click); // // магазиныToolStripMenuItem // this.магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(270, 34); + this.магазиныToolStripMenuItem.Size = new System.Drawing.Size(218, 34); this.магазиныToolStripMenuItem.Text = "Магазины"; this.магазиныToolStripMenuItem.Click += new System.EventHandler(this.ShopToolStripMenuItem_Click); // @@ -155,11 +156,22 @@ this.пополнениеМагазинаToolStripMenuItem.Text = "Пополнение магазина"; this.пополнениеМагазинаToolStripMenuItem.Click += new System.EventHandler(this.AddToShopToolStripMenuItem_Click); // + // buttonSell + // + this.buttonSell.Location = new System.Drawing.Point(1280, 450); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(208, 34); + this.buttonSell.TabIndex = 7; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.buttonSell_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1530, 450); + this.ClientSize = new System.Drawing.Size(1530, 505); + this.Controls.Add(this.buttonSell); this.Controls.Add(this.buttonRefresh); this.Controls.Add(this.buttonPut); this.Controls.Add(buttonReady); @@ -192,5 +204,6 @@ private ToolStripMenuItem изделияToolStripMenuItem; private ToolStripMenuItem магазиныToolStripMenuItem; private ToolStripMenuItem пополнениеМагазинаToolStripMenuItem; + private Button buttonSell; } } \ No newline at end of file diff --git a/FurnitureAssembly/FurnitureAssembly/MainForm.cs b/FurnitureAssembly/FurnitureAssembly/MainForm.cs index d06b0dd..7507279 100644 --- a/FurnitureAssembly/FurnitureAssembly/MainForm.cs +++ b/FurnitureAssembly/FurnitureAssembly/MainForm.cs @@ -123,8 +123,7 @@ namespace FurnitureAssemblyView { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); _logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id); - try - { + try { var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id, @@ -135,10 +134,8 @@ namespace FurnitureAssemblyView Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()), DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), }); - if (!operationResult) - { - throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); - } + + LoadData(); } catch (Exception ex) @@ -203,41 +200,20 @@ namespace FurnitureAssemblyView private void AddToShopToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormAddToShop)); - if (service is FormAddToShop form) - { - if (form.ShowDialog() == DialogResult.OK) - { - if (form.ShopModel == null || form.FurnitureModel == null) - { - return; - } - _logger.LogInformation("Добавление в магазин {ShopName} изделия: { FurnitureName}- { Count}", form.ShopModel.ShopName, form.FurnitureModel.FurnitureName, form.Count); + var service = Program.ServiceProvider?.GetService(typeof(FormAddToShop)); + if (service is FormAddToShop form) + { + form.ShowDialog(); + } + } - var modelShop = new ShopBindingModel - { - Id = form.Id, - ShopName = form.ShopModel.ShopName, - Address = form.ShopModel.Address, - DateOpening = form.ShopModel.DateOpening, - }; - - var modelFurn = new FurnitureBindingModel - { - Id = form.FurnitureId, - FurnitureName = form.FurnitureModel.FurnitureName, - Price = form.FurnitureModel.Price, - FurnitureComponents = form.FurnitureModel.FurnitureComponents - }; - - var operationResult = _shopLogic.AddFurniture(modelShop, modelFurn, form.Count); - if (!operationResult) - { - throw new Exception("Ошибка при пополнении магазина. Дополнительная информация в логах."); - } - } - - } + private void buttonSell_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSell)); + if (service is FormSell form) + { + form.ShowDialog(); + } } } } diff --git a/FurnitureAssembly/FurnitureAssembly/Program.cs b/FurnitureAssembly/FurnitureAssembly/Program.cs index 3f81912..6965a07 100644 --- a/FurnitureAssembly/FurnitureAssembly/Program.cs +++ b/FurnitureAssembly/FurnitureAssembly/Program.cs @@ -1,11 +1,11 @@ using FurnitureAssemblyContracts.StoragesContracts; using Microsoft.Extensions.DependencyInjection; -using FurnitureAssemblyFileImplement.Implements; +using FurnitureAssemFileImplement.Implements; using FurnitureAssemblyBusinessLogic.BusinessLogics; using FurnitureAssemblyContracts.BusinessLogicsContracts; -using AbstractShopBusinessLogic.BusinessLogics; -using NLog.Extensions.Logging; +using ShopBusinessLogic.BusinessLogics; using FurnitureAssemblyView; +using NLog.Extensions.Logging; using Microsoft.Extensions.Logging; namespace FurnitureAssembly @@ -51,6 +51,7 @@ namespace FurnitureAssembly services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/FurnitureLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/FurnitureLogic.cs index e8b1fee..d8435b3 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/FurnitureLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/FurnitureLogic.cs @@ -4,13 +4,8 @@ using FurnitureAssemblyContracts.SearchModels; using FurnitureAssemblyContracts.StoragesContracts; using FurnitureAssemblyContracts.ViewModels; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -namespace AbstractShopBusinessLogic.BusinessLogics +namespace ShopBusinessLogic.BusinessLogics { public class FurnitureLogic: IfurnitureLogic { diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/OrderLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/OrderLogic.cs index 47090f0..c1a7f71 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/OrderLogic.cs @@ -11,16 +11,18 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AbstractShopBusinessLogic.BusinessLogics +namespace ShopBusinessLogic.BusinessLogics { public class OrderLogic : IOrderLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly IShopLogic _shopLogic; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; } public bool CreateOrder(OrderBindingModel model) @@ -40,7 +42,11 @@ namespace AbstractShopBusinessLogic.BusinessLogics { CheckModel(model); if (model.Status != OrderStatus.Готов) return false; - + if (!_shopLogic.AddFurnituresAtShops(new FurnitureBindingModel() { Id = model.FurnitureId }, model.Count)) + { + _logger.LogWarning("There are not empty places at shops. Replenishment is impossible"); + return false; + } model.Status = OrderStatus.Выдан; model.DateImplement = DateTime.Now; if (_orderStorage.Update(model) == null) @@ -48,6 +54,7 @@ namespace AbstractShopBusinessLogic.BusinessLogics _logger.LogWarning("Update operation failed"); return false; } + return true; } diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/ShopLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/ShopLogic.cs index d380042..0a17f3a 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/BusinessLogics/ShopLogic.cs @@ -10,7 +10,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace AbstractShopBusinessLogic.BusinessLogics +namespace ShopBusinessLogic.BusinessLogics { public class ShopLogic : IShopLogic { @@ -109,6 +109,10 @@ namespace AbstractShopBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет даты открытия магазина", nameof(model.DateOpening)); } + if (model.MaxCount <= 0) + { + throw new ArgumentNullException("Нет вместимости магазина", nameof(model.DateOpening)); + } _logger.LogInformation("Shop. ShopName:{ShopName}. Address:{ Address}. DateOpening:{ DateOpening}. Id: { Id}", model.ShopName, model.Address, model.DateOpening, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel { ShopName = model.ShopName }); if (element != null && element.Id != model.Id) @@ -126,6 +130,10 @@ namespace AbstractShopBusinessLogic.BusinessLogics { return false; } + if (GetCountFreePlaces(model.Id) < count) + { + return false; + } if (shop.Furnitures.ContainsKey(furniture.Id)) { int prev_count = shop.Furnitures[furniture.Id].Item2; @@ -138,6 +146,7 @@ namespace AbstractShopBusinessLogic.BusinessLogics model.Furnitures = shop.Furnitures; model.DateOpening = shop.DateOpening; model.Address = shop.Address; + model.MaxCount = shop.MaxCount; model.ShopName = shop.ShopName; if (_shopStorage.Update(model) == null) { @@ -146,5 +155,54 @@ namespace AbstractShopBusinessLogic.BusinessLogics } return true; } + private int GetCountFreePlaces(int ShopId) + { + var shop = ReadElement(new ShopSearchModel { Id = ShopId }); + if (shop == null) + return 0; + int count = shop.MaxCount; + foreach (var f in shop.Furnitures) + { + count -= f.Value.Item2; + if (count <= 0) + { + return 0; + } + } + return count; + } + + private int GetCountFreePlaces_All() + { + return _shopStorage.GetFullList().Select(x => GetCountFreePlaces(x.Id)).Sum(); + } + + public bool AddFurnituresAtShops(FurnitureBindingModel furnitureModel, int count) + { + if (GetCountFreePlaces_All() < count) + { + return false; + } + + foreach (var shop in _shopStorage.GetFullList()) + { + int countShop = GetCountFreePlaces(shop.Id); + if (countShop >= count) + { + AddFurniture(new() { Id = shop.Id }, furnitureModel, count); + break; + } + else + { + AddFurniture(new() { Id = shop.Id }, furnitureModel, countShop); + } + count -= countShop; + } + return true; + } + public bool Sell(FurnitureBindingModel furnitureBindingModel, int count) + { + return _shopStorage.Sell(furnitureBindingModel, count); + } } } diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj index 4358031..6dcc1d3 100644 --- a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj @@ -6,9 +6,9 @@ enable - - - + + + diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs index 379850c..9bc04d1 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BindingModels/ShopBindingModel.cs @@ -15,6 +15,7 @@ namespace FurnitureAssemblyContracts.BindingModels public string Address { get; set; } = string.Empty; public DateTime DateOpening { get; set; } + public int MaxCount { get; set; } public Dictionary Furnitures { get; set; } = new(); } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IShopLogic.cs b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IShopLogic.cs index 9a3e106..2f10a32 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/BusinessLogicsContracts/IShopLogic.cs @@ -17,5 +17,7 @@ namespace FurnitureAssemblyContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddFurniture(ShopBindingModel model, FurnitureBindingModel furnitureModel, int count); + bool AddFurnituresAtShops(FurnitureBindingModel furnitureModel, int count); + bool Sell(FurnitureBindingModel furnitureModel, int count); } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/FurnitureAssemblyContracts.csproj b/FurnitureAssembly/FurnitureAssemblyContracts/FurnitureAssemblyContracts.csproj index 04f9955..57281df 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/FurnitureAssemblyContracts.csproj +++ b/FurnitureAssembly/FurnitureAssemblyContracts/FurnitureAssemblyContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs b/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs index 1eb24c4..6698804 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/StoragesContracts/IShopStorage.cs @@ -13,5 +13,6 @@ namespace FurnitureAssemblyContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool Sell(FurnitureBindingModel furnitureBindingModel, int count); } } diff --git a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs index 0a738fc..c7a0f8f 100644 --- a/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyContracts/ViewModels/ShopViewModel.cs @@ -17,6 +17,8 @@ namespace FurnitureAssemblyContracts.ViewModels public string Address { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime DateOpening { get; set; } + [DisplayName("Вместимость")] + public int MaxCount { get; set; } public Dictionary Furnitures { get; set; } = new(); } diff --git a/FurnitureAssembly/FurnitureAssemblyDataModels/FurnitureAssemblyDataModels.csproj b/FurnitureAssembly/FurnitureAssemblyDataModels/FurnitureAssemblyDataModels.csproj index 27ac386..0bef2cf 100644 --- a/FurnitureAssembly/FurnitureAssemblyDataModels/FurnitureAssemblyDataModels.csproj +++ b/FurnitureAssembly/FurnitureAssemblyDataModels/FurnitureAssemblyDataModels.csproj @@ -6,4 +6,10 @@ enable + + + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyDataModels/AbstractFurnitureAssemblyDataModels.sln b/FurnitureAssembly/FurnitureAssemblyDataModels/FurnitureAssemblyDataModels.sln similarity index 100% rename from FurnitureAssembly/FurnitureAssemblyDataModels/AbstractFurnitureAssemblyDataModels.sln rename to FurnitureAssembly/FurnitureAssemblyDataModels/FurnitureAssemblyDataModels.sln diff --git a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs index bd5efbf..e42995d 100644 --- a/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs +++ b/FurnitureAssembly/FurnitureAssemblyDataModels/Models/IShopModel.cs @@ -11,6 +11,7 @@ namespace FurnitureAssemblyDataModels.Models string ShopName { get; } string Address { get; } DateTime DateOpening { get; } + int MaxCount { get; } Dictionary Furnitures { get; } } } diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs index 585c244..94d0f72 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/DataFileSingleton.cs @@ -1,4 +1,5 @@ using FurnitureAssemblyFileImplement.Models; +using FurnitureAssemFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; @@ -6,7 +7,7 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; -namespace FurnitureAssemblyFileImplement +namespace FurnitureAssemFileImplement { public class DataFileSingleton { @@ -14,9 +15,11 @@ namespace FurnitureAssemblyFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string FurnitureFileName = "Furniture.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Furnitures { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -28,11 +31,13 @@ namespace FurnitureAssemblyFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SaveFurnitures() => SaveData(Furnitures, FurnitureFileName, "Furnitures", 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 => Component.Create(x)!)!; Furnitures = LoadData(FurnitureFileName, "Furniture", x => Furniture.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/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj b/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj index 52299f0..e40fce3 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/FurnitureAssemblyFileImplement.csproj @@ -6,7 +6,9 @@ enable enable - + + + diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs index aa09bed..e2d3450 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ComponentStorage.cs @@ -2,14 +2,14 @@ using FurnitureAssemblyContracts.SearchModels; using FurnitureAssemblyContracts.StoragesContracts; using FurnitureAssemblyContracts.ViewModels; -using FurnitureAssemblyFileImplement.Models; +using FurnitureAssemFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace FurnitureAssemblyFileImplement.Implements +namespace FurnitureAssemFileImplement.Implements { public class ComponentStorage : IComponentStorage { diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs index e06a0e2..f9d26f2 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/FurnitureStorage.cs @@ -2,14 +2,14 @@ using FurnitureAssemblyContracts.SearchModels; using FurnitureAssemblyContracts.StoragesContracts; using FurnitureAssemblyContracts.ViewModels; -using FurnitureAssemblyFileImplement.Models; +using FurnitureAssemFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace FurnitureAssemblyFileImplement.Implements +namespace FurnitureAssemFileImplement.Implements { public class FurnitureStorage : IFurnitureStorage { diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs index f275041..39ed910 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs @@ -2,14 +2,14 @@ using FurnitureAssemblyContracts.SearchModels; using FurnitureAssemblyContracts.StoragesContracts; using FurnitureAssemblyContracts.ViewModels; -using FurnitureAssemblyFileImplement.Models; +using FurnitureAssemFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace FurnitureAssemblyFileImplement.Implements +namespace FurnitureAssemFileImplement.Implements { public class OrderStorage : IOrderStorage { @@ -21,7 +21,7 @@ namespace FurnitureAssemblyFileImplement.Implements public List GetFullList() { - return source.Orders.Select(x => GetViewModel(x)).ToList(); + return source.Orders.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(OrderSearchModel model) @@ -32,8 +32,8 @@ namespace FurnitureAssemblyFileImplement.Implements } return source.Orders .Where(x => x.Id.Equals(model.Id)) - .Select(x => GetViewModel(x)) - .ToList(); + .Select(x => x.GetViewModel) + .ToList(); } public OrderViewModel? GetElement(OrderSearchModel model) @@ -44,7 +44,9 @@ namespace FurnitureAssemblyFileImplement.Implements } return source.Orders - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + .FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) @@ -57,7 +59,7 @@ namespace FurnitureAssemblyFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return GetViewModel(newOrder); + return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) @@ -69,7 +71,7 @@ namespace FurnitureAssemblyFileImplement.Implements } order.Update(model); source.SaveOrders(); - return GetViewModel(order); + return order.GetViewModel; } public OrderViewModel? Delete(OrderBindingModel model) @@ -79,19 +81,9 @@ namespace FurnitureAssemblyFileImplement.Implements { source.Orders.Remove(element); source.SaveOrders(); - return GetViewModel(element); + return element.GetViewModel; } return null; } - private OrderViewModel GetViewModel(Order order) - { - var viewModel = order.GetViewModel; - var furniture = source.Furnitures.FirstOrDefault(x => x.Id == order.FurnitureId); - if (furniture != null) - { - viewModel.FurnitureName = furniture.FurnitureName; - } - return viewModel; - } } } diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ShopStorage.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..9da14da --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,143 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.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.Equals(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 => + (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName))?.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 element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveShops(); + return element.GetViewModel; + } + return null; + } + private int GetCountFurnitureAllShops(FurnitureBindingModel furniture) + { + int count = 0; + foreach (var shop in source.Shops) + { + if (shop.Furnitures.ContainsKey(furniture.Id)) + { + count += shop.Furnitures[furniture.Id].Item2; + } + } + return count; + } + + + public bool Sell(FurnitureBindingModel furniture, int count) + { + if (GetCountFurnitureAllShops(furniture) < count) + { + return false; + } + + + foreach (var shop in source.Shops) + { + if (shop.Furnitures.ContainsKey(furniture.Id)) + { + if (shop.Furnitures[furniture.Id].Item2 > count) + { + shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, shop.Furnitures[furniture.Id].Item2 - count); + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + MaxCount = shop.MaxCount, + Furnitures = shop.Furnitures, + Address = shop.Address, + DateOpening = shop.DateOpening, + }); + break; + } + count -= shop.Furnitures[furniture.Id].Item2; + shop.Furnitures[furniture.Id] = (shop.Furnitures[furniture.Id].Item1, 0); + shop.Update(new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + MaxCount = shop.MaxCount, + Furnitures = shop.Furnitures, + Address = shop.Address, + DateOpening = shop.DateOpening, + }); + } + } + source.SaveShops(); + return true; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs index 7036dec..ce0dbca 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Component.cs @@ -3,7 +3,7 @@ using FurnitureAssemblyContracts.ViewModels; using FurnitureAssemblyDataModels.Models; using System.Xml.Linq; -namespace FurnitureAssemblyFileImplement.Models +namespace FurnitureAssemFileImplement.Models { public class Component : IComponentModel { diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs index 80ad7dc..42dc54e 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Furniture.cs @@ -3,7 +3,7 @@ using FurnitureAssemblyContracts.ViewModels; using FurnitureAssemblyDataModels.Models; using System.Xml.Linq; -namespace FurnitureAssemblyFileImplement.Models +namespace FurnitureAssemFileImplement.Models { public class Furniture : IFurnitureModel { diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs index 2d15e13..21000d3 100644 --- a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Order.cs @@ -11,12 +11,14 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; -namespace FurnitureAssemblyFileImplement.Models +namespace FurnitureAssemFileImplement.Models { public class Order : IOrderModel { public int FurnitureId { get; private set; } + public string FurnitureName { get; private set; } = string.Empty; + public int Count { get; private set; } public double Sum { get; private set; } @@ -39,6 +41,7 @@ namespace FurnitureAssemblyFileImplement.Models { Id = model.Id, FurnitureId = model.FurnitureId, + FurnitureName = model.FurnitureName, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -57,6 +60,7 @@ namespace FurnitureAssemblyFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), FurnitureId = Convert.ToInt32(element.Element("FurnitureId")!.Value), + FurnitureName = element.Element("FurnitureName")!.Value, Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null), @@ -79,7 +83,12 @@ namespace FurnitureAssemblyFileImplement.Models { return; } + FurnitureId = model.FurnitureId; + FurnitureName = model.FurnitureName; + Count = model.Count; + Sum = model.Sum; Status = model.Status; + DateCreate = model.DateCreate; DateImplement = model.DateImplement; } @@ -87,6 +96,7 @@ namespace FurnitureAssemblyFileImplement.Models { Id = Id, FurnitureId = FurnitureId, + FurnitureName = FurnitureName, Count = Count, Sum = Sum, Status = Status, @@ -97,6 +107,7 @@ namespace FurnitureAssemblyFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("FurnitureId", FurnitureId), + new XElement("FurnitureName", FurnitureName), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), new XElement("Status", Status.ToString()), diff --git a/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Shop.cs new file mode 100644 index 0000000..30e7ffc --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyFileImplement/Models/Shop.cs @@ -0,0 +1,120 @@ +using FurnitureAssemblyDataModels.Models; +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FurnitureAssemblyFileImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + private Dictionary? _furnitures = null; + public Dictionary Furnitures + { + get + { + if (_furnitures == null) + { + var source = DataFileSingleton.GetInstance(); + _furnitures = CurrentCount.ToDictionary( + x => x.Key, + y => (source.Furnitures.FirstOrDefault(z => z.Id == y.Key)! as IFurnitureModel, y.Value)); + + } + return _furnitures; + } + private set { } + } + public int MaxCount { get; private set; } + + public Dictionary CurrentCount { get; private set; } = new(); + + public static Shop? Create(ShopBindingModel model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCount = model.MaxCount, + CurrentCount = model.Furnitures.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + 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, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + MaxCount = Convert.ToInt32(element.Element("MaxCount")!.Value), + CurrentCount = element.Element("CurrentCount")!.Elements("CurrentCountFurniture") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + MaxCount = model.MaxCount; + DateOpening = model.DateOpening; + if (model.Furnitures.Count > 0) + { + CurrentCount = model.Furnitures.ToDictionary(x => x.Key, x => x.Value.Item2); + _furnitures = null; + } + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCount = MaxCount, + Furnitures = Furnitures + }; + + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("MaxCount", MaxCount), + new XElement("CurrentCount", + CurrentCount.Select( + x => new XElement("CurrentCountFurniture", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())); + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/FurnitureAssemblyListImplement.csproj b/FurnitureAssembly/FurnitureAssemblyListImplement/FurnitureAssemblyListImplement.csproj index 3481a48..9041cff 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/FurnitureAssemblyListImplement.csproj +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/FurnitureAssemblyListImplement.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,6 +6,10 @@ enable + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs index 65e0e2b..b811a58 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Implements/ShopStorage.cs @@ -109,5 +109,9 @@ namespace FurnitureAssemblyListImplement.Implements } return null; } + public bool Sell(FurnitureBindingModel furnitureBindingModel, int count) + { + throw new NotImplementedException(); + } } } diff --git a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs index daec617..646365c 100644 --- a/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs +++ b/FurnitureAssembly/FurnitureAssemblyListImplement/Models/Shop.cs @@ -18,6 +18,7 @@ namespace FurnitureAssemblyListImplement.Models public string Address { get; private set; } = string.Empty; public DateTime DateOpening { get; private set; } + public int MaxCount { get; private set; } public Dictionary Furnitures { get; private set; } = new(); @@ -32,6 +33,7 @@ namespace FurnitureAssemblyListImplement.Models Id = model.Id, ShopName = model.ShopName, Address = model.Address, + MaxCount = model.MaxCount, DateOpening = model.DateOpening, Furnitures = model.Furnitures }; @@ -45,6 +47,7 @@ namespace FurnitureAssemblyListImplement.Models } ShopName = model.ShopName; Address = model.Address; + MaxCount = model.MaxCount; DateOpening = model.DateOpening; Furnitures = model.Furnitures; } @@ -54,6 +57,7 @@ namespace FurnitureAssemblyListImplement.Models Id = Id, ShopName = ShopName, Address = Address, + MaxCount = MaxCount, DateOpening = DateOpening, Furnitures = Furnitures };