From 1a0cb1756bb6cebbee3ed37a84d14d22e725e1f1 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Mon, 27 Mar 2023 01:07:28 +0400 Subject: [PATCH] Created business logic project for construction company + binding models fixes --- ConstructionCompany/ConstructionCompany.sln | 6 + .../BusinessLogics/EmployeeLogic.cs | 114 +++++++++++++++ .../BusinessLogics/MaterialLogic.cs | 118 +++++++++++++++ .../BusinessLogics/OrderLogic.cs | 135 ++++++++++++++++++ .../BusinessLogics/PositionLogic.cs | 117 +++++++++++++++ .../Class1.cs | 7 + .../ConstructionCompanyBusinessLogic.csproj | 13 ++ .../BindingModels/OrderBindingModel.cs | 4 +- .../{IEmployeeModel.cs => IEmployeeLogic.cs} | 2 +- 9 files changed, 513 insertions(+), 3 deletions(-) create mode 100644 ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/EmployeeLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/MaterialLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/OrderLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/PositionLogic.cs create mode 100644 ConstructionCompany/ConstructionCompanyBusinessLogic/Class1.cs create mode 100644 ConstructionCompany/ConstructionCompanyBusinessLogic/ConstructionCompanyBusinessLogic.csproj rename ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/{IEmployeeModel.cs => IEmployeeLogic.cs} (94%) diff --git a/ConstructionCompany/ConstructionCompany.sln b/ConstructionCompany/ConstructionCompany.sln index 79d8a4e..33e343d 100644 --- a/ConstructionCompany/ConstructionCompany.sln +++ b/ConstructionCompany/ConstructionCompany.sln @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConstructionCompanyDataMode EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConstructionCompanyContracts", "ConstructionCompanyContracts\ConstructionCompanyContracts.csproj", "{CFB66158-7025-4605-9739-C4A2F07D2EB6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConstructionCompanyBusinessLogic", "ConstructionCompanyBusiness\ConstructionCompanyBusinessLogic.csproj", "{FB447245-07E1-4E0D-A4FC-701E295B7575}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {CFB66158-7025-4605-9739-C4A2F07D2EB6}.Debug|Any CPU.Build.0 = Debug|Any CPU {CFB66158-7025-4605-9739-C4A2F07D2EB6}.Release|Any CPU.ActiveCfg = Release|Any CPU {CFB66158-7025-4605-9739-C4A2F07D2EB6}.Release|Any CPU.Build.0 = Release|Any CPU + {FB447245-07E1-4E0D-A4FC-701E295B7575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB447245-07E1-4E0D-A4FC-701E295B7575}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB447245-07E1-4E0D-A4FC-701E295B7575}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB447245-07E1-4E0D-A4FC-701E295B7575}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/EmployeeLogic.cs b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/EmployeeLogic.cs new file mode 100644 index 0000000..77e64ab --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/EmployeeLogic.cs @@ -0,0 +1,114 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.BusinessLogicContracts; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyBusinessLogic.BusinessLogics +{ + public class EmployeeLogic :IEmployeeLogic + { + private readonly ILogger _logger; + private readonly IEmployeeStorage _employeeStorage; + + public EmployeeLogic(ILogger logger, IEmployeeStorage employeeStorage) + { + _logger = logger; + _employeeStorage = employeeStorage; + } + + public List? ReadList(EmployeeSearchModel? model) + { + _logger.LogInformation("ReadList. EmployeeName:{EmployeeName}. Id:{Id}", model?.EmployeeName, model?.Id); + var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public EmployeeViewModel? ReadElement(EmployeeSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. EmployeeName:{EmployeeName}. Id:{Id}", model.EmployeeName, model.Id); + var element = _employeeStorage.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(EmployeeBindingModel model) + { + CheckModel(model); + if (_employeeStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(EmployeeBindingModel model) + { + CheckModel(model); + if (_employeeStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(EmployeeBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_employeeStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(EmployeeBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.EmployeeName)) + { + throw new ArgumentNullException("Нет названия материала", nameof(model.EmployeeName)); + } + _logger.LogInformation("Employee. EmployeeName:{EmployeeName}. Id:{Id}", model.EmployeeName, model.Id); + var element = _employeeStorage.GetElement(new EmployeeSearchModel + { + EmployeeName = model.EmployeeName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Сотрудник с таким названием уже есть"); + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/MaterialLogic.cs b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/MaterialLogic.cs new file mode 100644 index 0000000..e122620 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/MaterialLogic.cs @@ -0,0 +1,118 @@ +using ConstructionCompanyContracts.BusinessLogicContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.BindingModels; + +namespace ConstructionCompanyBusinessLogic.BusinessLogics +{ + public class MaterialLogic : IMaterialLogic + { + private readonly ILogger _logger; + private readonly IMaterialStorage _materialStorage; + + public MaterialLogic(ILogger logger, IMaterialStorage materialStorage) + { + _logger = logger; + _materialStorage = materialStorage; + } + + public List? ReadList(MaterialSearchModel? model) + { + _logger.LogInformation("ReadList. MaterialName:{MaterialName}. Id:{Id}", model?.MaterialName, model?.Id); + var list = model == null ? _materialStorage.GetFullList() : _materialStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public MaterialViewModel? ReadElement(MaterialSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. MaterialName:{MaterialName}. Id:{Id}", model.MaterialName, model.Id); + var element = _materialStorage.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(MaterialBindingModel model) + { + CheckModel(model); + if (_materialStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(MaterialBindingModel model) + { + CheckModel(model); + if (_materialStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(MaterialBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_materialStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(MaterialBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.MaterialName)) + { + throw new ArgumentNullException("Нет названия материала", nameof(model.MaterialName)); + } + if (model.Quantity < 0) + { + throw new ArgumentNullException("Колчество материалов должна быть не меньше 0", nameof(model.Quantity)); + } + _logger.LogInformation("Material. IngredietnName:{MaterialName}. Quantity:{Quantity}. Id:{Id}", model.MaterialName, model.Quantity, model.Id); + var element = _materialStorage.GetElement(new MaterialSearchModel + { + MaterialName = model.MaterialName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Материал с таким названием уже есть"); + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/OrderLogic.cs b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..a8547f6 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,135 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.BusinessLogicContracts; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using ConstructionCompanyDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{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 OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{Id}", model.Id); + var element = _orderStorage.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 CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) return false; + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool FinishOrder(OrderBindingModel model) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (element.Status != OrderStatus.Выполняется) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Заказ должен быть переведен в статус выполнения перед готовностью!"); + } + model.Status = OrderStatus.Завершён; + _orderStorage.Update(model); + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + CheckModel(model, false); + var element = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (element == null) + { + _logger.LogWarning("Read operation failed"); + return false; + } + if (element.Status != OrderStatus.Принят) + { + _logger.LogWarning("Status change operation failed"); + throw new InvalidOperationException("Заказ должен быть переведен в статус принятого перед его выполнением!"); + } + model.Status = OrderStatus.Выполняется; + _orderStorage.Update(model); + return true; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + _logger.LogInformation("Order. Id:{Id}", model.Id); + var element = _orderStorage.GetElement(new OrderSearchModel + { + Id = model.Id + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Заказ с таким названием уже есть"); + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/PositionLogic.cs b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/PositionLogic.cs new file mode 100644 index 0000000..9dcb008 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusinessLogic/BusinessLogics/PositionLogic.cs @@ -0,0 +1,117 @@ +using ConstructionCompanyContracts.BindingModels; +using ConstructionCompanyContracts.SearchModels; +using ConstructionCompanyContracts.StorageContracts; +using ConstructionCompanyContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConstructionCompanyBusinessLogic.BusinessLogics +{ + public class PositionLogic + { + private readonly ILogger _logger; + private readonly IPositionStorage _positionStorage; + + public PositionLogic(ILogger logger, IPositionStorage positionStorage) + { + _logger = logger; + _positionStorage = positionStorage; + } + + public List? ReadList(PositionSearchModel? model) + { + _logger.LogInformation("ReadList. PositionName:{PositionName}. Id:{Id}", model?.PositionName, model?.Id); + var list = model == null ? _positionStorage.GetFullList() : _positionStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public PositionViewModel? ReadElement(PositionSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. PositionName:{PositionName}. Id:{Id}", model.PositionName, model.Id); + var element = _positionStorage.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(PositionBindingModel model) + { + CheckModel(model); + if (_positionStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(PositionBindingModel model) + { + CheckModel(model); + if (_positionStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(PositionBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_positionStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(PositionBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.PositionName)) + { + throw new ArgumentNullException("Нет названия материала", nameof(model.PositionName)); + } + if (model.Salary < 0) + { + throw new ArgumentNullException("Зарплата должна быть не меньше 0", nameof(model.Salary)); + } + _logger.LogInformation("Position. PositionName:{PositionName}. Salary:{Salary}. Id:{Id}", model.PositionName, model.Salary, model.Id); + var element = _positionStorage.GetElement(new PositionSearchModel + { + PositionName = model.PositionName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Должность с таким названием уже есть"); + } + } + } +} diff --git a/ConstructionCompany/ConstructionCompanyBusinessLogic/Class1.cs b/ConstructionCompany/ConstructionCompanyBusinessLogic/Class1.cs new file mode 100644 index 0000000..be8d116 --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusinessLogic/Class1.cs @@ -0,0 +1,7 @@ +namespace ConstructionCompanyBusiness +{ + public class Class1 + { + + } +} \ No newline at end of file diff --git a/ConstructionCompany/ConstructionCompanyBusinessLogic/ConstructionCompanyBusinessLogic.csproj b/ConstructionCompany/ConstructionCompanyBusinessLogic/ConstructionCompanyBusinessLogic.csproj new file mode 100644 index 0000000..3b52dab --- /dev/null +++ b/ConstructionCompany/ConstructionCompanyBusinessLogic/ConstructionCompanyBusinessLogic.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/ConstructionCompany/ConstructionCompanyContracts/BindingModels/OrderBindingModel.cs b/ConstructionCompany/ConstructionCompanyContracts/BindingModels/OrderBindingModel.cs index b8a3e6c..df4e0d4 100644 --- a/ConstructionCompany/ConstructionCompanyContracts/BindingModels/OrderBindingModel.cs +++ b/ConstructionCompany/ConstructionCompanyContracts/BindingModels/OrderBindingModel.cs @@ -14,9 +14,9 @@ namespace ConstructionCompanyContracts.BindingModels public string Adress { get; set; } = string.Empty; - public double Price => throw new NotImplementedException(); + public double Price { get; set; } - public OrderStatus Status => throw new NotImplementedException(); + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public string CustomerNumber { get; set; } = string.Empty; diff --git a/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeModel.cs b/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeLogic.cs similarity index 94% rename from ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeModel.cs rename to ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeLogic.cs index 2dfa3af..1041afe 100644 --- a/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeModel.cs +++ b/ConstructionCompany/ConstructionCompanyContracts/BusinessLogicContracts/IEmployeeLogic.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace ConstructionCompanyContracts.BusinessLogicContracts { - public interface IEmployeeModel + public interface IEmployeeLogic { List? ReadList(EmployeeSearchModel? model); EmployeeViewModel? ReadElement(EmployeeSearchModel model);