From b3091b8d9395f05c71f4b7d215985b2cdfc68878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B5=D1=80=D0=B8=D1=8F=20=D0=9D=D0=B8?= =?UTF-8?q?=D0=BA=D0=B8=D1=84=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= Date: Tue, 31 Jan 2023 22:54:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81=20=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=81=D1=83=D1=89=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B5=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AircraftPlant/AircraftPlant.sln | 6 + .../AircraftPlantBusinessLogic.csproj | 17 +++ .../ComponentLogic.cs | 119 ++++++++++++++++++ .../AircraftPlantBusinessLogic/OrderLogic.cs | 116 +++++++++++++++++ .../AircraftPlantBusinessLogic/PlaneLogic.cs | 119 ++++++++++++++++++ 5 files changed, 377 insertions(+) create mode 100644 AircraftPlant/AircraftPlantBusinessLogic/AircraftPlantBusinessLogic.csproj create mode 100644 AircraftPlant/AircraftPlantBusinessLogic/ComponentLogic.cs create mode 100644 AircraftPlant/AircraftPlantBusinessLogic/OrderLogic.cs create mode 100644 AircraftPlant/AircraftPlantBusinessLogic/PlaneLogic.cs diff --git a/AircraftPlant/AircraftPlant.sln b/AircraftPlant/AircraftPlant.sln index 5137035..41b11db 100644 --- a/AircraftPlant/AircraftPlant.sln +++ b/AircraftPlant/AircraftPlant.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantDataModels", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantContracts", "AircraftPlantContracts\AircraftPlantContracts.csproj", "{ECC2CC21-2172-4E1A-8806-CD3EFDC33F12}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AircraftPlantBusinessLogic", "AircraftPlantBusinessLogic\AircraftPlantBusinessLogic.csproj", "{65E42A4B-83FC-4029-B478-CE0E60A69F6B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {ECC2CC21-2172-4E1A-8806-CD3EFDC33F12}.Debug|Any CPU.Build.0 = Debug|Any CPU {ECC2CC21-2172-4E1A-8806-CD3EFDC33F12}.Release|Any CPU.ActiveCfg = Release|Any CPU {ECC2CC21-2172-4E1A-8806-CD3EFDC33F12}.Release|Any CPU.Build.0 = Release|Any CPU + {65E42A4B-83FC-4029-B478-CE0E60A69F6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {65E42A4B-83FC-4029-B478-CE0E60A69F6B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {65E42A4B-83FC-4029-B478-CE0E60A69F6B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {65E42A4B-83FC-4029-B478-CE0E60A69F6B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AircraftPlant/AircraftPlantBusinessLogic/AircraftPlantBusinessLogic.csproj b/AircraftPlant/AircraftPlantBusinessLogic/AircraftPlantBusinessLogic.csproj new file mode 100644 index 0000000..c4e60fb --- /dev/null +++ b/AircraftPlant/AircraftPlantBusinessLogic/AircraftPlantBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/AircraftPlant/AircraftPlantBusinessLogic/ComponentLogic.cs b/AircraftPlant/AircraftPlantBusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..98b47d1 --- /dev/null +++ b/AircraftPlant/AircraftPlantBusinessLogic/ComponentLogic.cs @@ -0,0 +1,119 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.BusinessLogicsContracts; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantBusinessLogic.BusinessLogics +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}.Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.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(ComponentSearchModel? 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"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия комплектующего", nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + 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 + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Комплектующий с таким названием уже есть"); + } + } + } +} diff --git a/AircraftPlant/AircraftPlantBusinessLogic/OrderLogic.cs b/AircraftPlant/AircraftPlantBusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..d246f41 --- /dev/null +++ b/AircraftPlant/AircraftPlantBusinessLogic/OrderLogic.cs @@ -0,0 +1,116 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.BusinessLogicsContracts; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using AircraftPlantDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) + { + CheckModel(model); + if (model.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + model.Status = newStatus; + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Готов); + } + + public bool FinishOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выдан); + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. OrderID:{Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выполняется); + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.PlaneId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор самолета", nameof(model.PlaneId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество самолетов в заказе должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + _logger.LogInformation("Order. OrderID:{Id}.Sum:{ Sum}. PlaneId: { PlaneId}", model.Id, model.Sum, model.PlaneId); + } + } +} diff --git a/AircraftPlant/AircraftPlantBusinessLogic/PlaneLogic.cs b/AircraftPlant/AircraftPlantBusinessLogic/PlaneLogic.cs new file mode 100644 index 0000000..294dea9 --- /dev/null +++ b/AircraftPlant/AircraftPlantBusinessLogic/PlaneLogic.cs @@ -0,0 +1,119 @@ +using AircraftPlantContracts.BindingModels; +using AircraftPlantContracts.BusinessLogicsContracts; +using AircraftPlantContracts.SearchModels; +using AircraftPlantContracts.StoragesContracts; +using AircraftPlantContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AircraftPlantBusinessLogic.BusinessLogics +{ + public class PlaneLogic : IPlaneLogic + { + private readonly ILogger _logger; + private readonly IPlaneStorage _planeStorage; + + public PlaneLogic(ILogger logger, IPlaneStorage planeStorage) + { + _logger = logger; + _planeStorage = planeStorage; + } + + public bool Create(PlaneBindingModel model) + { + CheckModel(model); + if (_planeStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(PlaneBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_planeStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public PlaneViewModel? ReadElement(PlaneSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. PlaneName:{PlaneName}.Id:{ Id}", model.PlaneName, model.Id); + var element = _planeStorage.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(PlaneSearchModel? model) + { + _logger.LogInformation("ReadList. PlaneName:{PlaneName}.Id:{ Id}", model?.PlaneName, model?.Id); + var list = model == null ? _planeStorage.GetFullList() : _planeStorage.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(PlaneBindingModel model) + { + CheckModel(model); + if (_planeStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(PlaneBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.PlaneName)) + { + throw new ArgumentNullException("Нет названия самолета", nameof(model.PlaneName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Стоимость самолета должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Plane. PlaneName:{PlaneName}.Price:{ Price}. Id: { Id}", model.PlaneName, model.Price, model.Id); + var element = _planeStorage.GetElement(new PlaneSearchModel + { + PlaneName = model.PlaneName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Самолет с таким названием уже есть"); + } + } + } +}