diff --git a/Confectionery/ConfectioneryBusinessLogic/ShopLogic.cs b/Confectionery/ConfectioneryBusinessLogic/ShopLogic.cs index 8bb7f7c..5685de7 100644 --- a/Confectionery/ConfectioneryBusinessLogic/ShopLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/ShopLogic.cs @@ -1,4 +1,10 @@ -using System; +using Microsoft.Extensions.Logging; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +12,149 @@ using System.Threading.Tasks; namespace ConfectioneryBusinessLogic { - public class ShopLogic + public class ShopLogic : IShopLogic { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + private readonly IPastryStorage _pastryStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage, IPastryStorage pastryStorage) + { + _logger = logger; + _shopStorage = shopStorage; + _pastryStorage = pastryStorage; + } + + 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 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 bool Create(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool MakeSupply(SupplyBindingModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (model.Count <= 0) + { + throw new ArgumentException("Количество изделий должно быть больше 0"); + } + var shop = _shopStorage.GetElement(new ShopSearchModel + { + Id = model.ShopId + }); + if (shop == null) + { + throw new ArgumentException("Магазина не существует"); + } + if (shop.ShopPastrys.ContainsKey(model.PastryId)) + { + var oldValue = shop.ShopPastrys[model.PastryId]; + oldValue.Item2 += model.Count; + shop.ShopPastrys[model.PastryId] = oldValue; + } + else + { + var Pastry = _pastryStorage.GetElement(new PastrySearchModel + { + Id = model.PastryId + }); + if (Pastry == null) + { + throw new ArgumentException($"Поставка: Товар с id:{model.PastryId} не найденн"); + } + shop.ShopPastrys.Add(model.PastryId, (Pastry, model.Count)); + } + 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.Address)) + { + throw new ArgumentException("Адрес магазина длжен быть заполнен", nameof(model.Address)); + } + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentException("Название магазина должно быть заполнено", nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Adres:{Adres}.OpeningDate:{OpeningDate}.Id:{ Id}", model.ShopName, model.Address, model.OpeningDate, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } } } diff --git a/Confectionery/ConfectioneryContracts/BindingModels/ShopBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/ShopBindingModel.cs index 73d2372..6cac88b 100644 --- a/Confectionery/ConfectioneryContracts/BindingModels/ShopBindingModel.cs +++ b/Confectionery/ConfectioneryContracts/BindingModels/ShopBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using ConfectioneryDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,12 @@ using System.Threading.Tasks; namespace ConfectioneryContracts.BindingModels { - public class ShopBindingModel + public class ShopBindingModel : IShopModel { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Address { get; set; } = string.Empty; + public DateTime OpeningDate { get; set; } = DateTime.Now; + public Dictionary ShopPastrys { get; set; } = new(); } } diff --git a/Confectionery/ConfectioneryContracts/BindingModels/SupplyBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/SupplyBindingModel.cs index 3f27b91..8bf9092 100644 --- a/Confectionery/ConfectioneryContracts/BindingModels/SupplyBindingModel.cs +++ b/Confectionery/ConfectioneryContracts/BindingModels/SupplyBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using ConfectioneryDataModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,10 @@ using System.Threading.Tasks; namespace ConfectioneryContracts.BindingModels { - public class SupplyBindingModel + public class SupplyBindingModel : ISupplyModel { + public int ShopId { get; set; } + public int PastryId { get; set; } + public int Count { get; set; } } } diff --git a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index 9692fce..20fd9da 100644 --- a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -1,4 +1,7 @@ -using System; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +11,11 @@ namespace ConfectioneryContracts.BusinessLogicsContracts { public interface IShopLogic { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool MakeSupply(SupplyBindingModel model); } } diff --git a/Confectionery/ConfectioneryContracts/SearchModels/ShopSearchModel.cs b/Confectionery/ConfectioneryContracts/SearchModels/ShopSearchModel.cs index ff32c27..36ddd80 100644 --- a/Confectionery/ConfectioneryContracts/SearchModels/ShopSearchModel.cs +++ b/Confectionery/ConfectioneryContracts/SearchModels/ShopSearchModel.cs @@ -8,5 +8,7 @@ namespace ConfectioneryContracts.SearchModels { public class ShopSearchModel { + public int? Id { get; set; } + public string? ShopName { get; set; } } } diff --git a/Confectionery/ConfectioneryContracts/StoragesContracts/IShopStorage.cs b/Confectionery/ConfectioneryContracts/StoragesContracts/IShopStorage.cs index 8ec518d..86d0e18 100644 --- a/Confectionery/ConfectioneryContracts/StoragesContracts/IShopStorage.cs +++ b/Confectionery/ConfectioneryContracts/StoragesContracts/IShopStorage.cs @@ -1,4 +1,7 @@ -using System; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +11,11 @@ namespace ConfectioneryContracts.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/Confectionery/ConfectioneryContracts/ViewModels/ShopViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/ShopViewModel.cs index d92717a..85b7b6f 100644 --- a/Confectionery/ConfectioneryContracts/ViewModels/ShopViewModel.cs +++ b/Confectionery/ConfectioneryContracts/ViewModels/ShopViewModel.cs @@ -1,12 +1,22 @@ -using System; +using ConfectioneryDataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConfectioneryContracts.ViewModels { - public class ShopViewModel + public class ShopViewModel : IShopModel { + public int Id { get; set; } + [DisplayName("Название")] + public string ShopName { get; set; } = string.Empty; + [DisplayName("Адрес")] + public string Address { get; set; } = string.Empty; + [DisplayName("Дата открытия")] + public DateTime OpeningDate { get; set; } + public Dictionary ShopPastrys { get; set; } = new(); } } diff --git a/Confectionery/ConfectioneryDataModels/IShopModel.cs b/Confectionery/ConfectioneryDataModels/IShopModel.cs index 7315e2f..1892e41 100644 --- a/Confectionery/ConfectioneryDataModels/IShopModel.cs +++ b/Confectionery/ConfectioneryDataModels/IShopModel.cs @@ -4,9 +4,13 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ConfectioneryDataModels +namespace ConfectioneryDataModels.Models { - internal interface IShopModel + public interface IShopModel : IId { + string ShopName { get; } + string Address { get; } + DateTime OpeningDate { get; } + Dictionary ShopPastrys { get; } } } diff --git a/Confectionery/ConfectioneryDataModels/ISupplyModel.cs b/Confectionery/ConfectioneryDataModels/ISupplyModel.cs index a4ad9c1..75a85e9 100644 --- a/Confectionery/ConfectioneryDataModels/ISupplyModel.cs +++ b/Confectionery/ConfectioneryDataModels/ISupplyModel.cs @@ -6,7 +6,10 @@ using System.Threading.Tasks; namespace ConfectioneryDataModels { - internal interface ISupplyModel + public interface ISupplyModel { + int ShopId { get; } + int PastryId { get; } + int Count { get; } } } diff --git a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs index 37eca03..c4077ee 100644 --- a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs +++ b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ConfectioneryListImplement.Models; +using static System.Formats.Asn1.AsnWriter; namespace ConfectioneryListImplement { @@ -13,11 +14,13 @@ namespace ConfectioneryListImplement public List Components { get; set; } public List Orders { get; set; } public List Pastrys { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pastrys = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/Confectionery/ConfectioneryListImplement/Shop.cs b/Confectionery/ConfectioneryListImplement/Shop.cs index 1ede304..9eb9ac4 100644 --- a/Confectionery/ConfectioneryListImplement/Shop.cs +++ b/Confectionery/ConfectioneryListImplement/Shop.cs @@ -1,12 +1,55 @@ -using System; +using ConfectioneryDataModels.Models; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ConfectioneryListImplement +namespace ConfectioneryListImplement.Models { - internal class Shop + public class Shop : IShopModel { + public int Id { get; private set; } + public string ShopName { get; private set; } = string.Empty; + public string Address { get; private set; } = string.Empty; + public DateTime OpeningDate { get; private set; } + public Dictionary ShopPastrys { get; private set; } = new(); + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + OpeningDate = model.OpeningDate + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Address = model.Address; + OpeningDate = model.OpeningDate; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + OpeningDate = OpeningDate, + ShopPastrys = ShopPastrys + }; } } diff --git a/Confectionery/ConfectioneryListImplement/ShopStorage.cs b/Confectionery/ConfectioneryListImplement/ShopStorage.cs index 571e6a7..f55b849 100644 --- a/Confectionery/ConfectioneryListImplement/ShopStorage.cs +++ b/Confectionery/ConfectioneryListImplement/ShopStorage.cs @@ -1,4 +1,9 @@ -using System; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,103 @@ using System.Threading.Tasks; namespace ConfectioneryListImplement { - internal class ShopStorage + public class ShopStorage : IShopStorage { + private readonly DataListSingleton _source; + + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ShopName)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.ShopName.Contains(model.ShopName)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + 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 ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } } } diff --git a/Confectionery/ConfectioneryView/FormCreateSupply.Designer.cs b/Confectionery/ConfectioneryView/FormCreateSupply.Designer.cs index a4a7a2b..0190fa1 100644 --- a/Confectionery/ConfectioneryView/FormCreateSupply.Designer.cs +++ b/Confectionery/ConfectioneryView/FormCreateSupply.Designer.cs @@ -31,9 +31,11 @@ labelShop = new Label(); labelPastry = new Label(); labelCount = new Label(); - comboBox1 = new ComboBox(); - comboBox2 = new ComboBox(); - textBox1 = new TextBox(); + comboBoxShop = new ComboBox(); + comboBoxPastry = new ComboBox(); + textBoxCount = new TextBox(); + buttonSave = new Button(); + buttonCancel = new Button(); SuspendLayout(); // // labelShop @@ -63,42 +65,65 @@ labelCount.TabIndex = 2; labelCount.Text = "Количество:"; // - // comboBox1 + // comboBoxShop // - comboBox1.FormattingEnabled = true; - comboBox1.Location = new Point(162, 39); - comboBox1.Name = "comboBox1"; - comboBox1.Size = new Size(383, 33); - comboBox1.TabIndex = 3; + comboBoxShop.FormattingEnabled = true; + comboBoxShop.Location = new Point(162, 39); + comboBoxShop.Name = "comboBoxShop"; + comboBoxShop.Size = new Size(383, 33); + comboBoxShop.TabIndex = 3; // - // comboBox2 + // comboBoxPastry // - comboBox2.FormattingEnabled = true; - comboBox2.Location = new Point(162, 98); - comboBox2.Name = "comboBox2"; - comboBox2.Size = new Size(383, 33); - comboBox2.TabIndex = 4; + comboBoxPastry.FormattingEnabled = true; + comboBoxPastry.Location = new Point(162, 98); + comboBoxPastry.Name = "comboBoxPastry"; + comboBoxPastry.Size = new Size(383, 33); + comboBoxPastry.TabIndex = 4; // - // textBox1 + // textBoxCount // - textBox1.Location = new Point(162, 158); - textBox1.Name = "textBox1"; - textBox1.Size = new Size(383, 31); - textBox1.TabIndex = 5; + textBoxCount.Location = new Point(162, 158); + textBoxCount.Name = "textBoxCount"; + textBoxCount.Size = new Size(383, 31); + textBoxCount.TabIndex = 5; + // + // buttonSave + // + buttonSave.Location = new Point(318, 214); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(127, 49); + buttonSave.TabIndex = 6; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(471, 214); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(127, 49); + buttonCancel.TabIndex = 7; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; // // FormCreateSupply // AutoScaleDimensions = new SizeF(10F, 25F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(608, 240); - Controls.Add(textBox1); - Controls.Add(comboBox2); - Controls.Add(comboBox1); + ClientSize = new Size(647, 288); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxCount); + Controls.Add(comboBoxPastry); + Controls.Add(comboBoxShop); Controls.Add(labelCount); Controls.Add(labelPastry); Controls.Add(labelShop); Name = "FormCreateSupply"; Text = "Поставка"; + Load += FormCreateSupply_Load; ResumeLayout(false); PerformLayout(); } @@ -108,8 +133,10 @@ private Label labelShop; private Label labelPastry; private Label labelCount; - private ComboBox comboBox1; - private ComboBox comboBox2; - private TextBox textBox1; + private ComboBox comboBoxShop; + private ComboBox comboBoxPastry; + private TextBox textBoxCount; + private Button buttonSave; + private Button buttonCancel; } } \ No newline at end of file diff --git a/Confectionery/ConfectioneryView/FormCreateSupply.cs b/Confectionery/ConfectioneryView/FormCreateSupply.cs index f1ce332..d276e30 100644 --- a/Confectionery/ConfectioneryView/FormCreateSupply.cs +++ b/Confectionery/ConfectioneryView/FormCreateSupply.cs @@ -1,4 +1,8 @@ -using System; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +16,82 @@ namespace ConfectioneryView { public partial class FormCreateSupply : Form { - public FormCreateSupply() + private readonly ILogger _logger; + private readonly IPastryLogic _logicP; + private readonly IShopLogic _logicS; + private List _shopList = new List(); + private List _pastryList = new List(); + + public FormCreateSupply(ILogger logger, IPastryLogic logicP, IShopLogic logicS) { InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + + private void FormCreateSupply_Load(object sender, EventArgs e) + { + _shopList = _logicS.ReadList(null); + _pastryList = _logicP.ReadList(null); + if (_shopList != null) + { + comboBoxShop.DisplayMember = "ShopName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = _shopList; + comboBoxShop.SelectedItem = null; + _logger.LogInformation("Загрузка магазинов для поставок"); + } + if (_pastryList != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember = "Id"; + comboBoxPastry.DataSource = _pastryList; + comboBoxPastry.SelectedItem = null; + _logger.LogInformation("Загрузка кондитерского изделия для поставок"); + } + } + + private void buttonSave_Click(object sender, EventArgs e) + { + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание поставки"); + try + { + var operationResult = _logicS.MakeSupply(new SupplyBindingModel + { + ShopId = Convert.ToInt32(comboBoxShop.SelectedValue), + PastryId = Convert.ToInt32(comboBoxPastry.SelectedValue), + Count = Convert.ToInt32(textBoxCount.Text) + }); + if (!operationResult) + { + throw new Exception("Ошибка при создании поставки. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка создания поставки"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); } } } diff --git a/Confectionery/ConfectioneryView/FormMain.Designer.cs b/Confectionery/ConfectioneryView/FormMain.Designer.cs index 3c95edf..4726972 100644 --- a/Confectionery/ConfectioneryView/FormMain.Designer.cs +++ b/Confectionery/ConfectioneryView/FormMain.Designer.cs @@ -33,6 +33,8 @@ toolStripMenuItem = new ToolStripMenuItem(); componentsToolStripMenuItem = new ToolStripMenuItem(); pastryToolStripMenuItem = new ToolStripMenuItem(); + shopsToolStripMenuItem = new ToolStripMenuItem(); + supplyToolStripMenuItem = new ToolStripMenuItem(); buttonCreateOrder = new Button(); buttonTakeOrderInWork = new Button(); buttonOrderReady = new Button(); @@ -56,7 +58,7 @@ // menuStrip // menuStrip.ImageScalingSize = new Size(24, 24); - menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { toolStripMenuItem, supplyToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Size = new Size(1375, 33); @@ -65,7 +67,7 @@ // // toolStripMenuItem // - toolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { componentsToolStripMenuItem, pastryToolStripMenuItem }); + toolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { componentsToolStripMenuItem, pastryToolStripMenuItem, shopsToolStripMenuItem }); toolStripMenuItem.Name = "toolStripMenuItem"; toolStripMenuItem.Size = new Size(139, 29); toolStripMenuItem.Text = "Справочники"; @@ -84,6 +86,20 @@ pastryToolStripMenuItem.Text = "Кондитерские изделия"; pastryToolStripMenuItem.Click += pastryToolStripMenuItem_Click; // + // shopsToolStripMenuItem + // + shopsToolStripMenuItem.Name = "shopsToolStripMenuItem"; + shopsToolStripMenuItem.Size = new Size(298, 34); + shopsToolStripMenuItem.Text = "Магазины"; + shopsToolStripMenuItem.Click += shopsToolStripMenuItem_Click; + // + // supplyToolStripMenuItem + // + supplyToolStripMenuItem.Name = "supplyToolStripMenuItem"; + supplyToolStripMenuItem.Size = new Size(130, 29); + supplyToolStripMenuItem.Text = "Пополнение"; + supplyToolStripMenuItem.Click += supplyToolStripMenuItem_Click; + // // buttonCreateOrder // buttonCreateOrder.Anchor = AnchorStyles.Top | AnchorStyles.Right; @@ -174,5 +190,7 @@ private Button buttonRef; private ToolStripMenuItem componentsToolStripMenuItem; private ToolStripMenuItem pastryToolStripMenuItem; + private ToolStripMenuItem supplyToolStripMenuItem; + private ToolStripMenuItem shopsToolStripMenuItem; } } \ No newline at end of file diff --git a/Confectionery/ConfectioneryView/FormMain.cs b/Confectionery/ConfectioneryView/FormMain.cs index 0cd8423..13470b5 100644 --- a/Confectionery/ConfectioneryView/FormMain.cs +++ b/Confectionery/ConfectioneryView/FormMain.cs @@ -157,5 +157,23 @@ namespace ConfectioneryView { LoadData(); } + + private void supplyToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormCreateSupply)); + if (service is FormCreateSupply form) + { + form.ShowDialog(); + } + } + + private void shopsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + if (service is FormShops form) + { + form.ShowDialog(); + } + } } } diff --git a/Confectionery/ConfectioneryView/FormShop.Designer.cs b/Confectionery/ConfectioneryView/FormShop.Designer.cs index af6d641..57f5e82 100644 --- a/Confectionery/ConfectioneryView/FormShop.Designer.cs +++ b/Confectionery/ConfectioneryView/FormShop.Designer.cs @@ -107,6 +107,7 @@ buttonSave.TabIndex = 7; buttonSave.Text = "Сохранить"; buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; // // buttonCancel // @@ -116,6 +117,7 @@ buttonCancel.TabIndex = 8; buttonCancel.Text = "Отмена"; buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; // // FormShop // @@ -133,6 +135,7 @@ Controls.Add(dataGridView); Name = "FormShop"; Text = "Магазин"; + Load += FormShop_Load; ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); PerformLayout(); diff --git a/Confectionery/ConfectioneryView/FormShop.cs b/Confectionery/ConfectioneryView/FormShop.cs index e872cd3..09be130 100644 --- a/Confectionery/ConfectioneryView/FormShop.cs +++ b/Confectionery/ConfectioneryView/FormShop.cs @@ -1,4 +1,9 @@ -using System; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryDataModels.Models; +using Microsoft.Extensions.Logging; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -7,14 +12,115 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Microsoft.VisualBasic.Logging; namespace ConfectioneryView { public partial class FormShop : Form { - public FormShop() + private readonly ILogger _logger; + private readonly IShopLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + private Dictionary _ShopPastrys; + private DateTime? _openingDate = null; + public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); + _logger = logger; + _logic = logic; + _ShopPastrys = 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.Address; + dateTimePickerOpen.Value = view.OpeningDate; + _ShopPastrys = view.ShopPastrys ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void LoadData() + { + _logger.LogInformation("Загрузка изделий в магазине"); + try + { + if (_ShopPastrys != null) + { + dataGridView.Rows.Clear(); + foreach (var sr in _ShopPastrys) + { + dataGridView.Rows.Add(new object[] { sr.Key, sr.Value.Item1.PastryName, sr.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, + Address = textBoxAddress.Text, + OpeningDate = dateTimePickerOpen.Value + }; + 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/Confectionery/ConfectioneryView/FormShops.Designer.cs b/Confectionery/ConfectioneryView/FormShops.Designer.cs index 0ef5b8c..1176161 100644 --- a/Confectionery/ConfectioneryView/FormShops.Designer.cs +++ b/Confectionery/ConfectioneryView/FormShops.Designer.cs @@ -28,24 +28,24 @@ /// private void InitializeComponent() { - dataGridView1 = new DataGridView(); + dataGridView = new DataGridView(); buttonAdd = new Button(); buttonUpd = new Button(); buttonDel = new Button(); buttonRef = new Button(); - ((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // - // dataGridView1 + // dataGridView // - dataGridView1.BackgroundColor = Color.AliceBlue; - dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView1.Location = new Point(12, 30); - dataGridView1.Name = "dataGridView1"; - dataGridView1.RowHeadersWidth = 62; - dataGridView1.RowTemplate.Height = 33; - dataGridView1.Size = new Size(756, 486); - dataGridView1.TabIndex = 0; + dataGridView.BackgroundColor = Color.AliceBlue; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 30); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 62; + dataGridView.RowTemplate.Height = 33; + dataGridView.Size = new Size(756, 486); + dataGridView.TabIndex = 0; // // buttonAdd // @@ -55,6 +55,7 @@ buttonAdd.TabIndex = 1; buttonAdd.Text = "Добавить"; buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; // // buttonUpd // @@ -64,6 +65,7 @@ buttonUpd.TabIndex = 2; buttonUpd.Text = "Изменить"; buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += buttonUpd_Click; // // buttonDel // @@ -73,6 +75,7 @@ buttonDel.TabIndex = 3; buttonDel.Text = "Удалить"; buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += buttonDel_Click; // // buttonRef // @@ -82,6 +85,7 @@ buttonRef.TabIndex = 4; buttonRef.Text = "Обновить"; buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += buttonRef_Click; // // FormShops // @@ -92,16 +96,17 @@ Controls.Add(buttonDel); Controls.Add(buttonUpd); Controls.Add(buttonAdd); - Controls.Add(dataGridView1); + Controls.Add(dataGridView); Name = "FormShops"; Text = "Магазины"; - ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); + Load += FormShops_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ResumeLayout(false); } #endregion - private DataGridView dataGridView1; + private DataGridView dataGridView; private Button buttonAdd; private Button buttonUpd; private Button buttonDel; diff --git a/Confectionery/ConfectioneryView/FormShops.cs b/Confectionery/ConfectioneryView/FormShops.cs index 75f0dfb..cc82fc5 100644 --- a/Confectionery/ConfectioneryView/FormShops.cs +++ b/Confectionery/ConfectioneryView/FormShops.cs @@ -1,4 +1,7 @@ -using System; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +15,102 @@ namespace ConfectioneryView { public partial class FormShops : Form { - public FormShops() + 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["ShopPastrys"].Visible = false; + dataGridView.Columns["ShopName"].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 buttonUpd_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 buttonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление магазина"); + 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 buttonRef_Click(object sender, EventArgs e) + { + LoadData(); } } } diff --git a/Confectionery/ConfectioneryView/Program.cs b/Confectionery/ConfectioneryView/Program.cs index 7ae9212..b9693d0 100644 --- a/Confectionery/ConfectioneryView/Program.cs +++ b/Confectionery/ConfectioneryView/Program.cs @@ -46,6 +46,11 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file