add Business logic part

This commit is contained in:
Viltskaa 2023-01-30 19:25:38 +04:00
parent 3e25205bf6
commit a6bcc01785
5 changed files with 363 additions and 0 deletions

View File

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDataModels", "Sushi
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "SushiBarContracts\SushiBarContracts.csproj", "{1185D549-D227-4BE3-8EAD-D294029BCA5D}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "SushiBarContracts\SushiBarContracts.csproj", "{1185D549-D227-4BE3-8EAD-D294029BCA5D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{A273021E-72E7-4C23-927C-A3E4851BD8D1}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{1185D549-D227-4BE3-8EAD-D294029BCA5D}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -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<ComponentLogic> logger, IComponentStorage componentStorage)
{
_logger = logger;
_componentStorage = componentStorage;
}
public List<ComponentViewModel>? 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");
}
}
}
}

View File

@ -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<OrderViewModel>? 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<OrderViewModel> 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");
}
}
}
}

View File

@ -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<ComponentLogic> 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<SushiViewModel>? 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");
}
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SushiBarContracts\SushiBarContracts.csproj" />
</ItemGroup>
</Project>