diff --git a/Hospital/Hospital.sln b/Hospital/Hospital.sln index ae2d68a..9487b59 100644 --- a/Hospital/Hospital.sln +++ b/Hospital/Hospital.sln @@ -3,11 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32901.215 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalWeb", "HospitalWeb\HospitalWeb.csproj", "{E227FF03-D689-4C2C-ACAD-5440A68A6C20}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalWeb", "HospitalWeb\HospitalWeb.csproj", "{E227FF03-D689-4C2C-ACAD-5440A68A6C20}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalDataModels", "HospitalDataModels\HospitalDataModels.csproj", "{0CB78C46-D21B-4BF8-BA79-CF2A2F4D5452}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalDataModels", "HospitalDataModels\HospitalDataModels.csproj", "{0CB78C46-D21B-4BF8-BA79-CF2A2F4D5452}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{F24FE3B9-EA65-4793-A950-68857BCC7DB2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{F24FE3B9-EA65-4793-A950-68857BCC7DB2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalDatabaseImplement", "HospitalDatabaseImplement\HospitalDatabaseImplement.csproj", "{B99AB6C1-2F4F-4E40-B933-45CE5E6CC37B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -27,6 +29,10 @@ Global {F24FE3B9-EA65-4793-A950-68857BCC7DB2}.Debug|Any CPU.Build.0 = Debug|Any CPU {F24FE3B9-EA65-4793-A950-68857BCC7DB2}.Release|Any CPU.ActiveCfg = Release|Any CPU {F24FE3B9-EA65-4793-A950-68857BCC7DB2}.Release|Any CPU.Build.0 = Release|Any CPU + {B99AB6C1-2F4F-4E40-B933-45CE5E6CC37B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B99AB6C1-2F4F-4E40-B933-45CE5E6CC37B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B99AB6C1-2F4F-4E40-B933-45CE5E6CC37B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B99AB6C1-2F4F-4E40-B933-45CE5E6CC37B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Hospital/HospitalDatabaseImplement/HospitalDatabase.cs b/Hospital/HospitalDatabaseImplement/HospitalDatabase.cs new file mode 100644 index 0000000..92ddb35 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/HospitalDatabase.cs @@ -0,0 +1,28 @@ +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HospitalDatabaseImplement +{ + public class HospitalDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=FurnitureAssemblyDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Apothecaries { set; get; } + public virtual DbSet Recipes { set; get; } + public virtual DbSet Medicines { set; get; } + public virtual DbSet Prescriptions { set; get; } + public virtual DbSet Patients { set; get; } + public virtual DbSet Procedures { set; get; } + public virtual DbSet Treatments { set; get; } + public virtual DbSet RecipeMedicines { set; get; } + public virtual DbSet RecipeTreatments { set; get; } + public virtual DbSet ProcedureMedicines { set; get; } + public virtual DbSet TreatmentProcedures { set; get; } + } +} diff --git a/Hospital/HospitalDatabaseImplement/HospitalDatabaseImplement.csproj b/Hospital/HospitalDatabaseImplement/HospitalDatabaseImplement.csproj new file mode 100644 index 0000000..d95079c --- /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/ApothecaryStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/ApothecaryStorage.cs new file mode 100644 index 0000000..a286c15 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/ApothecaryStorage.cs @@ -0,0 +1,88 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; + +namespace HospitalDatabaseImplement.Implements +{ + public class ApothecaryStorage : IApothecaryStorage + { + public ApothecaryViewModel? GetElement(ApothecarySearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Apothecaries.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + // TODO подумать над параметрами фильтрации + public List GetFilteredList(ApothecarySearchModel model) + { + if (string.IsNullOrEmpty(model.Login) || string.IsNullOrEmpty(model.Password)) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Apothecaries + .Where(x => x.Login.Equals(model.Login) && x.Password.Equals(model.Password)) + .Select(x => x.GetViewModel) + .ToList(); + } + + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Apothecaries + .Select(x => x.GetViewModel) + .ToList(); + } + + + public ApothecaryViewModel? Insert(ApothecaryBindingModel model) + { + var newUser = Apothecary.Create(model); + if (newUser == null) + { + return null; + } + using var context = new HospitalDatabase(); + context.Apothecaries.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + + public ApothecaryViewModel? Update(ApothecaryBindingModel model) + { + using var context = new HospitalDatabase(); + var user = context.Apothecaries.FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + + public ApothecaryViewModel? Delete(ApothecaryBindingModel model) + { + using var context = new HospitalDatabase(); + var user = context.Apothecaries.FirstOrDefault(rec => rec.Id == model.Id); + if (user != null) + { + context.Apothecaries.Remove(user); + context.SaveChanges(); + return user.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implements/MedicineStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/MedicineStorage.cs new file mode 100644 index 0000000..369059c --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/MedicineStorage.cs @@ -0,0 +1,86 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HospitalDatabaseImplement.Implements +{ + public class MedicineStorage : IMedicineStorage + { + public MedicineViewModel? GetElement(MedicineSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Medicines.Include(x => x.Apothecary).FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == + model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(MedicineSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Medicines.Include(x => x.Apothecary) + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Medicines.Include(x => x.Apothecary) + .Select(x => x.GetViewModel) + .ToList(); + } + + public MedicineViewModel? Insert(MedicineBindingModel model) + { + var newElement = Medicine.Create(model); + if (newElement == null) + { + return null; + } + using var context = new HospitalDatabase(); + context.Medicines.Add(newElement); + context.SaveChanges(); + return context.Medicines.Include(x => x.Apothecary).FirstOrDefault(x => x.Id == newElement.Id)?.GetViewModel; + } + + public MedicineViewModel? Update(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Medicines.Include(x => x.Apothecary).FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return context.Medicines.Include(x => x.Apothecary).FirstOrDefault(x => x.Id == element.Id)?.GetViewModel; + } + + public MedicineViewModel? Delete(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Medicines.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Medicines.Remove(element); + context.SaveChanges(); + return context.Medicines.Include(x => x.Apothecary).FirstOrDefault(x => x.Id == element.Id)?.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs new file mode 100644 index 0000000..750cf6f --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/PatientStorage.cs @@ -0,0 +1,84 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HospitalDatabaseImplement.Implements +{ + public class PatientStorage : IPatientStorage + { + public PatientViewModel? GetElement(PatientSearchModel model) + { + if (!model.Id.HasValue || !model.TreatmentId.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Patients.Include(x => x.Treatment).FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id) || (model.TreatmentId.HasValue && x.TreatmentId == model.TreatmentId)) + ?.GetViewModel; + } + + public List GetFilteredList(PatientSearchModel model) + { + if (!model.Id.HasValue || !model.TreatmentId.HasValue) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Patients.Include(x => x.Treatment) + .Where(x => (x.Id == model.Id) || (x.TreatmentId == model.TreatmentId)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Patients.Include(x => x.Treatment) + .Select(x => x.GetViewModel) + .ToList(); + } + + public PatientViewModel? Insert(PatientBindingModel model) + { + var newElement = Patient.Create(model); + if (newElement == null) + { + return null; + } + using var context = new HospitalDatabase(); + context.Patients.Add(newElement); + context.SaveChanges(); + return context.Patients.Include(x => x.Treatment).FirstOrDefault(x => x.Id == newElement.Id)?.GetViewModel; + } + + public PatientViewModel? Update(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Patients.Include(x => x.Treatment).FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return context.Patients.Include(x => x.Treatment).FirstOrDefault(x => x.Id == element.Id)?.GetViewModel; + } + + public PatientViewModel? Delete(PatientBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Patients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Patients.Remove(element); + context.SaveChanges(); + return context.Patients.Include(x => x.Treatment).FirstOrDefault(x => x.Id == element.Id)?.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implements/PrescriptionStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/PrescriptionStorage.cs new file mode 100644 index 0000000..c73184a --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/PrescriptionStorage.cs @@ -0,0 +1,84 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace HospitalDatabaseImplement.Implements +{ + public class PrescriptionStorage : IPrescriptionStorage + { + public PrescriptionViewModel? GetElement(PrescriptionSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Prescriptions.Include(x => x.Apothecary).Include(x=> x.Medicine).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(PrescriptionSearchModel model) + { + if (!model.Id.HasValue || !model.ApothecaryId.HasValue) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Prescriptions.Include(x => x.Apothecary).Include(x => x.Medicine) + .Where(x => x.Id == model.Id || x.ApothecaryId == model.ApothecaryId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Prescriptions.Include(x => x.Apothecary).Include(x => x.Medicine) + .Select(x => x.GetViewModel) + .ToList(); + } + + public PrescriptionViewModel? Insert(PrescriptionBindingModel model) + { + using var context = new HospitalDatabase(); + var newElement = Prescription.Create(model); + if (newElement == null) + { + return null; + } + context.Prescriptions.Add(newElement); + context.SaveChanges(); + return context.Prescriptions.Include(x => x.Apothecary).Include(x => x.Medicine).FirstOrDefault(x => x.Id == newElement.Id)?.GetViewModel; + } + + public PrescriptionViewModel? Update(PrescriptionBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Prescriptions.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return context.Prescriptions.Include(x => x.Apothecary).Include(x => x.Medicine).FirstOrDefault(x => x.Id == element.Id)?.GetViewModel; + } + + public PrescriptionViewModel? Delete(PrescriptionBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Prescriptions.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Prescriptions.Remove(element); + context.SaveChanges(); + return context.Prescriptions.Include(x => x.Apothecary).Include(x => x.Medicine).FirstOrDefault(x => x.Id == element.Id)?.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs new file mode 100644 index 0000000..34d3c1d --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/ProcedureStorage.cs @@ -0,0 +1,104 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HospitalDatabaseImplement.Implements +{ + public class ProcedureStorage : IProcedureStorage + { + public ProcedureViewModel? GetElement(ProcedureSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Procedures.Include(x => x.Medicines).ThenInclude(x => x.Medicine).FirstOrDefault(x => + model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + // TODO: подумать над параметрами фильтрации + public List GetFilteredList(ProcedureSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Procedures + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .Where(x => x.Name.Contains(model.Name)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Procedures + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ProcedureViewModel? Insert(ProcedureBindingModel model) + { + using var context = new HospitalDatabase(); + var newElement = Procedure.Create(context, model); + if (newElement == null) + { + return null; + } + context.Procedures.Add(newElement); + context.SaveChanges(); + return newElement.GetViewModel; + } + + public ProcedureViewModel? Update(ProcedureBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var element = context.Procedures.FirstOrDefault(rec => + rec.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + element.UpdateMedicines(context, model); + transaction.Commit(); + return element.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/Implements/RecipeStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/RecipeStorage.cs new file mode 100644 index 0000000..41cdd8b --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/RecipeStorage.cs @@ -0,0 +1,116 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HospitalDatabaseImplement.Implements +{ + public class RecipeStorage : IRecipeStorage + { + public RecipeViewModel? GetElement(RecipeSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && + !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Recipes + .Include(x => x.Apothecary) + .Include(x => x.Treatments) + .ThenInclude(x => x.Treatment) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && + x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(RecipeSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) || !model.ApothecaryId.HasValue) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Recipes.Include(x => x.Apothecary) + .Include(x => x.Treatments) + .ThenInclude(x => x.Treatment) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .Where(x => x.Name.Contains(model.Name) || x.ApothecaryId == model.ApothecaryId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Recipes.Include(x => x.Apothecary) + .Include(x => x.Treatments) + .ThenInclude(x => x.Treatment) + .Include(x => x.Medicines) + .ThenInclude(x => x.Medicine) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RecipeViewModel? Insert(RecipeBindingModel model) + { + using var context = new HospitalDatabase(); + var newElement = Recipe.Create(context, model); + if (newElement == null) + { + return null; + } + context.Recipes.Add(newElement); + context.SaveChanges(); + return newElement.GetViewModel; + } + + public RecipeViewModel? Update(RecipeBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var element = context.Recipes.FirstOrDefault(rec => + rec.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + element.UpdateMedicines(context, model); + element.UpdateTreatments(context, model); + transaction.Commit(); + return element.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public RecipeViewModel? Delete(RecipeBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Recipes + .Include(x => x.Medicines) + .Include(x => x.Treatments) + .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/Implements/TreatmentStorage.cs b/Hospital/HospitalDatabaseImplement/Implements/TreatmentStorage.cs new file mode 100644 index 0000000..dcc05c6 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Implements/TreatmentStorage.cs @@ -0,0 +1,109 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.SearchModels; +using HospitalContracts.StorageContracts; +using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HospitalDatabaseImplement.Implements +{ + public class TreatmentStorage : ITreatmentStorage + { + public TreatmentViewModel? GetElement(TreatmentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Treatments + .Include(x => x.Patients) // TODO: проверить, как такое работает - нужно для получения списка пациентов по лекарству + .Include(x => x.Procedures) + .ThenInclude(x => x.Procedure) + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(TreatmentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new HospitalDatabase(); + return context.Treatments + .Include(x => x.Patients) + .Include(x => x.Procedures) + .ThenInclude(x => x.Procedure) + .Where(x => x.Name.Contains(model.Name)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Treatments + .Include(x => x.Patients) + .Include(x => x.Procedures) + .ThenInclude(x => x.Procedure) + .Select(x => x.GetViewModel) + .ToList(); + } + + public TreatmentViewModel? Insert(TreatmentBindingModel model) + { + using var context = new HospitalDatabase(); + var newElement = Treatment.Create(context, model); + if (newElement == null) + { + return null; + } + context.Treatments.Add(newElement); + context.SaveChanges(); + return context.Treatments.Include(x => x.Patients).FirstOrDefault(x => x.Id == newElement.Id)?.GetViewModel; + } + + public TreatmentViewModel? Update(TreatmentBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var element = context.Treatments.FirstOrDefault(rec => + rec.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + element.UpdateProcedures(context, model); + transaction.Commit(); + return element.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public TreatmentViewModel? Delete(TreatmentBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Treatments + .Include(x => x.Procedures) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Treatments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Apothecary.cs b/Hospital/HospitalDatabaseImplement/Models/Apothecary.cs new file mode 100644 index 0000000..e5d2e4d --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Apothecary.cs @@ -0,0 +1,49 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace HospitalDatabaseImplement.Models +{ + public class Apothecary : IApothecaryModel + { + public int Id { get; private set; } + [Required] + [MaxLength(50)] + public string Login { get; private set; } = string.Empty; + [Required] + [MaxLength(55)] + public string Password { get; private set; } = string.Empty; + + public static Apothecary? Create(ApothecaryBindingModel model) + { + if (model == null) + { + return null; + } + return new Apothecary() + { + Id = model.Id, + Login = model.Login, + Password = model.Password + }; + } + + public void Update(ApothecaryBindingModel model) + { + if (model == null) + { + return; + } + + Login = model.Login; + Password = model.Password; + } + public ApothecaryViewModel GetViewModel => new() + { + Id = Id, + Login = Login, + Password = Password + }; + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Medicine.cs b/Hospital/HospitalDatabaseImplement/Models/Medicine.cs new file mode 100644 index 0000000..3ef9684 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Medicine.cs @@ -0,0 +1,75 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace HospitalDatabaseImplement.Models +{ + public class Medicine : IMedicineModel + { + public int Id { get; private set; } + + [Required] + [MaxLength(60)] + public string Name { get; private set; } = string.Empty; + + [Required] + public double Cost { get; private set; } + + [Required] + [MaxLength(10)] + public string Dose { get; private set; } = string.Empty; + + [Required] + public int ApothecaryId { get; private set; } + + public virtual Apothecary Apothecary { get; set; } + + [ForeignKey("MedicineId")] + public virtual List Prescriptions { get; set; } = new(); + + [ForeignKey("MedicineId")] + public virtual List Recipes { get; set; } = new(); + + [ForeignKey("MedicineId")] + public virtual List Procedures { get; set; } = new(); + + public static Medicine? Create(MedicineBindingModel model) + { + if (model == null) + { + return null; + } + return new Medicine() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + Dose = model.Dose, + ApothecaryId = model.ApothecaryId + }; + } + + public void Update(MedicineBindingModel model) + { + if (model == null) + { + return; + } + + Name = model.Name; + Cost = model.Cost; + Dose = model.Dose; + } + public MedicineViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + Dose = Dose, + ApothecaryId = ApothecaryId, + ApothecaryLogin = Apothecary.Login + }; + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Patient.cs b/Hospital/HospitalDatabaseImplement/Models/Patient.cs new file mode 100644 index 0000000..a1fef36 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Patient.cs @@ -0,0 +1,82 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +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 Patient : IPatientModel + { + public int Id { get; private set; } + + public string Surname { get; private set; } = string.Empty; + [Required] + public string Name { get; private set; } = string.Empty; + + public string Patronymic { get; private set; } = string.Empty; + [Required] + public DateTime BirthDate { get; private set; } + + public int TreatmentId { get; private set; } + public virtual Treatment? Treatment { get; set; } + + public static Patient? Create(PatientBindingModel model) + { + if (model == null) + { + return null; + } + return new Patient() + { + Id = model.Id, + Surname = model.Surname, + Name = model.Name, + Patronymic = model.Patronymic, + BirthDate = model.BirthDate, + TreatmentId = model.TreatmentId + }; + } + + public static Patient Create(PatientViewModel model) + { + return new Patient + { + Id = model.Id, + Surname = model.Surname, + Name = model.Name, + Patronymic = model.Patronymic, + BirthDate = model.BirthDate, + TreatmentId = model.TreatmentId + }; + } + + public void Update(PatientBindingModel model) + { + if (model == null) + { + return; + } + + Surname = model.Surname; + Name = model.Name; + Patronymic = model.Patronymic; + BirthDate = model.BirthDate; + TreatmentId = model.TreatmentId; + } + public PatientViewModel GetViewModel => new() + { + Id = Id, + Surname = Surname, + Name = Name, + Patronymic = Patronymic, + BirthDate = BirthDate, + TreatmentId = TreatmentId, + TreatmentName = Treatment?.Name + }; + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Prescription.cs b/Hospital/HospitalDatabaseImplement/Models/Prescription.cs new file mode 100644 index 0000000..191b334 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Prescription.cs @@ -0,0 +1,56 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +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 Prescription : IPrescriptionModel + { + public int Id { get; private set; } + [Required] + public DateTime Date { get; private set; } = DateTime.Now; + [Required] + public int Number { get; private set; } + [Required] + public int MedicineId { get; private set; } + + public virtual Medicine Medicine { get; set; } + [Required] + public int ApothecaryId { get; private set; } + + public virtual Apothecary Apothecary { get; set; } + + public static Prescription Create(PrescriptionBindingModel model) + { + return new Prescription() + { + Id = model.Id, + Date = model.Date, + Number = model.Number, + MedicineId = model.MedicineId, + ApothecaryId = model.ApothecaryId + }; + } + + public void Update(PrescriptionBindingModel model) + { + Date = model.Date; + } + public PrescriptionViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + Number = Number, + MedicineId = MedicineId, + MedicineName = Medicine.Name, + ApothecaryId = ApothecaryId, + ApothecaryLogin = Apothecary.Login + }; + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Procedure.cs b/Hospital/HospitalDatabaseImplement/Models/Procedure.cs new file mode 100644 index 0000000..37327d4 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Procedure.cs @@ -0,0 +1,88 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class Procedure : IProcedureModel + { + public int Id { get; private set; } + + [Required] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + + private Dictionary? _procedureMedicines = null; + + [NotMapped] + public Dictionary ProcedureMedicines { get + { + if (_procedureMedicines == null) + { + _procedureMedicines = Medicines.ToDictionary(rec => rec.MedicineId, rec => + rec.Medicine as IMedicineModel); + } + return _procedureMedicines; + } + } + + [ForeignKey("ProcedureId")] + public virtual List Medicines { get; set; } = new(); + + [ForeignKey("ProcedureId")] + public virtual List Treatments { 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 + }; + + public void UpdateMedicines(HospitalDatabase context, ProcedureBindingModel model) + { + var procedureMedicines = context.ProcedureMedicines.Where(rec => rec.ProcedureId == model.Id).ToList(); + if (procedureMedicines != null && procedureMedicines.Count > 0) + { + 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..730801b --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/ProcedureMedicine.cs @@ -0,0 +1,20 @@ +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..ca96a5c --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Recipe.cs @@ -0,0 +1,140 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; + +namespace HospitalDatabaseImplement.Models +{ + public class Recipe : IRecipeModel + { + public int Id { get; private set; } + [Required] + [MaxLength(50)] + public string Name { get; private set; } = string.Empty; + [Required] + public DateTime Date { get; private set; } = DateTime.Now; + [Required] + public int ApothecaryId { get; private set; } + + public virtual Apothecary Apothecary { get; set; } + + private Dictionary? _recipeMedicines = null; + + [NotMapped] + public Dictionary RecipeMedicines + { + get + { + if (_recipeMedicines == null) + { + _recipeMedicines = Medicines + .ToDictionary(rec => rec.MedicineId, rec => + rec.Medicine as IMedicineModel); + } + return _recipeMedicines; + } + } + + [ForeignKey("RecipeId")] + public virtual List Medicines { get; set; } = new(); + + private Dictionary? _recipeTreatments = null; + + [NotMapped] + public Dictionary RecipeTreatments + { + get + { + if (_recipeTreatments == null) + { + _recipeTreatments = Treatments + .ToDictionary(rec => rec.TreatmentId, rec => + rec.Treatment as ITreatmentModel); + } + return _recipeTreatments; + } + } + [ForeignKey("RecipeId")] + public virtual List Treatments { get; set; } = new(); + + public static Recipe Create(HospitalDatabase context, RecipeBindingModel model) + { + return new Recipe() + { + Id = model.Id, + Name = model.Name, + Date = model.Date, + ApothecaryId = model.ApothecaryId, + Medicines = model.RecipeMedicines.Select(x => new RecipeMedicine + { + Medicine = context.Medicines.First(y => y.Id == x.Key), + }).ToList(), + Treatments = model.RecipeTreatments.Select(x => new RecipeTreatment + { + Treatment = context.Treatments.First(y => y.Id == x.Key) + } + ).ToList() + }; + } + + public void Update(RecipeBindingModel model) + { + Name = model.Name; + Date = model.Date; + } + public RecipeViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Date = Date, + ApothecaryId = ApothecaryId, + ApothecaryLogin = Apothecary.Login + }; + + public void UpdateMedicines(HospitalDatabase context, RecipeBindingModel model) + { + var recipeMedicines = context.RecipeMedicines.Where(rec => rec.RecipeId == model.Id).ToList(); + if (recipeMedicines != null && recipeMedicines.Count > 0) + { + context.RecipeMedicines.RemoveRange(recipeMedicines.Where(rec + => !model.RecipeMedicines.ContainsKey(rec.MedicineId))); + context.SaveChanges(); + } + var recipe = context.Recipes.First(x => x.Id == Id); + foreach (var rec in model.RecipeMedicines) + { + context.RecipeMedicines.Add(new RecipeMedicine + { + Recipe = recipe, + Medicine = context.Medicines.First(x => x.Id == rec.Key), + }); + context.SaveChanges(); + } + _recipeMedicines = null; + } + + public void UpdateTreatments(HospitalDatabase context, RecipeBindingModel model) + { + var recipeTreatments = context.RecipeTreatments.Where(rec => rec.RecipeId == model.Id).ToList(); + if (recipeTreatments != null && recipeTreatments.Count > 0) + { + context.RecipeTreatments.RemoveRange(recipeTreatments.Where(rec + => !model.RecipeTreatments.ContainsKey(rec.TreatmentId))); + context.SaveChanges(); + } + var recipe = context.Recipes.First(x => x.Id == Id); + foreach (var rec in model.RecipeTreatments) + { + context.RecipeTreatments.Add(new RecipeTreatment + { + Recipe = recipe, + Treatment = context.Treatments.First(x => x.Id == rec.Key), + }); + context.SaveChanges(); + } + _recipeTreatments = null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs b/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs new file mode 100644 index 0000000..9916620 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/RecipeMedicine.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; + +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(); + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/RecipeTreatment.cs b/Hospital/HospitalDatabaseImplement/Models/RecipeTreatment.cs new file mode 100644 index 0000000..f17e179 --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/RecipeTreatment.cs @@ -0,0 +1,20 @@ +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 RecipeTreatment + { + public int Id { get; set; } + [Required] + public int RecipeId { get; set; } + [Required] + public int TreatmentId { get; set; } + public virtual Recipe Recipe { get; set; } = new(); + public virtual Treatment Treatment { get; set; } = new(); + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/Treatment.cs b/Hospital/HospitalDatabaseImplement/Models/Treatment.cs new file mode 100644 index 0000000..798abcd --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/Treatment.cs @@ -0,0 +1,89 @@ +using HospitalContracts.BindingModels; +using HospitalContracts.ViewModels; +using HospitalDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HospitalDatabaseImplement.Models +{ + public class Treatment : ITreatmentModel + { + public int Id { get; private set; } + [Required] + [MaxLength(55)] + public string Name { get; private set; } = string.Empty; + + private Dictionary? _treatmentProcedures = null; + [NotMapped] + public Dictionary TreatmentProcedures { + get + { + if (_treatmentProcedures == null) + { + _treatmentProcedures = Procedures + .ToDictionary(rec => rec.ProcedureId, rec => + rec.Procedure as IProcedureModel); + } + return _treatmentProcedures; + } + } + [ForeignKey("TreatmentId")] + public virtual List Patients { get; set; } = new(); + + [ForeignKey("TreatmentId")] + public virtual List Procedures { get; set; } = new(); + + [ForeignKey("TreatmentId")] + public virtual List Recipes { get; set; } = new(); + + public static Treatment Create(HospitalDatabase context, TreatmentBindingModel model) + { + return new Treatment() + { + Id = model.Id, + Name = model.Name, + Procedures = model.TreatmentProcedures.Select(x => new TreatmentProcedure + { + Procedure = context.Procedures.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(TreatmentBindingModel model) + { + Name = model.Name; + } + public TreatmentViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; + + public void UpdateProcedures(HospitalDatabase context, TreatmentBindingModel model) + { + var treatmentProcedures = context.TreatmentProcedures.Where(rec => rec.TreatmentId == model.Id).ToList(); + if (treatmentProcedures != null && treatmentProcedures.Count > 0) + { + context.TreatmentProcedures.RemoveRange(treatmentProcedures.Where(rec + => !model.TreatmentProcedures.ContainsKey(rec.ProcedureId))); + context.SaveChanges(); + } + var treatment = context.Treatments.First(x => x.Id == Id); + foreach (var pc in model.TreatmentProcedures) + { + context.TreatmentProcedures.Add(new TreatmentProcedure + { + Treatment = treatment, + Procedure = context.Procedures.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _treatmentProcedures = null; + } + } +} diff --git a/Hospital/HospitalDatabaseImplement/Models/TreatmentProcedure.cs b/Hospital/HospitalDatabaseImplement/Models/TreatmentProcedure.cs new file mode 100644 index 0000000..6cf23ec --- /dev/null +++ b/Hospital/HospitalDatabaseImplement/Models/TreatmentProcedure.cs @@ -0,0 +1,20 @@ +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 TreatmentProcedure + { + public int Id { get; set; } + [Required] + public int ProcedureId { get; set; } + [Required] + public int TreatmentId { get; set; } + public virtual Procedure Procedure { get; set; } = new(); + public virtual Treatment Treatment { get; set; } = new(); + } +}