From 445b9e3220700dff9c228f4110e3a8af68cd6e2b Mon Sep 17 00:00:00 2001 From: gg12 darfren Date: Wed, 24 Apr 2024 10:57:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D0=B1?= =?UTF-8?q?=D0=B8=D0=B7=D0=BD=D0=B5=D1=81=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/GuidanceLogic.cs | 108 +++++++++++++++ .../BusinessLogics/MedicineLogic.cs | 119 +++++++++++++++++ .../BusinessLogics/PharmacistLogic.cs | 123 ++++++++++++++++++ .../BusinessLogics/ServiceLogic.cs | 120 +++++++++++++++++ .../BusinessLogicsContracts/IMedicineLogic.cs | 2 +- 5 files changed, 471 insertions(+), 1 deletion(-) create mode 100644 VetClinic/VetClinicBusinessLogic/BusinessLogics/GuidanceLogic.cs create mode 100644 VetClinic/VetClinicBusinessLogic/BusinessLogics/MedicineLogic.cs create mode 100644 VetClinic/VetClinicBusinessLogic/BusinessLogics/PharmacistLogic.cs create mode 100644 VetClinic/VetClinicBusinessLogic/BusinessLogics/ServiceLogic.cs diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/GuidanceLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/GuidanceLogic.cs new file mode 100644 index 0000000..ba9c190 --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/GuidanceLogic.cs @@ -0,0 +1,108 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; + +namespace VetClinicBusinessLogic.BusinessLogics +{ + public class GuidanceLogic : IGuidanceLogic + { + private readonly ILogger _logger; + private readonly IGuidanceStorage _guidanceStorage; + + public GuidanceLogic(ILogger logger, IGuidanceStorage guidanceStorage) + { + _logger = logger; + _guidanceStorage = guidanceStorage; + } + + public GuidanceViewModel? ReadElement(GuidanceSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + var element = _guidanceStorage.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(GuidanceSearchModel? model) + { + _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); + var list = model == null ? _guidanceStorage.GetFullList() : + _guidanceStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Create(GuidanceBindingModel model) + { + CheckModel(model); + if (_guidanceStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(GuidanceBindingModel model) + { + CheckModel(model); + if (_guidanceStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(GuidanceBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_guidanceStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(GuidanceBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Text)) + { + throw new ArgumentNullException("Нет текста рекомендации", + nameof(model.Text)); + } + + _logger.LogInformation("Guidance. Text:{Text}.", model.Text); + } + } +} diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/MedicineLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/MedicineLogic.cs new file mode 100644 index 0000000..ac1bf77 --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/MedicineLogic.cs @@ -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 VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; + +namespace VetClinicBusinessLogic.BusinessLogics +{ + public class MedicineLogic : IMedicineLogic + { + private readonly ILogger _logger; + private readonly IMedicineStorage _medicineStorage; + public MedicineLogic(ILogger logger, IMedicineStorage iceCreamStorage) + { + _logger = logger; + _medicineStorage = iceCreamStorage; + } + + public List? ReadList(MedicineSearchModel? model) + { + _logger.LogInformation("ReadList. MedicineName:{MedicineName}. Id:{ Id}", model?.MedicineName, model?.Id); + var list = model == null ? _medicineStorage.GetFullList() : +_medicineStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public MedicineViewModel? ReadElement(MedicineSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. MedicineName:{MedicineName}.Id:{ Id}", model.MedicineName, model.Id); + var element = _medicineStorage.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(MedicineBindingModel model) + { + CheckModel(model); + if (_medicineStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(MedicineBindingModel model) + { + CheckModel(model); + if (_medicineStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(MedicineBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_medicineStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(MedicineBindingModel model, bool withParams = +true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.MedicineName)) + { + throw new ArgumentNullException("Нет названия медикамента", + nameof(model.MedicineName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена медикамента должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Medicine. Medicine:{MedicineName}. Price:{ Price }. Id: { Id}", model.MedicineName, model.Price, model.Id); + var element = _medicineStorage.GetElement(new MedicineSearchModel + { + MedicineName = model.MedicineName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Медикамент с таким названием уже есть"); + } + } + } +} diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/PharmacistLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/PharmacistLogic.cs new file mode 100644 index 0000000..5050ce5 --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/PharmacistLogic.cs @@ -0,0 +1,123 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; + +namespace VetClinicBusinessLogic.BusinessLogics +{ + public class PharmacistLogic : IPharmacistLogic + { + private readonly ILogger _logger; + private readonly IPharmacistStorage _pharmacistStorage; + public PharmacistLogic(ILogger logger, IPharmacistStorage + pharmacistStorage) + { + _logger = logger; + _pharmacistStorage = pharmacistStorage; + } + public List? ReadList(PharmacistSearchModel? model) + { + _logger.LogInformation("ReadList. PharmacistFIO:{PharmacistFIO}. Id:{ Id}", model?.PharmacistFIO, model?.Id); + var list = model == null ? _pharmacistStorage.GetFullList() : + _pharmacistStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public PharmacistViewModel? ReadElement(PharmacistSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. PharmacistFIO:{PharmacistFIO}.Id:{ Id}", model.PharmacistFIO, model.Id); + var element = _pharmacistStorage.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(PharmacistBindingModel model) + { + CheckModel(model); + if (_pharmacistStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(PharmacistBindingModel model) + { + CheckModel(model); + if (_pharmacistStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(PharmacistBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_pharmacistStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(PharmacistBindingModel model, bool withParams = + true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.PharmacistFIO)) + { + throw new ArgumentNullException("Нет ФИО клиента", + nameof(model.PharmacistFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет Email клиента", + nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", + nameof(model.Password)); + } + _logger.LogInformation("Pharmacist. PharmacistFIO:{PharmacistFIO}." + + "Email:{ Email}. Password:{ Password}. Id: { Id} ", model.PharmacistFIO, model.Email, model.Password, model.Id); + var element = _pharmacistStorage.GetElement(new PharmacistSearchModel + { + Email = model.Email, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже есть"); + } + } + } +} diff --git a/VetClinic/VetClinicBusinessLogic/BusinessLogics/ServiceLogic.cs b/VetClinic/VetClinicBusinessLogic/BusinessLogics/ServiceLogic.cs new file mode 100644 index 0000000..ba326ed --- /dev/null +++ b/VetClinic/VetClinicBusinessLogic/BusinessLogics/ServiceLogic.cs @@ -0,0 +1,120 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.BusinessLogicsContracts; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; + +namespace VetClinicBusinessLogic.BusinessLogics +{ + 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. ServiceName:{ServiceName}. Id:{ Id}", model?.ServiceName, 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. ServiceName:{ServiceName}.Id:{ Id}", model.ServiceName, 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.ServiceName)) + { + throw new ArgumentNullException("Нет названия услуги", + nameof(model.ServiceName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена мороженного услуги быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Service. ServiceName:{ServiceName}. Price:{ Price }. Id: { Id}", model.ServiceName, model.Price, model.Id); + var element = _serviceStorage.GetElement(new ServiceSearchModel + { + ServiceName = model.ServiceName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Услуга с таким названием уже есть"); + } + } + + } +} diff --git a/VetClinic/VetClinicContracts/BusinessLogicsContracts/IMedicineLogic.cs b/VetClinic/VetClinicContracts/BusinessLogicsContracts/IMedicineLogic.cs index ea20c0e..91e79c9 100644 --- a/VetClinic/VetClinicContracts/BusinessLogicsContracts/IMedicineLogic.cs +++ b/VetClinic/VetClinicContracts/BusinessLogicsContracts/IMedicineLogic.cs @@ -12,7 +12,7 @@ namespace VetClinicContracts.BusinessLogicsContracts public interface IMedicineLogic { List? ReadList(MedicineSearchModel? model); - MedicineSearchModel? ReadElement(MedicineSearchModel model); + MedicineViewModel? ReadElement(MedicineSearchModel model); bool Create(MedicineBindingModel model); bool Update(MedicineBindingModel model); bool Delete(MedicineBindingModel model);