business logic
This commit is contained in:
parent
6aa28811fd
commit
0e46bc8b1f
13
BusinessLogic/BusinessLogic.csproj
Normal file
13
BusinessLogic/BusinessLogic.csproj
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\TransportGuideContracts\TransportGuideContracts.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
119
BusinessLogic/BusinessLogic/RouteLogic.cs
Normal file
119
BusinessLogic/BusinessLogic/RouteLogic.cs
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TransportGuideContracts.BindingModels;
|
||||||
|
using TransportGuideContracts.BusinessLogicsContracts;
|
||||||
|
using TransportGuideContracts.SearchModels;
|
||||||
|
using TransportGuideContracts.StoragesContracts;
|
||||||
|
using TransportGuideContracts.ViewModels;
|
||||||
|
|
||||||
|
namespace BusinessLogic.BusinessLogic
|
||||||
|
{
|
||||||
|
public class RouteLogic : IRouteLogic
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly IRouteStorage _routeStorage;
|
||||||
|
|
||||||
|
public RouteLogic(ILogger<RouteLogic> logger, IRouteStorage routeStorage)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_routeStorage = routeStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Create(RouteBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_routeStorage.Insert(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Insert operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Delete(RouteBindingModel 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RouteViewModel? ReadElement(RouteSearchModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement. RouteName:{RouteName}.Id:{ Id}", model.Name, 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 List<RouteViewModel>? ReadList(RouteSearchModel? model)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("ReadList. RouteName:{RoutetName}.Id:{ Id}", model?.Name, 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 bool Update(RouteBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_routeStorage.Update(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Update operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private void CheckModel(RouteBindingModel 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.IP))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("No IP", nameof(model.IP));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("Route. Name:{Name}. IP:{ IP}. Id: { Id}", model.Name, model.IP, model.Id);
|
||||||
|
var element = _routeStorage.GetElement(new RouteSearchModel
|
||||||
|
{
|
||||||
|
Name = model.Name
|
||||||
|
});
|
||||||
|
if (element != null && element.Id != model.Id)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Route с таким названием уже есть");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
116
BusinessLogic/BusinessLogic/StopLogic.cs
Normal file
116
BusinessLogic/BusinessLogic/StopLogic.cs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TransportGuideContracts.BindingModels;
|
||||||
|
using TransportGuideContracts.BusinessLogicsContracts;
|
||||||
|
using TransportGuideContracts.SearchModels;
|
||||||
|
using TransportGuideContracts.StoragesContracts;
|
||||||
|
using TransportGuideContracts.ViewModels;
|
||||||
|
|
||||||
|
namespace BusinessLogic.BusinessLogic
|
||||||
|
{
|
||||||
|
public class StopLogic : IStopLogic
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly IStopStorage _stopStorage;
|
||||||
|
|
||||||
|
public StopLogic(ILogger<StopLogic> logger, IStopStorage stopStorage)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_stopStorage = stopStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Create(StopBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_stopStorage.Insert(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Insert operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Delete(StopBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model, false);
|
||||||
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||||
|
if (_stopStorage.Delete(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Delete operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StopViewModel? ReadElement(StopSearchModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
|
||||||
|
var element = _stopStorage.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<StopViewModel>? ReadList(StopSearchModel? model)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
|
||||||
|
var list = model == null ? _stopStorage.GetFullList() : _stopStorage.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(StopBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_stopStorage.Update(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Update operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private void CheckModel(StopBindingModel 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Stop. Name:{Name}. Id: { Id}", model.Name,model.Id);
|
||||||
|
var element = _stopStorage.GetElement(new StopSearchModel
|
||||||
|
{
|
||||||
|
Name = model.Name
|
||||||
|
});
|
||||||
|
if (element != null && element.Id != model.Id)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Stop с таким названием уже есть");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
119
BusinessLogic/BusinessLogic/TransportTypeLogic.cs
Normal file
119
BusinessLogic/BusinessLogic/TransportTypeLogic.cs
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TransportGuideContracts.BindingModels;
|
||||||
|
using TransportGuideContracts.BusinessLogicsContracts;
|
||||||
|
using TransportGuideContracts.SearchModels;
|
||||||
|
using TransportGuideContracts.StoragesContracts;
|
||||||
|
using TransportGuideContracts.ViewModels;
|
||||||
|
|
||||||
|
namespace BusinessLogic.BusinessLogic
|
||||||
|
{
|
||||||
|
public class TransportTypeLogic : ITransportTypeLogic
|
||||||
|
{
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
private readonly ITransportTypeStorage _transportTypeStorage;
|
||||||
|
|
||||||
|
public TransportTypeLogic(ILogger<TransportTypeLogic> logger, ITransportTypeStorage transportTypeStorage)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_transportTypeStorage = transportTypeStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Create(TransportTypeBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_transportTypeStorage.Insert(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Insert operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Delete(TransportTypeBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model, false);
|
||||||
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||||||
|
if (_transportTypeStorage.Delete(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Delete operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransportTypeViewModel? ReadElement(TransportTypeSearchModel model)
|
||||||
|
{
|
||||||
|
if (model == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(model));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
|
||||||
|
var element = _transportTypeStorage.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<TransportTypeViewModel>? ReadList(TransportTypeSearchModel? model)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
|
||||||
|
var list = model == null ? _transportTypeStorage.GetFullList() : _transportTypeStorage.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(TransportTypeBindingModel model)
|
||||||
|
{
|
||||||
|
CheckModel(model);
|
||||||
|
if (_transportTypeStorage.Update(model) == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Update operation failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
private void CheckModel(TransportTypeBindingModel 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 (model.Price <= 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("Цена должна быть больше 0", nameof(model.Price));
|
||||||
|
}
|
||||||
|
_logger.LogInformation("TransportType. Name:{Name}. Price:{ Price}. Id: { Id}", model.Name, model.Price, model.Id);
|
||||||
|
var element = _transportTypeStorage.GetElement(new TransportTypeSearchModel
|
||||||
|
{
|
||||||
|
Name = model.Name
|
||||||
|
});
|
||||||
|
if (element != null && element.Id != model.Id)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Type с таким названием уже есть");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransportGuideDatabaseImple
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransportGuideContracts", "..\TransportGuideContracts\TransportGuideContracts.csproj", "{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransportGuideContracts", "..\TransportGuideContracts\TransportGuideContracts.csproj", "{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BusinessLogic", "..\BusinessLogic\BusinessLogic.csproj", "{09F7C041-606C-4D60-AF2F-14B030954930}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@ -33,6 +35,10 @@ Global
|
|||||||
{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}.Release|Any CPU.Build.0 = Release|Any CPU
|
{C54EC4EF-09BD-4FFC-84A1-7C938F0803FB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{09F7C041-606C-4D60-AF2F-14B030954930}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{09F7C041-606C-4D60-AF2F-14B030954930}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{09F7C041-606C-4D60-AF2F-14B030954930}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{09F7C041-606C-4D60-AF2F-14B030954930}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
@ -9,9 +9,9 @@ namespace TransportGuideContracts.BindingModels
|
|||||||
{
|
{
|
||||||
public class TransportTypeBindingModel : ITransportTypeModel
|
public class TransportTypeBindingModel : ITransportTypeModel
|
||||||
{
|
{
|
||||||
public double price { get; set; }
|
public double Price { get; set; }
|
||||||
|
|
||||||
public string name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ namespace TransportGuideContracts.ViewModels
|
|||||||
public class TransportTypeViewModel : ITransportTypeModel
|
public class TransportTypeViewModel : ITransportTypeModel
|
||||||
{
|
{
|
||||||
[DisplayName("Ticket's Price")]
|
[DisplayName("Ticket's Price")]
|
||||||
public double price { get; set; }
|
public double Price { get; set; }
|
||||||
[DisplayName("Transport type name")]
|
[DisplayName("Transport type name")]
|
||||||
public string name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ namespace TransportGuideDataModels.Models
|
|||||||
{
|
{
|
||||||
public interface ITransportTypeModel : IId
|
public interface ITransportTypeModel : IId
|
||||||
{
|
{
|
||||||
double price { get;}
|
double Price { get;}
|
||||||
string name { get;}
|
string Name { get;}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user