добавлены таблицы бд работника

This commit is contained in:
ValAnn 2024-04-29 13:37:57 +04:00
parent 5e0f6fd0be
commit ced335c941
17 changed files with 900 additions and 25 deletions

View File

@ -8,6 +8,13 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HospitalBusinessLogic\HospitalBusinessLogic.csproj" />
<ProjectReference Include="..\HospitalContracts\HospitalContracts.csproj" />

View File

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

View File

@ -12,7 +12,16 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@ -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<Doctor> Doctors { set; get; }
public virtual DbSet<Patient> Patients { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; }
public virtual DbSet<Disease> Diseases { set; get; }
public virtual DbSet<Medicine> Medicines { set; get; }
public virtual DbSet<Procedure> Procedures { set; get; }
public virtual DbSet<PatientRecipe> PatientRecipes { set; get; }
public virtual DbSet<PatientProcedure> PatientProcedures { set; get; }
public virtual DbSet<RecipeMedicine> RecipeMedicines { set; get; }
public virtual DbSet<ProcedureMedicine> ProcedureMedicines { set; get; }
//TODO добавить сущности описание процедур и промежутучную сущость у фармацевта
}
}

View File

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

View File

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

View File

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

View File

@ -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<ProcedureViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Procedures
.Include(x => x.Medicines)
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProcedureViewModel> 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;
}
}
}

View File

@ -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<RecipeViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Recipes
.Include(x => x.Doctor)
.Include(x => x.DiseaseId)
.Select(x => x.GetViewModel).ToList();
}
public List<RecipeViewModel> 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;
}
}
}

View File

@ -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<MedicineRecipes> RecipeMedicines { get; set; } = new();
public virtual List<RecipeMedicine> RecipeMedicines { get; set; } = new();
public static Medicine? Create(MedicineBindingModel model)
{

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<int, IMedicineModel>? _recipeMedicines = null;
[NotMapped]
public Dictionary<int, IMedicineModel> RecipeMedicines
{
get
{
if (_recipeMedicines == null)
{
_recipeMedicines = Medicines.ToDictionary(recRM => recRM.MedicineId, recRM => (recRM.Medicine as IMedicineModel));
}
return _recipeMedicines;
}
}
[ForeignKey("RecipeId")]
public virtual List<RecipeMedicine> 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;
}
}
}

View File

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