diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ComponentLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..b6a8da5 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/ComponentLogic.cs @@ -0,0 +1,113 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContarcts; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; + +namespace FurnitureAssemblyBusinessLogic +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + public ComponentLogic(ILogger logger, IComponentStorage + componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + 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 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 bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update 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; + } + 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/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj new file mode 100644 index 0000000..0a4e5c8 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureAssemblyBusinessLogic.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureLogic.cs new file mode 100644 index 0000000..0326d00 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/FurnitureLogic.cs @@ -0,0 +1,115 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContarcts; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyBusinessLogic +{ + public class FurnitureLogic : IFurnitureLogic + { + private readonly ILogger _logger; + private readonly IFurnitureStorage _furnitureStorage; + public FurnitureLogic(ILogger logger, IFurnitureStorage furnitureStorage) + { + _logger = logger; + _furnitureStorage = furnitureStorage; + } + public bool Create(FurnitureBindingModel model) + { + CheckModel(model); + if (_furnitureStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(FurnitureBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_furnitureStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public FurnitureViewModel? ReadElement(FurnitureSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. FurnitureName:{FurnitureName}. Id:{ Id}", model.FurnitureName, model.Id); + var element = _furnitureStorage.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(FurnitureSearchModel? model) + { + _logger.LogInformation("ReadList. FurnitureName:{FurnitureName}. Id:{ Id}", model?.FurnitureName, model?.Id); + var list = model == null ? _furnitureStorage.GetFullList() : _furnitureStorage.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(FurnitureBindingModel model) + { + CheckModel(model); + if (_furnitureStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(FurnitureBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.FurnitureName)) + { + throw new ArgumentNullException("Нет названия изделия", + nameof(model.FurnitureName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена изделия должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Furniture. FurnitureName:{FurnitureName}. Price:{ Price}. Id: { Id}", model.FurnitureName, model.Price, model.Id); + var element = _furnitureStorage.GetElement(new FurnitureSearchModel { FurnitureName = model.FurnitureName }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Изделие с таким названием уже есть"); + } + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..3396e12 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemblyBusinessLogic/OrderLogic.cs @@ -0,0 +1,108 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.BusinessLogicsContarcts; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemblyDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemblyBusinessLogic +{ + 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 (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + model.Status = OrderStatus.Принят; + return true; + } + + public bool DeliveryOrder(OrderBindingModel model) + { + if (model.Status.Equals(OrderStatus.Готов)) + { + model.Status = OrderStatus.Выдан; + model.DateImplement = DateTime.Now; + return true; + } + return false; + } + + public bool FinishOrder(OrderBindingModel model) + { + if (model.Status.Equals(OrderStatus.Выполняется)) + { + model.Status = OrderStatus.Готов; + return true; + } + return false; + } + + 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 bool TakeOrderInWork(OrderBindingModel model) + { + if (model.Status.Equals(OrderStatus.Принят)) + { + model.Status = OrderStatus.Выполняется; + return true; + } + return false; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.FurnitureId <= 0) + { + throw new ArgumentNullException("Идентификатор изделия должен быть больше 0", nameof(model.FurnitureId)); + } + 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. FurnitureId:{FurnitureId}. Count:{ Count}. Sum:{ Sum}. OrderStatus: {OrderStatus} DateCreate: {DateCreate}. Id: { Id}", model.FurnitureId, model.Count, model.Sum, model.Status, model.DateCreate, model.Id); + } + } +}