Слой бизнес логики
This commit is contained in:
parent
5c660c6a9c
commit
090d50e663
14
Subd/BusinessLogic/BusinessLogic.csproj
Normal file
14
Subd/BusinessLogic/BusinessLogic.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Contracts\Contracts.csproj" />
|
||||
<ProjectReference Include="..\DataBase\DataBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
109
Subd/BusinessLogic/BusinessLogic/CarBL.cs
Normal file
109
Subd/BusinessLogic/BusinessLogic/CarBL.cs
Normal file
@ -0,0 +1,109 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessBL.BusinessBL
|
||||
{
|
||||
public class CarBL : ICarLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ICarStorage _carStorage;
|
||||
public CarBL(ILogger<CarBL> logger, ICarStorage carStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_carStorage = carStorage;
|
||||
}
|
||||
public List<CarVM>? ReadList(CarSM? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. CarName:{CarName}.Id:{ Id}", model?.Model, model?.Id);
|
||||
var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public CarVM? ReadElement(CarSM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. CarName:{CarName}. Id:{ Id}", model.Model, model.Id);
|
||||
var element = _carStorage.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(CarBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_carStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(CarBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_carStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(CarBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_carStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(CarBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Model))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия машины", nameof(model.Model));
|
||||
}
|
||||
if (model.Tonnage <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Грузоподъемность должна быть больше 0", nameof(model.Tonnage));
|
||||
}
|
||||
if (model.StatusId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Стутус должн быть ", nameof(model.StatusId));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
115
Subd/BusinessLogic/BusinessLogic/CompanyBL.cs
Normal file
115
Subd/BusinessLogic/BusinessLogic/CompanyBL.cs
Normal file
@ -0,0 +1,115 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class CompanyBL : ICompanyLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ICompanyStorage _companyStorage;
|
||||
public CompanyBL(ILogger<CompanyBL> logger, ICompanyStorage companyStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_companyStorage = companyStorage;
|
||||
}
|
||||
public List<CompanyVM>? ReadList(CompanySM? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. CompanyName:{CompanyName}.Id:{ Id}", model?.Title, model?.Id);
|
||||
var list = model == null ? _companyStorage.GetFullList() : _companyStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public CompanyVM? ReadElement(CompanySM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. CompanyName:{CompanyName}. Id:{ Id}", model.Title, model.Id);
|
||||
var element = _companyStorage.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(CompanyBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_companyStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(CompanyBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_companyStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(CompanyBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_companyStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(CompanyBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Title))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия ", nameof(model.Title));
|
||||
}
|
||||
|
||||
if (model.StatusId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Стутус должн быть ", nameof(model.StatusId));
|
||||
}
|
||||
_logger.LogInformation("Car. CarName:{CarName}.Cost:{ Cost}. Id: { Id}", model.Title);
|
||||
var element = _companyStorage.GetElement(new CompanySM
|
||||
{
|
||||
Id = model.Id,
|
||||
Title = model.Title
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Такой компания уже есть");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
120
Subd/BusinessLogic/BusinessLogic/HumanBL.cs
Normal file
120
Subd/BusinessLogic/BusinessLogic/HumanBL.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using BusinessBL.BusinessBL;
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using DataBase.Implements;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class HumanBL : IHumanLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IHumanStorage _humanStorage;
|
||||
public HumanBL(ILogger<HumanBL> logger, IHumanStorage humanStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_humanStorage = humanStorage;
|
||||
}
|
||||
public List<HumanVM>? ReadList(HumanSM? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. HumanName:{HumanName}.Id:{ Id}", model?.Name, model?.Id);
|
||||
var list = model == null ? _humanStorage.GetFullList() : _humanStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public HumanVM? ReadElement(HumanSM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. HumanName:{HumanName}. Id:{ Id}", model.Name, model.Id);
|
||||
var element = _humanStorage.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(HumanBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_humanStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(HumanBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_humanStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(HumanBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_humanStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(HumanBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
throw new ArgumentNullException("Нет ФИО ", nameof(model.Name));
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Phone))
|
||||
{
|
||||
throw new ArgumentNullException("Нет номера телефона", nameof(model.Phone));
|
||||
}
|
||||
if (model.StatusId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Стутус должн быть ", nameof(model.StatusId));
|
||||
}
|
||||
_logger.LogInformation("Car. CarName:{CarName}.Cost:{ Cost}. Id: { Id}", model.Name);
|
||||
var element = _humanStorage.GetElement(new HumanSM
|
||||
{
|
||||
Id = model.Id,
|
||||
Phone = model.Phone
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Такой водитель уже есть");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
115
Subd/BusinessLogic/BusinessLogic/PlaceBL.cs
Normal file
115
Subd/BusinessLogic/BusinessLogic/PlaceBL.cs
Normal file
@ -0,0 +1,115 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using DataBase.Implements;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class PlaceBL : IPlaceLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPlaceStorage _placeStorage;
|
||||
public PlaceBL(ILogger<PlaceBL> logger, IPlaceStorage placeStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_placeStorage = placeStorage;
|
||||
}
|
||||
|
||||
public PlaceVM? ReadElement(PlaceSM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
||||
var element = _placeStorage.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(PlaceBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_placeStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(PlaceBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_placeStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(PlaceBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_placeStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(PlaceBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Title))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия ", nameof(model.Title));
|
||||
}
|
||||
|
||||
|
||||
_logger.LogInformation("Car. CarName:{CarName}.Cost:{ Cost}. Id: { Id}", model.Title);
|
||||
var element = _placeStorage.GetElement(new PlaceSM
|
||||
{
|
||||
Id = model.Id,
|
||||
Title = model.Title
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Такой статус уже есть");
|
||||
}
|
||||
}
|
||||
|
||||
public List<PlaceVM>? ReadList(PlaceSM model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. CarName:{CarName}.Id:{ Id}", model?.Title, model?.Id);
|
||||
var list = model == null ? _placeStorage.GetFullList() : _placeStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
121
Subd/BusinessLogic/BusinessLogic/RouteBL.cs
Normal file
121
Subd/BusinessLogic/BusinessLogic/RouteBL.cs
Normal file
@ -0,0 +1,121 @@
|
||||
using BusinessBL.BusinessBL;
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using DataBase.Implements;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class RouteBL : IRouteLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IRouteStorage _routeStorage;
|
||||
public RouteBL(ILogger<RouteBL> logger, IRouteStorage routeStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_routeStorage = routeStorage;
|
||||
}
|
||||
public List<RouteVM>? ReadList(RouteSM? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
|
||||
var list = model == null ? _routeStorage.GetFullList() : _routeStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public RouteVM? ReadElement(RouteSM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
||||
var element = _routeStorage.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(RouteBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_routeStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(RouteBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_routeStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(RouteBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_routeStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(RouteBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (model.Length <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Нет длины маршрута", nameof(model.Length));
|
||||
}
|
||||
if (model.PlaceStart <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("нет т-ки отправления", nameof(model.PlaceStart));
|
||||
}
|
||||
if (model.PlaceStart <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("нет т-ки назначения", nameof(model.PlaceEnd));
|
||||
}
|
||||
|
||||
var element = _routeStorage.GetElement(new RouteSM
|
||||
{
|
||||
Id = model.Id,
|
||||
Start = model.PlaceStart,
|
||||
End = model.PlaceEnd
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Такой маршрут уже есть");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
112
Subd/BusinessLogic/BusinessLogic/StatusBL.cs
Normal file
112
Subd/BusinessLogic/BusinessLogic/StatusBL.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class StatusBL : IStatusLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IStatusStorage _statusStorage;
|
||||
public StatusBL(ILogger<StatusBL> logger, IStatusStorage statusStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_statusStorage = statusStorage;
|
||||
}
|
||||
public List<StatusVM>? ReadList()
|
||||
{
|
||||
_logger.LogInformation("ReadList");
|
||||
var list = _statusStorage.GetFullList() ;
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public StatusVM? ReadElement(StatusSM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
||||
var element = _statusStorage.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(StatusBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_statusStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Update(StatusBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_statusStorage.Update(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Update operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public bool Delete(StatusBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_statusStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(StatusBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (string.IsNullOrEmpty(model.Title))
|
||||
{
|
||||
throw new ArgumentNullException("Нет названия ", nameof(model.Title));
|
||||
}
|
||||
|
||||
|
||||
_logger.LogInformation("Car. CarName:{CarName}.Cost:{ Cost}. Id: { Id}", model.Title);
|
||||
var element = _statusStorage.GetElement(new StatusSM
|
||||
{
|
||||
Id = model.Id,
|
||||
Title = model.Title
|
||||
});
|
||||
if (element != null && element.Id != model.Id)
|
||||
{
|
||||
throw new InvalidOperationException("Такой статус уже есть");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
112
Subd/BusinessLogic/BusinessLogic/VoyageBL.cs
Normal file
112
Subd/BusinessLogic/BusinessLogic/VoyageBL.cs
Normal file
@ -0,0 +1,112 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BusinessLogic;
|
||||
using Contracts.SearchModel;
|
||||
using Contracts.Storage;
|
||||
using Contracts.ViewModels;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic.BusinessLogic
|
||||
{
|
||||
public class VoyageBL : IVoyageLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IVoyageStorage _voyageStorage;
|
||||
public VoyageBL(ILogger<VoyageBL> logger, IVoyageStorage voyageStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_voyageStorage = voyageStorage;
|
||||
}
|
||||
public List<VoyageVM>? ReadList(VoyageSM? model)
|
||||
{
|
||||
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
|
||||
var list = model == null ? _voyageStorage.GetFullList() : _voyageStorage.GetFilteredList(model);
|
||||
if (list == null)
|
||||
{
|
||||
_logger.LogWarning("ReadList return null list");
|
||||
return null;
|
||||
}
|
||||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||||
return list;
|
||||
}
|
||||
public VoyageVM? ReadElement(VoyageSM model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
||||
var element = _voyageStorage.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(VoyageBM model)
|
||||
{
|
||||
CheckModel(model);
|
||||
if (_voyageStorage.Insert(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Insert operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Delete(VoyageBM model)
|
||||
{
|
||||
CheckModel(model, false);
|
||||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||
if (_voyageStorage.Delete(model) == null)
|
||||
{
|
||||
_logger.LogWarning("Delete operation failed");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private void CheckModel(VoyageBM model, bool withParams =
|
||||
true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(model));
|
||||
}
|
||||
if (!withParams)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (model.CarId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("Нет машины", nameof(model.CarId));
|
||||
}
|
||||
if (model.CompanyId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("нет заказчкика", nameof(model.CompanyId));
|
||||
}
|
||||
if (model.HumanId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("нет водилы", nameof(model.HumanId));
|
||||
}
|
||||
if (model.HumanId <= 0)
|
||||
{
|
||||
throw new ArgumentNullException("нет водилы", nameof(model.HumanId));
|
||||
}
|
||||
if (!model.DateEnd.HasValue)
|
||||
{
|
||||
throw new ArgumentNullException("нет дедлайна", nameof(model.HumanId));
|
||||
}
|
||||
if (!model.DateStart.HasValue)
|
||||
{
|
||||
throw new ArgumentNullException("нет даты выезда", nameof(model.HumanId));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -14,7 +14,7 @@ namespace Contracts.BusinessLogic
|
||||
List<VoyageVM>? ReadList(VoyageSM model);
|
||||
VoyageVM? ReadElement(VoyageSM model);
|
||||
bool Create(VoyageBM model);
|
||||
bool Update(VoyageBM model);
|
||||
|
||||
bool Delete(VoyageBM model);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ namespace Contracts.SearchModel
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public int? Id { get; set; }
|
||||
public string? Phone { get; set; }
|
||||
public int? StatusId { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -10,5 +10,7 @@ namespace Contracts.SearchModel
|
||||
{
|
||||
public int? Length { get; set; }
|
||||
public int? Id { get; set; }
|
||||
public int? Start { get; set; }
|
||||
public int? End { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,6 @@ namespace Contracts.SearchModel
|
||||
public class StatusSM
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
public string? Title { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -39,13 +39,13 @@ namespace DataBase.Implements
|
||||
|
||||
public CompanyVM? GetElement(CompanySM model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Title))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LogisticContext();
|
||||
return context.Companies
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Title) && x.Title.Equals(model.Title)))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
|
@ -39,13 +39,13 @@ namespace DataBase.Implements
|
||||
|
||||
public HumanVM? GetElement(HumanSM model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Phone))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LogisticContext();
|
||||
return context.Humans
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Phone) && x.Phone.Equals(model.Phone)))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
|
@ -24,28 +24,39 @@ namespace DataBase.Implements
|
||||
|
||||
public List<RouteVM> GetFilteredList(RouteSM model)
|
||||
{
|
||||
if (model.Length.HasValue)
|
||||
if (!model.Length.HasValue && !model.Start.HasValue && !model.End.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new LogisticContext();
|
||||
if(model.Length.HasValue)
|
||||
return context.Routes
|
||||
.FromSqlRaw("Select * FROM Route WHERE Length <= {0}", model.Length)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
|
||||
else if(model.Start.HasValue)
|
||||
return context.Routes
|
||||
.FromSqlRaw("Select * FROM Route WHERE PlaceStart = {0} ", model.Start)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
return context.Routes
|
||||
.FromSqlRaw("Select * FROM Route WHERE PlaceEnd = {0} ", model.End)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public RouteVM? GetElement(RouteSM model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
if (!model.Id.HasValue && !model.Start.HasValue && !model.End.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LogisticContext();
|
||||
return context.Routes
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (model.Start.HasValue && model.End.HasValue && x.PlaceEnd == model.End && x.PlaceStart == model.Start ))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
|
@ -26,13 +26,13 @@ namespace DataBase.Implements
|
||||
|
||||
public StatusVM? GetElement(StatusSM model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Title))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LogisticContext();
|
||||
return context.Statuses
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (string.IsNullOrEmpty(model.Title) && x.Title.Equals(model.Title)))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataBase", "DataBase\DataBa
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Forms", "Forms\Forms.csproj", "{1901CFE4-7DA9-423E-A3A1-20A3444CC7F7}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BusinessLogic", "BusinessLogic\BusinessLogic.csproj", "{6ED0D589-EC71-42CE-A20E-C7C1E036F26B}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -33,6 +35,10 @@ Global
|
||||
{1901CFE4-7DA9-423E-A3A1-20A3444CC7F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1901CFE4-7DA9-423E-A3A1-20A3444CC7F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1901CFE4-7DA9-423E-A3A1-20A3444CC7F7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6ED0D589-EC71-42CE-A20E-C7C1E036F26B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6ED0D589-EC71-42CE-A20E-C7C1E036F26B}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6ED0D589-EC71-42CE-A20E-C7C1E036F26B}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6ED0D589-EC71-42CE-A20E-C7C1E036F26B}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
Reference in New Issue
Block a user