From da162462b58b3cab41d5c9f204bbe7d188d30136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sat, 11 Mar 2023 19:13:24 +0400 Subject: [PATCH] fix --- FoodOrders/FoodOrders/FoodOrdersView.csproj | 10 ++++++++++ FoodOrders/FoodOrders/FormComponents.cs | 5 +---- FoodOrders/FoodOrders/FormCreateOrder.cs | 5 +---- FoodOrders/FoodOrders/FormDish.Designer.cs | 1 + FoodOrders/FoodOrders/FormDish.cs | 14 ++++++-------- FoodOrders/FoodOrders/nlog.config | 15 +++++++++++++++ .../BusinessLogics/ComponentLogic.cs | 19 ++++++++----------- .../BusinessLogics/DishLogic.cs | 2 +- .../BusinessLogics/OrderLogic.cs | 5 ++--- .../BindingModels/DishBindingModel.cs | 6 +----- .../IComponentLogic.cs | 2 ++ .../FoodOrdersDataModels/Models/IDishModel.cs | 2 ++ .../Models/IOrderModel.cs | 1 + .../Implements/ComponentStorage.cs | 2 ++ .../Models/Component.cs | 7 +++++++ 15 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 FoodOrders/FoodOrders/nlog.config diff --git a/FoodOrders/FoodOrders/FoodOrdersView.csproj b/FoodOrders/FoodOrders/FoodOrdersView.csproj index d32249f..797b027 100644 --- a/FoodOrders/FoodOrders/FoodOrdersView.csproj +++ b/FoodOrders/FoodOrders/FoodOrdersView.csproj @@ -8,6 +8,16 @@ enable + + + + + + + Always + + + diff --git a/FoodOrders/FoodOrders/FormComponents.cs b/FoodOrders/FoodOrders/FormComponents.cs index 0f5f54b..914d217 100644 --- a/FoodOrders/FoodOrders/FormComponents.cs +++ b/FoodOrders/FoodOrders/FormComponents.cs @@ -74,10 +74,7 @@ namespace FoodOrdersView _logger.LogInformation("Удаление блюда"); try { - if (!_logic.Delete(new ComponentBindingModel - { - Id = id - })) + if (!_logic.Delete(new ComponentBindingModel { Id = id })) { throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); } diff --git a/FoodOrders/FoodOrders/FormCreateOrder.cs b/FoodOrders/FoodOrders/FormCreateOrder.cs index 14654f4..165c034 100644 --- a/FoodOrders/FoodOrders/FormCreateOrder.cs +++ b/FoodOrders/FoodOrders/FormCreateOrder.cs @@ -45,10 +45,7 @@ namespace FoodOrdersView try { int id = Convert.ToInt32(comboBoxDish.SelectedValue); - var product = _logicS.ReadElement(new DishSearchModel - { - Id = id - }); + var product = _logicS.ReadElement(new DishSearchModel { Id = id }); int count = Convert.ToInt32(textBoxCount.Text); textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString(); _logger.LogInformation("Расчет суммы заказа"); diff --git a/FoodOrders/FoodOrders/FormDish.Designer.cs b/FoodOrders/FoodOrders/FormDish.Designer.cs index 4c0256c..f2d151a 100644 --- a/FoodOrders/FoodOrders/FormDish.Designer.cs +++ b/FoodOrders/FoodOrders/FormDish.Designer.cs @@ -52,6 +52,7 @@ this.textBoxPrice.Location = new System.Drawing.Point(90, 36); this.textBoxPrice.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.textBoxPrice.Name = "textBoxPrice"; + this.textBoxPrice.ReadOnly = true; this.textBoxPrice.Size = new System.Drawing.Size(138, 23); this.textBoxPrice.TabIndex = 7; // diff --git a/FoodOrders/FoodOrders/FormDish.cs b/FoodOrders/FoodOrders/FormDish.cs index 9182364..7df6bec 100644 --- a/FoodOrders/FoodOrders/FormDish.cs +++ b/FoodOrders/FoodOrders/FormDish.cs @@ -35,6 +35,7 @@ namespace FoodOrdersView { textBoxName.Text = view.DishName; textBoxPrice.Text = view.Price.ToString(); + //если не null то слева, если null то справа _dishComponents = view.DishComponents ?? new Dictionary(); LoadData(); } @@ -82,13 +83,11 @@ namespace FoodOrdersView _logger.LogInformation("Добавление нового блюда: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count); if (_dishComponents.ContainsKey(form.Id)) { - _dishComponents[form.Id] = (form.ComponentModel, - form.Count); + _dishComponents[form.Id] = (form.ComponentModel, form.Count); } else { - _dishComponents.Add(form.Id, (form.ComponentModel, - form.Count)); + _dishComponents.Add(form.Id, (form.ComponentModel, form.Count)); } LoadData(); } @@ -101,8 +100,7 @@ namespace FoodOrdersView var service = Program.ServiceProvider?.GetService(typeof(FormDishComponents)); if (service is FormDishComponents form) { - int id = - Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value); form.Id = id; form.Count = _dishComponents[id].Item2; if (form.ShowDialog() == DialogResult.OK) @@ -127,8 +125,8 @@ namespace FoodOrdersView { try { - _logger.LogInformation("Удаление блюда: { ComponentName} - { Count} ", - dataGridView.SelectedRows[0].Cells[1].Value); _dishComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); + _logger.LogInformation("Удаление блюда: { ComponentName} - { Count} ", dataGridView.SelectedRows[0].Cells[1].Value, dataGridView.SelectedRows[0].Cells[2].Value); + _dishComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value)); } catch (Exception ex) { diff --git a/FoodOrders/FoodOrders/nlog.config b/FoodOrders/FoodOrders/nlog.config new file mode 100644 index 0000000..85797a7 --- /dev/null +++ b/FoodOrders/FoodOrders/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs b/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs index 7a86b71..b9f263b 100644 --- a/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -19,10 +19,8 @@ namespace FoodOrdersBusinessLogic.BusinessLogics } public List? ReadList(ComponentSearchModel? model) { - _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{Id}", - model?.ComponentName, model?.Id); - var list = model == null ? _componentStorage.GetFullList() : - _componentStorage.GetFilteredList(model); + _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{Id}", model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); @@ -38,7 +36,7 @@ namespace FoodOrdersBusinessLogic.BusinessLogics throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. ComponentName:{ComponentName}. Id:{Id}", model.ComponentName, model.Id); - var element = _componentStorage.GetElement(model); + var element = _componentStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); @@ -78,8 +76,7 @@ namespace FoodOrdersBusinessLogic.BusinessLogics } return true; } - private void CheckModel(ComponentBindingModel model, bool withParams = - true) + private void CheckModel(ComponentBindingModel model, bool withParams = true) { if (model == null) { @@ -99,10 +96,10 @@ namespace FoodOrdersBusinessLogic.BusinessLogics throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); } _logger.LogInformation("Component. ComponentName:{ComponentName}. Cost:{ Cost}. Id:{Id}", model.ComponentName, model.Cost, model.Id); - var element = _componentStorage.GetElement(new ComponentSearchModel - { - ComponentName = model.ComponentName - }); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Компонент с таким названием уже есть"); diff --git a/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/DishLogic.cs b/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/DishLogic.cs index bd8e613..f42394d 100644 --- a/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/DishLogic.cs +++ b/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/DishLogic.cs @@ -111,7 +111,7 @@ namespace FoodOrdersBusinessLogic.BusinessLogics }); if (element != null && element.Id != model.Id) { - throw new InvalidOperationException("Прдукт с таким названием уже есть"); + throw new InvalidOperationException("Продукт с таким названием уже есть"); } } } diff --git a/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs b/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs index 43a2dfa..4ae8577 100644 --- a/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FoodOrders/FoodOrdersBusinessLogic/BusinessLogics/OrderLogic.cs @@ -20,7 +20,7 @@ namespace FoodOrdersBusinessLogic.BusinessLogics public List? ReadList(OrderSearchModel? model) { - _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{Id}", model?.Id); + _logger.LogInformation("ReadList. Id:{Id}", model?.Id); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { @@ -89,7 +89,6 @@ namespace FoodOrdersBusinessLogic.BusinessLogics _logger.LogInformation("Order. OrderID:{Id}. Sum:{ Sum}. DishId: { DishId}", model.Id, model.Sum, model.Id); } - //??? public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) { var viewModel = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); @@ -111,7 +110,7 @@ namespace FoodOrdersBusinessLogic.BusinessLogics { model.DateImplement = viewModel.DateImplement; } - CheckModel(model); + CheckModel(model, false); if (_orderStorage.Update(model) == null) { _logger.LogWarning("Change status operation failed"); diff --git a/FoodOrders/FoodOrdersContracts/BindingModels/DishBindingModel.cs b/FoodOrders/FoodOrdersContracts/BindingModels/DishBindingModel.cs index 982fcce..50c27f9 100644 --- a/FoodOrders/FoodOrdersContracts/BindingModels/DishBindingModel.cs +++ b/FoodOrders/FoodOrdersContracts/BindingModels/DishBindingModel.cs @@ -7,10 +7,6 @@ namespace FoodOrdersContracts.BindingModels public int Id { get; set; } public string DishName { get; set; } = string.Empty; public double Price { get; set; } - public Dictionary DishComponents - { - get; - set; - } = new(); + public Dictionary DishComponents { get; set; } = new(); } } diff --git a/FoodOrders/FoodOrdersContracts/BuisnessLogicsContracts/IComponentLogic.cs b/FoodOrders/FoodOrdersContracts/BuisnessLogicsContracts/IComponentLogic.cs index 2e830fd..adb0db5 100644 --- a/FoodOrders/FoodOrdersContracts/BuisnessLogicsContracts/IComponentLogic.cs +++ b/FoodOrders/FoodOrdersContracts/BuisnessLogicsContracts/IComponentLogic.cs @@ -6,6 +6,8 @@ namespace FoodOrdersContracts.BusinessLogicsContracts { public interface IComponentLogic { + //чтение листа, если модель есть то с фильтром, если модели нет то весь + // знак вопроса так как модет вернуть null, а в качестве параметра, так как модель может не передаваться List? ReadList(ComponentSearchModel? model); ComponentViewModel? ReadElement(ComponentSearchModel model); bool Create(ComponentBindingModel model); diff --git a/FoodOrders/FoodOrdersDataModels/Models/IDishModel.cs b/FoodOrders/FoodOrdersDataModels/Models/IDishModel.cs index cca6f22..3b4d8c4 100644 --- a/FoodOrders/FoodOrdersDataModels/Models/IDishModel.cs +++ b/FoodOrders/FoodOrdersDataModels/Models/IDishModel.cs @@ -3,6 +3,8 @@ namespace FoodOrdersDataModels.Models { public interface IDishModel : IId { + //в словаре первый int это id, то есть по id компонента найдём сам компонент + //дальше идёт кортеж в котором находиться уже копмонент и то сколько таких компонентов в данном блюде string DishName { get; } double Price { get; } Dictionary DishComponents { get; } diff --git a/FoodOrders/FoodOrdersDataModels/Models/IOrderModel.cs b/FoodOrders/FoodOrdersDataModels/Models/IOrderModel.cs index 134fd34..a0cfe81 100644 --- a/FoodOrders/FoodOrdersDataModels/Models/IOrderModel.cs +++ b/FoodOrders/FoodOrdersDataModels/Models/IOrderModel.cs @@ -9,6 +9,7 @@ namespace FoodOrdersDataModels.Models double Sum { get; } OrderStatus Status { get; } DateTime DateCreate { get; } + //через "?" обозначается что поле может быть null DateTime? DateImplement { get; } } } diff --git a/FoodOrders/FoodOrdersListImplement/Implements/ComponentStorage.cs b/FoodOrders/FoodOrdersListImplement/Implements/ComponentStorage.cs index d8ba6cb..6986ceb 100644 --- a/FoodOrders/FoodOrdersListImplement/Implements/ComponentStorage.cs +++ b/FoodOrders/FoodOrdersListImplement/Implements/ComponentStorage.cs @@ -88,6 +88,8 @@ namespace FoodOrdersListImplement.Implements } public ComponentViewModel? Delete(ComponentBindingModel model) { + // не юзаем foreach так как при изменении данных (добавление и удаление записей) коллекции foreach ломается + // если бы просто меняли значение записи всё было бы в порядке for (int i = 0; i < _source.Components.Count; ++i) { if (_source.Components[i].Id == model.Id) diff --git a/FoodOrders/FoodOrdersListImplement/Models/Component.cs b/FoodOrders/FoodOrdersListImplement/Models/Component.cs index 2510fb9..9ae5b6a 100644 --- a/FoodOrders/FoodOrdersListImplement/Models/Component.cs +++ b/FoodOrders/FoodOrdersListImplement/Models/Component.cs @@ -4,11 +4,14 @@ using FoodOrdersDataModels.Models; namespace FoodOrdersListImplement.Models { + //класс отвечает не только за хранение данных но также и за их изменение public class Component : IComponentModel { public int Id { get; private set; } public string ComponentName { get; private set; } = string.Empty; public double Cost { get; set; } + + //создаём из ComponentBindingModel Component public static Component? Create(ComponentBindingModel? model) { if (model == null) @@ -22,6 +25,8 @@ namespace FoodOrdersListImplement.Models Cost = model.Cost }; } + + //изменённые данные из бизнес-логики передаём в поля Component public void Update(ComponentBindingModel? model) { if (model == null) @@ -31,6 +36,8 @@ namespace FoodOrdersListImplement.Models ComponentName = model.ComponentName; Cost = model.Cost; } + + //получение ComponentViewModel из Component public ComponentViewModel GetViewModel => new() { Id = Id,