From ced335c9412b5204a4dac8e68f647205cbdb4f98 Mon Sep 17 00:00:00 2001 From: ValAnn Date: Mon, 29 Apr 2024 13:37:57 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B=20?= =?UTF-8?q?=D0=B1=D0=B4=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BD=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Hospital/Hospital/HospitalView.csproj | 7 + .../ViewModels/PatientViewModel.cs | 6 +- .../HospitalDataBaseImplement.csproj | 11 +- .../HospitalDatabase.cs | 35 ++++- .../Implementss/DiseaseStorage.cs | 120 +++++++++++++++++ .../Implementss/DoctorStorage.cs | 114 ++++++++++++++++ .../Implementss/PatientStorage.cs | 122 +++++++++++++++++ .../Implementss/ProcedureStorage.cs | 108 +++++++++++++++ .../Implementss/RecipeStorage.cs | 123 ++++++++++++++++++ .../Models/Medicine.cs | 3 +- .../Models/MedicineRecipes.cs | 12 -- .../Models/PatientProcedure.cs | 25 ++++ .../Models/PatientRecipe.cs | 26 ++++ .../Models/Procedure.cs | 90 ++++++++++++- .../Models/ProcedureMedicine.cs | 4 +- .../Models/Recipe.cs | 94 ++++++++++++- .../Models/RecipeMedicine.cs | 25 ++++ 17 files changed, 900 insertions(+), 25 deletions(-) create mode 100644 Hospital/HospitalDatabaseImplement/Implementss/DiseaseStorage.cs create mode 100644 Hospital/HospitalDatabaseImplement/Implementss/DoctorStorage.cs create mode 100644 Hospital/HospitalDatabaseImplement/Implementss/PatientStorage.cs create mode 100644 Hospital/HospitalDatabaseImplement/Implementss/ProcedureStorage.cs create mode 100644 Hospital/HospitalDatabaseImplement/Implementss/RecipeStorage.cs delete mode 100644 Hospital/HospitalDatabaseImplement/Models/MedicineRecipes.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/PatientRecipe.cs create mode 100644 Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs diff --git a/Hospital/Hospital/HospitalView.csproj b/Hospital/Hospital/HospitalView.csproj index f97c885..2b28e74 100644 --- a/Hospital/Hospital/HospitalView.csproj +++ b/Hospital/Hospital/HospitalView.csproj @@ -8,6 +8,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Hospital/HospitalContracts/ViewModels/PatientViewModel.cs b/Hospital/HospitalContracts/ViewModels/PatientViewModel.cs index 073890c..c8f93be 100644 --- a/Hospital/HospitalContracts/ViewModels/PatientViewModel.cs +++ b/Hospital/HospitalContracts/ViewModels/PatientViewModel.cs @@ -13,13 +13,13 @@ namespace HospitalContracts.ViewModels public int Id { get; set; } [DisplayName("ФИО")] - public string FIO { get; } + public string FIO { get; set; } = string.Empty; [DisplayName("Дата рождения")] - public DateOnly BirthDate { get; } + public DateOnly BirthDate { get; set; } = new DateOnly(Int32.Parse("2000"), Int32.Parse("01"), Int32.Parse("01")); [DisplayName("Адрес")] - public string Adress { get; } + public string Address { get; set; } = string.Empty; public int DoctorId { get; set; } diff --git a/Hospital/HospitalDataBaseImplement/HospitalDataBaseImplement.csproj b/Hospital/HospitalDataBaseImplement/HospitalDataBaseImplement.csproj index 762fcc6..719a1d6 100644 --- a/Hospital/HospitalDataBaseImplement/HospitalDataBaseImplement.csproj +++ b/Hospital/HospitalDataBaseImplement/HospitalDataBaseImplement.csproj @@ -12,7 +12,16 @@ - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Hospital/HospitalDataBaseImplement/HospitalDatabase.cs b/Hospital/HospitalDataBaseImplement/HospitalDatabase.cs index de1b89f..88efe75 100644 --- a/Hospital/HospitalDataBaseImplement/HospitalDatabase.cs +++ b/Hospital/HospitalDataBaseImplement/HospitalDatabase.cs @@ -1,4 +1,7 @@ -using System; +using HospitalDatabaseImplement.Models; +using HospitalDatabaseImplement.Modelss; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -7,8 +10,36 @@ using System.Threading.Tasks; namespace HospitalDatabaseImplement { - public class HospitalDatabase + public class HospitalDatabase : DbContext { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=HospitalDatabase;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; } + + //TODO добавить сущности описание процедур и промежутучную сущость у фармацевта } } diff --git a/Hospital/HospitalDatabaseImplement/Implementss/DiseaseStorage.cs b/Hospital/HospitalDatabaseImplement/Implementss/DiseaseStorage.cs new file mode 100644 index 0000000..fb36a9a --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implementss/DiseaseStorage.cs @@ -0,0 +1,120 @@ +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.Implementss +{ + public class DiseaseStorage : IDiseaseStorage + { + public DiseaseViewModel? GetElement(DiseaseSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(DiseaseSearchModel model) + { + using var context = new HospitalDatabase(); + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.DoctorId.HasValue) + { + return context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .Where(x => x.DoctorId == model.DoctorId) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return new(); + } + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .Select(x => x.GetViewModel) + .ToList(); + } + + public DiseaseViewModel? Insert(DiseaseBindingModel model) + { + var newDisease = Disease.Create(model); + if (newDisease == null) + { + return null; + } + using var context = new HospitalDatabase(); + context.Diseases.Add(newDisease); + context.SaveChanges(); + return context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .FirstOrDefault(x => x.Id == newDisease.Id) + ?.GetViewModel; + } + + public DiseaseViewModel? Update(DiseaseBindingModel model) + { + using var context = new HospitalDatabase(); + var disease = context.Diseases.FirstOrDefault(x => x.Id == model.Id); + if (disease == null) + { + return null; + } + disease.Update(model); + context.SaveChanges(); + return context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public DiseaseViewModel? Delete(DiseaseBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Diseases.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Diseases + .Include(x => x.Recipes) + .Include(x => x.Doctor) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Diseases.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implementss/DoctorStorage.cs b/Hospital/HospitalDatabaseImplement/Implementss/DoctorStorage.cs new file mode 100644 index 0000000..fb5018a --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implementss/DoctorStorage.cs @@ -0,0 +1,114 @@ +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.Implementss +{ + public class DoctorStorage : IDoctorStorage + { + public DoctorViewModel? GetElement(DoctorSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Doctors + .Include(x => x.Recipes) + .Include(x => x.Diseases) + .Include(x => x.Patients) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(DoctorSearchModel model) + { + using var context = new HospitalDatabase(); + if (!string.IsNullOrEmpty(model.Login)) + { + return context.Doctors + .Include(x => x.Recipes) + .Include(x => x.Diseases) + .Include(x => x.Patients) + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Doctors + .Include(x => x.Recipes) + .Include(x => x.Diseases) + .Include(x => x.Patients) + .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 context.Doctors + .Include(x => x.Recipes) + .Include(x => x.Diseases) + .Include(x => x.Patients) + .FirstOrDefault(x => x.Id == newDoctor.Id) + ?.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 context.Doctors + .Include(x => x.Recipes) + .Include(x => x.Diseases) + .Include(x => x.Patients) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public DoctorViewModel? Delete(DoctorBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Doctors + .Include(x => x.Recipes) + .Include(x => x.Diseases) + .Include(x => x.Patients) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Doctors.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implementss/PatientStorage.cs b/Hospital/HospitalDatabaseImplement/Implementss/PatientStorage.cs new file mode 100644 index 0000000..4f6fcb6 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implementss/PatientStorage.cs @@ -0,0 +1,122 @@ +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.Implementss +{ + public class PatientStorage : IPatientStorage + { + public PatientViewModel? GetElement(PatientSearchModel model) + { + if (string.IsNullOrEmpty(model.FIO) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Patients + .Include(x => x.Doctor) + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe) + .FirstOrDefault(x => ((!string.IsNullOrEmpty(model.FIO) && x.FIO == model.FIO) + || (model.Id.HasValue && x.Id == model.Id)))?.GetViewModel; + } + + public List GetFilteredList(PatientSearchModel model) + { + using var context = new HospitalDatabase(); + if (!string.IsNullOrEmpty(model.FIO) ) + { + return context.Patients + .Include(x => x.Doctor) + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe) + .Where(x => (x.Id == model.Id)).ToList() + .Select(x => x.GetViewModel).ToList(); + } + else if (model.DoctorId.HasValue) + { + return context.Patients + .Include(x => x.Doctor) + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe) + .Where(x => x.DoctorId == model.DoctorId) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Patients + .Include(x => x.Doctor) + .Include(x => x.Recipes) + .ThenInclude(x => x.Recipe).ToList() + .Select(x => x.GetViewModel).ToList(); + } + + public PatientViewModel? Insert(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + var newPatient = Patient.Create(context, model); + if (newPatient == null) + { + return null; + } + context.Patients.Add(newPatient); + context.SaveChanges(); + return newPatient.GetViewModel; + } + + public PatientViewModel? Update(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var patient = context.Patients.FirstOrDefault(rec => rec.Id == model.Id); + if (patient == null) + { + return null; + } + patient.Update(model); + context.SaveChanges(); + patient.UpdateRecipes(context, model); + transaction.Commit(); + return patient.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PatientViewModel? Delete(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Patients + .Include(x => x.Doctor) + .Include(x => x.Recipes) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Patients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implementss/ProcedureStorage.cs b/Hospital/HospitalDatabaseImplement/Implementss/ProcedureStorage.cs new file mode 100644 index 0000000..2094aaf --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implementss/ProcedureStorage.cs @@ -0,0 +1,108 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Modelss; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Implementss +{ + public class ProcedureStorage : IProcedureStorage + { + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Procedures + .Include(x => x.Medicines) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProcedureSearchModel model) + { + using var context = new HospitalDatabase(); + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return context.Procedures + .Include(x => x.Medicines) + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + + public ProcedureViewModel? GetElement(ProcedureSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Procedures + .Include(x => x.Medicines) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ProcedureViewModel? Insert(ProcedureBindingModel model) + { + using var context = new HospitalDatabase(); + var newProcedure = Procedure.Create(context, model); + if (newProcedure == null) + { + return null; + } + context.Procedures.Add(newProcedure); + context.SaveChanges(); + return newProcedure.GetViewModel; + } + + public ProcedureViewModel? Update(ProcedureBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var procedure = context.Procedures + .FirstOrDefault(rec => rec.Id == model.Id); + if (procedure == null) + { + return null; + } + procedure.Update(model); + context.SaveChanges(); + procedure.UpdateMedicines(context, model); + transaction.Commit(); + return procedure.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ProcedureViewModel? Delete(ProcedureBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Procedures + .Include(x => x.Medicines) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Procedures.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implementss/RecipeStorage.cs b/Hospital/HospitalDatabaseImplement/Implementss/RecipeStorage.cs new file mode 100644 index 0000000..3e79556 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implementss/RecipeStorage.cs @@ -0,0 +1,123 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StoragesContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Modelss; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Implementss +{ + public class RecipeStorage : IRecipeStorage + { + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Recipes + .Include(x => x.Doctor) + .Include(x => x.DiseaseId) + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(RecipeSearchModel model) + { + using var context = new HospitalDatabase(); + if (!model.Id.HasValue) + { + return context.Recipes + .Include(x => x.Doctor) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .Where(x => x.Id == model.Id).ToList() + .Select(x => x.GetViewModel).ToList(); + } + else if (model.DoctorId.HasValue) + { + return context.Recipes + .Include(x => x.Doctor) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .Where(x => x.DoctorId == model.DoctorId) + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + + public RecipeViewModel? GetElement(RecipeSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Recipes + .Include(x => x.Doctor) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + + public RecipeViewModel? Insert(RecipeBindingModel model) + { + using var context = new HospitalDatabase(); + var newRecipe = Recipe.Create(context, model); + if (newRecipe == null) + { + return null; + } + context.Recipes.Add(newRecipe); + context.SaveChanges(); + return newRecipe.GetViewModel; + } + + public RecipeViewModel? Update(RecipeBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var recipe = context.Recipes + .Include(x => x.Doctor) + .FirstOrDefault(rec => rec.Id == model.Id); + if (recipe == null) + { + return null; + } + recipe.Update(model); + context.SaveChanges(); + recipe.UpdateMedicines(context, model); + transaction.Commit(); + return recipe.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public RecipeViewModel? Delete(RecipeBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Recipes + .Include(x => x.Doctor) + .Include(x => x.Medicines) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Recipes.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Medicine.cs b/Hospital/HospitalDatabaseImplement/Models/Medicine.cs index cbeb203..b5de1e2 100644 --- a/Hospital/HospitalDatabaseImplement/Models/Medicine.cs +++ b/Hospital/HospitalDatabaseImplement/Models/Medicine.cs @@ -8,6 +8,7 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using HospitalDatabaseImplement.Models; namespace HospitalDatabaseImplement.Modelss { @@ -23,7 +24,7 @@ namespace HospitalDatabaseImplement.Modelss public int PharmacistId { get; set; } [ForeignKey("MedicineId")] - public virtual List RecipeMedicines { get; set; } = new(); + public virtual List RecipeMedicines { get; set; } = new(); public static Medicine? Create(MedicineBindingModel model) { diff --git a/Hospital/HospitalDatabaseImplement/Models/MedicineRecipes.cs b/Hospital/HospitalDatabaseImplement/Models/MedicineRecipes.cs deleted file mode 100644 index 65104e8..0000000 --- a/Hospital/HospitalDatabaseImplement/Models/MedicineRecipes.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace HospitalDatabaseImplement.Modelss -{ - public class MedicineRecipes - { - } -} diff --git a/Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs b/Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs new file mode 100644 index 0000000..87f6313 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/PatientProcedure.cs @@ -0,0 +1,25 @@ +using HospitalDatabaseImplement.Modelss; +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..a4464ba --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/PatientRecipe.cs @@ -0,0 +1,26 @@ +using HospitalDatabaseImplement.Modelss; +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 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 index aab8a53..24ecd34 100644 --- a/Hospital/HospitalDatabaseImplement/Models/Procedure.cs +++ b/Hospital/HospitalDatabaseImplement/Models/Procedure.cs @@ -1,12 +1,98 @@ -using System; +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +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.Modelss { - public class Procedure + // TODO переделать под все необходимые атрибуты фармацевту + 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 DateTime Date => throw new NotImplementedException(); + + public int DescriptionOfTheProcedureId => throw new NotImplementedException(); + + public int PharmacistId => throw new NotImplementedException(); + + 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 index b3a93a7..7f275a2 100644 --- a/Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs +++ b/Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs @@ -12,12 +12,12 @@ namespace HospitalDatabaseImplement.Modelss public int Id { get; set; } [Required] - public int RecipeId { get; set; } + public int ProcedureId { get; set; } [Required] public int MedicineId { get; set; } - public virtual Recipe Recipe { get; set; } = new(); + 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 index 5e2b569..aa1d074 100644 --- a/Hospital/HospitalDatabaseImplement/Models/Recipe.cs +++ b/Hospital/HospitalDatabaseImplement/Models/Recipe.cs @@ -1,12 +1,102 @@ -using System; +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +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.Modelss { - public class Recipe + 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; + + [Required] + public string Description { get; private set; } = string.Empty; + + 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, + Description = model.Description, + 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; + Description = model.Description; + } + + 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..d7344c8 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs @@ -0,0 +1,25 @@ +using HospitalDatabaseImplement.Modelss; +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(); + } +}