From d1ca608083b68e4cc3831c76cde7ed6758691f0b Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Tue, 23 Apr 2024 22:32:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D0=BD=D0=B8?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D1=8C:=20=D1=81=D0=B4=D0=B5=D0=BB=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/OwnerLogic.cs | 115 ++++++++++++++++++ .../BusinessLogic/PetLogic.cs | 114 +++++++++++++++++ .../BusinessLogic/PurchaseLogic.cs | 82 +++++++++++++ .../BusinessLogic/VisitLogic.cs | 79 ++++++++++++ .../VeterinaryBusinessLogic.csproj | 4 +- .../BusinessLogicContracts/IPurchaseLogic.cs | 2 +- 6 files changed, 393 insertions(+), 3 deletions(-) create mode 100644 VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs create mode 100644 VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PetLogic.cs create mode 100644 VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs create mode 100644 VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/VisitLogic.cs diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs new file mode 100644 index 0000000..e741d1e --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/OwnerLogic.cs @@ -0,0 +1,115 @@ +using Microsoft.Extensions.Logging; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryBusinessLogic.BusinessLogic +{ + public class OwnerLogic : IOwnerLogic + { + private readonly ILogger _logger; + private readonly IOwnerStorage _ownerStorage; + public OwnerLogic(ILogger logger, IOwnerStorage ownerStorage) + { + _logger = logger; + _ownerStorage = ownerStorage; + } + public List? ReadList(OwnerSearchModel? model) + { + _logger.LogInformation("ReadList. OwnerFIO:{OwnerFIO}. Id:{ Id}", model?.OwnerFIO, model?.Id); + var list = model == null ? _ownerStorage.GetFullList() : _ownerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public OwnerViewModel? ReadElement(OwnerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. OwnerFIO:{OwnerFIO}.Id:{ Id}", model.OwnerFIO, model.Id); + var element = _ownerStorage.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(OwnerBindingModel model) + { + CheckModel(model); + if (_ownerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(OwnerBindingModel model) + { + CheckModel(model); + if (_ownerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(OwnerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_ownerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(OwnerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.OwnerFIO)) + { + throw new ArgumentNullException("Нет ФИО клиента", + nameof(model.OwnerFIO)); + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет Email клиента", + nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", + nameof(model.Password)); + } + _logger.LogInformation("Owner. OwnerFIO:{OwnerFIO}." + + "Login:{ Login}. Password:{ Password}. Id: { Id} ", model.OwnerFIO, model.Login, model.Password, model.Id); + var element = _ownerStorage.GetElement(new OwnerSearchModel + { + Login = model.Login, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже есть"); + } + } + } +} diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PetLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PetLogic.cs new file mode 100644 index 0000000..537874b --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PetLogic.cs @@ -0,0 +1,114 @@ +using Microsoft.Extensions.Logging; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryBusinessLogic.BusinessLogic +{ + public class PetLogic : IPetLogic + { + private readonly ILogger _logger; + private readonly IPetStorage _petStorage; + public PetLogic(ILogger logger, IPetStorage + petStorage) + { + _logger = logger; + _petStorage = petStorage; + } + public List? ReadList(PetSearchModel? model) + { + _logger.LogInformation("ReadList. PetName:{PetName}. Id:{ Id}", model?.PetName, model?.Id); + var list = model == null ? _petStorage.GetFullList() : _petStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public PetViewModel? ReadElement(PetSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. PetName:{PetName}.Id:{ Id}", model.PetName, model.Id); + var element = _petStorage.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(PetBindingModel model) + { + CheckModel(model); + if (_petStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(PetBindingModel model) + { + CheckModel(model); + if (_petStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(PetBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_petStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(PetBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.PetName)) + { + throw new ArgumentNullException("Нет Клички животного", + nameof(model.PetName)); + } + if (string.IsNullOrEmpty(model.PetType)) + { + throw new ArgumentNullException("Нет Вида животного", + nameof(model.PetType)); + } + if (string.IsNullOrEmpty(model.PetBreed)) + { + throw new ArgumentNullException("Нет Породы животного", + nameof(model.PetBreed)); + } + if (string.IsNullOrEmpty(model.PetGender)) + { + throw new ArgumentNullException("Нет Пола животного", + nameof(model.PetGender)); + } + _logger.LogInformation("Pet. PetName:{PetName}." + + "PetType:{ PetType}. PetBreed:{ PetBreed}. PetGender:{ PetGender}. Id: { Id} ", model.PetName, model.PetType, model.PetBreed, model.PetGender, model.Id); + } + } +} diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs new file mode 100644 index 0000000..ba9ad7b --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/PurchaseLogic.cs @@ -0,0 +1,82 @@ +using Microsoft.Extensions.Logging; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryBusinessLogic.BusinessLogic +{ + public class PurchaseLogic : IPurchaseLogic + { + private readonly ILogger _logger; + private readonly IPurchaseStorage _purchaseStorage; + + public PurchaseLogic(ILogger logger, IPurchaseStorage purchaseStorage) + { + _logger = logger; + _purchaseStorage = purchaseStorage; + } + + public PurchaseViewModel? ReadElement(PurchaseSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + var element = _purchaseStorage.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? ReadList(PurchaseSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _purchaseStorage.GetFullList() : _purchaseStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreatePurchase(PurchaseBindingModel model) + { + CheckModel(model); + if (_purchaseStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + private void CheckModel(PurchaseBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.DatePurchase <= DateTime.Now) + { + throw new ArgumentNullException("Дата покупки не должна быть в прошлом", nameof(model.DatePurchase)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count)); + } + _logger.LogInformation("Purchase. DatePuchase: { DatePurchase}. Count:{ Count}. Id: { Id}", model.DatePurchase, model.Count, model.Id); + } + } +} diff --git a/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/VisitLogic.cs b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/VisitLogic.cs new file mode 100644 index 0000000..f73d3cd --- /dev/null +++ b/VeterinaryView/VeterinaryBusinessLogic/BusinessLogic/VisitLogic.cs @@ -0,0 +1,79 @@ +using Microsoft.Extensions.Logging; +using VeterinaryContracts.BindingModels; +using VeterinaryContracts.BusinessLogicContracts; +using VeterinaryContracts.SearchModels; +using VeterinaryContracts.StorageContracts; +using VeterinaryContracts.ViewModels; + +namespace VeterinaryBusinessLogic.BusinessLogic +{ + public class VisitLogic : IVisitLogic + { + private readonly ILogger _logger; + private readonly IVisitStorage _visitStorage; + + public VisitLogic(ILogger logger, IVisitStorage visitStorage) + { + _logger = logger; + _visitStorage = visitStorage; + } + + public VisitViewModel? ReadElement(VisitSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + var element = _visitStorage.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? ReadList(VisitSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _visitStorage.GetFullList() : + _visitStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreateVisit(VisitBindingModel model) + { + CheckModel(model); + if (_visitStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + private void CheckModel(VisitBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.DateVisit >= DateTime.Now) + { + throw new ArgumentNullException("Дата посещения не должна быть в прошлом", nameof(model.DateVisit)); + } + _logger.LogInformation("Visit. DateVisit:{ DateVisit}. Id: { Id}", model.DateVisit); + } + } +} diff --git a/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj b/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj index 400377b..3110a4b 100644 --- a/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj +++ b/VeterinaryView/VeterinaryBusinessLogic/VeterinaryBusinessLogic.csproj @@ -7,11 +7,11 @@ - + - + diff --git a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IPurchaseLogic.cs b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IPurchaseLogic.cs index 2955fca..9066477 100644 --- a/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IPurchaseLogic.cs +++ b/VeterinaryView/VeterinaryContracts/BusinessLogicContracts/IPurchaseLogic.cs @@ -8,6 +8,6 @@ namespace VeterinaryContracts.BusinessLogicContracts { List? ReadList(PurchaseSearchModel? model); PurchaseViewModel? ReadElement(PurchaseSearchModel model); - bool CreateOrder(PurchaseBindingModel model); + bool CreatePurchase(PurchaseBindingModel model); } }