From 39b607df292c71d7ebf80d6645f0f810bf71cc56 Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Sun, 3 Mar 2024 18:37:52 +0400 Subject: [PATCH 1/3] All done --- SoftwareInstallation/SoftwareInstallation.sln | 16 ++- .../SoftwareInstallation/FormComponents.cs | 1 + .../SoftwareInstallation/FormMain.Designer.cs | 36 +++--- .../SoftwareInstallation/FormMain.cs | 1 + .../SoftwareInstallation/FormMain.resx | 2 +- .../FormPackage.Designer.cs | 32 +++--- .../SoftwareInstallation/FormPackage.cs | 14 +-- .../SoftwareInstallation/FormPackage.resx | 9 -- .../SoftwareInstallation/Program.cs | 2 +- .../SoftwareInstallationView.csproj | 1 + .../BusinessLogics/OrderLogic.cs | 1 - .../DataFileSingleton.cs | 54 +++++++++ .../Implements/ComponentStorage.cs | 81 ++++++++++++++ .../Implements/OrderStorage.cs | 105 ++++++++++++++++++ .../Implements/PackageStorage.cs | 94 ++++++++++++++++ .../Models/Component.cs | 64 +++++++++++ .../Models/Order.cs | 94 ++++++++++++++++ .../Models/Package.cs | 95 ++++++++++++++++ .../SoftwareInstallationFileImplement.csproj | 14 +++ .../Models/Order.cs | 7 ++ 20 files changed, 665 insertions(+), 58 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ComponentStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Implements/PackageStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Models/Component.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Models/Package.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj diff --git a/SoftwareInstallation/SoftwareInstallation.sln b/SoftwareInstallation/SoftwareInstallation.sln index 404a0d4..3ba76bb 100644 --- a/SoftwareInstallation/SoftwareInstallation.sln +++ b/SoftwareInstallation/SoftwareInstallation.sln @@ -3,15 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34408.163 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationView", "SoftwareInstallation\SoftwareInstallationView.csproj", "{953D2A36-3A8C-411D-A7EB-650E2508A9C1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationView", "SoftwareInstallation\SoftwareInstallationView.csproj", "{953D2A36-3A8C-411D-A7EB-650E2508A9C1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationDataModels", "SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj", "{4FE3D032-B5DD-4B3F-90FC-B6B3BCAEB0F3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationDataModels", "SoftwareInstallationDataModels\SoftwareInstallationDataModels.csproj", "{4FE3D032-B5DD-4B3F-90FC-B6B3BCAEB0F3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationContracts", "SoftwareInstallationContracts\SoftwareInstallationContracts.csproj", "{FF58CE99-D5AF-4033-BBCB-B9C218891FFB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationContracts", "SoftwareInstallationContracts\SoftwareInstallationContracts.csproj", "{FF58CE99-D5AF-4033-BBCB-B9C218891FFB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationListImplement", "SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{4784E414-2B47-43D0-AD65-19132F85C7EA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationListImplement", "SoftwareInstallationListImplement\SoftwareInstallationListImplement.csproj", "{4784E414-2B47-43D0-AD65-19132F85C7EA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationBusinessLogic", "SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{7F4E4DE0-F13B-4F3B-B86C-2C7017AD6CF3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftwareInstallationBusinessLogic", "SoftwareInstallationBusinessLogic\SoftwareInstallationBusinessLogic.csproj", "{7F4E4DE0-F13B-4F3B-B86C-2C7017AD6CF3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SoftwareInstallationFileImplement", "SoftwareInstallationFileImplement\SoftwareInstallationFileImplement.csproj", "{B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {7F4E4DE0-F13B-4F3B-B86C-2C7017AD6CF3}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F4E4DE0-F13B-4F3B-B86C-2C7017AD6CF3}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F4E4DE0-F13B-4F3B-B86C-2C7017AD6CF3}.Release|Any CPU.Build.0 = Release|Any CPU + {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B50E488D-DBCB-4C5F-BAB5-795ABF9C0112}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SoftwareInstallation/SoftwareInstallation/FormComponents.cs b/SoftwareInstallation/SoftwareInstallation/FormComponents.cs index 43046a2..e6fb7fa 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormComponents.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormComponents.cs @@ -23,6 +23,7 @@ namespace SoftwareInstallationView InitializeComponent(); _logger = logger; _logic = logic; + LoadData(); } private void FormComponents_Load(object sender, EventArgs e) { diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs index 7566509..26b30db 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs @@ -28,7 +28,7 @@ /// private void InitializeComponent() { - menuStrip = new MenuStrip(); + menuStrip1 = new MenuStrip(); справочникиToolStripMenuItem = new ToolStripMenuItem(); компонентыToolStripMenuItem = new ToolStripMenuItem(); изделияToolStripMenuItem = new ToolStripMenuItem(); @@ -38,20 +38,20 @@ buttonOrderReady = new Button(); buttonIssuedOrder = new Button(); buttonRefresh = new Button(); - menuStrip.SuspendLayout(); + menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // - // menuStrip + // menuStrip1 // - menuStrip.ImageScalingSize = new Size(20, 20); - menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem }); - menuStrip.Location = new Point(0, 0); - menuStrip.Name = "menuStrip"; - menuStrip.Padding = new Padding(7, 3, 0, 3); - menuStrip.Size = new Size(1077, 30); - menuStrip.TabIndex = 0; - menuStrip.Text = "menuStrip1"; + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Padding = new Padding(7, 3, 0, 3); + menuStrip1.Size = new Size(1077, 30); + menuStrip1.TabIndex = 0; + menuStrip1.Text = "menuStrip1"; // // справочникиToolStripMenuItem // @@ -63,14 +63,14 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(182, 26); + компонентыToolStripMenuItem.Size = new Size(224, 26); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += КомпонентыToolStripMenuItem_Click; // // изделияToolStripMenuItem // изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - изделияToolStripMenuItem.Size = new Size(182, 26); + изделияToolStripMenuItem.Size = new Size(224, 26); изделияToolStripMenuItem.Text = "Изделия"; изделияToolStripMenuItem.Click += ИзделияToolStripMenuItem_Click; // @@ -161,14 +161,14 @@ Controls.Add(buttonTakeOrderInWork); Controls.Add(buttonCreateOrder); Controls.Add(dataGridView); - Controls.Add(menuStrip); - MainMenuStrip = menuStrip; + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; Margin = new Padding(3, 4, 3, 4); Name = "FormMain"; Text = "Установка ПО"; Load += FormMain_Load; - menuStrip.ResumeLayout(false); - menuStrip.PerformLayout(); + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); PerformLayout(); @@ -176,7 +176,7 @@ #endregion - private MenuStrip menuStrip; + private MenuStrip menuStrip1; private ToolStripMenuItem справочникиToolStripMenuItem; private ToolStripMenuItem компонентыToolStripMenuItem; private ToolStripMenuItem изделияToolStripMenuItem; diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.cs index 0a42aae..107d13b 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.cs @@ -23,6 +23,7 @@ namespace SoftwareInstallationView InitializeComponent(); _logger = logger; _orderLogic = orderLogic; + LoadData(); } private void FormMain_Load(object sender, EventArgs e) { diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.resx b/SoftwareInstallation/SoftwareInstallation/FormMain.resx index 6c82d08..a0623c8 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.resx +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.resx @@ -117,7 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + 17, 17 \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormPackage.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormPackage.Designer.cs index 096bb9d..157ef22 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormPackage.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormPackage.Designer.cs @@ -37,14 +37,14 @@ buttonDelete = new Button(); buttonUpdate = new Button(); buttonAdd = new Button(); - componentsDataGridView = new DataGridView(); + dataGridView = new DataGridView(); ColumnId = new DataGridViewTextBoxColumn(); ColumnComponent = new DataGridViewTextBoxColumn(); ColumnCount = new DataGridViewTextBoxColumn(); buttonSave = new Button(); buttonCancel = new Button(); componentsGroupBox.SuspendLayout(); - ((System.ComponentModel.ISupportInitialize)componentsDataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // // labelName @@ -88,7 +88,7 @@ componentsGroupBox.Controls.Add(buttonDelete); componentsGroupBox.Controls.Add(buttonUpdate); componentsGroupBox.Controls.Add(buttonAdd); - componentsGroupBox.Controls.Add(componentsDataGridView); + componentsGroupBox.Controls.Add(dataGridView); componentsGroupBox.Location = new Point(14, 115); componentsGroupBox.Margin = new Padding(3, 4, 3, 4); componentsGroupBox.Name = "componentsGroupBox"; @@ -142,18 +142,18 @@ buttonAdd.UseVisualStyleBackColor = true; buttonAdd.Click += ButtonAdd_Click; // - // componentsDataGridView + // dataGridView // - componentsDataGridView.AllowUserToAddRows = false; - componentsDataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - componentsDataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnComponent, ColumnCount }); - componentsDataGridView.Location = new Point(7, 29); - componentsDataGridView.Margin = new Padding(3, 4, 3, 4); - componentsDataGridView.Name = "componentsDataGridView"; - componentsDataGridView.RowHeadersVisible = false; - componentsDataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders; - componentsDataGridView.Size = new Size(505, 332); - componentsDataGridView.TabIndex = 0; + dataGridView.AllowUserToAddRows = false; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnComponent, ColumnCount }); + dataGridView.Location = new Point(7, 29); + dataGridView.Margin = new Padding(3, 4, 3, 4); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersVisible = false; + dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders; + dataGridView.Size = new Size(505, 332); + dataGridView.TabIndex = 0; // // ColumnId // @@ -216,7 +216,7 @@ Text = "Изделие"; Load += FormPackage_Load; componentsGroupBox.ResumeLayout(false); - ((System.ComponentModel.ISupportInitialize)componentsDataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -229,7 +229,7 @@ private TextBox textBoxPrice; private GroupBox componentsGroupBox; private Button buttonAdd; - private DataGridView componentsDataGridView; + private DataGridView dataGridView; private Button buttonRefresh; private Button buttonDelete; private Button buttonUpdate; diff --git a/SoftwareInstallation/SoftwareInstallation/FormPackage.cs b/SoftwareInstallation/SoftwareInstallation/FormPackage.cs index 39a5ca8..45262af 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormPackage.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormPackage.cs @@ -65,10 +65,10 @@ namespace SoftwareInstallationView { if (_PackageComponents != null) { - componentsDataGridView.Rows.Clear(); + dataGridView.Rows.Clear(); foreach (var pc in _PackageComponents) { - componentsDataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 }); } textBoxPrice.Text = CalcPrice().ToString(); } @@ -109,14 +109,14 @@ namespace SoftwareInstallationView } private void ButtonUpd_Click(object sender, EventArgs e) { - if (componentsDataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count == 1) { var service = Program.ServiceProvider?.GetService(typeof(FormPackageComponent)); if (service is FormPackageComponent form) { int id = - Convert.ToInt32(componentsDataGridView.SelectedRows[0].Cells[0].Value); + Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); form.Id = id; form.Count = _PackageComponents[id].Item2; if (form.ShowDialog() == DialogResult.OK) @@ -135,15 +135,15 @@ namespace SoftwareInstallationView } private void ButtonDel_Click(object sender, EventArgs e) { - if (componentsDataGridView.SelectedRows.Count == 1) + if (dataGridView.SelectedRows.Count == 1) { if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { try { - _logger.LogInformation("Удаление компонента: {ComponentName} - {Count}", componentsDataGridView.SelectedRows[0].Cells[1].Value); - _PackageComponents?.Remove(Convert.ToInt32(componentsDataGridView.SelectedRows[0].Cells[0].Value)); + _logger.LogInformation("Удаление компонента: {ComponentName} - {Count}", dataGridView.SelectedRows[0].Cells[1].Value); + _PackageComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); } catch (Exception ex) { diff --git a/SoftwareInstallation/SoftwareInstallation/FormPackage.resx b/SoftwareInstallation/SoftwareInstallation/FormPackage.resx index 5c6df87..524a4e1 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormPackage.resx +++ b/SoftwareInstallation/SoftwareInstallation/FormPackage.resx @@ -126,13 +126,4 @@ True - - True - - - True - - - True - \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/Program.cs b/SoftwareInstallation/SoftwareInstallation/Program.cs index 07a18a6..3c94573 100644 --- a/SoftwareInstallation/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/SoftwareInstallation/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using SoftwareInstallationBusinessLogic.BusinessLogics; using SoftwareInstallationContracts.BusinessLogicContracts; using SoftwareInstallationContracts.StoragesContracts; -using SoftwareInstallationListImplement.Implements; +using SoftwareInstallationFileImplement.Implements; using SoftwareInstallationView; namespace SoftwareInstallation diff --git a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj index 28d9b44..e8917fb 100644 --- a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj +++ b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj @@ -28,6 +28,7 @@ + diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs index 56248de..cd11bd1 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs @@ -71,7 +71,6 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics model.Count = viewModel.Count; model.PackageId = viewModel.PackageId; - CheckModel(model, false); if (_orderStorage.Update(model) == null) { diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..6c1a577 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs @@ -0,0 +1,54 @@ +using SoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement +{ + internal class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string ComponentFileName = "Component.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PackageFileName = "Package.xml"; + public List Components { get; private set; } + public List Orders { get; private set; } + public List Packages { get; private set; } + public static DataFileSingleton GetInstance() + { + if (instance == null) + { + instance = new DataFileSingleton(); + } + return instance; + } + public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); + public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Packages = LoadData(PackageFileName, "Package", x => Package.Create(x)!)!; + Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + } + private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ComponentStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..2730296 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,81 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationFileImplement; +using SoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton source; + public ComponentStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Components.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + return source.Components + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == + model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1; + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + source.Components.Add(newComponent); + source.SaveComponents(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + var component = source.Components.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveComponents(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + var element = source.Components.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Components.Remove(element); + source.SaveComponents(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b555ea6 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,105 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + var element = source.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Orders.Remove(element); + source.SaveOrders(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + return source.Orders.Where(x => x.Id == model.Id).Select(x => GetPackageName(x.GetViewModel)).ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetPackageName(x.GetViewModel)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1; + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + source.Orders.Add(newOrder); + source.SaveOrders(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + source.SaveOrders(); + + return order.GetViewModel; + } + private OrderViewModel GetPackageName(OrderViewModel viewModel) + { + foreach (var package in source.Packages) + { + if (package.Id == viewModel.PackageId) + { + viewModel.PackageName = package.PackageName; + } + } + return viewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/PackageStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/PackageStorage.cs new file mode 100644 index 0000000..139c2b2 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/PackageStorage.cs @@ -0,0 +1,94 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationFileImplement.Implements +{ + public class PackageStorage : IPackageStorage + { + private readonly DataFileSingleton source; + + public PackageStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public PackageViewModel? Delete(PackageBindingModel model) + { + var element = source.Packages.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + source.Packages.Remove(element); + source.SavePackages(); + + return element.GetViewModel; + } + + return null; + } + + public PackageViewModel? GetElement(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue) + { + return null; + } + + return source.Packages.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PackageName) && x.PackageName == model.PackageName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName)) + { + return new(); + } + + return source.Packages.Where(x => x.PackageName.Contains(model.PackageName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return source.Packages.Select(x => x.GetViewModel).ToList(); + } + + public PackageViewModel? Insert(PackageBindingModel model) + { + model.Id = source.Packages.Count > 0 ? source.Packages.Max(x => x.Id) + 1 : 1; + var newPackage = Package.Create(model); + + if (newPackage == null) + { + return null; + } + + source.Packages.Add(newPackage); + source.SavePackages(); + + return newPackage.GetViewModel; + } + + public PackageViewModel? Update(PackageBindingModel model) + { + var package = source.Packages.FirstOrDefault(x => x.Id == model.Id); + + if (package == null) + { + return null; + } + + package.Update(model); + source.SavePackages(); + + return package.GetViewModel; + } + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Component.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Component.cs new file mode 100644 index 0000000..421b1ab --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Component() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ComponentName = element.Element("ComponentName")!.Value, + Cost = Convert.ToDouble(element.Element("Cost")!.Value) + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + public XElement GetXElement => new("Component", + new XAttribute("Id", Id), + new XElement("ComponentName", ComponentName), + new XElement("Cost", Cost.ToString())); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs new file mode 100644 index 0000000..d101ba7 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Order.cs @@ -0,0 +1,94 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Enums; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement.Models +{ + public class Order : IOrderModel + { + public int PackageId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public int Id { get; private set; } + public static Order? Create(OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PackageId = model.PackageId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + public static Order? Create(XElement element) + { + if (element == null) + { + return null; + } + + var order = new Order() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PackageId = Convert.ToInt32(element.Element("PackageId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), + DateCreate = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null) + }; + + DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl); + order.DateImplement = dateImpl; + + return order; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + PackageId = model.PackageId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + PackageId = PackageId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("PackageId", PackageId.ToString()), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", Status.ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Package.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Package.cs new file mode 100644 index 0000000..7ae17ff --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Package.cs @@ -0,0 +1,95 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement.Models +{ + public class Package : IPackageModel + { + public int Id { get; private set; } + public string PackageName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Components { get; private set; } = new(); + private Dictionary? _PackageComponents = null; + public Dictionary PackageComponents + { + get + { + if (_PackageComponents == null) + { + var source = DataFileSingleton.GetInstance(); + _PackageComponents = Components.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, + y.Value)); + } + return _PackageComponents; + } + } + public static Package? Create(PackageBindingModel model) + { + if (model == null) + { + return null; + } + return new Package() + { + Id = model.Id, + PackageName = model.PackageName, + Price = model.Price, + Components = model.PackageComponents.ToDictionary(x => x.Key, x + => x.Value.Item2) + }; + } + public static Package? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Package() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PackageName = element.Element("PackageName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Components = element.Element("PackageComponents")!.Elements("PackageComponent") + .ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => + Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(PackageBindingModel model) + { + if (model == null) + { + return; + } + PackageName = model.PackageName; + Price = model.Price; + Components = model.PackageComponents.ToDictionary(x => x.Key, x => x.Value.Item2); + _PackageComponents = null; + } + public PackageViewModel GetViewModel => new() + { + Id = Id, + PackageName = PackageName, + Price = Price, + PackageComponents = PackageComponents + }; + public XElement GetXElement => new("Package", + new XAttribute("Id", Id), + new XElement("PackageName", PackageName), + new XElement("Price", Price.ToString()), + new XElement("PackageComponents", Components.Select(x => + new XElement("PackageComponent", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +} + diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj b/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj new file mode 100644 index 0000000..d544409 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/SoftwareInstallationFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net8.0 + enable + enable + + + + + + + + diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs index ed4840e..281cebb 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Order.cs @@ -15,10 +15,15 @@ namespace SoftwareInstallationListImplement.Models public int PackageId { get; private set; } public int Count { get; private set; } public double Sum { get; private set; } + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } + public int Id { get; private set; } + public static Order? Create(OrderBindingModel? model) { if (model == null) return null; @@ -33,6 +38,7 @@ namespace SoftwareInstallationListImplement.Models Id = model.Id, }; } + public void Update(OrderBindingModel model) { if (model == null) @@ -52,5 +58,6 @@ namespace SoftwareInstallationListImplement.Models DateCreate = DateCreate, DateImplement = DateImplement }; + } } -- 2.25.1 From 2424828f2e52e775346e8facb464b80636d4202e Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Fri, 12 Apr 2024 21:08:39 +0400 Subject: [PATCH 2/3] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D1=83=20=D0=B8=D0=B7=20?= =?UTF-8?q?PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/OrderStorage.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs index b555ea6..5512397 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/OrderStorage.cs @@ -92,12 +92,9 @@ namespace SoftwareInstallationFileImplement.Implements } private OrderViewModel GetPackageName(OrderViewModel viewModel) { - foreach (var package in source.Packages) - { - if (package.Id == viewModel.PackageId) - { - viewModel.PackageName = package.PackageName; - } + var package = source.Packages.FirstOrDefault(x => x.Id == viewModel.PackageId); + if (package != null) { + viewModel.PackageName = package.PackageName; } return viewModel; } -- 2.25.1 From b5bd3fe73dd74a0ea64af277f6401c231e02c1b4 Mon Sep 17 00:00:00 2001 From: dyakonovr Date: Sun, 21 Apr 2024 13:09:00 +0400 Subject: [PATCH 3/3] All done --- .../SoftwareInstallation/FormMain.Designer.cs | 21 ++- .../SoftwareInstallation/FormMain.cs | 18 +++ .../FormSellPackage.Designer.cs | 119 +++++++++++++++++ .../SoftwareInstallation/FormSellPackage.cs | 103 +++++++++++++++ .../SoftwareInstallation/FormSellPackage.resx | 120 ++++++++++++++++++ .../SoftwareInstallation/FormShop.Designer.cs | 24 ++++ .../SoftwareInstallation/FormShop.cs | 92 +++++++------- .../SoftwareInstallation/FormShops.cs | 2 +- .../SoftwareInstallation/Program.cs | 1 + .../BusinessLogics/OrderLogic.cs | 26 +++- .../BusinessLogics/ShopLogic.cs | 76 ++++++++++- .../BindingModels/ShopBindingModel.cs | 3 +- .../BusinessLogicContracts/IShopLogic.cs | 2 + .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 4 +- .../Models/IShopModel.cs | 3 +- .../DataFileSingleton.cs | 7 +- .../Implements/ShopStorage.cs | 115 +++++++++++++++++ .../Models/Shop.cs | 107 ++++++++++++++++ .../Implements/ShopStorage.cs | 6 + .../Models/Shop.cs | 10 +- 21 files changed, 794 insertions(+), 67 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallation/FormSellPackage.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallation/FormSellPackage.cs create mode 100644 SoftwareInstallation/SoftwareInstallation/FormSellPackage.resx create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ShopStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Models/Shop.cs diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs index cde146c..a654928 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs @@ -33,6 +33,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(); @@ -57,7 +59,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem, магазиныToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem, магазиныToolStripMenuItem, поставкиToolStripMenuItem, продажиToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(117, 24); справочникиToolStripMenuItem.Text = "Справочники"; @@ -83,6 +85,21 @@ магазины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.AllowUserToAddRows = false; @@ -207,6 +224,8 @@ private Button buttonIssuedOrder; private Button buttonRefresh; private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripMenuItem поставкиToolStripMenuItem; + private ToolStripMenuItem продажиToolStripMenuItem; private Button shopReplenishment; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.cs index d6db763..e24bbc3 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.cs @@ -87,6 +87,24 @@ namespace SoftwareInstallationView form.ShowDialog(); } } + + private void поставкиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopReplenishment)); + if (service is FormShopReplenishment form) + { + form.ShowDialog(); + } + } + + private void продажиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPackage)); + if (service is FormSellPackage form) + { + form.ShowDialog(); + } + } private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); diff --git a/SoftwareInstallation/SoftwareInstallation/FormSellPackage.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormSellPackage.Designer.cs new file mode 100644 index 0000000..993a11c --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormSellPackage.Designer.cs @@ -0,0 +1,119 @@ +namespace SoftwareInstallationView +{ + partial class FormSellPackage + { + /// + /// 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.PackageLabel = new System.Windows.Forms.Label(); + this.QuantityLabel = new System.Windows.Forms.Label(); + this.PackageСomboBox = new System.Windows.Forms.ComboBox(); + this.QuantityTextBox = new System.Windows.Forms.TextBox(); + this.SaveButton = new System.Windows.Forms.Button(); + this.ButtonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // PackageLabel + // + this.PackageLabel.AutoSize = true; + this.PackageLabel.Location = new System.Drawing.Point(12, 18); + this.PackageLabel.Name = "PackageLabel"; + this.PackageLabel.Size = new System.Drawing.Size(56, 15); + this.PackageLabel.TabIndex = 0; + this.PackageLabel.Text = "Изделие:"; + // + // QuantityLabel + // + this.QuantityLabel.AutoSize = true; + this.QuantityLabel.Location = new System.Drawing.Point(12, 51); + this.QuantityLabel.Name = "QuantityLabel"; + this.QuantityLabel.Size = new System.Drawing.Size(75, 15); + this.QuantityLabel.TabIndex = 1; + this.QuantityLabel.Text = "Количество:"; + // + // PackageСomboBox + // + this.PackageСomboBox.FormattingEnabled = true; + this.PackageСomboBox.Location = new System.Drawing.Point(88, 15); + this.PackageСomboBox.Name = "PackageСomboBox"; + this.PackageСomboBox.Size = new System.Drawing.Size(184, 23); + this.PackageСomboBox.TabIndex = 2; + // + // QuantityTextBox + // + this.QuantityTextBox.Location = new System.Drawing.Point(88, 48); + this.QuantityTextBox.Name = "QuantityTextBox"; + this.QuantityTextBox.Size = new System.Drawing.Size(184, 23); + this.QuantityTextBox.TabIndex = 3; + // + // SaveButton + // + this.SaveButton.Location = new System.Drawing.Point(72, 100); + this.SaveButton.Name = "SaveButton"; + this.SaveButton.Size = new System.Drawing.Size(97, 29); + this.SaveButton.TabIndex = 4; + this.SaveButton.Text = "Сохранить"; + this.SaveButton.UseVisualStyleBackColor = true; + this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click); + // + // ButtonCancel + // + this.ButtonCancel.Location = new System.Drawing.Point(175, 100); + this.ButtonCancel.Name = "ButtonCancel"; + this.ButtonCancel.Size = new System.Drawing.Size(97, 29); + this.ButtonCancel.TabIndex = 5; + this.ButtonCancel.Text = "Отмена"; + this.ButtonCancel.UseVisualStyleBackColor = true; + this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FromSellPackage + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 141); + this.Controls.Add(this.ButtonCancel); + this.Controls.Add(this.SaveButton); + this.Controls.Add(this.QuantityTextBox); + this.Controls.Add(this.PackageСomboBox); + this.Controls.Add(this.QuantityLabel); + this.Controls.Add(this.PackageLabel); + this.Name = "FromSellPackage"; + this.Text = "Продать Изделие"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label PackageLabel; + private Label QuantityLabel; + private ComboBox PackageСomboBox; + private TextBox QuantityTextBox; + private Button SaveButton; + private Button ButtonCancel; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormSellPackage.cs b/SoftwareInstallation/SoftwareInstallation/FormSellPackage.cs new file mode 100644 index 0000000..f964e8f --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormSellPackage.cs @@ -0,0 +1,103 @@ +using Microsoft.Extensions.Logging; +using SoftwareInstallationContracts.BusinessLogicContracts; +using SoftwareInstallationContracts.SearchModels; +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 SoftwareInstallationView +{ + public partial class FormSellPackage : Form + { + private readonly ILogger _logger; + private readonly IPackageLogic _logicPackage; + private readonly IShopLogic _logicStore; + public FormSellPackage(ILogger logger, IPackageLogic logicPackage, IShopLogic logicStore) + { + InitializeComponent(); + _logger = logger; + _logicPackage = logicPackage; + _logicStore = logicStore; + LoadData(); + } + + private void FormSellPackage_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + _logger.LogInformation("Loading packages for sale."); + + try + { + var list = _logicPackage.ReadList(null); + if (list != null) + { + PackageСomboBox.DisplayMember = "PackageName"; + PackageСomboBox.ValueMember = "Id"; + PackageСomboBox.DataSource = list; + PackageСomboBox.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "List loading error."); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void SaveButton_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(QuantityTextBox.Text)) + { + MessageBox.Show("Укажите количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (PackageСomboBox.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Package sale."); + + try + { + var operationResult = _logicStore.SellPackage(_logicPackage.ReadElement(new PackageSearchModel() + { + Id = Convert.ToInt32(PackageСomboBox.SelectedValue) + })!, Convert.ToInt32(QuantityTextBox.Text)); + + if (!operationResult) + { + throw new Exception("Ошибка при продаже изделия. Дополнительная информация в логах."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Package 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/SoftwareInstallation/SoftwareInstallation/FormSellPackage.resx b/SoftwareInstallation/SoftwareInstallation/FormSellPackage.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormSellPackage.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/SoftwareInstallation/SoftwareInstallation/FormShop.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormShop.Designer.cs index 2245683..75708dd 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormShop.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormShop.Designer.cs @@ -40,7 +40,10 @@ SaveButton = new Button(); ButtonCancel = new Button(); nameTextBox = new TextBox(); + labelNumeric = new Label(); + numericUpDown = new NumericUpDown(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDown).BeginInit(); SuspendLayout(); // // shopNameLabel @@ -152,11 +155,29 @@ nameTextBox.Size = new Size(198, 27); nameTextBox.TabIndex = 10; // + // labelNumeric + // + labelNumeric.AutoSize = true; + labelNumeric.Location = new Point(416, 12); + labelNumeric.Name = "labelNumeric"; + labelNumeric.Size = new Size(161, 20); + labelNumeric.TabIndex = 11; + labelNumeric.Text = "Макс. кол-во товара: "; + // + // numericUpDown + // + numericUpDown.Location = new Point(576, 9); + numericUpDown.Name = "numericUpDown"; + numericUpDown.Size = new Size(150, 27); + numericUpDown.TabIndex = 12; + // // FormShop // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(914, 600); + Controls.Add(numericUpDown); + Controls.Add(labelNumeric); Controls.Add(nameTextBox); Controls.Add(ButtonCancel); Controls.Add(SaveButton); @@ -171,6 +192,7 @@ Text = "Магазин"; Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDown).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -189,5 +211,7 @@ private DataGridViewTextBoxColumn PackagePrice; private DataGridViewTextBoxColumn PackageCount; private TextBox nameTextBox; + private Label labelNumeric; + private NumericUpDown numericUpDown; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormShop.cs b/SoftwareInstallation/SoftwareInstallation/FormShop.cs index 079b7f3..71b4502 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormShop.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormShop.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Logging; using SoftwareInstallationContracts.BindingModels; using SoftwareInstallationContracts.BusinessLogicContracts; +using SoftwareInstallationContracts.SearchModels; using SoftwareInstallationContracts.ViewModels; using SoftwareInstallationDataModels.Models; using System; @@ -20,32 +21,18 @@ namespace SoftwareInstallationView private readonly List? _listShops; private readonly IShopLogic _logic; private readonly ILogger _logger; - public int Id { get; set; } + private int? _id; + private Dictionary _listStores; + public int Id { set { _id = value; } } public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; - _listShops = logic.ReadList(null); + _listShops = new(); _logic = logic; } - private IShopModel? GetShop(int id) - { - if (_listShops == null) - { - return null; - } - foreach (var elem in _listShops) - { - if (elem.Id == id) - { - return elem; - } - } - return null; - } - private void SaveButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(nameTextBox.Text)) @@ -60,31 +47,19 @@ namespace SoftwareInstallationView return; } - _logger.LogInformation("Сохранение изделия"); + _logger.LogInformation("Сохранение магазина"); try { - DateTime.TryParse(openingDatePicker.Text, out var dateTime); - ShopBindingModel model = new() + var model = new ShopBindingModel { + Id = _id ?? 0, ShopName = nameTextBox.Text, ShopAddress = addressTextBox.Text, - OpeningDate = dateTime + OpeningDate = openingDatePicker.Value.Date, + PackageMaxCount = (int)numericUpDown.Value }; - var vmodel = GetShop(Id); - bool operationResult = false; - - if (vmodel != null) - { - model.Id = vmodel.Id; - model.Packages = vmodel.Packages; - operationResult = _logic.Update(model); - } - else - { - operationResult = _logic.Create(model); - } - + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -96,7 +71,7 @@ namespace SoftwareInstallationView } catch (Exception ex) { - _logger.LogError(ex, "Ошибка сохранения изделия"); + _logger.LogError(ex, "Ошибка сохранения магазина"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -109,31 +84,52 @@ namespace SoftwareInstallationView private void FormShop_Load(object sender, EventArgs e) { - LoadData(); + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var view = _logic.ReadElement(new ShopSearchModel + { + Id = _id.Value + }); + if (view != null) + { + nameTextBox.Text = view.ShopName; + addressTextBox.Text = view.ShopAddress; + openingDatePicker.Text = view.OpeningDate.ToString(); + numericUpDown.Value = view.PackageMaxCount; + _listStores = view.ShopPackages ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } } private void LoadData(bool extendDate = true) { + _logger.LogInformation("Загрузка изделий магазина"); try { - var model = GetShop(extendDate ? Id : Convert.ToInt32(nameTextBox.Text)); - if (model != null) + if (_listStores != null) { - nameTextBox.Text = model.ShopName; - addressTextBox.Text = model.ShopAddress; - openingDatePicker.Text = Convert.ToString(model.OpeningDate); dataGridView.Rows.Clear(); - foreach (var el in model.Packages.Values) + foreach (var elem in _listStores) { - dataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 }); + dataGridView.Rows.Add(new object[] { elem.Value.Item1.PackageName, elem.Value.Item1.Price, elem.Value.Item2 }); } } _logger.LogInformation("Загрузка магазинов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки магазинов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + _logger.LogError(ex, "Ошибка загрузки изделий магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/SoftwareInstallation/SoftwareInstallation/FormShops.cs b/SoftwareInstallation/SoftwareInstallation/FormShops.cs index cb5b572..ab3c7b3 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormShops.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormShops.cs @@ -41,7 +41,7 @@ namespace SoftwareInstallationView dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; - dataGridView.Columns["Packages"].Visible = false; + dataGridView.Columns["ShopPackages"].Visible = false; } _logger.LogInformation("Загрузка магазинов"); diff --git a/SoftwareInstallation/SoftwareInstallation/Program.cs b/SoftwareInstallation/SoftwareInstallation/Program.cs index 9c9489d..ef83480 100644 --- a/SoftwareInstallation/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/SoftwareInstallation/Program.cs @@ -55,6 +55,7 @@ namespace SoftwareInstallation services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs index cd11bd1..78e271c 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/OrderLogic.cs @@ -17,11 +17,15 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IShopLogic _shopLogic; + private readonly IPackageStorage _packageStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, IPackageStorage packageStorage) { _logger = logger; _orderStorage = orderStorage; + _shopLogic = shopLogic; + _packageStorage = packageStorage; } public bool CreateOrder(OrderBindingModel model) @@ -48,7 +52,7 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { - var viewModel = _orderStorage.GetElement(new() { Id = model.Id }); + var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); if (viewModel == null) { throw new ArgumentNullException(nameof(model)); @@ -65,7 +69,25 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics if (model.Status == OrderStatus.Выдан) { model.DateImplement = DateTime.Now; + + var package = _packageStorage.GetElement(new() { Id = viewModel.PackageId }); + + if (package == null) + { + throw new ArgumentNullException(nameof(package)); + } + + if (!_shopLogic.AddPackage(package, viewModel.Count)) + { + throw new Exception($"AddPackage operation failed. Store is full."); + } } + else + { + model.DateImplement = viewModel.DateImplement; + } + + CheckModel(model, false); model.Sum = viewModel.Sum; model.Count = viewModel.Count; diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ShopLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ShopLogic.cs index ef5e5f8..942178a 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ShopLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogics/ShopLogic.cs @@ -43,16 +43,20 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics return false; } - _logger.LogInformation("AddPackageInShop find. Id:{Id}", element.Id); - - if (element.Packages.TryGetValue(package.Id, out var pair)) + if (element.PackageMaxCount - element.ShopPackages.Select(x => x.Value.Item2).Sum() < quantity) { - element.Packages[package.Id] = (package, quantity + pair.Item2); + throw new ArgumentNullException("Магазин переполнен", nameof(quantity)); + } + + _logger.LogInformation("AddPackageInShop find. Id:{Id}", element.Id); + if (element.ShopPackages.TryGetValue(package.Id, out var pair)) + { + element.ShopPackages[package.Id] = (package, quantity + pair.Item2); _logger.LogInformation("AddPackageInShop. Has been added {quantity} {package} in {ShopName}", quantity, package.PackageName, element.ShopName); } else { - element.Packages[package.Id] = (package, quantity); + element.ShopPackages[package.Id] = (package, quantity); _logger.LogInformation("AddPackageInShop. Has been added {quantity} new Package {package} in {ShopName}", quantity, package.PackageName, element.ShopName); } @@ -62,15 +66,63 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics ShopAddress = element.ShopAddress, ShopName = element.ShopName, OpeningDate = element.OpeningDate, - Packages = element.Packages + ShopPackages = element.ShopPackages, + PackageMaxCount = element.PackageMaxCount }); return true; } + public bool AddPackage(IPackageModel package, int quantity) + { + if (package == null) + { + throw new ArgumentNullException(nameof(package)); + } + + if (quantity <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity)); + } + + var freePlaces = _ShopStorage.GetFullList() + .Select(x => x.PackageMaxCount - x.ShopPackages + .Select(p => p.Value.Item2).Sum()).Sum() - quantity; + + if (freePlaces < 0) + { + _logger.LogInformation("AddPackage. Failed to add package to store. It's full."); + return false; + } + + foreach (var store in _ShopStorage.GetFullList()) + { + var temp = Math.Min(quantity, store.PackageMaxCount - store.ShopPackages.Select(x => x.Value.Item2).Sum()); + + if (temp <= 0) + { + continue; + } + + if (!AddPackage(new() { Id = store.Id }, package, temp)) + { + _logger.LogWarning("An error occurred while adding package to stores"); + return false; + } + + quantity -= temp; + + if (quantity == 0) + { + return true; + } + } + return true; + } + public bool Create(ShopBindingModel model) { CheckModel(model); - model.Packages = new(); + model.ShopPackages = new(); if (_ShopStorage.Insert(model) == null) { @@ -81,6 +133,11 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics return true; } + public bool SellPackage(IPackageModel package, int quantity) + { + return _ShopStorage.SellPackage(package, quantity); + } + public bool Delete(ShopBindingModel model) { CheckModel(model, false); @@ -165,6 +222,11 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogics throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); } + if (model.PackageMaxCount < 0) + { + throw new ArgumentException("Максимальное количество изделий в магазине не может быть меньше нуля", nameof(model.PackageMaxCount)); + } + _logger.LogInformation("Shop. ShopName:{0}.ShopAddress:{1}. Id: {2}", model.ShopName, model.ShopAddress, model.Id); var element = _ShopStorage.GetElement(new ShopSearchModel { diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs index ef5c66e..0e2b248 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs @@ -12,7 +12,8 @@ namespace SoftwareInstallationContracts.BindingModels public string ShopName { get; set; } = string.Empty; public string ShopAddress { get; set; } = string.Empty; public DateTime OpeningDate { get; set; } = DateTime.Now; - public Dictionary Packages { get; set; } = new(); + public Dictionary ShopPackages { get; set; } = new(); public int Id { get; set; } + public int PackageMaxCount { get; set; } } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicContracts/IShopLogic.cs b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicContracts/IShopLogic.cs index abecdbe..0851fed 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicContracts/IShopLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace SoftwareInstallationContracts.BusinessLogicContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddPackage(ShopSearchModel model, IPackageModel package, int quantity); + bool AddPackage(IPackageModel package, int quantity); + bool SellPackage(IPackageModel package, int quantity); } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs index b7db259..1ac0b6f 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs @@ -1,6 +1,7 @@ using SoftwareInstallationContracts.BindingModels; using SoftwareInstallationContracts.SearchModels; using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -17,5 +18,6 @@ namespace SoftwareInstallationContracts.StoragesContracts ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + bool SellPackage(IPackageModel model, int quantity); } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs index fbda17d..f32e718 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs @@ -10,7 +10,7 @@ namespace SoftwareInstallationContracts.ViewModels { public class ShopViewModel : IShopModel { - public Dictionary Packages { get; set; } = new(); + public Dictionary ShopPackages { get; set; } = new(); public int Id { get; set; } [DisplayName("Название магазина")] @@ -19,5 +19,7 @@ namespace SoftwareInstallationContracts.ViewModels public string ShopAddress { get; set; } = string.Empty; [DisplayName("Дата открытия")] public DateTime OpeningDate { get; set; } = DateTime.Now; + [DisplayName("Вместимость магазина")] + public int PackageMaxCount { get; set; } } } diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/Models/IShopModel.cs b/SoftwareInstallation/SoftwareInstallationDataModels/Models/IShopModel.cs index a644274..8745474 100644 --- a/SoftwareInstallation/SoftwareInstallationDataModels/Models/IShopModel.cs +++ b/SoftwareInstallation/SoftwareInstallationDataModels/Models/IShopModel.cs @@ -11,6 +11,7 @@ namespace SoftwareInstallationDataModels.Models public string ShopName { get; } public string ShopAddress { get; } DateTime OpeningDate { get; } - Dictionary Packages { get; } + Dictionary ShopPackages { get; } + public int PackageMaxCount { get; } } } diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs index 6c1a577..3b4af3c 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; +using static System.Formats.Asn1.AsnWriter; namespace SoftwareInstallationFileImplement { @@ -14,9 +15,11 @@ namespace SoftwareInstallationFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PackageFileName = "Package.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Packages { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -26,13 +29,15 @@ namespace SoftwareInstallationFileImplement return instance; } public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); - public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", x => x.GetXElement); + public void SavePackages() => SaveData(Packages, PackageFileName, "ShopPackages", 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)!)!; Packages = LoadData(PackageFileName, "Package", x => Package.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/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..cef9649 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,115 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using SoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Formats.Asn1.AsnWriter; + +namespace SoftwareInstallationFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + + 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; + } + + 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 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 List GetFullList() + { + return source.Shops.Select(x => x.GetViewModel).ToList(); + } + + 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 bool SellPackage(IPackageModel model, int quantity) + { + if (source.Shops.Select(x => x.ShopPackages.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < quantity) + { + return false; + } + foreach (var Shop in source.Shops.Where(x => x.ShopPackages.ContainsKey(model.Id))) + { + int QuantityInCurrentShop = Shop.ShopPackages[model.Id].Item2; + if (QuantityInCurrentShop <= quantity) + { + Shop.ShopPackages.Remove(model.Id); + quantity -= QuantityInCurrentShop; + } + else + { + Shop.ShopPackages[model.Id] = (Shop.ShopPackages[model.Id].Item1, QuantityInCurrentShop - quantity); + quantity = 0; + } + if (quantity == 0) + { + return true; + } + } + return false; + } + + 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; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Shop.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Shop.cs new file mode 100644 index 0000000..5c27c02 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Shop.cs @@ -0,0 +1,107 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement.Models +{ + public class Shop : IShopModel + { + public string ShopName { get; private set; } = string.Empty; + public string ShopAddress { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + public Dictionary Packages { get; private set; } = new(); + + public Dictionary _Packages = null; + public Dictionary ShopPackages + { + get + { + if (_Packages == null) + { + var source = DataFileSingleton.GetInstance(); + _Packages = Packages.ToDictionary(x => x.Key, y => ((source.Packages.FirstOrDefault(z => z.Id == y.Key) as IPackageModel)!, y.Value)); + } + return _Packages; + } + } + + public int Id { get; private set; } + + public int PackageMaxCount { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + ShopAddress = model.ShopAddress, + PackageMaxCount = model.PackageMaxCount, + OpeningDate = model.OpeningDate, + Packages = model.ShopPackages.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, + ShopAddress = element.Element("ShopAddress")!.Value, + OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value), + PackageMaxCount = Convert.ToInt32(element.Element("PackageMaxCount")!.Value), + Packages = element.Element("ShopPackages")!.Elements("Package").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; + ShopAddress = model.ShopAddress; + OpeningDate = model.OpeningDate; + PackageMaxCount = model.PackageMaxCount; + Packages = model.ShopPackages.ToDictionary(x => x.Key, x => x.Value.Item2); + _Packages = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAddress = ShopAddress, + ShopPackages = ShopPackages, + OpeningDate = OpeningDate, + PackageMaxCount = PackageMaxCount, + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("ShopAddress", ShopAddress), + new XElement("OpeningDate", OpeningDate), + new XElement("PackageMaxCount", PackageMaxCount), + new XElement("ShopPackages", Packages + .Select(x => new XElement("Package", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + ).ToArray())); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ShopStorage.cs index 8befbfc..3120e9b 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/ShopStorage.cs @@ -2,6 +2,7 @@ using SoftwareInstallationContracts.SearchModels; using SoftwareInstallationContracts.StoragesContracts; using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; using SoftwareInstallationListImplement.Models; using System; using System.Collections.Generic; @@ -107,6 +108,11 @@ namespace SoftwareInstallationListImplement.Implements return newShop.GetViewModel; } + public bool SellPackage(IPackageModel model, int quantity) + { + throw new NotImplementedException(); + } + public ShopViewModel? Update(ShopBindingModel model) { foreach (var shop in _source.Shops) diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Shop.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Shop.cs index 0df1b1b..cb43a48 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Shop.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Shop.cs @@ -16,7 +16,7 @@ namespace SoftwareInstallationListImplement.Models public DateTime OpeningDate { get; private set; } - public Dictionary Packages { get; private set; } = new(); + public Dictionary ShopPackages { get; private set; } = new(); public int Id { get; private set; } @@ -32,7 +32,7 @@ namespace SoftwareInstallationListImplement.Models ShopName = model.ShopName, ShopAddress = model.ShopAddress, OpeningDate = model.OpeningDate, - Packages = new() + ShopPackages = new() }; } @@ -45,7 +45,7 @@ namespace SoftwareInstallationListImplement.Models ShopName = model.ShopName; ShopAddress = model.ShopAddress; OpeningDate = model.OpeningDate; - Packages = model.Packages; + ShopPackages = model.ShopPackages; } public ShopViewModel GetViewModel => new() @@ -54,7 +54,9 @@ namespace SoftwareInstallationListImplement.Models ShopName = ShopName, ShopAddress = ShopAddress, OpeningDate = OpeningDate, - Packages = Packages + ShopPackages = ShopPackages }; + + public int PackageMaxCount => throw new NotImplementedException(); } } -- 2.25.1