From 7e123f347aaa1163590b947eccac3d4a6473a39d Mon Sep 17 00:00:00 2001 From: Anastasia Date: Fri, 7 Apr 2023 09:43:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A7=D0=B0=D1=81=D1=82=D0=B8=D1=87=D0=BD?= =?UTF-8?q?=D0=B0=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20HospitalBusinessLogic=20=D0=B8=20HospitalDatabas?= =?UTF-8?q?eImplement.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hospital/Hospital.sln | 14 +- .../BusinessLogics/DiseaseLogic.cs | 116 +++++++++++++++ .../BusinessLogics/DoctorLogic.cs | 124 ++++++++++++++++ .../BusinessLogics/MedicineLogic.cs | 115 +++++++++++++++ .../BusinessLogics/PatientLogic.cs | 129 +++++++++++++++++ .../BusinessLogics/ProcedureLogic.cs | 115 +++++++++++++++ .../BusinessLogics/RecipeLogic.cs | 115 +++++++++++++++ .../HospitalBusinessLogic.csproj | 17 +++ .../BindingModels/MedicineBindingModel.cs | 2 - .../ViewModels/MedicineViewModel.cs | 3 - .../Models/IMedicineModel.cs | 2 - .../Properties/launchSettings.json | 12 ++ .../HospitalDatabase.cs | 42 ++++++ .../HospitalDatabaseImplement.csproj | 23 +++ .../Implements/DoctorStorage.cs | 85 +++++++++++ .../Implements/PatientStorage.cs | 84 +++++++++++ .../Models/Disease.cs | 75 ++++++++++ .../Models/Doctor.cs | 83 +++++++++++ .../Models/Medicine.cs | 60 ++++++++ .../Models/Patient.cs | 136 ++++++++++++++++++ .../Models/PatientProcedure.cs | 24 ++++ .../Models/PatientRecipe.cs | 26 ++++ .../Models/Procedure.cs | 91 ++++++++++++ .../Models/ProcedureMedicine.cs | 24 ++++ .../Models/Recipe.cs | 96 +++++++++++++ .../Models/RecipeMedicine.cs | 24 ++++ 26 files changed, 1629 insertions(+), 8 deletions(-) create mode 100644 Hospital/HospitalBusinessLogic/BusinessLogics/DiseaseLogic.cs create mode 100644 Hospital/HospitalBusinessLogic/BusinessLogics/DoctorLogic.cs create mode 100644 Hospital/HospitalBusinessLogic/BusinessLogics/MedicineLogic.cs create mode 100644 Hospital/HospitalBusinessLogic/BusinessLogics/PatientLogic.cs create mode 100644 Hospital/HospitalBusinessLogic/BusinessLogics/ProcedureLogic.cs create mode 100644 Hospital/HospitalBusinessLogic/BusinessLogics/RecipeLogic.cs create mode 100644 Hospital/HospitalBusinessLogic/HospitalBusinessLogic.csproj create mode 100644 Hospital/HospitalDataModels/Properties/launchSettings.json create mode 100644 Hospital/HospitalDatabaseImplement/HospitalDatabase.cs create mode 100644 Hospital/HospitalDatabaseImplement/HospitalDatabaseImplement.csproj create mode 100644 Hospital/HospitalDatabaseImplement/Implements/DoctorStorage.cs create mode 100644 Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/Disease.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/Doctor.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/Medicine.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/Patient.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/PatientRecipe.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/Procedure.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/Recipe.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs diff --git a/Hospital/Hospital.sln b/Hospital/Hospital.sln index 4115a32..11a9648 100644 --- a/Hospital/Hospital.sln +++ b/Hospital/Hospital.sln @@ -7,7 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hospital", "Hospital\Hospit EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalDataModels", "HospitalDataModels\HospitalDataModels.csproj", "{84B572A0-F8F5-40D2-9D9E-ACAEC7A91859}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalBusinessLogic", "HospitalBusinessLogic\HospitalBusinessLogic.csproj", "{861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalDatabaseImplement", "HospitalDatabaseImplement\HospitalDatabaseImplement.csproj", "{2C40C8A1-137C-4B36-BA44-888CD7230E50}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +31,14 @@ Global {0A3FDB59-29E9-459A-8D17-62CA88D1C56E}.Debug|Any CPU.Build.0 = Debug|Any CPU {0A3FDB59-29E9-459A-8D17-62CA88D1C56E}.Release|Any CPU.ActiveCfg = Release|Any CPU {0A3FDB59-29E9-459A-8D17-62CA88D1C56E}.Release|Any CPU.Build.0 = Release|Any CPU + {861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Release|Any CPU.Build.0 = Release|Any CPU + {2C40C8A1-137C-4B36-BA44-888CD7230E50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2C40C8A1-137C-4B36-BA44-888CD7230E50}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2C40C8A1-137C-4B36-BA44-888CD7230E50}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2C40C8A1-137C-4B36-BA44-888CD7230E50}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/DiseaseLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/DiseaseLogic.cs new file mode 100644 index 0000000..d9c5a94 --- /dev/null +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/DiseaseLogic.cs @@ -0,0 +1,116 @@ +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.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalBusinessLogic.BusinessLogics +{ + public class DiseaseLogic : IDiseaseLogic + { + private readonly ILogger _logger; + private readonly IDiseaseStorage _diseaseStorage; + + public DiseaseLogic(ILogger logger, IDiseaseStorage diseaseStorage) + { + _logger = logger; + _diseaseStorage = diseaseStorage; + } + + public bool Create(DiseaseBindingModel model) + { + CheckModel(model); + if (_diseaseStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(DiseaseBindingModel model) + { + CheckModel(model); + 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. Id:{Id}", model.Id); + if (_diseaseStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public DiseaseViewModel? ReadElement(DiseaseSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _diseaseStorage.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(DiseaseSearchModel? model) + { + _logger.LogInformation("ReadList. DiseaseId:{Id}", model?.Id); + var list = model == null ? _diseaseStorage.GetFullList() : _diseaseStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + 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)); + } + _logger.LogInformation("Disease. Name:{Name}. Symptoms:{Symptoms}. DoctorId:{DoctorId}. Id:{ Id}", model.Name, model.Symptoms, model.DoctorId, model.Id); + var element = _diseaseStorage.GetElement(new DiseaseSearchModel + { + Name = model.Name, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Болезнь с такими данными уже есть"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/DoctorLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/DoctorLogic.cs new file mode 100644 index 0000000..a8c5919 --- /dev/null +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/DoctorLogic.cs @@ -0,0 +1,124 @@ +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 HospitalBusinessLogic.BusinessLogics +{ + public class DoctorLogic : IDoctorLogic + { + private readonly ILogger _logger; + private readonly IDoctorStorage _doctorStorage; + + public DoctorLogic(ILogger logger, IDoctorStorage doctorStorage) + { + _logger = logger; + _doctorStorage = doctorStorage; + } + + public bool Create(DoctorBindingModel model) + { + CheckModel(model); + if (_doctorStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(DoctorBindingModel model) + { + CheckModel(model); + 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. Id:{Id}", model.Id); + if (_doctorStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public DoctorViewModel? ReadElement(DoctorSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Login:{Login}. PhoneNumber:{PhoneNumber}. Id:{ Id}", model.Login, model.PhoneNumber, model.Id); + var element = _doctorStorage.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(DoctorSearchModel? model) + { + _logger.LogInformation("ReadList. DoctorId:{Id}", model?.Id); + var list = model == null ? _doctorStorage.GetFullList() : _doctorStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(DoctorBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Login)) + { + throw new ArgumentNullException("Нет логина врача", nameof(model.Login)); + } + if (string.IsNullOrEmpty(model.PhoneNumber)) + { + throw new ArgumentNullException("Нет номера телефона врача", nameof(model.PhoneNumber)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля врача", nameof(model.Password)); + } + _logger.LogInformation("Doctor. Login:{Login}. PhoneNumber:{PhoneNumber}. Password:{Password}. Id:{ Id}", model.Login, model.PhoneNumber, model.Password, model.Id); + var element = _doctorStorage.GetElement(new DoctorSearchModel + { + Login = model.Login, + PhoneNumber = model.PhoneNumber, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Врач с таким именем уже есть"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/MedicineLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/MedicineLogic.cs new file mode 100644 index 0000000..dc25593 --- /dev/null +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/MedicineLogic.cs @@ -0,0 +1,115 @@ +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 HospitalBusinessLogic.BusinessLogics +{ + public class MedicineLogic : IMedicineLogic + { + private readonly ILogger _logger; + private readonly IMedicineStorage _medicineStorage; + + public MedicineLogic(ILogger logger, IMedicineStorage medicineStorage) + { + _logger = logger; + _medicineStorage = medicineStorage; + } + + 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; + } + + public MedicineViewModel? ReadElement(MedicineSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, 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 List? ReadList(MedicineSearchModel? model) + { + _logger.LogInformation("ReadList. MedicineId:{Id}", 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; + } + + 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("Medicine. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _medicineStorage.GetElement(new MedicineSearchModel + { + Name = model.Name, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Лекарство с таким названием уже есть"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/PatientLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/PatientLogic.cs new file mode 100644 index 0000000..b9ce59f --- /dev/null +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/PatientLogic.cs @@ -0,0 +1,129 @@ +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 HospitalBusinessLogic.BusinessLogics +{ + public class PatientLogic : IPatientLogic + { + private readonly ILogger _logger; + private readonly IPatientStorage _patientStorage; + + public PatientLogic(ILogger logger, IPatientStorage patientStorage) + { + _logger = logger; + _patientStorage = patientStorage; + } + + public bool Create(PatientBindingModel model) + { + CheckModel(model); + if (_patientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(PatientBindingModel model) + { + CheckModel(model); + 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. Id:{Id}", model.Id); + if (_patientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public PatientViewModel? ReadElement(PatientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Surname:{Surname}. Name:{Name}. Patronymic:{Patronymic}. Id:{ Id}", model.Surname, model.Name, model.Patronymic, model.Id); + var element = _patientStorage.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(PatientSearchModel? model) + { + _logger.LogInformation("ReadList. PatientId:{Id}", model?.Id); + var list = model == null ? _patientStorage.GetFullList() : _patientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(PatientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Surname)) + { + throw new ArgumentNullException("Нет фамилии пациента", nameof(model.Surname)); + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет имени пациента", nameof(model.Name)); + } + if (string.IsNullOrEmpty(model.Patronymic)) + { + throw new ArgumentNullException("Нет отчества пациента", nameof(model.Patronymic)); + } + if (model.Age < 0) + { + throw new ArgumentNullException("Возраст пациента должен быть больше 0", nameof(model.Patronymic)); + } + _logger.LogInformation("Patient. Login:{Login}. PhoneNumber:{PhoneNumber}. Password:{Password}. Age:{Age}. DoctorId:{DoctorId}. Id:{ Id}", model.Surname, model.Name, model.Patronymic, model.Age, model.DoctorId, model.Id); + var element = _patientStorage.GetElement(new PatientSearchModel + { + Surname = model.Surname, + Name = model.Name, + Patronymic = model.Patronymic, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Пациент с такими данными уже есть"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/ProcedureLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/ProcedureLogic.cs new file mode 100644 index 0000000..4d9bed1 --- /dev/null +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/ProcedureLogic.cs @@ -0,0 +1,115 @@ +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 HospitalBusinessLogic.BusinessLogics +{ + public class ProcedureLogic : IProcedureLogic + { + private readonly ILogger _logger; + private readonly IProcedureStorage _procedureStorage; + + public ProcedureLogic(ILogger logger, IProcedureStorage procedureStorage) + { + _logger = logger; + _procedureStorage = procedureStorage; + } + + public bool Create(ProcedureBindingModel model) + { + CheckModel(model); + if (_procedureStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ProcedureBindingModel model) + { + CheckModel(model); + 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. Id:{Id}", model.Id); + if (_procedureStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public ProcedureViewModel? ReadElement(ProcedureSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _procedureStorage.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(ProcedureSearchModel? model) + { + _logger.LogInformation("ReadList. ProcedureId:{Id}", model?.Id); + var list = model == null ? _procedureStorage.GetFullList() : _procedureStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + 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("Procedure. Name:{Name}. Id:{ Id}", model.Name, model.Id); + var element = _procedureStorage.GetElement(new ProcedureSearchModel + { + Name = model.Name, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Процедура с таким названием уже есть"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogic/BusinessLogics/RecipeLogic.cs b/Hospital/HospitalBusinessLogic/BusinessLogics/RecipeLogic.cs new file mode 100644 index 0000000..5bcd944 --- /dev/null +++ b/Hospital/HospitalBusinessLogic/BusinessLogics/RecipeLogic.cs @@ -0,0 +1,115 @@ +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 HospitalBusinessLogic.BusinessLogics +{ + public class RecipeLogic : IRecipeLogic + { + private readonly ILogger _logger; + private readonly IRecipeStorage _recipeStorage; + + public RecipeLogic(ILogger logger, IRecipeStorage recipeStorage) + { + _logger = logger; + _recipeStorage = recipeStorage; + } + + public bool Create(RecipeBindingModel model) + { + CheckModel(model); + if (_recipeStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(RecipeBindingModel model) + { + CheckModel(model); + 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. Id:{Id}", model.Id); + if (_recipeStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public RecipeViewModel? ReadElement(RecipeSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id:{ Id}", model.Id); + var element = _recipeStorage.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(RecipeSearchModel? model) + { + _logger.LogInformation("ReadList. RecipeId:{Id}", model?.Id); + var list = model == null ? _recipeStorage.GetFullList() : _recipeStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(RecipeBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.RecipeMedicines == null || model.RecipeMedicines.Count == 0) + { + throw new ArgumentNullException("Нет лекарств в рецепте", nameof(model.RecipeMedicines)); + } + _logger.LogInformation("Recipe. Id:{ Id}. DiseaseId:{DiseaseId}. DoctorId:{DoctorId}", model.Id, model.DiseaseId, model.DoctorId); + var element = _recipeStorage.GetElement(new RecipeSearchModel + { + Id = model.Id, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Пациент с такими данными уже есть"); + } + } + } +} diff --git a/Hospital/HospitalBusinessLogic/HospitalBusinessLogic.csproj b/Hospital/HospitalBusinessLogic/HospitalBusinessLogic.csproj new file mode 100644 index 0000000..f27c57a --- /dev/null +++ b/Hospital/HospitalBusinessLogic/HospitalBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/Hospital/HospitalContracts/BindingModels/MedicineBindingModel.cs b/Hospital/HospitalContracts/BindingModels/MedicineBindingModel.cs index 5be8458..df6213f 100644 --- a/Hospital/HospitalContracts/BindingModels/MedicineBindingModel.cs +++ b/Hospital/HospitalContracts/BindingModels/MedicineBindingModel.cs @@ -12,7 +12,5 @@ namespace HospitalContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; - - public string Form { get; set; } = string.Empty; } } diff --git a/Hospital/HospitalContracts/ViewModels/MedicineViewModel.cs b/Hospital/HospitalContracts/ViewModels/MedicineViewModel.cs index 0aadfff..88c922e 100644 --- a/Hospital/HospitalContracts/ViewModels/MedicineViewModel.cs +++ b/Hospital/HospitalContracts/ViewModels/MedicineViewModel.cs @@ -14,8 +14,5 @@ namespace HospitalContracts.ViewModels [DisplayName("Название лекарства")] public string Name { get; set; } = string.Empty; - - [DisplayName("Форма выпуска лекарства")] - public string Form { get; set; } = string.Empty; } } diff --git a/Hospital/HospitalDataModels/Models/IMedicineModel.cs b/Hospital/HospitalDataModels/Models/IMedicineModel.cs index 15c07f7..b1f931b 100644 --- a/Hospital/HospitalDataModels/Models/IMedicineModel.cs +++ b/Hospital/HospitalDataModels/Models/IMedicineModel.cs @@ -9,7 +9,5 @@ namespace HospitalDataModels.Models public interface IMedicineModel : IId { string Name { get; } - - string Form { get; } } } diff --git a/Hospital/HospitalDataModels/Properties/launchSettings.json b/Hospital/HospitalDataModels/Properties/launchSettings.json new file mode 100644 index 0000000..ed0f25f --- /dev/null +++ b/Hospital/HospitalDataModels/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "HospitalDataModels": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:65343;http://localhost:65344" + } + } +} \ No newline at end of file diff --git a/Hospital/HospitalDatabaseImplement/HospitalDatabase.cs b/Hospital/HospitalDatabaseImplement/HospitalDatabase.cs new file mode 100644 index 0000000..0d737a7 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/HospitalDatabase.cs @@ -0,0 +1,42 @@ +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement +{ + public class HospitalDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=HospitalDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Doctors { set; get; } + + public virtual DbSet Patients { set; get; } + + public virtual DbSet Recipes { set; get; } + + public virtual DbSet Diseases { set; get; } + + public virtual DbSet Medicines { set; get; } + + public virtual DbSet Procedures { set; get; } + + public virtual DbSet PatientRecipes { set; get; } + + public virtual DbSet PatientProcedures { set; get; } + + public virtual DbSet RecipeMedicines { set; get; } + + public virtual DbSet ProcedureMedicines { set; get; } + } +} diff --git a/Hospital/HospitalDatabaseImplement/HospitalDatabaseImplement.csproj b/Hospital/HospitalDatabaseImplement/HospitalDatabaseImplement.csproj new file mode 100644 index 0000000..b227ee5 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/HospitalDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Hospital/HospitalDatabaseImplement/Implements/DoctorStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/DoctorStorage.cs new file mode 100644 index 0000000..1dc0c50 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/DoctorStorage.cs @@ -0,0 +1,85 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Implements +{ + public class DoctorStorage : IDoctorStorage + { + public DoctorViewModel? GetElement(DoctorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.PhoneNumber) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Doctors.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login && + !string.IsNullOrEmpty(model.PhoneNumber) && x.PhoneNumber == model.PhoneNumber && + !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(DoctorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Doctors.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Doctors.Select(x => x.GetViewModel).ToList(); + } + + public DoctorViewModel? Insert(DoctorBindingModel model) + { + var newDoctor = Doctor.Create(model); + if (newDoctor == null) + { + return null; + } + using var context = new HospitalDatabase(); + context.Doctors.Add(newDoctor); + context.SaveChanges(); + return newDoctor.GetViewModel; + } + + public DoctorViewModel? Update(DoctorBindingModel model) + { + using var context = new HospitalDatabase(); + var doctor = context.Doctors.FirstOrDefault(x => x.Id == model.Id); + if (doctor == null) + { + return null; + } + doctor.Update(model); + context.SaveChanges(); + return doctor.GetViewModel; + } + + public DoctorViewModel? Delete(DoctorBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Doctors.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs new file mode 100644 index 0000000..d8b12c0 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs @@ -0,0 +1,84 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Implements +{ + public class PatientStorage : IPatientStorage + { + public PatientViewModel? GetElement(PatientSearchModel model) + { + if (string.IsNullOrEmpty(model.Surname) && string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.Patronymic) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Doctors.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login && + !string.IsNullOrEmpty(model.PhoneNumber) && x.PhoneNumber == model.PhoneNumber && + !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(PatientSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Doctors.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Doctors.Select(x => x.GetViewModel).ToList(); + } + + public PatientViewModel? Insert(PatientBindingModel model) + { + var newDoctor = Doctor.Create(model); + if (newDoctor == null) + { + return null; + } + using var context = new HospitalDatabase(); + context.Doctors.Add(newDoctor); + context.SaveChanges(); + return newDoctor.GetViewModel; + } + + public PatientViewModel? Update(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + var doctor = context.Doctors.FirstOrDefault(x => x.Id == model.Id); + if (doctor == null) + { + return null; + } + doctor.Update(model); + context.SaveChanges(); + return doctor.GetViewModel; + } + + public PatientViewModel? Delete(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Doctors.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Disease.cs b/Hospital/HospitalDatabaseImplement/Models/Disease.cs new file mode 100644 index 0000000..7f100c1 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Disease.cs @@ -0,0 +1,75 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class Disease : IDiseaseModel + { + public int Id { get; set; } + + public int DoctorId { get; private set; } + + public virtual Doctor Doctor { get; set; } + + [Required] + [MaxLength(50)] + public string Name { get; set; } = string.Empty; + + [Required] + [MaxLength(150)] + public string Symptoms { get; set; } = string.Empty; + + [ForeignKey("DiseaseId")] + public virtual List Recipes { get; set; } = new(); + + public static Disease? Create(DiseaseBindingModel model) + { + if (model == null) + { + return null; + } + return new Disease() + { + Id = model.Id, + Name = model.Name, + Symptoms = model.Symptoms, + DoctorId = model.DoctorId + }; + } + + public static Disease Create(DiseaseViewModel model) + { + return new Disease + { + Id = model.Id, + Name = model.Name, + Symptoms = model.Symptoms + }; + } + + public void Update(DiseaseBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + Symptoms = model.Symptoms; + } + + public DiseaseViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Symptoms = Symptoms + }; + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Doctor.cs b/Hospital/HospitalDatabaseImplement/Models/Doctor.cs new file mode 100644 index 0000000..029ffea --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Doctor.cs @@ -0,0 +1,83 @@ +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; + +namespace HospitalDatabaseImplement.Models +{ + public class Doctor : IDoctorModel + { + public int Id { get; set; } + + [Required] + [MaxLength(25)] + public string Login { get; set; } = string.Empty; + + [Required] + [MaxLength(11)] + public string PhoneNumber { get; set; } = string.Empty; + + [Required] + [MaxLength(30)] + public string Password { get; set; } = string.Empty; + + [ForeignKey("DoctorId")] + public virtual List Patients { get; set; } = new(); + + [ForeignKey("DoctorId")] + public virtual List Recipes { get; set; } = new(); + + [ForeignKey("DoctorId")] + public virtual List Diseases { get; set; } = new(); + + public static Doctor? Create(DoctorBindingModel model) + { + if (model == null) + { + return null; + } + return new Doctor() + { + Id = model.Id, + Login = model.Login, + PhoneNumber = model.PhoneNumber, + Password = model.Password + }; + } + public static Doctor Create(DoctorViewModel model) + { + return new Doctor + { + Id = model.Id, + Login = model.Login, + PhoneNumber = model.PhoneNumber, + Password = model.Password + }; + } + public void Update(DoctorBindingModel model) + { + if (model == null) + { + return; + } + Login = model.Login; + PhoneNumber = model.PhoneNumber; + Password = model.Password; + } + public DoctorViewModel GetViewModel => new() + { + Id = Id, + Login = Login, + PhoneNumber = PhoneNumber, + Password = Password + }; + } +} + diff --git a/Hospital/HospitalDatabaseImplement/Models/Medicine.cs b/Hospital/HospitalDatabaseImplement/Models/Medicine.cs new file mode 100644 index 0000000..667c58e --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Medicine.cs @@ -0,0 +1,60 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.ComponentModel; + +namespace HospitalDatabaseImplement.Models +{ + public class Medicine : IMedicineModel + { + public int Id { get; set; } + + [Required] + [MaxLength(50)] + public string Name { get; set; } = string.Empty; + + [ForeignKey("MedicineId")] + public virtual List RecipeMedicines { get; set; } = new(); + + public static Medicine? Create(MedicineBindingModel model) + { + if (model == null) + { + return null; + } + return new Medicine() + { + Id = model.Id, + Name = model.Name + }; + } + public static Medicine Create(MedicineViewModel model) + { + return new Medicine + { + Id = model.Id, + Name = model.Name + }; + } + public void Update(MedicineBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + } + public MedicineViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Patient.cs b/Hospital/HospitalDatabaseImplement/Models/Patient.cs new file mode 100644 index 0000000..589126b --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Patient.cs @@ -0,0 +1,136 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; + +namespace HospitalDatabaseImplement.Models +{ + public class Patient : IPatientModel + { + public int Id { get; set; } + + public int DoctorId { get; private set; } + + public virtual Doctor Doctor { get; set; } + + [Required] + [MaxLength(50)] + public string Surname { get; set; } = string.Empty; + + [Required] + [MaxLength(50)] + public string Name { get; set; } = string.Empty; + + [Required] + [MaxLength(50)] + public string Patronymic { get; set; } = string.Empty; + + [Required] + public int Age { get; set; } + + private Dictionary? _patientRecipes = null; + + [NotMapped] + public Dictionary PatientRecipes + { + get + { + if (_patientRecipes == null) + { + _patientRecipes = Recipes.ToDictionary(recPR => recPR.RecipeId, recPR => (recPR.Recipe as IRecipeModel)); + } + return _patientRecipes; + } + } + + [ForeignKey("PatientId")] + public virtual List Recipes { get; set; } = new(); + + private Dictionary? _patientProcedures = null; + + [NotMapped] + public Dictionary PatientProcedures + { + get + { + if (_patientProcedures == null) + { + _patientProcedures = Procedures.ToDictionary(recPP => recPP.ProcedureId, recPP => (recPP.Procedure as IProcedureModel)); + } + return _patientProcedures; + } + } + + [ForeignKey("PatientId")] + public virtual List Procedures { get; set; } = new(); + + public static Patient Create(HospitalDatabase context, PatientBindingModel model) + { + return new Patient() + { + Id = model.Id, + Surname = model.Surname, + Name = model.Name, + Patronymic = model.Patronymic, + Age = model.Age, + DoctorId = model.DoctorId, + Doctor = context.Doctors.First(x => x.Id == model.DoctorId), + Recipes = model.PatientRecipes.Select(x => new PatientRecipe + { + Recipe = context.Recipes.First(y => y.Id == x.Key), + }).ToList(), + Procedures = model.PatientProcedures.Select(x => new PatientProcedure + { + Procedure = context.Procedures.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(PatientBindingModel model) + { + Surname = model.Surname; + Name = model.Name; + Patronymic = model.Patronymic; + Age = model.Age; + } + + public PatientViewModel GetViewModel => new() + { + Surname = Surname, + Name = Name, + Patronymic = Patronymic, + Age = Age, + PatientRecipes = PatientRecipes, + PatientProcedures = PatientProcedures + }; + + public void UpdateRecipes(HospitalDatabase context, PatientBindingModel model) + { + var patientRecipes = context.PatientRecipes.Where(rec => rec.PatientId == model.Id).ToList(); + if (patientRecipes != null) + { // удалили те, которых нет в модели + context.PatientRecipes.RemoveRange(patientRecipes.Where(rec => !model.PatientRecipes.ContainsKey(rec.RecipeId))); + context.SaveChanges(); + } + var patient = context.Patients.First(x => x.Id == Id); + foreach (var pr in model.PatientRecipes) + { + context.PatientRecipes.Add(new PatientRecipe + { + Patient = patient, + Recipe = context.Recipes.First(x => x.Id == pr.Key), + }); + context.SaveChanges(); + } + _patientRecipes = null; + } + } +} + diff --git a/Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs b/Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs new file mode 100644 index 0000000..21cefa1 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class PatientProcedure + { + public int Id { get; set; } + + [Required] + public int PatientId { get; set; } + + [Required] + public int ProcedureId { get; set; } + + public virtual Patient Patient { get; set; } = new(); + + public virtual Procedure Procedure { get; set; } = new(); + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/PatientRecipe.cs b/Hospital/HospitalDatabaseImplement/Models/PatientRecipe.cs new file mode 100644 index 0000000..0778402 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/PatientRecipe.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class PatientRecipe + { + public int Id { get; set; } + + [Required] + public int PatientId { get; set; } + + [Required] + public int RecipeId { get; set; } + + public virtual Patient Patient { get; set; } = new(); + + public virtual Recipe Recipe { get; set; } = new(); + + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Procedure.cs b/Hospital/HospitalDatabaseImplement/Models/Procedure.cs new file mode 100644 index 0000000..2a00474 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Procedure.cs @@ -0,0 +1,91 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Diagnostics; + +namespace HospitalDatabaseImplement.Models +{ + public class Procedure : IProcedureModel + { + public int Id { get; set; } + + [Required] + [MaxLength(50)] + public string Name { get; set; } = string.Empty; + + [ForeignKey("ProcedureId")] + public virtual List PatientProcedures { get; set; } = new(); + + private Dictionary? _procedureMedicines = null; + + [NotMapped] + public Dictionary ProcedureMedicines + { + get + { + if (_procedureMedicines == null) + { + _procedureMedicines = Medicines.ToDictionary(recPM => recPM.MedicineId, recPM =>(recPM.Medicine as IMedicineModel)); + } + return _procedureMedicines; + } + } + + [ForeignKey("ProcedureId")] + public virtual List Medicines { get; set; } = new(); + + public static Procedure Create(HospitalDatabase context, ProcedureBindingModel model) + { + return new Procedure() + { + Id = model.Id, + Name = model.Name, + Medicines = model.ProcedureMedicines.Select(x => new ProcedureMedicine + { + Medicine = context.Medicines.First(y => y.Id == x.Key) + }).ToList() + }; + } + + public void Update(ProcedureBindingModel model) + { + Name = model.Name; + } + + public ProcedureViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + ProcedureMedicines = ProcedureMedicines + }; + + public void UpdateMedicines(HospitalDatabase context, ProcedureBindingModel model) + { + var procedureMedicines = context.ProcedureMedicines.Where(rec => rec.ProcedureId == model.Id).ToList(); + if (procedureMedicines != null) + { // удалили те, которых нет в модели + context.ProcedureMedicines.RemoveRange(procedureMedicines.Where(rec => !model.ProcedureMedicines.ContainsKey(rec.MedicineId))); + context.SaveChanges(); + } + var procedure = context.Procedures.First(x => x.Id == Id); + foreach (var pm in model.ProcedureMedicines) + { + context.ProcedureMedicines.Add(new ProcedureMedicine + { + Procedure = procedure, + Medicine = context.Medicines.First(x => x.Id == pm.Key) + }); + context.SaveChanges(); + } + _procedureMedicines = null; + } + + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs b/Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs new file mode 100644 index 0000000..1ba81cb --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class ProcedureMedicine + { + public int Id { get; set; } + + [Required] + public int ProcedureId { get; set; } + + [Required] + public int MedicineId { get; set; } + + public virtual Procedure Procedure { get; set; } = new(); + + public virtual Medicine Medicine { get; set; } = new(); + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Recipe.cs b/Hospital/HospitalDatabaseImplement/Models/Recipe.cs new file mode 100644 index 0000000..bf087d6 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Recipe.cs @@ -0,0 +1,96 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class Recipe : IRecipeModel + { + public int Id { get; set; } + + public int DoctorId { get; private set; } + + public virtual Doctor Doctor { get; set; } + + public int DiseaseId { get; private set; } + + public virtual Disease Disease { get; set; } + + [Required] + public DateTime IssueDate { get; private set; } = DateTime.Now; + + private Dictionary? _recipeMedicines = null; + + [NotMapped] + public Dictionary RecipeMedicines + { + get + { + if (_recipeMedicines == null) + { + _recipeMedicines = Medicines.ToDictionary(recRM => recRM.MedicineId, recRM => (recRM.Medicine as IMedicineModel)); + } + return _recipeMedicines; + } + } + + [ForeignKey("RecipeId")] + public virtual List Medicines { get; set; } = new(); + + public static Recipe Create(HospitalDatabase context, RecipeBindingModel model) + { + return new Recipe() + { + Id = model.Id, + IssueDate = model.IssueDate, + DiseaseId = model.DiseaseId, + DoctorId = model.DoctorId, + Doctor = context.Doctors.First(x => x.Id == model.DoctorId), + Medicines = model.RecipeMedicines.Select(x => new RecipeMedicine + { + Medicine = context.Medicines.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(RecipeBindingModel model) + { + IssueDate = model.IssueDate; + } + + public RecipeViewModel GetViewModel => new() + { + Id = Id, + IssueDate = IssueDate, + RecipeMedicines = RecipeMedicines + }; + + public void UpdateMedicines(HospitalDatabase context, RecipeBindingModel model) + { + var recipeMedicines = context.RecipeMedicines.Where(rec => rec.RecipeId == model.Id).ToList(); + if (recipeMedicines != null) + { // удалили те, которых нет в модели + context.RecipeMedicines.RemoveRange(recipeMedicines.Where(rec => !model.RecipeMedicines.ContainsKey(rec.MedicineId))); + context.SaveChanges(); + } + var recipe = context.Recipes.First(x => x.Id == Id); + foreach (var pr in model.RecipeMedicines) + { + context.RecipeMedicines.Add(new RecipeMedicine + { + Recipe = recipe, + Medicine = context.Medicines.First(x => x.Id == pr.Key), + }); + context.SaveChanges(); + } + _recipeMedicines = null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs b/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs new file mode 100644 index 0000000..ec3a9ad --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class RecipeMedicine + { + public int Id { get; set; } + + [Required] + public int RecipeId { get; set; } + + [Required] + public int MedicineId { get; set; } + + public virtual Recipe Recipe { get; set; } = new(); + + public virtual Medicine Medicine { get; set; } = new(); + } +}