From 6d70c6edc75eb031c28d5dc6c5294481730a336c Mon Sep 17 00:00:00 2001 From: bocchanskyy Date: Sun, 18 Feb 2024 12:22:10 +0400 Subject: [PATCH 1/3] guess I'm done --- .../BusinessLogics/ShopLogic.cs | 180 ++++++++++++++++ .../BindingModels/ShopBindingModel.cs | 18 ++ .../BusinessLogicContracts/IShopLogic.cs | 22 ++ .../SearchModels/ShopSearchModel.cs | 14 ++ .../StorageContracts/IShopStorage.cs | 21 ++ .../ViewModels/ShopViewModel.cs | 23 ++ .../DataListSingleton.cs | 2 + .../Implements/ShopStorage.cs | 124 +++++++++++ .../ComputersShopListImplement/Models/Shop.cs | 59 +++++ .../ComputersShopView/FormMain.Designer.cs | 40 +++- ComputersShop/ComputersShopView/FormMain.cs | 20 +- .../ComputersShopView/FormShop.Designer.cs | 201 ++++++++++++++++++ ComputersShop/ComputersShopView/FormShop.cs | 126 +++++++++++ ComputersShop/ComputersShopView/FormShop.resx | 60 ++++++ .../FormShopReplenishment.Designer.cs | 142 +++++++++++++ .../FormShopReplenishment.cs | 104 +++++++++ .../FormShopReplenishment.resx | 60 ++++++ .../ComputersShopView/FormShops.Designer.cs | 114 ++++++++++ ComputersShop/ComputersShopView/FormShops.cs | 122 +++++++++++ .../ComputersShopView/FormShops.resx | 60 ++++++ ComputersShop/ComputersShopView/Program.cs | 9 +- .../Models/IShopModel.cs | 16 ++ 22 files changed, 1523 insertions(+), 14 deletions(-) create mode 100644 ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs create mode 100644 ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs create mode 100644 ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs create mode 100644 ComputersShop/ComputersShopContracts/SearchModels/ShopSearchModel.cs create mode 100644 ComputersShop/ComputersShopContracts/StorageContracts/IShopStorage.cs create mode 100644 ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs create mode 100644 ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs create mode 100644 ComputersShop/ComputersShopListImplement/Models/Shop.cs create mode 100644 ComputersShop/ComputersShopView/FormShop.Designer.cs create mode 100644 ComputersShop/ComputersShopView/FormShop.cs create mode 100644 ComputersShop/ComputersShopView/FormShop.resx create mode 100644 ComputersShop/ComputersShopView/FormShopReplenishment.Designer.cs create mode 100644 ComputersShop/ComputersShopView/FormShopReplenishment.cs create mode 100644 ComputersShop/ComputersShopView/FormShopReplenishment.resx create mode 100644 ComputersShop/ComputersShopView/FormShops.Designer.cs create mode 100644 ComputersShop/ComputersShopView/FormShops.cs create mode 100644 ComputersShop/ComputersShopView/FormShops.resx create mode 100644 ComputersShop/СomputersShopDataModels/Models/IShopModel.cs diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs new file mode 100644 index 0000000..2d963b1 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/ShopLogic.cs @@ -0,0 +1,180 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic.BusinessLogics +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + public ShopLogic(ILogger logger, IShopStorage ShopStorage) + { + _logger = logger; + _shopStorage = ShopStorage; + } + public bool AddComputer(ShopSearchModel model, IComputerModel computer, int quantity) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (quantity <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity)); + } + + _logger.LogInformation("AddComputerInShop. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id); + var element = _shopStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("AddComputerInShop element not found"); + return false; + } + + _logger.LogInformation("AddComputerInShop find. Id:{Id}", element.Id); + + if (element.Computers.TryGetValue(computer.Id, out var pair)) + { + element.Computers[computer.Id] = (computer, quantity + pair.Item2); + _logger.LogInformation("AddComputerInShop. Has been added {quantity} {Computer} in {ShopName}", quantity, computer.ComputerName, element.ShopName); + } + else + { + element.Computers[computer.Id] = (computer, quantity); + _logger.LogInformation("AddPastryInShop. Has been added {quantity} new Computer {Computer} in {ShopName}", quantity, computer.ComputerName, element.ShopName); + } + + _shopStorage.Update(new() + { + Id = element.Id, + ShopAddress = element.ShopAddress, + ShopName = element.ShopName, + DateOpening = element.DateOpening, + Computers = element.Computers + }); + return true; + } + + public bool Create(ShopBindingModel model) + { + CheckModel(model); + model.Computers = new(); + + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + + return true; + } + + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", model.ShopName, model.Id); + var element = _shopStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id} ", model?.ShopName, model?.Id); + + var list = (model == null) ? _shopStorage.GetFullList() : _shopStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Update(ShopBindingModel model) + { + CheckModel(model, false); + + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); + } + + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (!withParams) + { + return; + } + + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Нет названия магазина", nameof(model.ShopName)); + } + + _logger.LogInformation("Shop. ShopName:{0}.ShopAdress:{1}. Id: {2}", model.ShopName, model.ShopAddress, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + + if (element != null && element.Id != model.Id && element.ShopName == model.ShopName) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..59d6ff3 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,18 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BindingModels +{ + public class ShopBindingModel : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string ShopAddress { get; set; } = string.Empty; + public DateTime DateOpening { get; set; } = DateTime.Now; + public Dictionary Computers { get; set; } = new(); + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs new file mode 100644 index 0000000..a1f8951 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IShopLogic.cs @@ -0,0 +1,22 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BusinessLogicContracts +{ + public interface IShopLogic + { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool AddComputer(ShopSearchModel model, IComputerModel computer, int quantity); + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopContracts/SearchModels/ShopSearchModel.cs b/ComputersShop/ComputersShopContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..4428974 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? ShopName { get; set; } + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopContracts/StorageContracts/IShopStorage.cs b/ComputersShop/ComputersShopContracts/StorageContracts/IShopStorage.cs new file mode 100644 index 0000000..74abeaf --- /dev/null +++ b/ComputersShop/ComputersShopContracts/StorageContracts/IShopStorage.cs @@ -0,0 +1,21 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.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); + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..62d4e1a --- /dev/null +++ b/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,23 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.ViewModels +{ + public class ShopViewModel : IShopModel + { + public Dictionary Computers { get; set; } = new(); + public int Id { get; set; } + + [DisplayName("Название магазина")] + public string ShopName { get; set; } = string.Empty; + [DisplayName("Адрес магазина")] + public string ShopAddress { get; set; } = string.Empty; + [DisplayName("Дата открытия")] + public DateTime DateOpening { get; set; } = DateTime.Now; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopListImplement/DataListSingleton.cs b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs index ea77028..6224deb 100644 --- a/ComputersShop/ComputersShopListImplement/DataListSingleton.cs +++ b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs @@ -14,11 +14,13 @@ namespace ComputersShopListImplement public List Components { get; set; } public List Orders { get; set; } public List Computers { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Computers = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..83e4af7 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/ShopStorage.cs @@ -0,0 +1,124 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Implements +{ + 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.ShopName) && !model.Id.HasValue) + { + return null; + } + + foreach (var Shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.ShopName) && Shop.ShopName == model.ShopName) || (model.Id.HasValue && Shop.Id == model.Id)) + { + return Shop.GetViewModel; + } + } + + return null; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + + foreach (var Shop in _source.Shops) + { + if (Shop.ShopName.Contains(model.ShopName)) + { + result.Add(Shop.GetViewModel); + } + } + + return result; + } + + public 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; + } + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopListImplement/Models/Shop.cs b/ComputersShop/ComputersShopListImplement/Models/Shop.cs new file mode 100644 index 0000000..7631bda --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Shop.cs @@ -0,0 +1,59 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Shop : IShopModel + { + public string ShopName { get; private set; } = string.Empty; + public string ShopAddress { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary Computers { 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, + ShopName = model.ShopName, + ShopAddress = model.ShopAddress, + DateOpening = model.DateOpening, + Computers = new() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + ShopAddress = model.ShopAddress; + DateOpening = model.DateOpening; + Computers = model.Computers; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAddress = ShopAddress, + DateOpening = DateOpening, + Computers = Computers + }; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormMain.Designer.cs b/ComputersShop/ComputersShopView/FormMain.Designer.cs index 30ff138..70c4eb8 100644 --- a/ComputersShop/ComputersShopView/FormMain.Designer.cs +++ b/ComputersShop/ComputersShopView/FormMain.Designer.cs @@ -29,9 +29,11 @@ private void InitializeComponent() { this.menuStrip = new System.Windows.Forms.MenuStrip(); - this.справочникToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.directoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.computerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.componentsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.shopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.replenishmentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); @@ -45,21 +47,23 @@ // menuStrip // this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.справочникToolStripMenuItem}); + this.directoryToolStripMenuItem, + this.replenishmentToolStripMenuItem}); this.menuStrip.Location = new System.Drawing.Point(0, 0); this.menuStrip.Name = "menuStrip"; this.menuStrip.Size = new System.Drawing.Size(1047, 24); this.menuStrip.TabIndex = 0; this.menuStrip.Text = "menuStrip1"; // - // справочникToolStripMenuItem + // directoryToolStripMenuItem // - this.справочникToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.directoryToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.computerToolStripMenuItem, - this.componentsToolStripMenuItem}); - this.справочникToolStripMenuItem.Name = "справочникToolStripMenuItem"; - this.справочникToolStripMenuItem.Size = new System.Drawing.Size(94, 20); - this.справочникToolStripMenuItem.Text = "Cправочники"; + this.componentsToolStripMenuItem, + this.shopsToolStripMenuItem}); + this.directoryToolStripMenuItem.Name = "directoryToolStripMenuItem"; + this.directoryToolStripMenuItem.Size = new System.Drawing.Size(94, 20); + this.directoryToolStripMenuItem.Text = "Cправочники"; // // computerToolStripMenuItem // @@ -75,6 +79,20 @@ this.componentsToolStripMenuItem.Text = "Компоненты"; this.componentsToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); // + // shopsToolStripMenuItem + // + this.shopsToolStripMenuItem.Name = "shopsToolStripMenuItem"; + this.shopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.shopsToolStripMenuItem.Text = "Магазины"; + this.shopsToolStripMenuItem.Click += new System.EventHandler(this.ShopToolStripMenuItem_Click); + // + // replenishmentToolStripMenuItem + // + this.replenishmentToolStripMenuItem.Name = "replenishmentToolStripMenuItem"; + this.replenishmentToolStripMenuItem.Size = new System.Drawing.Size(143, 20); + this.replenishmentToolStripMenuItem.Text = "Пополнение магазина"; + this.replenishmentToolStripMenuItem.Click += new System.EventHandler(this.ShopReplenishmentToolStripMenuItem_Click); + // // dataGridView // this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -161,7 +179,7 @@ #endregion private MenuStrip menuStrip; - private ToolStripMenuItem справочникToolStripMenuItem; + private ToolStripMenuItem directoryToolStripMenuItem; private DataGridView dataGridView; private Button buttonCreateOrder; private Button buttonTakeOrderInWork; @@ -170,5 +188,7 @@ private Button buttonRef; private ToolStripMenuItem computerToolStripMenuItem; private ToolStripMenuItem componentsToolStripMenuItem; - } + private ToolStripMenuItem replenishmentToolStripMenuItem; + private ToolStripMenuItem shopsToolStripMenuItem; + } } \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormMain.cs b/ComputersShop/ComputersShopView/FormMain.cs index c660b1a..2d588c3 100644 --- a/ComputersShop/ComputersShopView/FormMain.cs +++ b/ComputersShop/ComputersShopView/FormMain.cs @@ -65,7 +65,15 @@ namespace ComputersShopView form.ShowDialog(); } } - private void ButtonCreateOrder_Click(object sender, EventArgs e) + private void ShopToolStripMenuItem_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)); if (service is FormCreateOrder form) @@ -169,5 +177,13 @@ namespace ComputersShopView { LoadData(); } - } + private void ShopReplenishmentToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopReplenishment)); + if (service is FormShopReplenishment form) + { + form.ShowDialog(); + } + } + } } diff --git a/ComputersShop/ComputersShopView/FormShop.Designer.cs b/ComputersShop/ComputersShopView/FormShop.Designer.cs new file mode 100644 index 0000000..7ded334 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShop.Designer.cs @@ -0,0 +1,201 @@ +namespace ComputersShopView +{ + 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() + { + this.dateTimePicker = new System.Windows.Forms.DateTimePicker(); + this.textBoxName = new System.Windows.Forms.TextBox(); + this.textBoxAddress = new System.Windows.Forms.TextBox(); + this.labelTime = new System.Windows.Forms.Label(); + this.labelAddress = new System.Windows.Forms.Label(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.ColumnID = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnManufactureName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.Price = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.ColumnCount = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.labelShop = new System.Windows.Forms.Label(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dateTimePicker + // + this.dateTimePicker.Location = new System.Drawing.Point(384, 27); + this.dateTimePicker.Name = "dateTimePicker"; + this.dateTimePicker.Size = new System.Drawing.Size(207, 23); + this.dateTimePicker.TabIndex = 26; + // + // textBoxName + // + this.textBoxName.Location = new System.Drawing.Point(10, 27); + this.textBoxName.Name = "textBoxName"; + this.textBoxName.Size = new System.Drawing.Size(141, 23); + this.textBoxName.TabIndex = 25; + // + // textBoxAddress + // + this.textBoxAddress.Location = new System.Drawing.Point(158, 27); + this.textBoxAddress.Name = "textBoxAddress"; + this.textBoxAddress.Size = new System.Drawing.Size(221, 23); + this.textBoxAddress.TabIndex = 24; + // + // labelTime + // + this.labelTime.AutoSize = true; + this.labelTime.Location = new System.Drawing.Point(384, 9); + this.labelTime.Name = "labelTime"; + this.labelTime.Size = new System.Drawing.Size(87, 15); + this.labelTime.TabIndex = 23; + this.labelTime.Text = "Дата открытия"; + // + // labelAddress + // + this.labelAddress.AutoSize = true; + this.labelAddress.Location = new System.Drawing.Point(158, 9); + this.labelAddress.Name = "labelAddress"; + this.labelAddress.Size = new System.Drawing.Size(40, 15); + this.labelAddress.TabIndex = 22; + this.labelAddress.Text = "Адрес"; + // + // dataGridView + // + this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.ColumnID, + this.ColumnManufactureName, + this.Price, + this.ColumnCount}); + this.dataGridView.Location = new System.Drawing.Point(10, 56); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowHeadersWidth = 62; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(581, 327); + this.dataGridView.TabIndex = 21; + // + // ColumnID + // + this.ColumnID.HeaderText = "ID"; + this.ColumnID.MinimumWidth = 8; + this.ColumnID.Name = "ColumnID"; + this.ColumnID.Visible = false; + this.ColumnID.Width = 150; + // + // ColumnManufactureName + // + this.ColumnManufactureName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.ColumnManufactureName.HeaderText = "Название компьютера"; + this.ColumnManufactureName.MinimumWidth = 8; + this.ColumnManufactureName.Name = "ColumnManufactureName"; + // + // Price + // + this.Price.HeaderText = "Цена"; + this.Price.MinimumWidth = 6; + this.Price.Name = "Price"; + this.Price.Width = 125; + // + // ColumnCount + // + this.ColumnCount.HeaderText = "Количество"; + this.ColumnCount.MinimumWidth = 8; + this.ColumnCount.Name = "ColumnCount"; + this.ColumnCount.Width = 150; + // + // labelShop + // + this.labelShop.AutoSize = true; + this.labelShop.Location = new System.Drawing.Point(10, 9); + this.labelShop.Name = "labelShop"; + this.labelShop.Size = new System.Drawing.Size(54, 15); + this.labelShop.TabIndex = 20; + this.labelShop.Text = "Магазин"; + // + // buttonSave + // + this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSave.Location = new System.Drawing.Point(363, 392); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(120, 22); + this.buttonSave.TabIndex = 28; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + // + // buttonCancel + // + this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.Location = new System.Drawing.Point(488, 392); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(103, 22); + this.buttonCancel.TabIndex = 27; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + // + // FormShop + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(616, 425); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.dateTimePicker); + this.Controls.Add(this.textBoxName); + this.Controls.Add(this.textBoxAddress); + this.Controls.Add(this.labelTime); + this.Controls.Add(this.labelAddress); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.labelShop); + this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); + this.Name = "FormShop"; + this.Text = "Магазин"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private DateTimePicker dateTimePicker; + private TextBox textBoxName; + private TextBox textBoxAddress; + private Label labelTime; + private Label labelAddress; + private DataGridView dataGridView; + private Label labelShop; + private Button buttonSave; + private Button buttonCancel; + private DataGridViewTextBoxColumn ColumnID; + private DataGridViewTextBoxColumn ColumnManufactureName; + private DataGridViewTextBoxColumn Price; + private DataGridViewTextBoxColumn ColumnCount; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormShop.cs b/ComputersShop/ComputersShopView/FormShop.cs new file mode 100644 index 0000000..6a1fda4 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShop.cs @@ -0,0 +1,126 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ComputersShopView +{ + public partial class FormShop : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + private int? _id; + private Dictionary _shopComputers; + public int Id { set { _id = value; } } + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + _shopComputers = new Dictionary(); + } + private void FormShop_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + _logger.LogInformation("Загрузка магазина"); + try + { + var view = _logic.ReadElement(new ShopSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ShopName; + textBoxAddress.Text = view.ShopAddress.ToString(); + dateTimePicker.Text = view.DateOpening.ToString(); + _shopComputers = view.Computers ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void LoadData() + { + _logger.LogInformation("Загрузка компонент магазина"); + try + { + if (_shopComputers != null) + { + dataGridView.Rows.Clear(); + foreach (var pc in _shopComputers) + { + dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComputerName, pc.Value.Item1.Price, pc.Value.Item2 }); + } + } + } + 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(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxAddress.Text)) + { + MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxName.Text, + ShopAddress = textBoxAddress.Text, + DateOpening = dateTimePicker.Value.Date, + Computers = _shopComputers + }; + var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/ComputersShop/ComputersShopView/FormShop.resx b/ComputersShop/ComputersShopView/FormShop.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShop.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormShopReplenishment.Designer.cs b/ComputersShop/ComputersShopView/FormShopReplenishment.Designer.cs new file mode 100644 index 0000000..d9374c3 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShopReplenishment.Designer.cs @@ -0,0 +1,142 @@ +namespace ComputersShopView +{ + partial class FormShopReplenishment + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.ShopNameLabel = new System.Windows.Forms.Label(); + this.ComputerNameLabel = new System.Windows.Forms.Label(); + this.CountLabel = new System.Windows.Forms.Label(); + this.сomboBoxShopName = new System.Windows.Forms.ComboBox(); + this.comboBoxComputerName = new System.Windows.Forms.ComboBox(); + this.CountTextBox = new System.Windows.Forms.TextBox(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // ShopNameLabel + // + this.ShopNameLabel.AutoSize = true; + this.ShopNameLabel.Location = new System.Drawing.Point(12, 9); + this.ShopNameLabel.Name = "ShopNameLabel"; + this.ShopNameLabel.Size = new System.Drawing.Size(119, 15); + this.ShopNameLabel.TabIndex = 0; + this.ShopNameLabel.Text = "Название магазина: "; + // + // ComputerNameLabel + // + this.ComputerNameLabel.AutoSize = true; + this.ComputerNameLabel.Location = new System.Drawing.Point(12, 37); + this.ComputerNameLabel.Name = "ComputerNameLabel"; + this.ComputerNameLabel.Size = new System.Drawing.Size(137, 15); + this.ComputerNameLabel.TabIndex = 1; + this.ComputerNameLabel.Text = "Название компьютера: "; + // + // CountLabel + // + this.CountLabel.AutoSize = true; + this.CountLabel.Location = new System.Drawing.Point(12, 66); + this.CountLabel.Name = "CountLabel"; + this.CountLabel.Size = new System.Drawing.Size(78, 15); + this.CountLabel.TabIndex = 2; + this.CountLabel.Text = "Количество: "; + // + // сomboBoxShopName + // + this.сomboBoxShopName.FormattingEnabled = true; + this.сomboBoxShopName.Location = new System.Drawing.Point(155, 6); + this.сomboBoxShopName.Name = "сomboBoxShopName"; + this.сomboBoxShopName.Size = new System.Drawing.Size(192, 23); + this.сomboBoxShopName.TabIndex = 3; + // + // comboBoxComputerName + // + this.comboBoxComputerName.FormattingEnabled = true; + this.comboBoxComputerName.Location = new System.Drawing.Point(155, 35); + this.comboBoxComputerName.Name = "comboBoxComputerName"; + this.comboBoxComputerName.Size = new System.Drawing.Size(192, 23); + this.comboBoxComputerName.TabIndex = 4; + // + // CountTextBox + // + this.CountTextBox.Location = new System.Drawing.Point(155, 64); + this.CountTextBox.Name = "CountTextBox"; + this.CountTextBox.Size = new System.Drawing.Size(192, 23); + this.CountTextBox.TabIndex = 5; + // + // buttonSave + // + this.buttonSave.Location = new System.Drawing.Point(191, 108); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(75, 23); + this.buttonSave.TabIndex = 6; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Location = new System.Drawing.Point(272, 108); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(75, 23); + this.buttonCancel.TabIndex = 7; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormShopReplenishment + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(359, 150); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.CountTextBox); + this.Controls.Add(this.comboBoxComputerName); + this.Controls.Add(this.сomboBoxShopName); + this.Controls.Add(this.CountLabel); + this.Controls.Add(this.ComputerNameLabel); + this.Controls.Add(this.ShopNameLabel); + this.Name = "FormShopReplenishment"; + this.Text = "Пополнение магазина"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label ShopNameLabel; + private Label ComputerNameLabel; + private Label CountLabel; + private ComboBox сomboBoxShopName; + private ComboBox comboBoxComputerName; + private TextBox CountTextBox; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormShopReplenishment.cs b/ComputersShop/ComputersShopView/FormShopReplenishment.cs new file mode 100644 index 0000000..270574d --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShopReplenishment.cs @@ -0,0 +1,104 @@ +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.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; + +namespace ComputersShopView +{ + public partial class FormShopReplenishment : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _shopLogic; + private readonly IComputerLogic _computerLogic; + private readonly List? _listStores; + private readonly List? _listcomputers; + public FormShopReplenishment(ILogger logger, IShopLogic shopLogic, IComputerLogic computerLogic) + { + InitializeComponent(); + _shopLogic = shopLogic; + _computerLogic = computerLogic; + _logger = logger; + _listStores = shopLogic.ReadList(null); + if (_listStores != null) + { + сomboBoxShopName.DisplayMember = "ShopName"; + сomboBoxShopName.ValueMember = "Id"; + сomboBoxShopName.DataSource = _listStores; + сomboBoxShopName.SelectedItem = null; + } + + _listcomputers = computerLogic.ReadList(null); + if (_listcomputers != null) + { + comboBoxComputerName.DisplayMember = "ComputerName"; + comboBoxComputerName.ValueMember = "Id"; + comboBoxComputerName.DataSource = _listcomputers; + comboBoxComputerName.SelectedItem = null; + } + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + if (сomboBoxShopName.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + if (comboBoxComputerName.SelectedValue == null) + { + MessageBox.Show("Выберите компьютер", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + _logger.LogInformation("Добавление компьютер в магазин"); + + try + { + var computer = _computerLogic.ReadElement(new() + { + Id = (int)comboBoxComputerName.SelectedValue + }); + + if (computer == null) + { + throw new Exception("Не найден компьютер. Дополнительная информация в логах."); + } + + var resultOperation = _shopLogic.AddComputer( + model: new() { Id = (int)сomboBoxShopName.SelectedValue }, + computer: computer, + quantity: Convert.ToInt32(CountTextBox.Text) + ); + + 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/ComputersShop/ComputersShopView/FormShopReplenishment.resx b/ComputersShop/ComputersShopView/FormShopReplenishment.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShopReplenishment.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormShops.Designer.cs b/ComputersShop/ComputersShopView/FormShops.Designer.cs new file mode 100644 index 0000000..7a0a092 --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShops.Designer.cs @@ -0,0 +1,114 @@ +namespace ComputersShopView +{ + 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() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.buttonChange = new System.Windows.Forms.Button(); + this.buttonDelete = new System.Windows.Forms.Button(); + this.buttonUpdate = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(539, 426); + this.dataGridView.TabIndex = 0; + // + // buttonAdd + // + this.buttonAdd.Location = new System.Drawing.Point(585, 12); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(121, 40); + this.buttonAdd.TabIndex = 1; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.AddButton_Click); + // + // buttonChange + // + this.buttonChange.Location = new System.Drawing.Point(585, 67); + this.buttonChange.Name = "buttonChange"; + this.buttonChange.Size = new System.Drawing.Size(121, 40); + this.buttonChange.TabIndex = 2; + this.buttonChange.Text = "Изменить"; + this.buttonChange.UseVisualStyleBackColor = true; + this.buttonChange.Click += new System.EventHandler(this.ChangeButton_Click); + // + // buttonDelete + // + this.buttonDelete.Location = new System.Drawing.Point(585, 122); + this.buttonDelete.Name = "buttonDelete"; + this.buttonDelete.Size = new System.Drawing.Size(121, 40); + this.buttonDelete.TabIndex = 3; + this.buttonDelete.Text = "Удалить"; + this.buttonDelete.UseVisualStyleBackColor = true; + this.buttonDelete.Click += new System.EventHandler(this.DeleteButton_Click); + // + // buttonUpdate + // + this.buttonUpdate.Location = new System.Drawing.Point(585, 179); + this.buttonUpdate.Name = "buttonUpdate"; + this.buttonUpdate.Size = new System.Drawing.Size(121, 40); + this.buttonUpdate.TabIndex = 4; + this.buttonUpdate.Text = "Обновить"; + this.buttonUpdate.UseVisualStyleBackColor = true; + this.buttonUpdate.Click += new System.EventHandler(this.UpdateButton_Click); + // + // FormShops + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(718, 450); + this.Controls.Add(this.buttonUpdate); + this.Controls.Add(this.buttonDelete); + this.Controls.Add(this.buttonChange); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormShops"; + this.Text = "Магазины"; + this.Load += new System.EventHandler(this.FormShops_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonChange; + private Button buttonDelete; + private Button buttonUpdate; + } +} \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/FormShops.cs b/ComputersShop/ComputersShopView/FormShops.cs new file mode 100644 index 0000000..f7eaa6c --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShops.cs @@ -0,0 +1,122 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ComputersShopView +{ + public partial class FormShops : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public FormShops(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormShops_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ShopName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Computers"].Visible = false; + } + + _logger.LogInformation("Загрузка магазинов"); + + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void UpdateButton_Click(object sender, EventArgs e) + { + LoadData(); + } + + private void DeleteButton_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление магазина"); + + try + { + if (!_logic.Delete(new ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + + private void ChangeButton_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 AddButton_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShop)); + + if (service is FormShop form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } +} diff --git a/ComputersShop/ComputersShopView/FormShops.resx b/ComputersShop/ComputersShopView/FormShops.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/ComputersShop/ComputersShopView/FormShops.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index a93980f..346b608 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -38,18 +38,23 @@ namespace ComputersShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - } + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + } } } \ No newline at end of file diff --git a/ComputersShop/СomputersShopDataModels/Models/IShopModel.cs b/ComputersShop/СomputersShopDataModels/Models/IShopModel.cs new file mode 100644 index 0000000..2ce1e52 --- /dev/null +++ b/ComputersShop/СomputersShopDataModels/Models/IShopModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels.Models +{ + public interface IShopModel : IId + { + public string ShopName { get; } + public string ShopAddress { get; } + DateTime DateOpening { get; } + Dictionary Computers { get; } + } +} \ No newline at end of file From ddca26075644085e39152f70baffbf325baa70fc Mon Sep 17 00:00:00 2001 From: PIbd-13_Valiullin Date: Tue, 20 Feb 2024 12:41:10 +0400 Subject: [PATCH 2/3] quick fixes in FormShop --- ComputersShop/ComputersShopView/FormShop.Designer.cs | 7 +++++++ ComputersShop/ComputersShopView/FormShop.cs | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ComputersShop/ComputersShopView/FormShop.Designer.cs b/ComputersShop/ComputersShopView/FormShop.Designer.cs index 7ded334..ac4e4e6 100644 --- a/ComputersShop/ComputersShopView/FormShop.Designer.cs +++ b/ComputersShop/ComputersShopView/FormShop.Designer.cs @@ -148,6 +148,7 @@ this.buttonSave.TabIndex = 28; this.buttonSave.Text = "Сохранить"; this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += ButtonSave_Click; // // buttonCancel // @@ -158,6 +159,7 @@ this.buttonCancel.TabIndex = 27; this.buttonCancel.Text = "Отмена"; this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += ButtonCancel_Click; // // FormShop // @@ -182,6 +184,11 @@ } + private void ButtonCancel_Click1(object sender, EventArgs e) + { + throw new NotImplementedException(); + } + #endregion private DateTimePicker dateTimePicker; diff --git a/ComputersShop/ComputersShopView/FormShop.cs b/ComputersShop/ComputersShopView/FormShop.cs index 6a1fda4..305f2ac 100644 --- a/ComputersShop/ComputersShopView/FormShop.cs +++ b/ComputersShop/ComputersShopView/FormShop.cs @@ -88,7 +88,7 @@ namespace ComputersShopView } if (string.IsNullOrEmpty(textBoxAddress.Text)) { - MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } _logger.LogInformation("Сохранение магазина"); From fad2d2c6651cd65608faeb72e8f5ecdba1e8409f Mon Sep 17 00:00:00 2001 From: bocchanskyy Date: Mon, 18 Mar 2024 16:08:47 +0400 Subject: [PATCH 3/3] quick fix with form shop loading --- ComputersShop/ComputersShopView/FormShop.Designer.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ComputersShop/ComputersShopView/FormShop.Designer.cs b/ComputersShop/ComputersShopView/FormShop.Designer.cs index ac4e4e6..3ae9494 100644 --- a/ComputersShop/ComputersShopView/FormShop.Designer.cs +++ b/ComputersShop/ComputersShopView/FormShop.Designer.cs @@ -178,6 +178,7 @@ this.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.Name = "FormShop"; this.Text = "Магазин"; + Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); this.PerformLayout();