From c65f7168a82a5d8009f8f1628e5a0ba12f658374 Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 5 Mar 2024 21:14:06 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B5=D0=BC=20=D0=B1=D1=8B=D0=BB=20?= =?UTF-8?q?=D1=81=D0=B8=D0=BB=D1=8C=D0=BD=D0=B5=D0=B9=20=D0=BA=D0=BE=D0=BD?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=BB=D1=8C,=20=D1=82=D0=B5=D0=BC=20=D1=85?= =?UTF-8?q?=D0=B0=D0=BE=D1=81=20=D0=B1=D0=B5=D0=B7=D0=B3=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=87=D0=BD=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FishFactory/FormMain.Designer.cs | 26 ++- FishFactory/FormMain.cs | 21 +++ FishFactory/FormShop.Designer.cs | 16 +- FishFactory/FormShop.cs | 112 +++++++++++-- FishFactory/FormShopReplenish.cs | 83 ++++++++-- FishFactory/FormShops.Designer.cs | 32 ++-- FishFactory/FormShops.cs | 96 +++++++++-- FishFactory/Program.cs | 5 + .../BusinessLogic/CannedLogic.cs | 5 - .../BusinessLogic/ShopLogic.cs | 154 ++++++++++++++++++ ...ndingModel.cs => ReplenishBindingModel.cs} | 2 +- .../BusinessLogicsContracts/IShopLogic.cs | 7 +- FishFactoryListImplement/DataListSingleton.cs | 2 +- FishFactoryListImplement/Models/Shop.cs | 2 +- 14 files changed, 484 insertions(+), 79 deletions(-) create mode 100644 FishFactoryBusinessLogic/BusinessLogic/ShopLogic.cs rename FishFactoryContracts/BindingModels/{ShopCannedBindingModel.cs => ReplenishBindingModel.cs} (88%) diff --git a/FishFactory/FormMain.Designer.cs b/FishFactory/FormMain.Designer.cs index 73fef74..f76b064 100644 --- a/FishFactory/FormMain.Designer.cs +++ b/FishFactory/FormMain.Designer.cs @@ -33,12 +33,14 @@ toolStripDropDownButton1 = new ToolStripDropDownButton(); компонентыToolStripMenuItem = new ToolStripMenuItem(); консервыToolStripMenuItem = new ToolStripMenuItem(); + магазиныToolStripMenuItem = new ToolStripMenuItem(); buttonCreateOrder = new Button(); buttonTakeOrderInWork = new Button(); buttonOrderReady = new Button(); buttonIssuedOrder = new Button(); buttonRef = new Button(); dataGridView = new DataGridView(); + toolStripLabelReplenish = new ToolStripLabel(); toolStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -46,7 +48,7 @@ // toolStrip1 // toolStrip1.ImageScalingSize = new Size(20, 20); - toolStrip1.Items.AddRange(new ToolStripItem[] { toolStripDropDownButton1 }); + toolStrip1.Items.AddRange(new ToolStripItem[] { toolStripDropDownButton1, toolStripLabelReplenish }); toolStrip1.Location = new Point(0, 0); toolStrip1.Name = "toolStrip1"; toolStrip1.Size = new Size(1107, 27); @@ -56,7 +58,7 @@ // toolStripDropDownButton1 // toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text; - toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, консервыToolStripMenuItem }); + toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, консервыToolStripMenuItem, магазиныToolStripMenuItem }); toolStripDropDownButton1.Image = (Image)resources.GetObject("toolStripDropDownButton1.Image"); toolStripDropDownButton1.ImageTransparentColor = Color.Magenta; toolStripDropDownButton1.Name = "toolStripDropDownButton1"; @@ -66,17 +68,24 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(182, 26); + компонентыToolStripMenuItem.Size = new Size(224, 26); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += компонентыToolStripMenuItem_Click; // // консервыToolStripMenuItem // консервыToolStripMenuItem.Name = "консервыToolStripMenuItem"; - консервыToolStripMenuItem.Size = new Size(182, 26); + консервыToolStripMenuItem.Size = new Size(224, 26); консервыToolStripMenuItem.Text = "Консервы"; консервыToolStripMenuItem.Click += консервыToolStripMenuItem_Click; // + // магазиныToolStripMenuItem + // + магазиныToolStripMenuItem.Name = "магазиныToolStripMenuItem"; + магазиныToolStripMenuItem.Size = new Size(224, 26); + магазиныToolStripMenuItem.Text = "Магазины"; + магазиныToolStripMenuItem.Click += магазиныToolStripMenuItem_Click; + // // buttonCreateOrder // buttonCreateOrder.Location = new Point(914, 75); @@ -145,6 +154,13 @@ dataGridView.Size = new Size(872, 580); dataGridView.TabIndex = 6; // + // toolStripLabelReplenish + // + toolStripLabelReplenish.Name = "toolStripLabelReplenish"; + toolStripLabelReplenish.Size = new Size(168, 24); + toolStripLabelReplenish.Text = "Пополнение магазина"; + toolStripLabelReplenish.Click += toolStripLabelReplenish_Click; + // // FormMain // AutoScaleDimensions = new SizeF(8F, 20F); @@ -180,5 +196,7 @@ private ToolStripDropDownButton toolStripDropDownButton1; private ToolStripMenuItem компонентыToolStripMenuItem; private ToolStripMenuItem консервыToolStripMenuItem; + private ToolStripMenuItem магазиныToolStripMenuItem; + private ToolStripLabel toolStripLabelReplenish; } } \ No newline at end of file diff --git a/FishFactory/FormMain.cs b/FishFactory/FormMain.cs index b8c0b8f..50e2296 100644 --- a/FishFactory/FormMain.cs +++ b/FishFactory/FormMain.cs @@ -65,6 +65,27 @@ namespace FishFactory.Forms form.ShowDialog(); } } + + private void магазиныToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShops)); + + if (service is FormShops form) + { + form.ShowDialog(); + } + } + + private void toolStripLabelReplenish_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormShopReplenish)); + if (service is FormShopReplenish form) + { + form.ShowDialog(); + LoadData(); + } + } + private void buttonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); diff --git a/FishFactory/FormShop.Designer.cs b/FishFactory/FormShop.Designer.cs index 8c47b80..2a0da5b 100644 --- a/FishFactory/FormShop.Designer.cs +++ b/FishFactory/FormShop.Designer.cs @@ -38,7 +38,7 @@ CannedName = new DataGridViewTextBoxColumn(); Count = new DataGridViewTextBoxColumn(); label3 = new Label(); - dateTimePicker = new DateTimePicker(); + openingDatePicker = new DateTimePicker(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // @@ -133,19 +133,19 @@ label3.TabIndex = 7; label3.Text = "Дата открытия:"; // - // dateTimePicker + // openingDatePicker // - dateTimePicker.Location = new Point(557, 20); - dateTimePicker.Name = "dateTimePicker"; - dateTimePicker.Size = new Size(294, 27); - dateTimePicker.TabIndex = 8; + openingDatePicker.Location = new Point(557, 20); + openingDatePicker.Name = "openingDatePicker"; + openingDatePicker.Size = new Size(294, 27); + openingDatePicker.TabIndex = 8; // // FormShop // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(863, 568); - Controls.Add(dateTimePicker); + Controls.Add(openingDatePicker); Controls.Add(label3); Controls.Add(dataGridView); Controls.Add(buttonCancel); @@ -173,6 +173,6 @@ private DataGridViewTextBoxColumn CannedName; private DataGridViewTextBoxColumn Count; private Label label3; - private DateTimePicker dateTimePicker; + private DateTimePicker openingDatePicker; } } \ No newline at end of file diff --git a/FishFactory/FormShop.cs b/FishFactory/FormShop.cs index c0e3c13..e7dcf3b 100644 --- a/FishFactory/FormShop.cs +++ b/FishFactory/FormShop.cs @@ -1,30 +1,120 @@ -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 FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; +using FishFactoryDataModel.Models; +using Microsoft.Extensions.Logging; using System.Windows.Forms; namespace FishFactory { 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 _shopCanneds; + private DateTime? _openingDate = null; + + public FormShop(ILogger logger, IShopLogic logic) { InitializeComponent(); + _logger = logger; + _logic = logic; + _shopCanneds = 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) + { + textBoxShopName.Text = view.ShopName; + textBoxAdress.Text = view.Adress; + openingDatePicker.Value = view.OpeningDate; + _shopCanneds = view.ShopCanneds ?? new Dictionary(); + LoadData(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазина"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + + private void LoadData() + { + _logger.LogInformation("Загрузка изделий в магазине"); + try + { + if (_shopCanneds != null) + { + dataGridView.Rows.Clear(); + foreach (var sr in _shopCanneds) + { + dataGridView.Rows.Add(new object[] { sr.Key, sr.Value.Item1.CannedName, 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(textBoxShopName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxAdress.Text)) + { + MessageBox.Show("Заполните адрес", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение магазина"); + try + { + var model = new ShopBindingModel + { + Id = _id ?? 0, + ShopName = textBoxShopName.Text, + Adress = textBoxAdress.Text, + OpeningDate = openingDatePicker.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/FishFactory/FormShopReplenish.cs b/FishFactory/FormShopReplenish.cs index b332189..122d9ae 100644 --- a/FishFactory/FormShopReplenish.cs +++ b/FishFactory/FormShopReplenish.cs @@ -1,30 +1,89 @@ -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 FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.ViewModels; +using Microsoft.Extensions.Logging; namespace FishFactory { public partial class FormShopReplenish : Form { - public FormShopReplenish() + private readonly ILogger _logger; + private readonly ICannedLogic _logicP; + private readonly IShopLogic _logicS; + private List _shopList = new List(); + private List _cannedList = new List(); + + public FormShopReplenish(ILogger logger, ICannedLogic logicP, IShopLogic logicS) { InitializeComponent(); + _logger = logger; + _logicP = logicP; + _logicS = logicS; + } + + private void FormCreateReplenish_Load(object sender, EventArgs e) + { + _shopList = _logicS.ReadList(null); + _cannedList = _logicP.ReadList(null); + if (_shopList != null) + { + comboBoxShopCanneds.DisplayMember = "ShopName"; + comboBoxShopCanneds.ValueMember = "Id"; + comboBoxShopCanneds.DataSource = _shopList; + comboBoxShopCanneds.SelectedItem = null; + _logger.LogInformation("Загрузка магазинов для поставок"); + } + if (_cannedList != null) + { + comboBoxCanned.DisplayMember = "CannedName"; + comboBoxCanned.ValueMember = "Id"; + comboBoxCanned.DataSource = _cannedList; + comboBoxCanned.SelectedItem = null; + _logger.LogInformation("Загрузка консерв для поставок"); + } } private void buttonSave_Click(object sender, EventArgs e) { - + if (comboBoxShopCanneds.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxCanned.SelectedValue == null) + { + MessageBox.Show("Выберите консерву", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Создание поставки"); + try + { + var operationResult = _logicS.Replenish(new ReplenishBindingModel + { + ShopId = Convert.ToInt32(comboBoxShopCanneds.SelectedValue), + CannedId = Convert.ToInt32(comboBoxCanned.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/FishFactory/FormShops.Designer.cs b/FishFactory/FormShops.Designer.cs index 1b66f80..b7c0239 100644 --- a/FishFactory/FormShops.Designer.cs +++ b/FishFactory/FormShops.Designer.cs @@ -28,26 +28,26 @@ /// 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.AllowUserToAddRows = false; - dataGridView1.AllowUserToDeleteRows = false; - dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView1.Location = new Point(0, 0); - dataGridView1.Name = "dataGridView1"; - dataGridView1.ReadOnly = true; - dataGridView1.RowHeadersWidth = 51; - dataGridView1.RowTemplate.Height = 29; - dataGridView1.Size = new Size(525, 673); - dataGridView1.TabIndex = 0; + dataGridView.AllowUserToAddRows = false; + dataGridView.AllowUserToDeleteRows = false; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(0, 0); + dataGridView.Name = "dataGridView"; + dataGridView.ReadOnly = true; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 29; + dataGridView.Size = new Size(525, 673); + dataGridView.TabIndex = 0; // // buttonAdd // @@ -102,16 +102,16 @@ Controls.Add(buttonDel); Controls.Add(buttonUpd); Controls.Add(buttonAdd); - Controls.Add(dataGridView1); + Controls.Add(dataGridView); Name = "FormShops"; Text = "Список магазинов"; - ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); + ((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/FishFactory/FormShops.cs b/FishFactory/FormShops.cs index aaa2d41..2d0c90f 100644 --- a/FishFactory/FormShops.cs +++ b/FishFactory/FormShops.cs @@ -1,40 +1,108 @@ -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 FishFactory.Forms; +using FishFactoryDataModel.Models; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; +using Microsoft.Extensions.Logging; namespace FishFactory { public partial class FormShops : Form { - public FormShops() + private readonly ILogger _logger; + private readonly IComponentLogic _logic; + + public FormShops(ILogger logger, IComponentLogic 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["ShopCanneds"].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 ComponentBindingModel + { + 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/FishFactory/Program.cs b/FishFactory/Program.cs index 08c8b2d..fabfb88 100644 --- a/FishFactory/Program.cs +++ b/FishFactory/Program.cs @@ -38,9 +38,11 @@ namespace FishFactory services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -48,6 +50,9 @@ namespace FishFactory services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/FishFactoryBusinessLogic/BusinessLogic/CannedLogic.cs b/FishFactoryBusinessLogic/BusinessLogic/CannedLogic.cs index 4bdf067..1b6822f 100644 --- a/FishFactoryBusinessLogic/BusinessLogic/CannedLogic.cs +++ b/FishFactoryBusinessLogic/BusinessLogic/CannedLogic.cs @@ -4,11 +4,6 @@ using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; using FishFactoryContracts.ViewModels; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FishFactoryBusinessLogic.BusinessLogic { diff --git a/FishFactoryBusinessLogic/BusinessLogic/ShopLogic.cs b/FishFactoryBusinessLogic/BusinessLogic/ShopLogic.cs new file mode 100644 index 0000000..b2cbc12 --- /dev/null +++ b/FishFactoryBusinessLogic/BusinessLogic/ShopLogic.cs @@ -0,0 +1,154 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace FishFactoryBusinessLogic.BusinessLogic +{ + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + private readonly ICannedStorage _cannedStorage; + + public ShopLogic(ILogger logger, IShopStorage shopStorage, ICannedStorage cannedStorage) + { + _logger = logger; + _shopStorage = shopStorage; + _cannedStorage = cannedStorage; + } + 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 Replenish(ReplenishBindingModel 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.ShopCanneds.ContainsKey(model.CannedId)) + { + var oldValue = shop.ShopCanneds[model.CannedId]; + oldValue.Item2 += model.Count; + shop.ShopCanneds[model.CannedId] = oldValue; + } + else + { + var canned = _cannedStorage.GetElement(new CannedSearchModel + { + Id = model.CannedId + }); + if (canned == null) + { + throw new ArgumentException($"Поставка: Товар с id:{model.CannedId} не найденн"); + } + shop.ShopCanneds.Add(model.CannedId, (canned, 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.Adress)) + { + throw new ArgumentException("Нет адреса магазина", nameof(model.Adress)); + } + if (string.IsNullOrEmpty(model.ShopName)) + { + throw new ArgumentNullException("Нет названия компонента", nameof(model.ShopName)); + } + _logger.LogInformation("Shop. ShopName:{ShopName}.Adress:{Adress}. Id: {Id}", model.ShopName, model.Adress, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + ShopName = model.ShopName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + } +} diff --git a/FishFactoryContracts/BindingModels/ShopCannedBindingModel.cs b/FishFactoryContracts/BindingModels/ReplenishBindingModel.cs similarity index 88% rename from FishFactoryContracts/BindingModels/ShopCannedBindingModel.cs rename to FishFactoryContracts/BindingModels/ReplenishBindingModel.cs index e8861c6..f7210d5 100644 --- a/FishFactoryContracts/BindingModels/ShopCannedBindingModel.cs +++ b/FishFactoryContracts/BindingModels/ReplenishBindingModel.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace FishFactoryContracts.BindingModels { - public class ShopCannedBindingModel + public class ReplenishBindingModel { public int ShopId { get; set; } public int CannedId { get; set; } diff --git a/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs b/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs index 27854e1..8005dde 100644 --- a/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/FishFactoryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -1,11 +1,6 @@ using FishFactoryContracts.BindingModels; using FishFactoryContracts.SearchModels; using FishFactoryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FishFactoryContracts.BusinessLogicsContracts { @@ -16,6 +11,6 @@ namespace FishFactoryContracts.BusinessLogicsContracts bool Create(ShopBindingModel model); bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); - bool MakeSupply(ShopCannedBindingModel model); + bool Replenish(ReplenishBindingModel model); } } diff --git a/FishFactoryListImplement/DataListSingleton.cs b/FishFactoryListImplement/DataListSingleton.cs index a2ec10e..27a6009 100644 --- a/FishFactoryListImplement/DataListSingleton.cs +++ b/FishFactoryListImplement/DataListSingleton.cs @@ -19,7 +19,7 @@ namespace FishFactoryListImplement Components = new List(); Orders = new List(); Canneds = new List(); - Shop = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/FishFactoryListImplement/Models/Shop.cs b/FishFactoryListImplement/Models/Shop.cs index c5f18d2..b82b19c 100644 --- a/FishFactoryListImplement/Models/Shop.cs +++ b/FishFactoryListImplement/Models/Shop.cs @@ -13,7 +13,7 @@ namespace FishFactoryListImplement.Models { public int Id { get; private set; } public string ShopName { get; private set; } = string.Empty; - public string Adress { get; private set; } + public string Adress { get; private set; } = string.Empty; public DateTime OpeningDate { get; private set; } public Dictionary ShopCanneds {