From a6bcc01785ecf9a19f8e7d3a73e7faea2735e18b Mon Sep 17 00:00:00 2001 From: Viltskaa Date: Mon, 30 Jan 2023 19:25:38 +0400 Subject: [PATCH] add Business logic part --- SushiBar/SushiBar.sln | 6 + .../BusinessLogics/ComponentLogic.cs | 109 ++++++++++++++++ .../BusinessLogics/OrderLogic.cs | 118 ++++++++++++++++++ .../BusinessLogics/SushiLogic.cs | 113 +++++++++++++++++ .../SushiBarBusinessLogic.csproj | 17 +++ 5 files changed, 363 insertions(+) create mode 100644 SushiBar/SushiBarBusinessLogic/BusinessLogics/ComponentLogic.cs create mode 100644 SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs create mode 100644 SushiBar/SushiBarBusinessLogic/BusinessLogics/SushiLogic.cs create mode 100644 SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index 41efb0e..f102172 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDataModels", "Sushi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "SushiBarContracts\SushiBarContracts.csproj", "{1185D549-D227-4BE3-8EAD-D294029BCA5D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{A273021E-72E7-4C23-927C-A3E4851BD8D1}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {1185D549-D227-4BE3-8EAD-D294029BCA5D}.Debug|Any CPU.Build.0 = Debug|Any CPU {1185D549-D227-4BE3-8EAD-D294029BCA5D}.Release|Any CPU.ActiveCfg = Release|Any CPU {1185D549-D227-4BE3-8EAD-D294029BCA5D}.Release|Any CPU.Build.0 = Release|Any CPU + {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A273021E-72E7-4C23-927C-A3E4851BD8D1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/ComponentLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..55f35b6 --- /dev/null +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,109 @@ +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace SushiBarBusinessLogic.BusinessLogics +{ + 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("Name of component is empty", nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Cost must be more then zero", 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("This name is already exists"); + } + } + } + +} + diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..71f7ad1 --- /dev/null +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,118 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; + +namespace SushiBarBusinessLogic.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); + model.Status = SushiBarDataModels.Enums.OrderStatus.Accepted; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool DeliveryOrder(OrderBindingModel model) + { + CheckModel(model); + OrderViewModel? element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (element == null || model.Status - element?.Status != 1) + { + _logger.LogWarning("Delivery order is not ready"); + return false; + } + element.Status = SushiBarDataModels.Enums.OrderStatus.Ready; + return true; + } + + public bool FinishOrder(OrderBindingModel model) + { + CheckModel(model); + OrderViewModel? element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id }); + if (element == null || model.Status - element?.Status != 1) + { + _logger.LogWarning("Finish order is not ready"); + return false; + } + element.Status = SushiBarDataModels.Enums.OrderStatus.Issued; + return true; + } + + 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) + { + _logger.LogInformation("ReadList .Id:{Id}", model?.Id); + List list = _orderStorage.GetFullList(); + foreach (OrderViewModel order in list) + { + if (order.Status is >= (SushiBarDataModels.Enums.OrderStatus)1 and not (SushiBarDataModels.Enums.OrderStatus)3) + { + 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.SushiId <= 0) + { + throw new ArgumentNullException("Sushi id must be more then zero", nameof(model.SushiId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Count must be more then zero", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Sum must be more then zero", nameof(model.Sum)); + } + _logger.LogInformation("Order. OrderId:{Id} .Count:{Count} .Sum:{Sum} .Status{Status} .DateCreate{DateCreate}", model.Id, model.Count, model.Sum, model.Status.ToString(), model.DateCreate.ToString()); + var element = _orderStorage.GetElement(new OrderSearchModel { + Id = model.Id + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("This name is already exists"); + } + } + } +} diff --git a/SushiBar/SushiBarBusinessLogic/BusinessLogics/SushiLogic.cs b/SushiBar/SushiBarBusinessLogic/BusinessLogics/SushiLogic.cs new file mode 100644 index 0000000..9dc95fe --- /dev/null +++ b/SushiBar/SushiBarBusinessLogic/BusinessLogics/SushiLogic.cs @@ -0,0 +1,113 @@ +using Microsoft.Extensions.Logging; +using SushiBarContracts.BindingModels; +using SushiBarContracts.BusinessLogicsContracts; +using SushiBarContracts.SearchModels; +using SushiBarContracts.StoragesContracts; +using SushiBarContracts.ViewModels; + +namespace SushiBarBusinessLogic.BusinessLogics +{ + public class SushiLogic : ISushiLogic + { + private readonly ILogger _logger; + private readonly ISushiStorage _sushiStorage; + + public SushiLogic(ILogger logger, ISushiStorage sushiStorage) + { + _logger = logger; + _sushiStorage = sushiStorage; + } + + public bool Create(SushiBindingModel model) + { + CheckModel(model); + if (_sushiStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(SushiBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_sushiStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public SushiViewModel? ReadElement(SushiSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. SushiName:{SushiName}.Id:{Id}", model.SushiName, model.Id); + var element = _sushiStorage.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(SushiSearchModel? model) + { + _logger.LogInformation("ReadList. SushiName:{SushiName}.Id:{Id}", model?.SushiName, model?.Id); + var list = model == null ? _sushiStorage.GetFullList() : _sushiStorage.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(SushiBindingModel model) + { + CheckModel(model); + if (_sushiStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(SushiBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.SushiName)) + { + throw new ArgumentNullException("Name of sushi is empty", nameof(model.SushiName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Price must be more then zero", nameof(model.Price)); + } + _logger.LogInformation("Sushi. SushiName:{SushiName} .Price:{Price}. Id:{Id}", model.SushiName, model.Price, model.Id); + var element = _sushiStorage.GetElement(new SushiSearchModel { + SushiName = model.SushiName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("This name is already exists"); + } + } + } +} diff --git a/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj b/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj new file mode 100644 index 0000000..015a4b0 --- /dev/null +++ b/SushiBar/SushiBarBusinessLogic/SushiBarBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + +