From da69110d3f708218d0dd8a239ed2f69467ea10d6 Mon Sep 17 00:00:00 2001 From: Ivan_Starostin Date: Wed, 1 May 2024 14:30:09 +0400 Subject: [PATCH] Upload files to 'STOBusinessLogic' --- STOBusinessLogic/CarLogic.cs | 124 +++++++++++++++++++++++ STOBusinessLogic/CarPartLogic.cs | 106 +++++++++++++++++++ STOBusinessLogic/ClientLogic.cs | 124 +++++++++++++++++++++++ STOBusinessLogic/STOBusinessLogic.csproj | 19 ++++ STOBusinessLogic/ServiceLogic.cs | 106 +++++++++++++++++++ 5 files changed, 479 insertions(+) create mode 100644 STOBusinessLogic/CarLogic.cs create mode 100644 STOBusinessLogic/CarPartLogic.cs create mode 100644 STOBusinessLogic/ClientLogic.cs create mode 100644 STOBusinessLogic/STOBusinessLogic.csproj create mode 100644 STOBusinessLogic/ServiceLogic.cs diff --git a/STOBusinessLogic/CarLogic.cs b/STOBusinessLogic/CarLogic.cs new file mode 100644 index 0000000..3fdb50e --- /dev/null +++ b/STOBusinessLogic/CarLogic.cs @@ -0,0 +1,124 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StorageContracts; +using STOContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShipyardBusinessLogic +{ + public class CarLogic: ICarLogic + { + private readonly ILogger _logger; + private readonly ICarStorage _CarStorage; + public CarLogic(ILogger logger, ICarStorage CarStorage) + { + _logger = logger; + _CarStorage = CarStorage; + } + + public List? ReadList(CarSearchModel? model) + { + _logger.LogInformation("ReadList. CarNumber:{CarNumber}. Id:{ Id}", model?.CarNumber, 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 CarViewModel? ReadElement(CarSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. CarNumber:{CarNumber}.Id:{ Id}", model.CarNumber, 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(CarBindingModel model) + { + CheckModel(model); + if (_CarStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(CarBindingModel model) + { + CheckModel(model); + if (_CarStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(CarBindingModel 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(CarBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.CarNumber)) + { + throw new ArgumentNullException("Нет гос номера автомобиля", + nameof(model.CarNumber)); + } + if (string.IsNullOrEmpty(model.CarBrand)) + { + throw new ArgumentNullException("Нет марки автомобиля", + nameof(model.CarNumber)); + } + if (string.IsNullOrEmpty(model.CarModel)) + { + throw new ArgumentNullException("Нет модели автомобиля", + nameof(model.CarNumber)); + } + _logger.LogInformation("Car. Car:{Car}. Id: { Id}", model.CarNumber, model.Id); + var element = _CarStorage.GetElement(new CarSearchModel + { + CarNumber = model.CarNumber + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("авто с таким номером уже есть"); + } + } + } +} diff --git a/STOBusinessLogic/CarPartLogic.cs b/STOBusinessLogic/CarPartLogic.cs new file mode 100644 index 0000000..b8a6f71 --- /dev/null +++ b/STOBusinessLogic/CarPartLogic.cs @@ -0,0 +1,106 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicContracts; +using STOContracts.SearchModels; +using STOContracts.StorageContracts; +using STOContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STOBusinessLogic +{ + public class CarPartLogic: ICarPartLogic + { + private readonly ILogger _logger; + private readonly ICarPartStorage _CarPartStorage; + public CarPartLogic(ILogger logger, ICarPartStorage CarPartStorage) + { + _logger = logger; + _CarPartStorage = CarPartStorage; + } + + public List? ReadList(CarPartSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _CarPartStorage.GetFullList() : _CarPartStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public CarPartViewModel? ReadElement(CarPartSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement.Id:{ Id}", model.Id); + var element = _CarPartStorage.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(CarPartBindingModel model) + { + CheckModel(model); + if (_CarPartStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(CarPartBindingModel model) + { + CheckModel(model); + if (_CarPartStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(CarPartBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_CarPartStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(CarPartBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.CarPartName)) + { + throw new ArgumentNullException("Нет запчасти", + nameof(model.CarPartName)); + } + _logger.LogInformation("CarPart. CarPart:{CarPartName}. Id: { Id}", model.CarPartName, model.Id); + } + } +} diff --git a/STOBusinessLogic/ClientLogic.cs b/STOBusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..156081d --- /dev/null +++ b/STOBusinessLogic/ClientLogic.cs @@ -0,0 +1,124 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicContracts; +using STOContracts.SearchModels; +using STOContracts.StorageContracts; +using STOContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STOBusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _ClientStorage; + public ClientLogic(ILogger logger, IClientStorage ClientStorage) + { + _logger = logger; + _ClientStorage = ClientStorage; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. ClientEmail:{Email}. Id:{ Id}", model?.Email, model?.Id); + 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. ClientEmail:{Email}.Id:{ Id}", model.Email, model.Id); + var element = _ClientStorage.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(ClientBindingModel model) + { + CheckModel(model); + if (_ClientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_ClientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_ClientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ClientBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет email", + nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля", + nameof(model.Password)); + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет ФИО", + nameof(model.ClientFIO)); + } + _logger.LogInformation("Client. ClientEmail:{Email}. Id: { Id}", model.Email, model.Id); + var element = _ClientStorage.GetElement(new ClientSearchModel + { + Email = model.Email + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("работник с такой почтой уже есть"); + } + } + } +} diff --git a/STOBusinessLogic/STOBusinessLogic.csproj b/STOBusinessLogic/STOBusinessLogic.csproj new file mode 100644 index 0000000..562bfe5 --- /dev/null +++ b/STOBusinessLogic/STOBusinessLogic.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/STOBusinessLogic/ServiceLogic.cs b/STOBusinessLogic/ServiceLogic.cs new file mode 100644 index 0000000..59539b9 --- /dev/null +++ b/STOBusinessLogic/ServiceLogic.cs @@ -0,0 +1,106 @@ +using Microsoft.Extensions.Logging; +using STOContracts.BindingModels; +using STOContracts.BusinessLogicsContracts; +using STOContracts.SearchModels; +using STOContracts.StorageContracts; +using STOContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace STOBusinessLogic +{ + public class ServiceLogic: IServiceLogic + { + private readonly ILogger _logger; + private readonly IServiceStorage _ServiceStorage; + public ServiceLogic(ILogger logger, IServiceStorage ServiceStorage) + { + _logger = logger; + _ServiceStorage = ServiceStorage; + } + + public List? ReadList(ServiceSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _ServiceStorage.GetFullList() : _ServiceStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ServiceViewModel? ReadElement(ServiceSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement.Id:{ Id}", model.Id); + var element = _ServiceStorage.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(ServiceBindingModel model) + { + CheckModel(model); + if (_ServiceStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ServiceBindingModel model) + { + CheckModel(model); + if (_ServiceStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ServiceBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_ServiceStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ServiceBindingModel 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("CarPart. Id: { Id}", model.Id); + } + } +}