diff --git a/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/CargoLogic.cs b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/CargoLogic.cs new file mode 100644 index 0000000..7b166e1 --- /dev/null +++ b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/CargoLogic.cs @@ -0,0 +1,149 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.BusinessLogicsContracts; +using TransportCompanyContracts.SearchModels; +using TransportCompanyContracts.StoragesContracts; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompanyBusinessLogic.BusinessLogic +{ + public class CargoLogic : ICargoLogic + { + private readonly ILogger _logger; + + private readonly ICargoStorage _cargoStorage; + + //конструктор + public CargoLogic(ILogger logger, ICargoStorage cargoStorage) + { + _logger = logger; + _cargoStorage = cargoStorage; + } + + public List? ReadList(CargoSearchModel? model) + { + _logger.LogInformation("ReadList. TypeCargo:{TypeCargo}. Id:{Id}", model?.TypeCargo, model?.Id); + + //list хранит весь список в случае, если model пришло со значением null на вход метода + var list = model == null ? _cargoStorage.GetFullList() : _cargoStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public CargoViewModel? ReadElement(CargoSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. TypeCargo:{TypeCargo}. Id:{Id}", model.TypeCargo, model.Id); + + var element = _cargoStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", model.Id); + + return element; + } + + public bool Create(CargoBindingModel model) + { + CheckModel(model); + + if (_cargoStorage.Insert(model) == null) + { + _logger.LogWarning("Create operation failed"); + + return false; + } + + return true; + } + + public bool Update(CargoBindingModel model) + { + CheckModel(model); + + if (_cargoStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + public bool Delete(CargoBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_cargoStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(CargoBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + //так как при удалении параметром withParams передаём false + if (!withParams) + { + return; + } + + //проверка на наличие названия типа груза + if (string.IsNullOrEmpty(model.TypeCargo)) + { + throw new ArgumentNullException("названия типа груза", nameof(model.TypeCargo)); + } + + _logger.LogInformation("Cargo. TypeCargo:{TypeCargo}. Id:{Id}", + model.TypeCargo, model.Id); + + //проверка на наличие такого же типа груза в списке + var element = _cargoStorage.GetElement(new CargoSearchModel + { + TypeCargo = model.TypeCargo, + }); + + //если элемент найден и его Id не совпадает с Id объекта, переданного на вход + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Такой тип груза уже есть"); + } + } + } +} diff --git a/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/ClientLogic.cs b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/ClientLogic.cs index 0c8fc77..9c80d5c 100644 --- a/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/ClientLogic.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,6 +7,7 @@ using System.Threading.Tasks; using TransportCompanyContracts.BindingModels; using TransportCompanyContracts.BusinessLogicsContracts; using TransportCompanyContracts.SearchModels; +using TransportCompanyContracts.StoragesContracts; using TransportCompanyContracts.ViewModels; namespace TransportCompanyBusinessLogic.BusinessLogic @@ -14,29 +16,158 @@ namespace TransportCompanyBusinessLogic.BusinessLogic { private readonly ILogger _logger; - public ClientViewModel? ReadElement(ClientSearchModel model) + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) { - throw new NotImplementedException(); + _logger = logger; + _clientStorage = clientStorage; } public List? ReadList(ClientSearchModel? model) { - throw new NotImplementedException(); + _logger.LogInformation("ReadList. Surname:{Surname}. Id:{Id}", model?.Surname, model?.Id); + + //list хранит весь список в случае, если model пришло со значением null на вход метода + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Surname:{Surname}. Id:{Id}", model.Surname, model.Id); + + var element = _clientStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", model.Id); + + return element; } public bool Create(ClientBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Create operation failed"); + + return false; + } + + return true; } public bool Update(ClientBindingModel model) { - throw new NotImplementedException(); + CheckModel(model); + + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; } public bool Delete(ClientBindingModel model) { - throw new NotImplementedException(); + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + //так как при удалении параметром withParams передаём false + if (!withParams) + { + return; + } + + //проверка на наличие имени + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет имени клиента", nameof(model.Name)); + } + + //проверка на наличие фамилии + if (string.IsNullOrEmpty(model.Surname)) + { + throw new ArgumentNullException("Нет фамилии клиента", nameof(model.Surname)); + } + + //проверка на наличие отчества + if (string.IsNullOrEmpty(model.Patronymic)) + { + throw new ArgumentNullException("Нет отчества клиента", nameof(model.Patronymic)); + } + + //проверка на наличие телефонного номера + if (string.IsNullOrEmpty(model.TelephoneNumber)) + { + throw new ArgumentNullException("Нет телефонного номера клиента", nameof(model.TelephoneNumber)); + } + + //проверка на наличие почты + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет электронной почты клиента", nameof(model.Email)); + } + + _logger.LogInformation("Client. Name:{Name}. Surname:{Surname}. Patronymic:{Patronymic}. " + + "TelephoneNumber:{TelephoneNumber}. Email:{Email}. Id:{Id}", + model.Name, model.Surname, model.Patronymic, model.TelephoneNumber, model.Email, model.Id); + + //проверка на наличие такой же почты в списке + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email, + }); + + //если почта найдена и его Id не совпадает с Id объекта, переданного на вход + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с такой почтой уже есть"); + } } } } diff --git a/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TransportLogic.cs b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TransportLogic.cs new file mode 100644 index 0000000..8102dab --- /dev/null +++ b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TransportLogic.cs @@ -0,0 +1,148 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.BusinessLogicsContracts; +using TransportCompanyContracts.SearchModels; +using TransportCompanyContracts.StoragesContracts; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompanyBusinessLogic.BusinessLogic +{ + public class TransportLogic : ITransportLogic + { + private readonly ILogger _logger; + + private readonly ITransportStorage _transportStorage; + + //конструктор + public TransportLogic(ILogger logger, ITransportStorage transportStorage) + { + _logger = logger; + _transportStorage = transportStorage; + } + + public List? ReadList(TransportSearchModel? model) + { + _logger.LogInformation("ReadList. Tranport:{Tranport}. Id:{Id}", model?.Tranport, model?.Id); + + //list хранит весь список в случае, если model пришло со значением null на вход метода + var list = model == null ? _transportStorage.GetFullList() : _transportStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public TransportViewModel? ReadElement(TransportSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Tranport:{Tranport}. Id:{Id}", model.Tranport, model.Id); + + var element = _transportStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", model.Id); + + return element; + } + + public bool Create(TransportBindingModel model) + { + CheckModel(model); + + if (_transportStorage.Insert(model) == null) + { + _logger.LogWarning("Create operation failed"); + + return false; + } + + return true; + } + + public bool Update(TransportBindingModel model) + { + CheckModel(model); + + if (_transportStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + public bool Delete(TransportBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_transportStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(TransportBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + //так как при удалении параметром withParams передаём false + if (!withParams) + { + return; + } + + //проверка на наличие названия транспортного средства + if (string.IsNullOrEmpty(model.Tranport)) + { + throw new ArgumentNullException("Нет названия транспортного средства", nameof(model.Tranport)); + } + + _logger.LogInformation("Tranport. Tranport:{Tranport}. Id:{Id}", model.Tranport, model.Id); + + //проверка на наличие такого же транспортного средства в списке + var element = _transportStorage.GetElement(new TransportSearchModel + { + Tranport = model.Tranport, + }); + + //если элемент найден и его Id не совпадает с Id объекта, переданного на вход + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Такое транспортное средство уже есть"); + } + } + } +} diff --git a/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TransportationLogic.cs b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TransportationLogic.cs new file mode 100644 index 0000000..d87f557 --- /dev/null +++ b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TransportationLogic.cs @@ -0,0 +1,150 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.BusinessLogicsContracts; +using TransportCompanyContracts.SearchModels; +using TransportCompanyContracts.StoragesContracts; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompanyBusinessLogic.BusinessLogic +{ + public class TransportationLogic : ITransportationLogic + { + private readonly ILogger _logger; + + private readonly ITransportationStorage _transportationStorage; + + //конструктор + public TransportationLogic(ILogger logger, ITransportationStorage transportationStorage) + { + _logger = logger; + _transportationStorage = transportationStorage; + } + + + public List? ReadList(TransportationSearchModel? model) + { + _logger.LogInformation("ReadList. TransportationType:{TransportationType}. Id:{Id}", model?.TransportationType, model?.Id); + + //list хранит весь список в случае, если model пришло со значением null на вход метода + var list = model == null ? _transportationStorage.GetFullList() : _transportationStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public TransportationViewModel? ReadElement(TransportationSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. TransportationType:{TransportationType}. Id:{Id}", model.TransportationType, model.Id); + + var element = _transportationStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", model.Id); + + return element; + } + + public bool Create(TransportationBindingModel model) + { + CheckModel(model); + + if (_transportationStorage.Insert(model) == null) + { + _logger.LogWarning("Create operation failed"); + + return false; + } + + return true; + } + + public bool Update(TransportationBindingModel model) + { + CheckModel(model); + + if (_transportationStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + public bool Delete(TransportationBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_transportationStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(TransportationBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + //так как при удалении параметром withParams передаём false + if (!withParams) + { + return; + } + + //проверка на наличие типа перевозки + if (string.IsNullOrEmpty(model.TransportationType)) + { + throw new ArgumentNullException("Нет названия изделия", nameof(model.TransportationType)); + } + + _logger.LogInformation("Transportation. TransportationType:{TransportationType}. Id:{Id}", + model.TransportationType, model.Id); + + //проверка на наличие такого же типа перевозки в списке + var element = _transportationStorage.GetElement(new TransportationSearchModel + { + TransportationType = model.TransportationType, + }); + + //если элемент найден и его Id не совпадает с Id объекта, переданного на вход + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Такой тип перевозки уже есть"); + } + } + } +} diff --git a/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TruckingLogic.cs b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TruckingLogic.cs new file mode 100644 index 0000000..7c08d4d --- /dev/null +++ b/TransportCompany/TransportCompanyBusinessLogic/BusinessLogic/TruckingLogic.cs @@ -0,0 +1,162 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TransportCompanyContracts.BindingModels; +using TransportCompanyContracts.BusinessLogicsContracts; +using TransportCompanyContracts.SearchModels; +using TransportCompanyContracts.StoragesContracts; +using TransportCompanyContracts.ViewModels; + +namespace TransportCompanyBusinessLogic.BusinessLogic +{ + public class TruckingLogic : ITruckingLogic + { + private readonly ILogger _logger; + + private readonly ITruckingStorage _truckingStorage; + + //конструктор + public TruckingLogic(ILogger logger, ITruckingStorage truckingStorage) + { + _logger = logger; + _truckingStorage = truckingStorage; + } + + public List? ReadList(TruckingSearchModel? model) + { + _logger.LogInformation("ReadList. ClientId:{ClientId}. DateStart:{DateStart} Id:{Id}", model?.ClientId, model?.DateStart, model?.Id); + + //list хранит весь список в случае, если model пришло со значением null на вход метода + var list = model == null ? _truckingStorage.GetFullList() : _truckingStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + + return list; + } + + public TruckingViewModel? ReadElement(TruckingSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. ClientId:{ClientId}. DateStart:{DateStart} Id:{Id}", model?.ClientId, model?.DateStart, model?.Id); + + var element = _truckingStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", model.Id); + + return element; + } + + public bool Create(TruckingBindingModel model) + { + CheckModel(model); + + if (_truckingStorage.Insert(model) == null) + { + _logger.LogWarning("Create operation failed"); + + return false; + } + + return true; + } + + public bool Update(TruckingBindingModel model) + { + CheckModel(model); + + if (_truckingStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + + return true; + } + + public bool Delete(TruckingBindingModel model) + { + CheckModel(model, false); + + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_truckingStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + + return false; + } + + return true; + } + + //проверка входного аргумента для методов Insert, Update и Delete + private void CheckModel(TruckingBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + //так как при удалении параметром withParams передаём false + if (!withParams) + { + return; + } + + //проверка на корректный id заказчика + if (model.ClientId <= 0) + { + throw new ArgumentNullException("Некорректный id заказчика", nameof(model.ClientId)); + } + + //проверка на корректный id груза + if (model.CargoId <= 0) + { + throw new ArgumentNullException("Некорректный id груза", nameof(model.CargoId)); + } + + //проверка на корректный id транспорта + if (model.TransportId <= 0) + { + throw new ArgumentNullException("Некорректный id транспорта", nameof(model.TransportId)); + } + + //проверка на корректный id типа транспортировки + if (model.TransportationId <= 0) + { + throw new ArgumentNullException("Некорректный id типа транспортировки", nameof(model.TransportationId)); + } + + //проверка на корректную дату начала транспортировки + if (model.DateStart > model.DateEnd) + { + throw new ArgumentNullException("Дата начала транспортировки должна быть раньше даты окончания перевозки", nameof(model.DateStart)); + } + + _logger.LogInformation("Trucking. ClientId:{ClientId}. CargoId:{CargoId}. TransportId:{TransportId}." + + "TransportationId:{TransportationId}. DateStart:{DateStart}. DateEnd:{DateEnd}. Id:{Id}", + model.ClientId, model.CargoId, model.TransportId, model.TransportationId, model.DateStart, model.DateEnd, model.Id); + } + } +} diff --git a/TransportCompany/TransportCompanyBusinessLogic/TransportCompanyBusinessLogic.csproj b/TransportCompany/TransportCompanyBusinessLogic/TransportCompanyBusinessLogic.csproj index 132c02c..7e49707 100644 --- a/TransportCompany/TransportCompanyBusinessLogic/TransportCompanyBusinessLogic.csproj +++ b/TransportCompany/TransportCompanyBusinessLogic/TransportCompanyBusinessLogic.csproj @@ -6,4 +6,14 @@ enable + + + + + + + + + + diff --git a/TransportCompany/TransportCompanyContracts/SearchModels/ClientSearchModel.cs b/TransportCompany/TransportCompanyContracts/SearchModels/ClientSearchModel.cs index bd75d54..a996803 100644 --- a/TransportCompany/TransportCompanyContracts/SearchModels/ClientSearchModel.cs +++ b/TransportCompany/TransportCompanyContracts/SearchModels/ClientSearchModel.cs @@ -18,6 +18,6 @@ namespace TransportCompanyContracts.SearchModels public string? TelephoneNumber { get; set; } - public string? Email { get;} + public string? Email { get; set; } } } diff --git a/TransportCompany/TransportCompanyContracts/TransportCompanyContracts.csproj b/TransportCompany/TransportCompanyContracts/TransportCompanyContracts.csproj index 132c02c..c16170c 100644 --- a/TransportCompany/TransportCompanyContracts/TransportCompanyContracts.csproj +++ b/TransportCompany/TransportCompanyContracts/TransportCompanyContracts.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/TransportCompany/TransportCompanyDataModels/TransportCompanyDataModels.csproj b/TransportCompany/TransportCompanyDataModels/TransportCompanyDataModels.csproj index 132c02c..cf5b579 100644 --- a/TransportCompany/TransportCompanyDataModels/TransportCompanyDataModels.csproj +++ b/TransportCompany/TransportCompanyDataModels/TransportCompanyDataModels.csproj @@ -6,4 +6,8 @@ enable + + + +