From a1f4d998423158d9931df15ed3e46fa22aebf773 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 13 Mar 2024 10:46:44 +0400 Subject: [PATCH 01/10] =?UTF-8?q?=D0=B4=D0=B0=D0=BB=D1=8C=D1=88=D0=B5=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D0=BE=D1=87=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ShopLogic.cs | 156 ++++++++++++++++++ .../BindingModels/ShopBindingModel.cs | 26 +++ .../BusinessLogicsContracts/IShopLogic.cs | 22 +++ .../SearchModels/ShopSearchModel.cs | 13 ++ .../StoragesContracts/IShopStorage.cs | 21 +++ .../ViewModels/ShopViewModel.cs | 31 ++++ .../IShopModel.cs | 17 ++ .../DataListSingleton.cs | 2 + .../SoftwareInstallationListImplement/Shop.cs | 64 +++++++ .../ShopStorage.cs | 112 +++++++++++++ 10 files changed, 464 insertions(+) create mode 100644 SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ShopSearchModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs create mode 100644 SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs create mode 100644 SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs new file mode 100644 index 0000000..3120b4b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs @@ -0,0 +1,156 @@ +using Microsoft.Extensions.Logging; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.BusinessLogicsContracts; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationBusinessLogic +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + public ShopLogic(ILogger logger, IShopStorage shopStorage) + { + _logger = logger; + _shopStorage = shopStorage; + } + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id} ", + model?.Name, model?.Id); + var list = (model == null) ? _shopStorage.GetFullList() : + _shopStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", + model.Name, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ShopBindingModel model) + { + CheckModel(model); + model.Packages = new(); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ShopBindingModel model) + { + CheckModel(model, false); + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия магазина", + nameof(model.Name)); + } + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия магазина", + nameof(model.Name)); + } + _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", + model.Name, model.Address, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id && element.Name == model.Name) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + + public bool AddPackage(ShopSearchModel model, IPackageModel package, int count) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (count <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count)); + } + _logger.LogInformation("AddPackageInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("AddPackageInShop element not found"); + return false; + } + _logger.LogInformation("AddPackageInShop find. Id:{Id}", element.Id); + + var prevCount = element.Packages.GetValueOrDefault(package.Id, (package, 0)).Item2; + element.Packages[package.Id] = (package, prevCount + count); + _logger.LogInformation( + "AddPackageInShop. Has been added {count} {package} in {ShopName}", + count, package.PackageName, element.Name); + + _shopStorage.Update(new() + { + Id = element.Id, + Address = element.Address, + Name = element.Name, + DateOpening = element.DateOpening, + Packages = element.Packages + }); + return true; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..152fad0 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,26 @@ +using SoftwareInstallationDataModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public string Name { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + public DateTime DateOpening { get; set; } = DateTime.Now; + + public Dictionary Packages + { + get; + set; + } = new(); + public int Id { get; set; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..9f91211 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,22 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.BusinessLogicsContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool AddPackage(ShopSearchModel model, IPackageModel package, int count); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ShopSearchModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..99efc52 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.SearchModels +{ public class ShopSearchModel + { + public int? Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs new file mode 100644 index 0000000..fb36229 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs @@ -0,0 +1,21 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.StoragesContracts +{ + public interface IShopStorage + { + List GetFullList(); + List GetFilteredList(ShopSearchModel model); + ShopViewModel? GetElement(ShopSearchModel model); + ShopViewModel? Insert(ShopBindingModel model); + ShopViewModel? Update(ShopBindingModel model); + ShopViewModel? Delete(ShopBindingModel model); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..0649057 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,31 @@ +using SoftwareInstallationDataModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + [DisplayName("Название магазина")] + public string Name { get; set; } = string.Empty; + + [DisplayName("Адрес магазина")] + public string Address { get; set; } = string.Empty; + + [DisplayName("Время открытия")] + public DateTime DateOpening { get; set; } = DateTime.Now; + + public Dictionary Packages + { + get; + set; + } = new(); + + public int Id { get; set; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs b/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs new file mode 100644 index 0000000..046852b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs @@ -0,0 +1,17 @@ +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationDataModels +{ + public interface IShopModel : IId + { + string Name { get; } + string Address { get; } + DateTime DateOpening { get; } + Dictionary Packages { get; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs b/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs index 00565cd..81f4fb1 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace SoftwareInstallationListImplement public List Components { get; set; } public List Orders { get; set; } public List Packages { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Packages = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs new file mode 100644 index 0000000..9d231b4 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs @@ -0,0 +1,64 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using SoftwareInstallationDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationListImplement +{ + public class Shop : IShopModel + { + public string Name { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary Packages + { + get; + private set; + } = new(); + + public int Id { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + Packages = new() + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + Packages = model.Packages; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Packages = Packages, + DateOpening = DateOpening, + }; + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs new file mode 100644 index 0000000..498ab79 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs @@ -0,0 +1,112 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationListImplement +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.Name == model.Name) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.Name.Contains(model.Name ?? string.Empty)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + } +} -- 2.25.1 From adb65b19d2f6350105f18b9e16711e2d6307edce Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Mon, 18 Mar 2024 20:09:26 +0400 Subject: [PATCH 02/10] =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormMain.Designer.cs | 27 +++- .../SoftwareInstallationView/FormMain.cs | 9 ++ .../FormShop.Designer.cs | 153 ++++++++++++++++++ .../SoftwareInstallationView/FormShop.cs | 140 ++++++++++++++++ .../SoftwareInstallationView/FormShop.resx | 120 ++++++++++++++ 5 files changed, 446 insertions(+), 3 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormShop.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormShop.resx diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs index d04428c..94ec043 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs @@ -38,6 +38,8 @@ buttonIssuedOrder = new Button(); buttonOrderReady = new Button(); buttonRefresh = new Button(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); + buttonAddPackageInShop = new Button(); menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -53,7 +55,7 @@ // // справочникиToolStripMenuItem // - справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem }); + справочникиToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, изделияToolStripMenuItem, магазиныToolStripMenuItem }); справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; справочникиToolStripMenuItem.Size = new Size(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -61,14 +63,14 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(145, 22); + компонентыToolStripMenuItem.Size = new Size(180, 22); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += компонентыToolStripMenuItem_Click; // // изделияToolStripMenuItem // изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - изделияToolStripMenuItem.Size = new Size(145, 22); + изделияToolStripMenuItem.Size = new Size(180, 22); изделияToolStripMenuItem.Text = "Изделия"; изделияToolStripMenuItem.Click += изделияToolStripMenuItem_Click; // @@ -130,11 +132,28 @@ buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += buttonRefresh_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(180, 22); + магазиныToolStripMenuItem.Text = "Магазины"; + // + // buttonAddPackageInShop + // + buttonAddPackageInShop.Location = new Point(785, 335); + buttonAddPackageInShop.Name = "buttonAddPackageInShop"; + buttonAddPackageInShop.Size = new Size(144, 39); + buttonAddPackageInShop.TabIndex = 7; + buttonAddPackageInShop.Text = "Пополнение магазина"; + buttonAddPackageInShop.UseVisualStyleBackColor = true; + buttonAddPackageInShop.Click += buttonAddPackageInShop_Click; + // // FormMain // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(958, 439); + Controls.Add(buttonAddPackageInShop); Controls.Add(buttonRefresh); Controls.Add(buttonOrderReady); Controls.Add(buttonIssuedOrder); @@ -165,5 +184,7 @@ private Button buttonIssuedOrder; private Button buttonOrderReady; private Button buttonRefresh; + private ToolStripMenuItem магазиныToolStripMenuItem; + private Button buttonAddPackageInShop; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs index c0d136d..08db204 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs @@ -160,5 +160,14 @@ namespace SoftwareInstallationView { LoadData(); } + + private void buttonAddPackageInShop_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddPackageInShop)); + if (service is FormAddPackageInShop form) + { + form.ShowDialog(); + } + } } } diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs new file mode 100644 index 0000000..95b0b63 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs @@ -0,0 +1,153 @@ +namespace SoftwareInstallationView +{ + partial class FormShop + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + labelNameShop = new Label(); + labelAddress = new Label(); + labelDate = new Label(); + textBoxShop = new TextBox(); + textBoxAddress = new TextBox(); + dateTimePicker1 = new DateTimePicker(); + dataGridView = new DataGridView(); + buttonSave = new Button(); + buttonCancel = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // labelNameShop + // + labelNameShop.AutoSize = true; + labelNameShop.Location = new Point(12, 9); + labelNameShop.Name = "labelNameShop"; + labelNameShop.Size = new Size(113, 15); + labelNameShop.TabIndex = 0; + labelNameShop.Text = "Название магазина"; + // + // labelAddress + // + labelAddress.AutoSize = true; + labelAddress.Location = new Point(12, 64); + labelAddress.Name = "labelAddress"; + labelAddress.Size = new Size(94, 15); + labelAddress.TabIndex = 1; + labelAddress.Text = "Адрес магазина"; + // + // labelDate + // + labelDate.AutoSize = true; + labelDate.Location = new Point(334, 9); + labelDate.Name = "labelDate"; + labelDate.Size = new Size(87, 15); + labelDate.TabIndex = 2; + labelDate.Text = "Дата открытия"; + // + // textBoxShop + // + textBoxShop.Location = new Point(12, 27); + textBoxShop.Name = "textBoxShop"; + textBoxShop.Size = new Size(248, 23); + textBoxShop.TabIndex = 3; + // + // textBoxAddress + // + textBoxAddress.Location = new Point(12, 82); + textBoxAddress.Name = "textBoxAddress"; + textBoxAddress.Size = new Size(248, 23); + textBoxAddress.TabIndex = 4; + // + // dateTimePicker1 + // + dateTimePicker1.Location = new Point(334, 36); + dateTimePicker1.Name = "dateTimePicker1"; + dateTimePicker1.Size = new Size(175, 23); + dateTimePicker1.TabIndex = 5; + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 128); + dataGridView.Name = "dataGridView"; + dataGridView.Size = new Size(601, 242); + dataGridView.TabIndex = 6; + // + // buttonSave + // + buttonSave.Location = new Point(384, 376); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(107, 31); + buttonSave.TabIndex = 7; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(497, 376); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(107, 31); + buttonCancel.TabIndex = 8; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // FormShop + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(629, 417); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(dataGridView); + Controls.Add(dateTimePicker1); + Controls.Add(textBoxAddress); + Controls.Add(textBoxShop); + Controls.Add(labelDate); + Controls.Add(labelAddress); + Controls.Add(labelNameShop); + Name = "FormShop"; + Text = "FormShop"; + Load += FormShop_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelNameShop; + private Label labelAddress; + private Label labelDate; + private TextBox textBoxShop; + private TextBox textBoxAddress; + private DateTimePicker dateTimePicker1; + private DataGridView dataGridView; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs new file mode 100644 index 0000000..ab947bb --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs @@ -0,0 +1,140 @@ +using SoftwareInstallationContracts.BusinessLogicsContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using SoftwareInstallationContracts.BindingModels; + +namespace SoftwareInstallationView +{ + public partial class FormShop : Form + { + private readonly List? _listShops; + private readonly IShopLogic _logic; + private readonly ILogger _logger; + + public int Id + { + get; set; + } + + private IShopModel? GetShop(int id) + { + if (_listShops == null) + { + return null; + } + foreach (var elem in _listShops) + { + if (elem.Id == id) + { + return elem; + } + } + return null; + } + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _listShops = logic.ReadList(null); + _logic = logic; + + } + private void LoadData(bool extendDate = true) + { + try + { + var model = GetShop(extendDate ? Id : Convert.ToInt32(null)); + if (model != null) + { + textBoxShop.Text = model.Name; + textBoxAddress.Text = model.Address; + textBoxDateOpening.Text = Convert.ToString(model.DateOpening); + dataGridView.Rows.Clear(); + foreach (var el in model.Packages.Values) + { + dataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 }); + } + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxShop.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxAddress.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение изделия"); + try + { + DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); + ShopBindingModel model = new() + { + Name = textBoxShop.Text, + Address = textBoxAddress.Text, + DateOpening = dateTime + }; + var vmodel = GetShop(Id); + bool operationResult = false; + + if (vmodel != null) + { + model.Id = vmodel.Id; + operationResult = _logic.Update(model); + } + else + { + operationResult = _logic.Create(model); + } + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + private void FormShop_Load(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file -- 2.25.1 From 3077f482fceb754340c883300bf8cce87ac32f8b Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Tue, 26 Mar 2024 12:18:48 +0400 Subject: [PATCH 03/10] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D1=8F=201=20=D1=83=D1=81=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormAddPackageInShop.Designer.cs | 143 ++++++++++++++++++ .../FormAddPackageInShop.cs | 105 +++++++++++++ .../FormAddPackageInShop.resx | 120 +++++++++++++++ .../FormMain.Designer.cs | 19 +-- .../SoftwareInstallationView/FormMain.cs | 27 +++- .../FormShop.Designer.cs | 38 ++++- .../SoftwareInstallationView/FormShop.cs | 4 +- .../SoftwareInstallationView/FormShop.resx | 9 ++ .../FormShops.Designer.cs | 112 ++++++++++++++ .../SoftwareInstallationView/FormShops.cs | 124 +++++++++++++++ .../SoftwareInstallationView/FormShops.resx | 120 +++++++++++++++ .../SoftwareInstallationView/Program.cs | 6 + 12 files changed, 800 insertions(+), 27 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.resx create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormShops.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormShops.resx diff --git a/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.Designer.cs new file mode 100644 index 0000000..9ead54d --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.Designer.cs @@ -0,0 +1,143 @@ +namespace SoftwareInstallationView +{ + partial class FormAddPackageInShop + { + /// + /// 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() + { + labelNameShop = new Label(); + labelNamePackage = new Label(); + labelCount = new Label(); + textBoxShop = new ComboBox(); + textBoxPackage = new ComboBox(); + numericUpDownCount = new NumericUpDown(); + buttonSave = new Button(); + buttonCancel = new Button(); + ((System.ComponentModel.ISupportInitialize)numericUpDownCount).BeginInit(); + SuspendLayout(); + // + // labelNameShop + // + labelNameShop.AutoSize = true; + labelNameShop.Location = new Point(22, 21); + labelNameShop.Name = "labelNameShop"; + labelNameShop.Size = new Size(113, 15); + labelNameShop.TabIndex = 0; + labelNameShop.Text = "Название магазина"; + // + // labelNamePackage + // + labelNamePackage.AutoSize = true; + labelNamePackage.Location = new Point(22, 55); + labelNamePackage.Name = "labelNamePackage"; + labelNamePackage.Size = new Size(106, 15); + labelNamePackage.TabIndex = 1; + labelNamePackage.Text = "Название изделия"; + // + // labelCount + // + labelCount.AutoSize = true; + labelCount.Location = new Point(56, 87); + labelCount.Name = "labelCount"; + labelCount.Size = new Size(72, 15); + labelCount.TabIndex = 2; + labelCount.Text = "Количество"; + // + // textBoxShop + // + textBoxShop.FormattingEnabled = true; + textBoxShop.Location = new Point(160, 13); + textBoxShop.Name = "textBoxShop"; + textBoxShop.Size = new Size(187, 23); + textBoxShop.TabIndex = 3; + // + // textBoxPackage + // + textBoxPackage.FormattingEnabled = true; + textBoxPackage.Location = new Point(160, 47); + textBoxPackage.Name = "textBoxPackage"; + textBoxPackage.Size = new Size(187, 23); + textBoxPackage.TabIndex = 4; + // + // numericUpDownCount + // + numericUpDownCount.Location = new Point(160, 85); + numericUpDownCount.Name = "numericUpDownCount"; + numericUpDownCount.Size = new Size(187, 23); + numericUpDownCount.TabIndex = 5; + // + // buttonSave + // + buttonSave.Location = new Point(201, 125); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(75, 23); + buttonSave.TabIndex = 6; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(291, 125); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(75, 23); + buttonCancel.TabIndex = 7; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // FormAddPackageInShop + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(381, 160); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(numericUpDownCount); + Controls.Add(textBoxPackage); + Controls.Add(textBoxShop); + Controls.Add(labelCount); + Controls.Add(labelNamePackage); + Controls.Add(labelNameShop); + Name = "FormAddPackageInShop"; + Text = "Пополнение магазина"; + ((System.ComponentModel.ISupportInitialize)numericUpDownCount).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label labelNameShop; + private Label labelNamePackage; + private Label labelCount; + private ComboBox textBoxShop; + private ComboBox textBoxPackage; + private NumericUpDown numericUpDownCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.cs b/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.cs new file mode 100644 index 0000000..1fa0baf --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.cs @@ -0,0 +1,105 @@ +using SoftwareInstallationContracts.BusinessLogicsContracts; +using SoftwareInstallationContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using SoftwareInstallationBusinessLogic; + +namespace SoftwareInstallationView +{ + public partial class FormAddPackageInShop : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _shopLogic; + private readonly IPackageLogic _packageLogic; + private readonly List? _listShops; + private readonly List? _listPackages; + public FormAddPackageInShop(ILogger logger, IShopLogic shopLogic, IPackageLogic packageLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _packageLogic = packageLogic; + _logger = logger; + _listShops = shopLogic.ReadList(null); + if (_listShops != null) + { + textBoxShop.DisplayMember = "Name"; + textBoxShop.ValueMember = "Id"; + textBoxShop.DataSource = _listShops; + textBoxShop.SelectedItem = null; + } + + _listPackages = packageLogic.ReadList(null); + if (_listPackages != null) + { + textBoxPackage.DisplayMember = "PackageName"; + textBoxPackage.ValueMember = "Id"; + textBoxPackage.DataSource = _listPackages; + textBoxPackage.SelectedItem = null; + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (textBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (textBoxPackage.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Добавление изделия в магазин"); + + try + { + var package = _packageLogic.ReadElement(new() + { + Id = (int)textBoxPackage.SelectedValue + }); + + if (package == null) + { + throw new Exception("Не найдено изделие. Дополнительная информация в логах."); + } + + var resultOperation = _shopLogic.AddPackage( + model: new() { Id = (int)textBoxShop.SelectedValue }, + package: package, + count: (int)numericUpDownCount.Value + ); + + if (!resultOperation) + { + throw new Exception("Ошибка при добавлении. Дополнительная информация в логах."); + } + + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.resx b/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormAddPackageInShop.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/SoftwareInstallationView/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs index 94ec043..529c9c8 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs @@ -32,13 +32,13 @@ справочникиToolStripMenuItem = new ToolStripMenuItem(); компонентыToolStripMenuItem = new ToolStripMenuItem(); изделияToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); buttonTakeOrderInWork = new Button(); buttonIssuedOrder = new Button(); buttonOrderReady = new Button(); buttonRefresh = new Button(); - магазиныToolStripMenuItem = new ToolStripMenuItem(); buttonAddPackageInShop = new Button(); menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); @@ -63,17 +63,24 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(180, 22); + компонентыToolStripMenuItem.Size = new Size(145, 22); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += компонентыToolStripMenuItem_Click; // // изделияToolStripMenuItem // изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - изделияToolStripMenuItem.Size = new Size(180, 22); + изделияToolStripMenuItem.Size = new Size(145, 22); изделияToolStripMenuItem.Text = "Изделия"; изделияToolStripMenuItem.Click += изделияToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(145, 22); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; + // // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -132,12 +139,6 @@ buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += buttonRefresh_Click; // - // магазиныToolStripMenuItem - // - магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(180, 22); - магазиныToolStripMenuItem.Text = "Магазины"; - // // buttonAddPackageInShop // buttonAddPackageInShop.Location = new Point(785, 335); diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs index 08db204..f3888b9 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs @@ -50,6 +50,14 @@ namespace SoftwareInstallationView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private void buttonAddPackageInShop_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddPackageInShop)); + if (service is FormAddPackageInShop form) + { + form.ShowDialog(); + } + } private void компонентыToolStripMenuItem_Click(object sender, EventArgs e) { @@ -70,6 +78,16 @@ namespace SoftwareInstallationView } } + private void магазиныToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + + if (service is FormShops form) + { + form.ShowDialog(); + } + } + private void buttonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); @@ -161,13 +179,6 @@ namespace SoftwareInstallationView LoadData(); } - private void buttonAddPackageInShop_Click(object sender, EventArgs e) - { - var service = Program.ServiceProvider?.GetService(typeof(FormAddPackageInShop)); - if (service is FormAddPackageInShop form) - { - form.ShowDialog(); - } - } + } } diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs index 95b0b63..505e28a 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs @@ -33,10 +33,13 @@ labelDate = new Label(); textBoxShop = new TextBox(); textBoxAddress = new TextBox(); - dateTimePicker1 = new DateTimePicker(); + openingDatePicker = new DateTimePicker(); dataGridView = new DataGridView(); buttonSave = new Button(); buttonCancel = new Button(); + PackageName = new DataGridViewTextBoxColumn(); + Price = new DataGridViewTextBoxColumn(); + Count = new DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // @@ -81,16 +84,17 @@ textBoxAddress.Size = new Size(248, 23); textBoxAddress.TabIndex = 4; // - // dateTimePicker1 + // openingDatePicker // - dateTimePicker1.Location = new Point(334, 36); - dateTimePicker1.Name = "dateTimePicker1"; - dateTimePicker1.Size = new Size(175, 23); - dateTimePicker1.TabIndex = 5; + openingDatePicker.Location = new Point(334, 36); + openingDatePicker.Name = "openingDatePicker"; + openingDatePicker.Size = new Size(175, 23); + openingDatePicker.TabIndex = 5; // // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { PackageName, Price, Count }); dataGridView.Location = new Point(12, 128); dataGridView.Name = "dataGridView"; dataGridView.Size = new Size(601, 242); @@ -116,6 +120,21 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += buttonCancel_Click; // + // PackageName + // + PackageName.HeaderText = "Изделие"; + PackageName.Name = "PackageName"; + // + // Price + // + Price.HeaderText = "Цена"; + Price.Name = "Price"; + // + // Count + // + Count.HeaderText = "Количество"; + Count.Name = "Count"; + // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); @@ -124,7 +143,7 @@ Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(dataGridView); - Controls.Add(dateTimePicker1); + Controls.Add(openingDatePicker); Controls.Add(textBoxAddress); Controls.Add(textBoxShop); Controls.Add(labelDate); @@ -145,9 +164,12 @@ private Label labelDate; private TextBox textBoxShop; private TextBox textBoxAddress; - private DateTimePicker dateTimePicker1; + private DateTimePicker openingDatePicker; private DataGridView dataGridView; private Button buttonSave; private Button buttonCancel; + private DataGridViewTextBoxColumn PackageName; + private DataGridViewTextBoxColumn Price; + private DataGridViewTextBoxColumn Count; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs index ab947bb..b8a05db 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs @@ -58,7 +58,7 @@ namespace SoftwareInstallationView { textBoxShop.Text = model.Name; textBoxAddress.Text = model.Address; - textBoxDateOpening.Text = Convert.ToString(model.DateOpening); + openingDatePicker.Text = Convert.ToString(model.DateOpening); dataGridView.Rows.Clear(); foreach (var el in model.Packages.Values) { @@ -92,7 +92,7 @@ namespace SoftwareInstallationView _logger.LogInformation("Сохранение изделия"); try { - DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); + DateTime.TryParse(openingDatePicker.Text, out var dateTime); ShopBindingModel model = new() { Name = textBoxShop.Text, diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx index af32865..e591603 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx @@ -117,4 +117,13 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + + + True + + + True + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs new file mode 100644 index 0000000..cbf6f9e --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs @@ -0,0 +1,112 @@ +namespace SoftwareInstallationView +{ + partial class FormShops + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + dataGridView = new DataGridView(); + buttonAdd = new Button(); + buttonUpdate = new Button(); + buttonDelete = new Button(); + buttonRefresh = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(8, 8); + dataGridView.Name = "dataGridView"; + dataGridView.Size = new Size(607, 438); + dataGridView.TabIndex = 0; + // + // buttonAdd + // + buttonAdd.Location = new Point(642, 108); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(129, 41); + buttonAdd.TabIndex = 1; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // + // buttonUpdate + // + buttonUpdate.Location = new Point(642, 173); + buttonUpdate.Name = "buttonUpdate"; + buttonUpdate.Size = new Size(129, 41); + buttonUpdate.TabIndex = 2; + buttonUpdate.Text = "Изменить"; + buttonUpdate.UseVisualStyleBackColor = true; + buttonUpdate.Click += buttonUpdate_Click; + // + // buttonDelete + // + buttonDelete.Location = new Point(642, 241); + buttonDelete.Name = "buttonDelete"; + buttonDelete.Size = new Size(129, 41); + buttonDelete.TabIndex = 3; + buttonDelete.Text = "Удалить"; + buttonDelete.UseVisualStyleBackColor = true; + buttonDelete.Click += buttonDelete_Click; + // + // buttonRefresh + // + buttonRefresh.Location = new Point(642, 306); + buttonRefresh.Name = "buttonRefresh"; + buttonRefresh.Size = new Size(129, 41); + buttonRefresh.TabIndex = 4; + buttonRefresh.Text = "Обновить "; + buttonRefresh.UseVisualStyleBackColor = true; + buttonRefresh.Click += buttonRefresh_Click; + // + // FormShops + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(buttonRefresh); + Controls.Add(buttonDelete); + Controls.Add(buttonUpdate); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormShops"; + Text = "FormShops"; + Load += FormShops_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpdate; + private Button buttonDelete; + private Button buttonRefresh; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShops.cs b/SoftwareInstallation/SoftwareInstallationView/FormShops.cs new file mode 100644 index 0000000..31b88b1 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormShops.cs @@ -0,0 +1,124 @@ +using Microsoft.Extensions.Logging; +using SoftwareInstallation; +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.BusinessLogicsContracts; +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 FormShops : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public FormShops(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormShops_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + + if (list != null) + { + dataGridView.DataSource = list; + + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Packages"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка магазинов"); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + + if (service is FormShop form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + + private void buttonUpdate_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + + if (service is FormShop form) + { + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + + private void buttonDelete_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление магазина"); + + try + { + if (!_logic.Delete(new ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void buttonRefresh_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShops.resx b/SoftwareInstallation/SoftwareInstallationView/FormShops.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormShops.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/Program.cs b/SoftwareInstallation/SoftwareInstallationView/Program.cs index 22c33ab..301e386 100644 --- a/SoftwareInstallation/SoftwareInstallationView/Program.cs +++ b/SoftwareInstallation/SoftwareInstallationView/Program.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; +using SoftwareInstallationBusinessLogic; using SoftwareInstallationBusinessLogic.BusinessLogics; using SoftwareInstallationContracts.BusinessLogicsContracts; using SoftwareInstallationContracts.StoragesContracts; @@ -41,9 +42,11 @@ namespace SoftwareInstallation services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -51,6 +54,9 @@ namespace SoftwareInstallation services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From bc38151b420e321860a27d7dc782f07fd4335ddd Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 27 Mar 2024 11:13:42 +0400 Subject: [PATCH 04/10] =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F=201=20=D1=83=D1=81=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareInstallationView/FormShop.cs | 161 ++++++++++-------- 1 file changed, 92 insertions(+), 69 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs index b8a05db..31d24da 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs @@ -12,129 +12,152 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using SoftwareInstallationContracts.BindingModels; +using System.Security.Cryptography; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationDataModels.Models; namespace SoftwareInstallationView { public partial class FormShop : Form { - private readonly List? _listShops; - private readonly IShopLogic _logic; + /// + /// Логгер + /// private readonly ILogger _logger; - - public int Id - { - get; set; - } - - private IShopModel? GetShop(int id) - { - if (_listShops == null) - { - return null; - } - foreach (var elem in _listShops) - { - if (elem.Id == id) - { - return elem; - } - } - return null; - } + /// + /// Бизнес-логика для магазинов + /// + private readonly IShopLogic _logic; + /// + /// Идентификатор + /// + private int? _id; + public int Id { set { _id = value; } } + /// + /// Список изделий в магазине + /// + private Dictionary _shopPackages; + /// + /// Конструктор + /// + /// + /// public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); _logger = logger; - _listShops = logic.ReadList(null); _logic = logic; - + _shopPackages = new Dictionary(); } - private void LoadData(bool extendDate = true) + /// + /// Загрузка списка изделий в магазине + /// + /// + /// + private void FormShop_Load(object sender, EventArgs e) { - try + if (_id.HasValue) { - var model = GetShop(extendDate ? Id : Convert.ToInt32(null)); - if (model != null) + _logger.LogInformation("Загрузка магазина"); + try { - textBoxShop.Text = model.Name; - textBoxAddress.Text = model.Address; - openingDatePicker.Text = Convert.ToString(model.DateOpening); - dataGridView.Rows.Clear(); - foreach (var el in model.Packages.Values) + var view = _logic.ReadElement(new ShopSearchModel { Id = _id.Value }); + if (view != null) { - dataGridView.Rows.Add(new object[] { el.Item1.PackageName, el.Item1.Price, el.Item2 }); + textBoxShop.Text = view.Name; + textBoxAddress.Text = view.Address; + openingDatePicker.Text = view.DateOpening.ToString(); + _shopPackages = view.Packages ?? new Dictionary(); + LoadData(); } } - _logger.LogInformation("Загрузка магазинов"); - } - catch (Exception ex) - { - _logger.LogError(ex, "Ошибка загрузки магазинов"); - MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } } - + /// + /// Кнопка "Сохранить" + /// + /// + /// private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxShop.Text)) { - MessageBox.Show("Заполните название", "Ошибка", - MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(textBoxAddress.Text)) { - MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, - MessageBoxIcon.Error); + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - _logger.LogInformation("Сохранение изделия"); + _logger.LogInformation("Сохранение магазина"); try { - DateTime.TryParse(openingDatePicker.Text, out var dateTime); - ShopBindingModel model = new() + var model = new ShopBindingModel { + Id = _id ?? 0, Name = textBoxShop.Text, Address = textBoxAddress.Text, - DateOpening = dateTime + DateOpening = openingDatePicker.Value.Date, + Packages = _shopPackages }; - var vmodel = GetShop(Id); - bool operationResult = false; - - if (vmodel != null) - { - model.Id = vmodel.Id; - operationResult = _logic.Update(model); - } - else - { - operationResult = _logic.Create(model); - } + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } - MessageBox.Show("Сохранение прошло успешно", "Сообщение", - MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); DialogResult = DialogResult.OK; Close(); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка сохранения изделия"); + _logger.LogError(ex, "Ошибка сохранения магазина"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - + /// + /// Кнопка "Отмена" + /// + /// + /// private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); } - private void FormShop_Load(object sender, EventArgs e) + /// + /// Метод загрузки изделий магазина + /// + private void LoadData() { - LoadData(); + _logger.LogInformation("Загрузка изделий магазина"); + try + { + if (_shopPackages != null) + { + dataGridView.Rows.Clear(); + foreach (var elem in _shopPackages) + { + dataGridView.Rows.Add(new object[] + { + elem.Key, + elem.Value.Item1.PackageName, + elem.Value.Item2 + }); + } + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки изделий магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } } } -- 2.25.1 From e4feedd6bf3aa8dabd9528e3fa1d8b8be926e3d8 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Mon, 22 Apr 2024 13:28:29 +0400 Subject: [PATCH 05/10] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OrderLogic.cs | 29 ++++- .../ShopLogic.cs | 82 +++++++++++- .../BindingModels/ShopBindingModel.cs | 7 +- .../BusinessLogicsContracts/IShopLogic.cs | 2 + .../StoragesContracts/IShopStorage.cs | 2 + .../ViewModels/ShopViewModel.cs | 10 +- .../IShopModel.cs | 3 +- .../DataFileSingleton.cs | 4 + .../SoftwareInstallationFileImplement/Shop.cs | 108 ++++++++++++++++ .../ShopStorage.cs | 113 +++++++++++++++++ .../SoftwareInstallationListImplement/Shop.cs | 15 +-- .../ShopStorage.cs | 5 + .../FormComponents.cs | 1 + .../FormMain.Designer.cs | 28 +++- .../SoftwareInstallationView/FormMain.cs | 19 ++- .../FormPackage.Designer.cs | 2 +- .../FormSellPackage.Designer.cs | 119 +++++++++++++++++ .../FormSellPackage.cs | 101 +++++++++++++++ .../FormSellPackage.resx | 120 ++++++++++++++++++ .../FormShop.Designer.cs | 55 +++++--- .../SoftwareInstallationView/FormShop.resx | 9 ++ .../FormShops.Designer.cs | 2 +- 22 files changed, 780 insertions(+), 56 deletions(-) create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/Shop.cs create mode 100644 SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormSellPackage.Designer.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs create mode 100644 SoftwareInstallation/SoftwareInstallationView/FormSellPackage.resx diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs index 0f93b79..c7b2506 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/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,13 +69,26 @@ 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; - model.PackageId = viewModel.PackageId; - - CheckModel(model, false); + model.PackageId = viewModel.PackageId; if (_orderStorage.Update(model) == null) { diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs index 3120b4b..55965d0 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/ShopLogic.cs @@ -56,7 +56,7 @@ namespace SoftwareInstallationBusinessLogic public bool Create(ShopBindingModel model) { CheckModel(model); - model.Packages = new(); + model.ShopPackages = new(); if (_shopStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); @@ -90,6 +90,10 @@ namespace SoftwareInstallationBusinessLogic } return true; } + public bool SellPackage(IPackageModel package, int quantity) + { + return _shopStorage.SellPackage(package, quantity); + } private void CheckModel(ShopBindingModel model, bool withParams = true) { if (model == null) @@ -105,6 +109,11 @@ namespace SoftwareInstallationBusinessLogic throw new ArgumentNullException("Нет названия магазина", nameof(model.Name)); } + if (model.PackageMaxCount < 0) + { + throw new ArgumentException("Максимальное количество изделий в магазине не может быть меньше нуля", nameof(model.PackageMaxCount)); + } + _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.Name, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel @@ -128,19 +137,31 @@ namespace SoftwareInstallationBusinessLogic throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count)); } _logger.LogInformation("AddPackageInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) { _logger.LogWarning("AddPackageInShop element not found"); return false; } + + if (element.PackageMaxCount - element.ShopPackages.Select(x => x.Value.Item2).Sum() < count) + { + throw new ArgumentNullException("Магазин переполнен", nameof(count)); + } _logger.LogInformation("AddPackageInShop find. Id:{Id}", element.Id); - var prevCount = element.Packages.GetValueOrDefault(package.Id, (package, 0)).Item2; - element.Packages[package.Id] = (package, prevCount + count); - _logger.LogInformation( - "AddPackageInShop. Has been added {count} {package} in {ShopName}", - count, package.PackageName, element.Name); + if (element.ShopPackages.TryGetValue(package.Id, out var pair)) + { + element.ShopPackages[package.Id] = (package, count + pair.Item2); + _logger.LogInformation("AddPackageInShop. Has been added {quantity} {package} in {ShopName}", count, package.PackageName, element.Name); + } + else + { + element.ShopPackages[package.Id] = (package, count); + _logger.LogInformation("AddPackageInShop. Has been added {quantity} new Package {package} in {ShopName}", count, package.PackageName, element.Name); + } _shopStorage.Update(new() { @@ -148,9 +169,56 @@ namespace SoftwareInstallationBusinessLogic Address = element.Address, Name = element.Name, DateOpening = element.DateOpening, - Packages = element.Packages + ShopPackages = element.ShopPackages, + PackageMaxCount = element.PackageMaxCount }); return true; } + public bool AddPackage(IPackageModel package, int count) + { + if (package == null) + { + throw new ArgumentNullException(nameof(package)); + } + + if (count <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count)); + } + + var freePlaces = _shopStorage.GetFullList() + .Select(x => x.PackageMaxCount - x.ShopPackages + .Select(p => p.Value.Item2).Sum()).Sum() - count; + + 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(count, 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; + } + + count -= temp; + + if (count == 0) + { + return true; + } + } + return true; + } } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs index 152fad0..2d98532 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/BindingModels/ShopBindingModel.cs @@ -16,11 +16,8 @@ namespace SoftwareInstallationContracts.BindingModels public DateTime DateOpening { 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; } } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs index 9f91211..8cd148f 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/BusinessLogicsContracts/IShopLogic.cs @@ -18,5 +18,7 @@ namespace SoftwareInstallationContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddPackage(ShopSearchModel model, IPackageModel package, int count); + bool AddPackage(IPackageModel package, int count); + bool SellPackage(IPackageModel package, int count); } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs b/SoftwareInstallation/SoftwareInstallationContracts/StoragesContracts/IShopStorage.cs index fb36229..a172eca 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 count); } } diff --git a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs index 0649057..4fabdf2 100644 --- a/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs +++ b/SoftwareInstallation/SoftwareInstallationContracts/ViewModels/ShopViewModel.cs @@ -20,12 +20,12 @@ namespace SoftwareInstallationContracts.ViewModels [DisplayName("Время открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; - public Dictionary Packages - { - get; - set; - } = new(); + [DisplayName("Вместимость магазина")] + public int PackageMaxCount { get; set; } + + public Dictionary ShopPackages { get; set; } = new(); public int Id { get; set; } + } } diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs b/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs index 046852b..e8f014b 100644 --- a/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs +++ b/SoftwareInstallation/SoftwareInstallationDataModels/IShopModel.cs @@ -12,6 +12,7 @@ namespace SoftwareInstallationDataModels string Name { get; } string Address { get; } DateTime DateOpening { get; } - Dictionary Packages { get; } + Dictionary ShopPackages { get; } + public int PackageMaxCount { get; } } } diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs index f38fc23..da61923 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs @@ -13,9 +13,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) @@ -27,11 +29,13 @@ namespace SoftwareInstallationFileImplement 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); + 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/Shop.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Shop.cs new file mode 100644 index 0000000..ede4c54 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Shop.cs @@ -0,0 +1,108 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using SoftwareInstallationDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement +{ + public class Shop : IShopModel + { + public string Name { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { 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, + Name = model.Name, + Address = model.Address, + PackageMaxCount = model.PackageMaxCount, + DateOpening = model.DateOpening, + 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), + Name = element.Element("ShopName")!.Value, + Address = element.Element("ShopAddress")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.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; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + PackageMaxCount = model.PackageMaxCount; + Packages = model.ShopPackages.ToDictionary(x => x.Key, x => x.Value.Item2); + _Packages = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + ShopPackages = ShopPackages, + DateOpening = DateOpening, + PackageMaxCount = PackageMaxCount, + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", Name), + new XElement("ShopAddress", Address), + new XElement("DateOpening", DateOpening), + 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/SoftwareInstallationFileImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs new file mode 100644 index 0000000..96e13bb --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs @@ -0,0 +1,113 @@ +using SoftwareInstallationContracts.BindingModels; +using SoftwareInstallationContracts.SearchModels; +using SoftwareInstallationContracts.StoragesContracts; +using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationFileImplement +{ + 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.Name) && !model.Id.HasValue) + { + return null; + } + return source.Shops.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + return source.Shops.Where(x => + x.Name.Contains(model.Name)).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/SoftwareInstallationListImplement/Shop.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs index 9d231b4..991ef2b 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Shop.cs @@ -1,7 +1,7 @@ using SoftwareInstallationContracts.BindingModels; using SoftwareInstallationContracts.ViewModels; -using SoftwareInstallationDataModels.Models; using SoftwareInstallationDataModels; +using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -18,11 +18,7 @@ namespace SoftwareInstallationListImplement public DateTime DateOpening { get; private set; } - public Dictionary Packages - { - get; - private set; - } = new(); + public Dictionary ShopPackages { get; private set; } = new(); public int Id { get; private set; } @@ -38,7 +34,7 @@ namespace SoftwareInstallationListImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, - Packages = new() + ShopPackages = new() }; } public void Update(ShopBindingModel? model) @@ -50,15 +46,16 @@ namespace SoftwareInstallationListImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - Packages = model.Packages; + ShopPackages = model.ShopPackages; } public ShopViewModel GetViewModel => new() { Id = Id, Name = Name, Address = Address, - Packages = Packages, + ShopPackages = ShopPackages, DateOpening = DateOpening, }; + public int PackageMaxCount => throw new NotImplementedException(); } } diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs index 498ab79..4612199 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using SoftwareInstallationContracts.SearchModels; using SoftwareInstallationContracts.StoragesContracts; using SoftwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -95,6 +96,10 @@ namespace SoftwareInstallationListImplement _source.Shops.Add(newShop); return newShop.GetViewModel; } + public bool SellPackage(IPackageModel model, int quantity) + { + throw new NotImplementedException(); + } public ShopViewModel? Update(ShopBindingModel model) { diff --git a/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs b/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs index 60e55c7..70564b2 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormComponents.cs @@ -24,6 +24,7 @@ namespace SoftwareInstallationView InitializeComponent(); _logger = logger; _logic = logic; + LoadData(); } private void FormComponents_Load(object sender, EventArgs e) { diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs index 529c9c8..00379be 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/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(); @@ -55,7 +57,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(94, 20); справочникиToolStripMenuItem.Text = "Справочники"; @@ -63,24 +65,38 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(145, 22); + компонентыToolStripMenuItem.Size = new Size(180, 22); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += компонентыToolStripMenuItem_Click; // // изделияToolStripMenuItem // изделияToolStripMenuItem.Name = "изделияToolStripMenuItem"; - изделияToolStripMenuItem.Size = new Size(145, 22); + изделияToolStripMenuItem.Size = new Size(180, 22); изделияToolStripMenuItem.Text = "Изделия"; изделияToolStripMenuItem.Click += изделияToolStripMenuItem_Click; // // магазиныToolStripMenuItem // магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; - магазиныToolStripMenuItem.Size = new Size(145, 22); + магазины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; + // + // продажиToolStripMenuItem + // + продажиToolStripMenuItem.Name = "продажиToolStripMenuItem"; + продажиToolStripMenuItem.Size = new Size(180, 22); + продажиToolStripMenuItem.Text = "Продажи"; + продажиToolStripMenuItem.Click += продажиToolStripMenuItem_Click; + // // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -164,7 +180,7 @@ Controls.Add(menuStrip); MainMenuStrip = menuStrip; Name = "FormMain"; - Text = "FormMain"; + Text = "Установка ПО"; Load += FormMain_Load; menuStrip.ResumeLayout(false); menuStrip.PerformLayout(); @@ -187,5 +203,7 @@ private Button buttonRefresh; private ToolStripMenuItem магазиныToolStripMenuItem; private Button buttonAddPackageInShop; + private ToolStripMenuItem поставкиToolStripMenuItem; + private ToolStripMenuItem продажиToolStripMenuItem; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs index f3888b9..44e5a95 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormMain.cs @@ -23,6 +23,7 @@ namespace SoftwareInstallationView InitializeComponent(); _logger = logger; _orderLogic = orderLogic; + LoadData(); } private void FormMain_Load(object sender, EventArgs e) { @@ -87,6 +88,22 @@ namespace SoftwareInstallationView form.ShowDialog(); } } + private void поставкиToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddPackageInShop)); + if (service is FormAddPackageInShop 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) { @@ -179,6 +196,6 @@ namespace SoftwareInstallationView LoadData(); } - + } } diff --git a/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs index e05be79..259e3b6 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormPackage.Designer.cs @@ -193,7 +193,7 @@ Controls.Add(labelPrice); Controls.Add(labelName); Name = "FormPackage"; - Text = "FormPackage"; + Text = "Изделие"; Load += FormPackage_Load; componentsGroupBox.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)componentsDataGridView).EndInit(); diff --git a/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.Designer.cs new file mode 100644 index 0000000..7a6172c --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/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() + { + CountLabel = new Label(); + PackageСomboBox = new ComboBox(); + PackageLabel = new Label(); + CountTextBox = new TextBox(); + ButtonCancel = new Button(); + SaveButton = new Button(); + SuspendLayout(); + // + // CountLabel + // + CountLabel.AutoSize = true; + CountLabel.Location = new Point(12, 58); + CountLabel.Name = "CountLabel"; + CountLabel.Size = new Size(75, 15); + CountLabel.TabIndex = 2; + CountLabel.Text = "Количество:"; + // + // PackageСomboBox + // + PackageСomboBox.FormattingEnabled = true; + PackageСomboBox.Location = new Point(98, 20); + PackageСomboBox.Name = "PackageСomboBox"; + PackageСomboBox.Size = new Size(184, 23); + PackageСomboBox.TabIndex = 4; + // + // PackageLabel + // + PackageLabel.AutoSize = true; + PackageLabel.Location = new Point(22, 23); + PackageLabel.Name = "PackageLabel"; + PackageLabel.Size = new Size(56, 15); + PackageLabel.TabIndex = 3; + PackageLabel.Text = "Изделие:"; + // + // CountTextBox + // + CountTextBox.Location = new Point(98, 58); + CountTextBox.Name = "CountTextBox"; + CountTextBox.Size = new Size(184, 23); + CountTextBox.TabIndex = 5; + // + // ButtonCancel + // + ButtonCancel.Location = new Point(230, 92); + ButtonCancel.Name = "ButtonCancel"; + ButtonCancel.Size = new Size(97, 29); + ButtonCancel.TabIndex = 7; + ButtonCancel.Text = "Отмена"; + ButtonCancel.UseVisualStyleBackColor = true; + ButtonCancel.Click += ButtonCancel_Click; + // + // SaveButton + // + SaveButton.Location = new Point(127, 92); + SaveButton.Name = "SaveButton"; + SaveButton.Size = new Size(97, 29); + SaveButton.TabIndex = 6; + SaveButton.Text = "Сохранить"; + SaveButton.UseVisualStyleBackColor = true; + SaveButton.Click += SaveButton_Click; + // + // FormSellPackage + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(339, 128); + Controls.Add(ButtonCancel); + Controls.Add(SaveButton); + Controls.Add(CountTextBox); + Controls.Add(PackageСomboBox); + Controls.Add(PackageLabel); + Controls.Add(CountLabel); + Name = "FormSellPackage"; + Text = "Продать изделие"; + Load += FormSellPackage_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private Label CountLabel; + private ComboBox PackageСomboBox; + private Label PackageLabel; + private TextBox CountTextBox; + private Button ButtonCancel; + private Button SaveButton; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs new file mode 100644 index 0000000..8466957 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.cs @@ -0,0 +1,101 @@ +using Microsoft.Extensions.Logging; +using SoftwareInstallationContracts.BusinessLogicsContracts; +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(CountTextBox.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(CountTextBox.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/SoftwareInstallationView/FormSellPackage.resx b/SoftwareInstallation/SoftwareInstallationView/FormSellPackage.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationView/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/SoftwareInstallationView/FormShop.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs index 505e28a..8244c1e 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs @@ -35,12 +35,15 @@ textBoxAddress = new TextBox(); openingDatePicker = new DateTimePicker(); dataGridView = new DataGridView(); - buttonSave = new Button(); - buttonCancel = new Button(); PackageName = new DataGridViewTextBoxColumn(); Price = new DataGridViewTextBoxColumn(); Count = new DataGridViewTextBoxColumn(); + buttonSave = new Button(); + buttonCancel = new Button(); + labelNumeric = new Label(); + numericUpDown = new NumericUpDown(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDown).BeginInit(); SuspendLayout(); // // labelNameShop @@ -86,7 +89,7 @@ // // openingDatePicker // - openingDatePicker.Location = new Point(334, 36); + openingDatePicker.Location = new Point(334, 27); openingDatePicker.Name = "openingDatePicker"; openingDatePicker.Size = new Size(175, 23); openingDatePicker.TabIndex = 5; @@ -100,6 +103,21 @@ dataGridView.Size = new Size(601, 242); dataGridView.TabIndex = 6; // + // PackageName + // + PackageName.HeaderText = "Изделие"; + PackageName.Name = "PackageName"; + // + // Price + // + Price.HeaderText = "Цена"; + Price.Name = "Price"; + // + // Count + // + Count.HeaderText = "Количество"; + Count.Name = "Count"; + // // buttonSave // buttonSave.Location = new Point(384, 376); @@ -120,26 +138,30 @@ buttonCancel.UseVisualStyleBackColor = true; buttonCancel.Click += buttonCancel_Click; // - // PackageName + // labelNumeric // - PackageName.HeaderText = "Изделие"; - PackageName.Name = "PackageName"; + labelNumeric.AutoSize = true; + labelNumeric.Location = new Point(334, 64); + labelNumeric.Name = "labelNumeric"; + labelNumeric.Size = new Size(129, 15); + labelNumeric.TabIndex = 12; + labelNumeric.Text = "Макс. кол-во товара: "; // - // Price + // numericUpDown // - Price.HeaderText = "Цена"; - Price.Name = "Price"; - // - // Count - // - Count.HeaderText = "Количество"; - Count.Name = "Count"; + numericUpDown.Location = new Point(334, 83); + numericUpDown.Margin = new Padding(3, 2, 3, 2); + numericUpDown.Name = "numericUpDown"; + numericUpDown.Size = new Size(131, 23); + numericUpDown.TabIndex = 13; // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(629, 417); + Controls.Add(numericUpDown); + Controls.Add(labelNumeric); Controls.Add(buttonCancel); Controls.Add(buttonSave); Controls.Add(dataGridView); @@ -150,9 +172,10 @@ Controls.Add(labelAddress); Controls.Add(labelNameShop); Name = "FormShop"; - Text = "FormShop"; + Text = "Магазин"; Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ((System.ComponentModel.ISupportInitialize)numericUpDown).EndInit(); ResumeLayout(false); PerformLayout(); } @@ -171,5 +194,7 @@ private DataGridViewTextBoxColumn PackageName; private DataGridViewTextBoxColumn Price; private DataGridViewTextBoxColumn Count; + private Label labelNumeric; + private NumericUpDown numericUpDown; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx index e591603..5389e21 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx @@ -126,4 +126,13 @@ True + + True + + + True + + + True + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs index cbf6f9e..1f86d48 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShops.Designer.cs @@ -95,7 +95,7 @@ Controls.Add(buttonAdd); Controls.Add(dataGridView); Name = "FormShops"; - Text = "FormShops"; + Text = "Магазины"; Load += FormShops_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); -- 2.25.1 From d6a4ef895eaee9310f6211028d8a20f9b1e2b06a Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Mon, 22 Apr 2024 23:05:38 +0400 Subject: [PATCH 06/10] poka shlyapa --- SoftwareInstallation/SoftwareInstallationView/FormShop.cs | 7 ++++--- SoftwareInstallation/SoftwareInstallationView/FormShops.cs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs index 31d24da..39ac5d7 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.cs @@ -20,6 +20,7 @@ namespace SoftwareInstallationView { public partial class FormShop : Form { + private readonly List? _listShops; /// /// Логгер /// @@ -46,8 +47,8 @@ namespace SoftwareInstallationView { InitializeComponent(); _logger = logger; + _listShops = new (); _logic = logic; - _shopPackages = new Dictionary(); } /// /// Загрузка списка изделий в магазине @@ -67,7 +68,7 @@ namespace SoftwareInstallationView textBoxShop.Text = view.Name; textBoxAddress.Text = view.Address; openingDatePicker.Text = view.DateOpening.ToString(); - _shopPackages = view.Packages ?? new Dictionary(); + _shopPackages = view.ShopPackages ?? new Dictionary(); LoadData(); } } @@ -104,7 +105,7 @@ namespace SoftwareInstallationView Name = textBoxShop.Text, Address = textBoxAddress.Text, DateOpening = openingDatePicker.Value.Date, - Packages = _shopPackages + PackageMaxCount = (int)numericUpDown.Value }; var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); if (!operationResult) diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShops.cs b/SoftwareInstallation/SoftwareInstallationView/FormShops.cs index 31b88b1..cdcf2f5 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShops.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShops.cs @@ -41,7 +41,7 @@ namespace SoftwareInstallationView dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["Packages"].Visible = false; + dataGridView.Columns["ShopPackages"].Visible = false; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } -- 2.25.1 From b5d3ccbe9a9f6f3ee73aa89fb7e021c81df23090 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Fri, 3 May 2024 15:31:08 +0400 Subject: [PATCH 07/10] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=20?= =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SoftwareInstallation/SoftwareInstallationView/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/SoftwareInstallation/SoftwareInstallationView/Program.cs b/SoftwareInstallation/SoftwareInstallationView/Program.cs index ac36709..f3ffece 100644 --- a/SoftwareInstallation/SoftwareInstallationView/Program.cs +++ b/SoftwareInstallation/SoftwareInstallationView/Program.cs @@ -53,6 +53,7 @@ namespace SoftwareInstallation services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file -- 2.25.1 From a253a606bd434086d5e2412aec73a3f6e88458cd Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 8 May 2024 12:42:11 +0400 Subject: [PATCH 08/10] =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B8=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormShop.Designer.cs | 44 +++++++++---------- .../SoftwareInstallationView/FormShop.resx | 9 ---- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs index 8244c1e..8570616 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.Designer.cs @@ -35,13 +35,13 @@ textBoxAddress = new TextBox(); openingDatePicker = new DateTimePicker(); dataGridView = new DataGridView(); - PackageName = new DataGridViewTextBoxColumn(); - Price = new DataGridViewTextBoxColumn(); - Count = new DataGridViewTextBoxColumn(); buttonSave = new Button(); buttonCancel = new Button(); labelNumeric = new Label(); numericUpDown = new NumericUpDown(); + Price = new DataGridViewTextBoxColumn(); + PackageName = new DataGridViewTextBoxColumn(); + Count = new DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); ((System.ComponentModel.ISupportInitialize)numericUpDown).BeginInit(); SuspendLayout(); @@ -97,27 +97,12 @@ // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Columns.AddRange(new DataGridViewColumn[] { PackageName, Price, Count }); + dataGridView.Columns.AddRange(new DataGridViewColumn[] { Price, PackageName, Count }); dataGridView.Location = new Point(12, 128); dataGridView.Name = "dataGridView"; dataGridView.Size = new Size(601, 242); dataGridView.TabIndex = 6; // - // PackageName - // - PackageName.HeaderText = "Изделие"; - PackageName.Name = "PackageName"; - // - // Price - // - Price.HeaderText = "Цена"; - Price.Name = "Price"; - // - // Count - // - Count.HeaderText = "Количество"; - Count.Name = "Count"; - // // buttonSave // buttonSave.Location = new Point(384, 376); @@ -155,6 +140,21 @@ numericUpDown.Size = new Size(131, 23); numericUpDown.TabIndex = 13; // + // Price + // + Price.HeaderText = "Номер"; + Price.Name = "Price"; + // + // PackageName + // + PackageName.HeaderText = "Изделие"; + PackageName.Name = "PackageName"; + // + // Count + // + Count.HeaderText = "Количество"; + Count.Name = "Count"; + // // FormShop // AutoScaleDimensions = new SizeF(7F, 15F); @@ -191,10 +191,10 @@ private DataGridView dataGridView; private Button buttonSave; private Button buttonCancel; - private DataGridViewTextBoxColumn PackageName; - private DataGridViewTextBoxColumn Price; - private DataGridViewTextBoxColumn Count; private Label labelNumeric; private NumericUpDown numericUpDown; + private DataGridViewTextBoxColumn PackageName; + private DataGridViewTextBoxColumn Count; + private DataGridViewTextBoxColumn Price; } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx index 5389e21..75726ad 100644 --- a/SoftwareInstallation/SoftwareInstallationView/FormShop.resx +++ b/SoftwareInstallation/SoftwareInstallationView/FormShop.resx @@ -117,21 +117,12 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - True - True - - True - True - - True - True -- 2.25.1 From c700d5dc294c0721a557f262ad563d1e866c08b1 Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Sun, 12 May 2024 23:29:31 +0400 Subject: [PATCH 09/10] =?UTF-8?q?=D0=B5=D1=89=D0=B5=20=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ShopStorage.cs | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs index 96e13bb..526630e 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs @@ -6,6 +6,7 @@ using SoftwareInstallationDataModels.Models; using System; using System.Collections.Generic; using System.Linq; +using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -73,29 +74,48 @@ namespace SoftwareInstallationFileImplement public bool SellPackage(IPackageModel model, int quantity) { - if (source.Shops.Select(x => x.ShopPackages.FirstOrDefault(y => y.Key == model.Id).Value.Item2).Sum() < quantity) + + var package = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (package == null || !CheckCount(model, quantity)) { return false; } - foreach (var Shop in source.Shops.Where(x => x.ShopPackages.ContainsKey(model.Id))) + foreach (var shop in source.Shops) { - int QuantityInCurrentShop = Shop.ShopPackages[model.Id].Item2; - if (QuantityInCurrentShop <= quantity) + var packages = shop.ShopPackages; + foreach (var elem in packages.Where(x => x.Value.Item1.Id == package.Id)) { - Shop.ShopPackages.Remove(model.Id); - quantity -= QuantityInCurrentShop; + var selling = Math.Min(elem.Value.Item2, quantity); + packages[elem.Value.Item1.Id] = (elem.Value.Item1, elem.Value.Item2 - selling); + quantity -= selling; + + if (quantity <= 0) + { + break; + } } - else + + shop.Update(new ShopBindingModel { - Shop.ShopPackages[model.Id] = (Shop.ShopPackages[model.Id].Item1, QuantityInCurrentShop - quantity); - quantity = 0; - } - if (quantity == 0) - { - return true; - } + Id = model.Id, + Name = shop.Name, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopPackages = packages, + PackageMaxCount = shop.PackageMaxCount + }); } - return false; + source.SaveShops(); + return true; + + } + public bool CheckCount(IPackageModel model, int quantity) + { + int store = source.Shops + .Select(x => x.ShopPackages + .Select(y => (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)) + .Sum()).Sum(); + return store >= quantity; } public ShopViewModel? Update(ShopBindingModel model) -- 2.25.1 From 14adec6f170c8d8b7f8ea95af6fb96536f2480ee Mon Sep 17 00:00:00 2001 From: kaznacheeva Date: Wed, 22 May 2024 11:37:22 +0400 Subject: [PATCH 10/10] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BB=D0=B0=20=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D1=83=D1=8E=20=D1=83=D1=81=D0=BB=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SoftwareInstallationFileImplement/ShopStorage.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs index 526630e..f7eea17 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/ShopStorage.cs @@ -112,9 +112,9 @@ namespace SoftwareInstallationFileImplement public bool CheckCount(IPackageModel model, int quantity) { int store = source.Shops - .Select(x => x.ShopPackages - .Select(y => (y.Value.Item1.Id == model.Id ? y.Value.Item2 : 0)) - .Sum()).Sum(); + .SelectMany(x => x.ShopPackages) + .Where(y => y.Value.Item1.Id == model.Id && y.Value.Item2 > 0) + .Sum(y => y.Value.Item2); return store >= quantity; } -- 2.25.1