From 4257dea204b7da7220ee69e5dc2a6922f427980f Mon Sep 17 00:00:00 2001 From: Factorino73 Date: Thu, 25 Apr 2024 23:50:21 +0400 Subject: [PATCH] BusinessLogics --- Hospital/Hospital.sln | 8 +- .../BusinessLogics/DiseaseLogic.cs | 179 +++++++++++++++++ .../BusinessLogics/DoctorLogic.cs | 183 +++++++++++++++++ .../BusinessLogics/MedicineLogic.cs | 175 ++++++++++++++++ .../BusinessLogics/PatientLogic.cs | 187 ++++++++++++++++++ .../BusinessLogics/ProcedureLogic.cs | 175 ++++++++++++++++ .../BusinessLogics/RecipeLogic.cs | 170 ++++++++++++++++ .../HospitalBusinessLogics.csproj | 17 ++ ...cedureStrorage.cs => IProcedureStorage.cs} | 2 +- .../Implements/ProcedureStorage.cs | 2 +- 10 files changed, 1095 insertions(+), 3 deletions(-) create mode 100644 Hospital/HospitalBusinessLogics/BusinessLogics/DiseaseLogic.cs create mode 100644 Hospital/HospitalBusinessLogics/BusinessLogics/DoctorLogic.cs create mode 100644 Hospital/HospitalBusinessLogics/BusinessLogics/MedicineLogic.cs create mode 100644 Hospital/HospitalBusinessLogics/BusinessLogics/PatientLogic.cs create mode 100644 Hospital/HospitalBusinessLogics/BusinessLogics/ProcedureLogic.cs create mode 100644 Hospital/HospitalBusinessLogics/BusinessLogics/RecipeLogic.cs create mode 100644 Hospital/HospitalBusinessLogics/HospitalBusinessLogics.csproj rename Hospital/HospitalContracts/StoragesContracts/{IProcedureStrorage.cs => IProcedureStorage.cs} (97%) diff --git a/Hospital/Hospital.sln b/Hospital/Hospital.sln index 35af821..ceff635 100644 --- a/Hospital/Hospital.sln +++ b/Hospital/Hospital.sln @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalDataModels", "Hospi EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{435124E0-E0A5-4EB8-A46C-C093C47A65F7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalDatabaseImplement", "HospitalDatabaseImplement\HospitalDatabaseImplement.csproj", "{595F63B0-79FF-4EBA-9582-2A0652D00B58}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalDatabaseImplement", "HospitalDatabaseImplement\HospitalDatabaseImplement.csproj", "{595F63B0-79FF-4EBA-9582-2A0652D00B58}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalBusinessLogics", "HospitalBusinessLogics\HospitalBusinessLogics.csproj", "{1A20292D-5690-4C8A-859E-9CC72F0AACB5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {595F63B0-79FF-4EBA-9582-2A0652D00B58}.Debug|Any CPU.Build.0 = Debug|Any CPU {595F63B0-79FF-4EBA-9582-2A0652D00B58}.Release|Any CPU.ActiveCfg = Release|Any CPU {595F63B0-79FF-4EBA-9582-2A0652D00B58}.Release|Any CPU.Build.0 = Release|Any CPU + {1A20292D-5690-4C8A-859E-9CC72F0AACB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A20292D-5690-4C8A-859E-9CC72F0AACB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A20292D-5690-4C8A-859E-9CC72F0AACB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A20292D-5690-4C8A-859E-9CC72F0AACB5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Hospital/HospitalBusinessLogics/BusinessLogics/DiseaseLogic.cs b/Hospital/HospitalBusinessLogics/BusinessLogics/DiseaseLogic.cs new file mode 100644 index 0000000..7f327ce --- /dev/null +++ b/Hospital/HospitalBusinessLogics/BusinessLogics/DiseaseLogic.cs @@ -0,0 +1,179 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogics.BusinessLogics +{ + /// + /// Бизнес-логика для сущности "Болезнь" + /// + public class DiseaseLogic : IDiseaseLogic + { + /// + /// Логгер + /// + private readonly ILogger _logger; + + /// + /// Хранилище + /// + private readonly IDiseaseStorage _diseaseStorage; + + /// + /// Конструктор + /// + /// + /// + public DiseaseLogic(ILogger logger, IDiseaseStorage diseaseStorage) + { + _logger = logger; + _diseaseStorage = diseaseStorage; + } + + /// + /// Получить список + /// + /// + /// + public List? ReadList(DiseaseSearchModel? model) + { + _logger.LogInformation("ReadList. Disease.Id: {Id}. RecipeId: {RecipeId}. Name: {Name}", model?.Id, model?.RecipeId, model?.Name); + + var list = model == null ? _diseaseStorage.GetFullList() : _diseaseStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList. Returned null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + /// + /// Получить отдельную запись + /// + /// + /// + /// + public DiseaseViewModel? ReadElement(DiseaseSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Disease.Id: {Id}. Name: {Name}", model?.Id, model?.Name); + + var element = _diseaseStorage.GetElement(model!); + if (element == null) + { + _logger.LogWarning("ReadElement. Element not found"); + return null; + } + + _logger.LogInformation("ReadElement. Find Disease.Id: {Id}", element?.Id); + return element; + } + + /// + /// Создать запись + /// + /// + /// + public bool Create(DiseaseBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Create. Disease.Id: {Id}", model.Id); + + if (_diseaseStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + /// + /// Изменить запись + /// + /// + /// + public bool Update(DiseaseBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Update. Disease.Id: {Id}", model.Id); + + if (_diseaseStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + /// + /// Удалить запись + /// + /// + /// + public bool Delete(DiseaseBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Disease.Id: {Id}", model.Id); + + if (_diseaseStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + /// + /// Проверка модели + /// + /// + /// + /// + /// + private void CheckModel(DiseaseBindingModel 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.RecipeId <= 0) + { + throw new ArgumentNullException("Не указан идентификатор рецепта", nameof(model.RecipeId)); + } + + _logger.LogInformation("CheckModel. Disease.Id: {Id}", model.Id); + + var element = _diseaseStorage.GetElement(new DiseaseSearchModel + { + Name = model.Name + }); + if (element != null && !element.Id.Equals(model.Id)) + { + throw new InvalidOperationException("Болезнь с таким названием уже существует"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogics/BusinessLogics/DoctorLogic.cs b/Hospital/HospitalBusinessLogics/BusinessLogics/DoctorLogic.cs new file mode 100644 index 0000000..df42112 --- /dev/null +++ b/Hospital/HospitalBusinessLogics/BusinessLogics/DoctorLogic.cs @@ -0,0 +1,183 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogics.BusinessLogics +{ + /// + /// Бизнес-логика для сущности "Доктор" + /// + public class DoctorLogic : IDoctorLogic + { + /// + /// Логгер + /// + private readonly ILogger _logger; + + /// + /// Хранилище + /// + private readonly IDoctorStorage _doctorStorage; + + /// + /// Конструктор + /// + /// + /// + public DoctorLogic(ILogger logger, IDoctorStorage doctorStorage) + { + _logger = logger; + _doctorStorage = doctorStorage; + } + + /// + /// Получить список + /// + /// + /// + public List? ReadList(DoctorSearchModel? model) + { + _logger.LogInformation("ReadList. Doctor.Id: {Id}. Email: {Email}. FullName: {FullName}", model?.Id, model?.Email, model?.FullName); + + var list = model == null ? _doctorStorage.GetFullList() : _doctorStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList. Returned null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + /// + /// Получить отдельную запись + /// + /// + /// + /// + public DoctorViewModel? ReadElement(DoctorSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Doctor.Id: {Id}. Email: {Email}. Password: {Password}", model?.Id, model?.Email, model?.Password); + + var element = _doctorStorage.GetElement(model!); + if (element == null) + { + _logger.LogWarning("ReadElement. Element not found"); + return null; + } + + _logger.LogInformation("ReadElement. Find Doctor.Id: {Id}", element?.Id); + return element; + } + + /// + /// Создать запись + /// + /// + /// + public bool Create(DoctorBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Create. Doctor.Id: {Id}", model.Id); + + if (_doctorStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + /// + /// Изменить запись + /// + /// + /// + public bool Update(DoctorBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Update. Doctor.Id: {Id}", model.Id); + + if (_doctorStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + /// + /// Удалить запись + /// + /// + /// + public bool Delete(DoctorBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Doctor.Id: {Id}", model.Id); + + if (_doctorStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + /// + /// Проверка модели + /// + /// + /// + /// + /// + private void CheckModel(DoctorBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.FullName)) + { + throw new ArgumentNullException("Не указано ФИО доктора", nameof(model.FullName)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Не указана электронная почта (логин)", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Не указан пароль", nameof(model.Password)); + } + + _logger.LogInformation("CheckModel. Doctor.Id: {Id}", model.Id); + + var element = _doctorStorage.GetElement(new DoctorSearchModel + { + Email = model.Email + }); + if (element != null && !element.Id.Equals(model.Id)) + { + throw new InvalidOperationException("Доктор с такой электронной почтой (логином) уже существует"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogics/BusinessLogics/MedicineLogic.cs b/Hospital/HospitalBusinessLogics/BusinessLogics/MedicineLogic.cs new file mode 100644 index 0000000..77fecfc --- /dev/null +++ b/Hospital/HospitalBusinessLogics/BusinessLogics/MedicineLogic.cs @@ -0,0 +1,175 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogics.BusinessLogics +{ + /// + /// Бизнес-логика для сущности "Лекарство" + /// + public class MedicineLogic : IMedicineLogic + { + /// + /// Логгер + /// + private readonly ILogger _logger; + + /// + /// Хранилище + /// + private readonly IMedicineStorage _medicineStorage; + + /// + /// Конструктор + /// + /// + /// + public MedicineLogic(ILogger logger, IMedicineStorage medicineStorage) + { + _logger = logger; + _medicineStorage = medicineStorage; + } + + /// + /// Получить список + /// + /// + /// + public List? ReadList(MedicineSearchModel? model) + { + _logger.LogInformation("ReadList. Medicine.Id: {Id}. Name: {Name}", model?.Id, model?.Name); + + var list = model == null ? _medicineStorage.GetFullList() : _medicineStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList. Returned 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. Medicine.Id: {Id}. Name: {Name}", model?.Id, model?.Name); + + var element = _medicineStorage.GetElement(model!); + if (element == null) + { + _logger.LogWarning("ReadElement. Element not found"); + return null; + } + + _logger.LogInformation("ReadElement. Find Medicine.Id: {Id}", element?.Id); + return element; + } + + /// + /// Создать запись + /// + /// + /// + public bool Create(MedicineBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Create. Medicine.Id: {Id}", model.Id); + + if (_medicineStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + /// + /// Изменить запись + /// + /// + /// + public bool Update(MedicineBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Update. Medicine.Id: {Id}", model.Id); + + 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. Medicine.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.Name)) + { + throw new ArgumentNullException("Не указано название лекарства", nameof(model.Name)); + } + + _logger.LogInformation("CheckModel. Medicine.Id: {Id}", model.Id); + + var element = _medicineStorage.GetElement(new MedicineSearchModel + { + Name = model.Name + }); + if (element != null && !element.Id.Equals(model.Id)) + { + throw new InvalidOperationException("Лекарство с таким названием уже существует"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogics/BusinessLogics/PatientLogic.cs b/Hospital/HospitalBusinessLogics/BusinessLogics/PatientLogic.cs new file mode 100644 index 0000000..8024c76 --- /dev/null +++ b/Hospital/HospitalBusinessLogics/BusinessLogics/PatientLogic.cs @@ -0,0 +1,187 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogics.BusinessLogics +{ + /// + /// Бизнес-логика для сущности "Пациент" + /// + public class PatientLogic : IPatientLogic + { + /// + /// Логгер + /// + private readonly ILogger _logger; + + /// + /// Хранилище + /// + private readonly IPatientStorage _patientStorage; + + /// + /// Конструктор + /// + /// + /// + public PatientLogic(ILogger logger, IPatientStorage patientStorage) + { + _logger = logger; + _patientStorage = patientStorage; + } + + /// + /// Получить список + /// + /// + /// + public List? ReadList(PatientSearchModel? model) + { + _logger.LogInformation("ReadList. Patient.Id: {Id}. DoctorId: {DoctorId}. FullName: {FullName}", model?.Id, model?.DoctorId, model?.FullName); + + var list = model == null ? _patientStorage.GetFullList() : _patientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList. Returned null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + /// + /// Получить отдельную запись + /// + /// + /// + /// + public PatientViewModel? ReadElement(PatientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Patient.Id: {Id}. Phone: {Phone}", model?.Id, model?.Phone); + + var element = _patientStorage.GetElement(model!); + if (element == null) + { + _logger.LogWarning("ReadElement. Element not found"); + return null; + } + + _logger.LogInformation("ReadElement. Find Patient.Id: {Id}", element?.Id); + return element; + } + + /// + /// Создать запись + /// + /// + /// + public bool Create(PatientBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Create. Patient.Id: {Id}", model.Id); + + if (_patientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + /// + /// Изменить запись + /// + /// + /// + public bool Update(PatientBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Update. Patient.Id: {Id}", model.Id); + + if (_patientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + /// + /// Удалить запись + /// + /// + /// + public bool Delete(PatientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Patient.Id: {Id}", model.Id); + + if (_patientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + /// + /// Проверка модели + /// + /// + /// + /// + /// + private void CheckModel(PatientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.FullName)) + { + throw new ArgumentNullException("Не указано ФИО пациента", nameof(model.FullName)); + } + if (model.BirthDate == DateTime.MinValue) + { + throw new ArgumentNullException("Не указана дата рождения пациента", nameof(model.BirthDate)); + } + if (string.IsNullOrEmpty(model.Phone)) + { + throw new ArgumentNullException("Не указан номер телефона", nameof(model.Phone)); + } + if (model.DoctorId <= 0) + { + throw new ArgumentNullException("Не указан идентификатор лечащего врача", nameof(model.DoctorId)); + } + + _logger.LogInformation("CheckModel. Patient.Id: {Id}", model.Id); + + var element = _patientStorage.GetElement(new PatientSearchModel + { + Phone = model.Phone + }); + if (element != null && !element.Id.Equals(model.Id)) + { + throw new InvalidOperationException("Пациент с таким номером телефона уже существует"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogics/BusinessLogics/ProcedureLogic.cs b/Hospital/HospitalBusinessLogics/BusinessLogics/ProcedureLogic.cs new file mode 100644 index 0000000..a571321 --- /dev/null +++ b/Hospital/HospitalBusinessLogics/BusinessLogics/ProcedureLogic.cs @@ -0,0 +1,175 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogics.BusinessLogics +{ + /// + /// Бизнес-логика для сущности "Процедура" + /// + public class ProcedureLogic : IProcedureLogic + { + /// + /// Логгер + /// + private readonly ILogger _logger; + + /// + /// Хранилище + /// + private readonly IProcedureStorage _procedureStorage; + + /// + /// Конструктор + /// + /// + /// + public ProcedureLogic(ILogger logger, IProcedureStorage procedureStorage) + { + _logger = logger; + _procedureStorage = procedureStorage; + } + + /// + /// Получить список + /// + /// + /// + public List? ReadList(ProcedureSearchModel? model) + { + _logger.LogInformation("ReadList. Procedure.Id: {Id}. Name: {Name}", model?.Id, model?.Name); + + var list = model == null ? _procedureStorage.GetFullList() : _procedureStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList. Returned null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + /// + /// Получить отдельную запись + /// + /// + /// + /// + public ProcedureViewModel? ReadElement(ProcedureSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Procedure.Id: {Id}. Name: {Name}", model?.Id, model?.Name); + + var element = _procedureStorage.GetElement(model!); + if (element == null) + { + _logger.LogWarning("ReadElement. Element not found"); + return null; + } + + _logger.LogInformation("ReadElement. Find Procedure.Id: {Id}", element?.Id); + return element; + } + + /// + /// Создать запись + /// + /// + /// + public bool Create(ProcedureBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Create. Procedure.Id: {Id}", model.Id); + + if (_procedureStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + /// + /// Изменить запись + /// + /// + /// + public bool Update(ProcedureBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Update. Procedure.Id: {Id}", model.Id); + + if (_procedureStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + /// + /// Удалить запись + /// + /// + /// + public bool Delete(ProcedureBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Procedure.Id: {Id}", model.Id); + + if (_procedureStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + /// + /// Проверка модели + /// + /// + /// + /// + /// + private void CheckModel(ProcedureBindingModel 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("CheckModel. Procedure.Id: {Id}", model.Id); + + var element = _procedureStorage.GetElement(new ProcedureSearchModel + { + Name = model.Name + }); + if (element != null && !element.Id.Equals(model.Id)) + { + throw new InvalidOperationException("Процедура с таким названием уже существует"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogics/BusinessLogics/RecipeLogic.cs b/Hospital/HospitalBusinessLogics/BusinessLogics/RecipeLogic.cs new file mode 100644 index 0000000..b470bc3 --- /dev/null +++ b/Hospital/HospitalBusinessLogics/BusinessLogics/RecipeLogic.cs @@ -0,0 +1,170 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.BusinessLogicsContracts; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogics.BusinessLogics +{ + /// + /// Бизнес-логика для сущности "Рецепт" + /// + public class RecipeLogic : IRecipeLogic + { + /// + /// Логгер + /// + private readonly ILogger _logger; + + /// + /// Хранилище + /// + private readonly IRecipeStorage _recipeStorage; + + /// + /// Конструктор + /// + /// + /// + public RecipeLogic(ILogger logger, IRecipeStorage recipeStorage) + { + _logger = logger; + _recipeStorage = recipeStorage; + } + + /// + /// Получить список + /// + /// + /// + public List? ReadList(RecipeSearchModel? model) + { + _logger.LogInformation("ReadList. Recipe.Id: {Id}. DoctorId: {DoctorId}", model?.Id, model?.DoctorId); + + var list = model == null ? _recipeStorage.GetFullList() : _recipeStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList. Returned null list"); + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + return list; + } + + /// + /// Получить отдельную запись + /// + /// + /// + /// + public RecipeViewModel? ReadElement(RecipeSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. Recipe.Id: {Id}", model?.Id); + + var element = _recipeStorage.GetElement(model!); + if (element == null) + { + _logger.LogWarning("ReadElement. Element not found"); + return null; + } + + _logger.LogInformation("ReadElement. Find Recipe.Id: {Id}", element?.Id); + return element; + } + + /// + /// Создать запись + /// + /// + /// + public bool Create(RecipeBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Create. Recipe.Id: {Id}", model.Id); + + if (_recipeStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + /// + /// Изменить запись + /// + /// + /// + public bool Update(RecipeBindingModel model) + { + CheckModel(model); + _logger.LogInformation("Update. Recipe.Id: {Id}", model.Id); + + if (_recipeStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + /// + /// Удалить запись + /// + /// + /// + public bool Delete(RecipeBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Recipe.Id: {Id}", model.Id); + + if (_recipeStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + /// + /// Проверка модели + /// + /// + /// + /// + /// + private void CheckModel(RecipeBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.IssueDate == DateTime.MinValue) + { + throw new ArgumentNullException("Не указана дата выписки рецепта", nameof(model.IssueDate)); + } + if (model.DoctorId <= 0) + { + throw new ArgumentNullException("Не указан идентификатор доктора", nameof(model.DoctorId)); + } + + _logger.LogInformation("CheckModel. Recipe.Id: {Id}", model.Id); + } + } +} diff --git a/Hospital/HospitalBusinessLogics/HospitalBusinessLogics.csproj b/Hospital/HospitalBusinessLogics/HospitalBusinessLogics.csproj new file mode 100644 index 0000000..49d19fc --- /dev/null +++ b/Hospital/HospitalBusinessLogics/HospitalBusinessLogics.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/Hospital/HospitalContracts/StoragesContracts/IProcedureStrorage.cs b/Hospital/HospitalContracts/StoragesContracts/IProcedureStorage.cs similarity index 97% rename from Hospital/HospitalContracts/StoragesContracts/IProcedureStrorage.cs rename to Hospital/HospitalContracts/StoragesContracts/IProcedureStorage.cs index 7b6dfc3..2b3d1be 100644 --- a/Hospital/HospitalContracts/StoragesContracts/IProcedureStrorage.cs +++ b/Hospital/HospitalContracts/StoragesContracts/IProcedureStorage.cs @@ -12,7 +12,7 @@ namespace HospitalContracts.StoragesContracts /// /// Интерфейс для описания работы с хранилищем для сущности "Процедура" /// - public interface IProcedureStrorage + public interface IProcedureStorage { /// /// Получить полный список diff --git a/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs index a7d3206..a52f21e 100644 --- a/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs +++ b/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs @@ -16,7 +16,7 @@ namespace HospitalDatabaseImplement.Implements /// /// Хранилище для сущности "Процедура" /// - public class ProcedureStorage : IProcedureStrorage + public class ProcedureStorage : IProcedureStorage { /// /// Получить полный список