Этап 3. Реализация слоя хранения данных БД

This commit is contained in:
prodigygirl 2023-04-05 11:47:15 +04:00
parent 52829f9e46
commit c23bffa073
21 changed files with 1386 additions and 3 deletions

View File

@ -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

View File

@ -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<Apothecary> Apothecaries { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; }
public virtual DbSet<Medicine> Medicines { set; get; }
public virtual DbSet<Prescription> Prescriptions { set; get; }
public virtual DbSet<Patient> Patients { set; get; }
public virtual DbSet<Procedure> Procedures { set; get; }
public virtual DbSet<Treatment> Treatments { set; get; }
public virtual DbSet<RecipeMedicine> RecipeMedicines { set; get; }
public virtual DbSet<RecipeTreatment> RecipeTreatments { set; get; }
public virtual DbSet<ProcedureMedicine> ProcedureMedicines { set; get; }
public virtual DbSet<TreatmentProcedure> TreatmentProcedures { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HospitalContracts\HospitalContracts.csproj" />
<ProjectReference Include="..\HospitalDataModels\HospitalDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -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<ApothecaryViewModel> 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<ApothecaryViewModel> 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;
}
}
}

View File

@ -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<MedicineViewModel> 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<MedicineViewModel> 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;
}
}
}

View File

@ -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<PatientViewModel> 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<PatientViewModel> 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;
}
}
}

View File

@ -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<PrescriptionViewModel> 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<PrescriptionViewModel> 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;
}
}
}

View File

@ -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<ProcedureViewModel> 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<ProcedureViewModel> 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;
}
}
}

View File

@ -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<RecipeViewModel> 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<RecipeViewModel> 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;
}
}
}

View File

@ -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<TreatmentViewModel> 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<TreatmentViewModel> 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;
}
}
}

View File

@ -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
};
}
}

View File

@ -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<Prescription> Prescriptions { get; set; } = new();
[ForeignKey("MedicineId")]
public virtual List<RecipeMedicine> Recipes { get; set; } = new();
[ForeignKey("MedicineId")]
public virtual List<ProcedureMedicine> 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
};
}
}

View File

@ -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
};
}
}

View File

@ -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
};
}
}

View File

@ -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<int, IMedicineModel>? _procedureMedicines = null;
[NotMapped]
public Dictionary<int, IMedicineModel> ProcedureMedicines { get
{
if (_procedureMedicines == null)
{
_procedureMedicines = Medicines.ToDictionary(rec => rec.MedicineId, rec =>
rec.Medicine as IMedicineModel);
}
return _procedureMedicines;
}
}
[ForeignKey("ProcedureId")]
public virtual List<ProcedureMedicine> Medicines { get; set; } = new();
[ForeignKey("ProcedureId")]
public virtual List<TreatmentProcedure> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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<int, IMedicineModel>? _recipeMedicines = null;
[NotMapped]
public Dictionary<int, IMedicineModel> RecipeMedicines
{
get
{
if (_recipeMedicines == null)
{
_recipeMedicines = Medicines
.ToDictionary(rec => rec.MedicineId, rec =>
rec.Medicine as IMedicineModel);
}
return _recipeMedicines;
}
}
[ForeignKey("RecipeId")]
public virtual List<RecipeMedicine> Medicines { get; set; } = new();
private Dictionary<int, ITreatmentModel>? _recipeTreatments = null;
[NotMapped]
public Dictionary<int, ITreatmentModel> RecipeTreatments
{
get
{
if (_recipeTreatments == null)
{
_recipeTreatments = Treatments
.ToDictionary(rec => rec.TreatmentId, rec =>
rec.Treatment as ITreatmentModel);
}
return _recipeTreatments;
}
}
[ForeignKey("RecipeId")]
public virtual List<RecipeTreatment> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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<int, IProcedureModel>? _treatmentProcedures = null;
[NotMapped]
public Dictionary<int, IProcedureModel> TreatmentProcedures {
get
{
if (_treatmentProcedures == null)
{
_treatmentProcedures = Procedures
.ToDictionary(rec => rec.ProcedureId, rec =>
rec.Procedure as IProcedureModel);
}
return _treatmentProcedures;
}
}
[ForeignKey("TreatmentId")]
public virtual List<Patient> Patients { get; set; } = new();
[ForeignKey("TreatmentId")]
public virtual List<TreatmentProcedure> Procedures { get; set; } = new();
[ForeignKey("TreatmentId")]
public virtual List<RecipeTreatment> 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;
}
}
}

View File

@ -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();
}
}