изменила ProcedureRecipe на RecipeProcedure, пересоздала бд и миграцию

This commit is contained in:
Елена Бакальская 2024-04-30 16:20:00 +04:00
parent bee93877d1
commit ad4377ffc7
15 changed files with 173 additions and 163 deletions

View File

@ -2,13 +2,11 @@
namespace PolyclinicContracts.BindingModels namespace PolyclinicContracts.BindingModels
{ {
public class ProcedureBindingModel : IProcedureModel public class ProcedureBindingModel : IProcedureModel
{ {
public int Id { get; set; } public int Id { get; set; }
public int UserId { get; set; } public int UserId { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public string Comment { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty;
public Dictionary<int, IRecipeModel> ProcedureRecipes { get; set; } = new();
} }
} }

View File

@ -7,5 +7,6 @@ namespace PolyclinicContracts.BindingModels
public int Id { get; set; } public int Id { get; set; }
public int ProceduresCount { get; set; } public int ProceduresCount { get; set; }
public string Comment { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty;
public Dictionary<int, IProcedureModel> RecipeProcedures { get; } = new();
} }
} }

View File

@ -11,6 +11,5 @@ namespace PolyclinicContracts.ViewModels
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[DisplayName("Комментарий")] [DisplayName("Комментарий")]
public string Comment { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty;
public Dictionary<int, IRecipeModel> ProcedureRecipes { get; set; } = new();
} }
} }

View File

@ -12,5 +12,7 @@ namespace PolyclinicContracts.ViewModels
[DisplayName("Комментарий")] [DisplayName("Комментарий")]
public string Comment { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty;
public Dictionary<int, IProcedureModel> RecipeProcedures { get; } = new();
} }
} }

View File

@ -5,6 +5,5 @@
string Name { get; } string Name { get; }
string Comment { get; } string Comment { get; }
int UserId { get; } int UserId { get; }
Dictionary<int, IRecipeModel> ProcedureRecipes { get; }
} }
} }

View File

@ -4,5 +4,6 @@
{ {
int ProceduresCount { get; set; } int ProceduresCount { get; set; }
string Comment { get; set; } string Comment { get; set; }
Dictionary<int, IProcedureModel> RecipeProcedures { get; }
} }
} }

View File

@ -14,8 +14,6 @@ namespace PolyclinicDatabaseImplement.Implements
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
return database.Procedures return database.Procedures
.Include(x => x.Recipes)
.ThenInclude(x => x.Recipe)
.ToList() .ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
@ -29,8 +27,6 @@ namespace PolyclinicDatabaseImplement.Implements
} }
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
return database.Procedures return database.Procedures
.Include(x => x.Recipes)
.ThenInclude(x => x.Recipe)
.Where(x => x.Name == model.Name) .Where(x => x.Name == model.Name)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
@ -45,8 +41,6 @@ namespace PolyclinicDatabaseImplement.Implements
} }
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
return database.Procedures return database.Procedures
.Include(x => x.Recipes)
.ThenInclude(x => x.Recipe)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) ||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
} }
@ -54,7 +48,7 @@ namespace PolyclinicDatabaseImplement.Implements
public ProcedureViewModel? Insert(ProcedureBindingModel model) public ProcedureViewModel? Insert(ProcedureBindingModel model)
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
var newProcedure = Procedure.Create(database, model); var newProcedure = Procedure.Create(model);
if (newProcedure == null) if (newProcedure == null)
{ {
return null; return null;
@ -66,31 +60,19 @@ namespace PolyclinicDatabaseImplement.Implements
public ProcedureViewModel? Update(ProcedureBindingModel model) public ProcedureViewModel? Update(ProcedureBindingModel model)
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
using var transaction = database.Database.BeginTransaction(); var procedure = database.Procedures.FirstOrDefault(x => x.Id == model.Id);
try if (procedure == null)
{ {
var Procedure = database.Procedures.FirstOrDefault(rec => rec.Id == model.Id); return null;
if (Procedure == null)
{
return null;
}
Procedure.Update(model);
database.SaveChanges();
Procedure.UpdateRecipes(database, model);
transaction.Commit();
return Procedure.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
} }
procedure.Update(model);
database.SaveChanges();
return procedure.GetViewModel;
} }
public ProcedureViewModel? Delete(ProcedureBindingModel model) public ProcedureViewModel? Delete(ProcedureBindingModel model)
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
var element = database.Procedures var element = database.Procedures
.Include(x => x.Recipes)
.FirstOrDefault(rec => rec.Id == model.Id); .FirstOrDefault(rec => rec.Id == model.Id);
if (element != null) if (element != null)
{ {

View File

@ -1,4 +1,5 @@
using PolyclinicContracts.BindingModels; using Microsoft.EntityFrameworkCore;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.SearchModels; using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts; using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels; using PolyclinicContracts.ViewModels;
@ -12,7 +13,11 @@ namespace PolyclinicDatabaseImplement.Implements
public List<RecipeViewModel> GetFullList() public List<RecipeViewModel> GetFullList()
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
return database.Recipes.Select(x => x.GetViewModel).ToList(); return database.Recipes
.Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.Select(x => x.GetViewModel)
.ToList();
} }
public List<RecipeViewModel> GetFilteredList(RecipeSearchModel bindingModel) public List<RecipeViewModel> GetFilteredList(RecipeSearchModel bindingModel)
{ {
@ -21,7 +26,12 @@ namespace PolyclinicDatabaseImplement.Implements
return new(); return new();
} }
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
return database.Recipes.Where(x => x.Comment.Contains(bindingModel.Comment)).Select(x => x.GetViewModel).ToList(); return database.Recipes
.Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.Where(x => x.Comment.Contains(bindingModel.Comment))
.Select(x => x.GetViewModel)
.ToList();
} }
public RecipeViewModel? GetElement(RecipeSearchModel bindingModel) public RecipeViewModel? GetElement(RecipeSearchModel bindingModel)
@ -31,15 +41,20 @@ namespace PolyclinicDatabaseImplement.Implements
return null; return null;
} }
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
return database.Recipes.FirstOrDefault(x => (!string.IsNullOrEmpty(bindingModel.Comment) && return database.Recipes
x.Comment == bindingModel.Comment) || (bindingModel.Id.HasValue && x.Id == bindingModel.Id))?.GetViewModel; .Include(x => x.Procedures)
.ThenInclude(x => x.Procedure)
.FirstOrDefault(x => (!string.IsNullOrEmpty(bindingModel.Comment) &&
x.Comment == bindingModel.Comment) ||
(bindingModel.Id.HasValue && x.Id == bindingModel.Id))?
.GetViewModel;
} }
public RecipeViewModel? Insert(RecipeBindingModel bindingModel) public RecipeViewModel? Insert(RecipeBindingModel bindingModel)
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
var newRecipe = Recipe.Create(bindingModel); var newRecipe = Recipe.Create(database, bindingModel);
if(newRecipe == null) if (newRecipe == null)
{ {
return null; return null;
} }
@ -51,20 +66,33 @@ namespace PolyclinicDatabaseImplement.Implements
public RecipeViewModel? Update(RecipeBindingModel bindingModel) public RecipeViewModel? Update(RecipeBindingModel bindingModel)
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
var recipe = database.Recipes.FirstOrDefault(x => x.Id == bindingModel.Id); using var transaction = database.Database.BeginTransaction();
if(recipe == null) try
{ {
return null; var Recipe = database.Recipes.FirstOrDefault(rec => rec.Id == bindingModel.Id);
if (Recipe == null)
{
return null;
}
Recipe.Update(bindingModel);
database.SaveChanges();
Recipe.UpdateProcedures(database, bindingModel);
transaction.Commit();
return Recipe.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
} }
recipe.Update(bindingModel);
database.SaveChanges();
return recipe.GetViewModel;
} }
public RecipeViewModel? Delete(RecipeBindingModel bindingModel) public RecipeViewModel? Delete(RecipeBindingModel bindingModel)
{ {
using var database = new PolyclinicDatabase(); using var database = new PolyclinicDatabase();
var recipe = database.Recipes.FirstOrDefault(x => x.Id == bindingModel.Id); var recipe = database.Recipes
.Include(x => x.Procedures)
.FirstOrDefault(x => x.Id == bindingModel.Id);
if (recipe == null) if (recipe == null)
{ {
return null; return null;

View File

@ -11,8 +11,8 @@ using SecuritySystemDatabaseImplement;
namespace PolyclinicDatabaseImplement.Migrations namespace PolyclinicDatabaseImplement.Migrations
{ {
[DbContext(typeof(PolyclinicDatabase))] [DbContext(typeof(PolyclinicDatabase))]
[Migration("20240430074427_InitializeDatabase")] [Migration("20240430121818_InitCreate")]
partial class InitializeDatabase partial class InitCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -156,7 +156,27 @@ namespace PolyclinicDatabaseImplement.Migrations
b.ToTable("Procedures"); b.ToTable("Procedures");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.ProcedureRecipe", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Recipe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Comment")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProceduresCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Recipes");
});
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.RecipeProcedure", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@ -176,27 +196,7 @@ namespace PolyclinicDatabaseImplement.Migrations
b.HasIndex("RecipeId"); b.HasIndex("RecipeId");
b.ToTable("ProcedureRecipes"); b.ToTable("RecipeProcedures");
});
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Recipe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Comment")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProceduresCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Recipes");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b =>
@ -354,16 +354,16 @@ namespace PolyclinicDatabaseImplement.Migrations
b.Navigation("Symptom"); b.Navigation("Symptom");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.ProcedureRecipe", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.RecipeProcedure", b =>
{ {
b.HasOne("PolyclinicDatabaseImplement.Models.Procedure", "Procedure") b.HasOne("PolyclinicDatabaseImplement.Models.Procedure", "Procedure")
.WithMany("Recipes") .WithMany()
.HasForeignKey("ProcedureId") .HasForeignKey("ProcedureId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.HasOne("PolyclinicDatabaseImplement.Models.Recipe", "Recipe") b.HasOne("PolyclinicDatabaseImplement.Models.Recipe", "Recipe")
.WithMany() .WithMany("Procedures")
.HasForeignKey("RecipeId") .HasForeignKey("RecipeId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
@ -416,9 +416,9 @@ namespace PolyclinicDatabaseImplement.Migrations
b.Navigation("Diagnoses"); b.Navigation("Diagnoses");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Procedure", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Recipe", b =>
{ {
b.Navigation("Recipes"); b.Navigation("Procedures");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b =>

View File

@ -5,7 +5,7 @@
namespace PolyclinicDatabaseImplement.Migrations namespace PolyclinicDatabaseImplement.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
public partial class InitializeDatabase : Migration public partial class InitCreate : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@ -92,7 +92,7 @@ namespace PolyclinicDatabaseImplement.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "ProcedureRecipes", name: "RecipeProcedures",
columns: table => new columns: table => new
{ {
Id = table.Column<int>(type: "int", nullable: false) Id = table.Column<int>(type: "int", nullable: false)
@ -102,15 +102,15 @@ namespace PolyclinicDatabaseImplement.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_ProcedureRecipes", x => x.Id); table.PrimaryKey("PK_RecipeProcedures", x => x.Id);
table.ForeignKey( table.ForeignKey(
name: "FK_ProcedureRecipes_Procedures_ProcedureId", name: "FK_RecipeProcedures_Procedures_ProcedureId",
column: x => x.ProcedureId, column: x => x.ProcedureId,
principalTable: "Procedures", principalTable: "Procedures",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
table.ForeignKey( table.ForeignKey(
name: "FK_ProcedureRecipes_Recipes_RecipeId", name: "FK_RecipeProcedures_Recipes_RecipeId",
column: x => x.RecipeId, column: x => x.RecipeId,
principalTable: "Recipes", principalTable: "Recipes",
principalColumn: "Id", principalColumn: "Id",
@ -275,13 +275,13 @@ namespace PolyclinicDatabaseImplement.Migrations
column: "SymptomId"); column: "SymptomId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_ProcedureRecipes_ProcedureId", name: "IX_RecipeProcedures_ProcedureId",
table: "ProcedureRecipes", table: "RecipeProcedures",
column: "ProcedureId"); column: "ProcedureId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_ProcedureRecipes_RecipeId", name: "IX_RecipeProcedures_RecipeId",
table: "ProcedureRecipes", table: "RecipeProcedures",
column: "RecipeId"); column: "RecipeId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
@ -315,7 +315,7 @@ namespace PolyclinicDatabaseImplement.Migrations
name: "Medicaments"); name: "Medicaments");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ProcedureRecipes"); name: "RecipeProcedures");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "SymptomDiagnoses"); name: "SymptomDiagnoses");

View File

@ -153,7 +153,27 @@ namespace PolyclinicDatabaseImplement.Migrations
b.ToTable("Procedures"); b.ToTable("Procedures");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.ProcedureRecipe", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Recipe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Comment")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProceduresCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Recipes");
});
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.RecipeProcedure", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@ -173,27 +193,7 @@ namespace PolyclinicDatabaseImplement.Migrations
b.HasIndex("RecipeId"); b.HasIndex("RecipeId");
b.ToTable("ProcedureRecipes"); b.ToTable("RecipeProcedures");
});
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Recipe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Comment")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ProceduresCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Recipes");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b =>
@ -351,16 +351,16 @@ namespace PolyclinicDatabaseImplement.Migrations
b.Navigation("Symptom"); b.Navigation("Symptom");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.ProcedureRecipe", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.RecipeProcedure", b =>
{ {
b.HasOne("PolyclinicDatabaseImplement.Models.Procedure", "Procedure") b.HasOne("PolyclinicDatabaseImplement.Models.Procedure", "Procedure")
.WithMany("Recipes") .WithMany()
.HasForeignKey("ProcedureId") .HasForeignKey("ProcedureId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.HasOne("PolyclinicDatabaseImplement.Models.Recipe", "Recipe") b.HasOne("PolyclinicDatabaseImplement.Models.Recipe", "Recipe")
.WithMany() .WithMany("Procedures")
.HasForeignKey("RecipeId") .HasForeignKey("RecipeId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
@ -413,9 +413,9 @@ namespace PolyclinicDatabaseImplement.Migrations
b.Navigation("Diagnoses"); b.Navigation("Diagnoses");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Procedure", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Recipe", b =>
{ {
b.Navigation("Recipes"); b.Navigation("Procedures");
}); });
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b => modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b =>

View File

@ -3,7 +3,6 @@ using PolyclinicContracts.ViewModels;
using PolyclinicDataModels.Models; using PolyclinicDataModels.Models;
using SecuritySystemDatabaseImplement; using SecuritySystemDatabaseImplement;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PolyclinicDatabaseImplement.Models namespace PolyclinicDatabaseImplement.Models
{ {
@ -15,30 +14,12 @@ namespace PolyclinicDatabaseImplement.Models
public int UserId { get; set; } public int UserId { get; set; }
[Required] [Required]
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[Required] [Required]
public string Comment { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty;
private Dictionary<int, IRecipeModel>? _procedureRecipes = null; public static Procedure Create(ProcedureBindingModel bindingModel)
[ForeignKey("ProcedureId")]
public virtual List<ProcedureRecipe> Recipes { get; set; } = new();
[NotMapped]
public Dictionary<int, IRecipeModel> ProcedureRecipes
{
get
{
if(_procedureRecipes == null)
{
_procedureRecipes = Recipes.ToDictionary(recPC => recPC.RecipeId, recPC => (recPC.Recipe as IRecipeModel));
}
return _procedureRecipes;
}
}
public static Procedure Create(PolyclinicDatabase database, ProcedureBindingModel bindingModel)
{ {
return new Procedure() return new Procedure()
{ {
@ -46,10 +27,6 @@ namespace PolyclinicDatabaseImplement.Models
UserId = bindingModel.UserId, UserId = bindingModel.UserId,
Name = bindingModel.Name, Name = bindingModel.Name,
Comment = bindingModel.Comment, Comment = bindingModel.Comment,
Recipes = bindingModel.ProcedureRecipes.Select(x => new ProcedureRecipe
{
Recipe = database.Recipes.First(y => y.Id == x.Key)
}).ToList()
}; };
} }
@ -65,28 +42,6 @@ namespace PolyclinicDatabaseImplement.Models
Name = Name, Name = Name,
UserId = UserId, UserId = UserId,
Comment = Comment, Comment = Comment,
}; };
public void UpdateRecipes(PolyclinicDatabase database, ProcedureBindingModel bindingModel)
{
var ProcedureRecipes = database.ProcedureRecipes.Where(x => x.ProcedureId == bindingModel.Id).ToList();
if(ProcedureRecipes != null)
{
// удалили те, которых нет в модели
database.ProcedureRecipes.RemoveRange(ProcedureRecipes.Where(rec => !bindingModel.ProcedureRecipes.ContainsKey(rec.RecipeId)));
database.SaveChanges();
}
var Procedure = database.Procedures.First(x => x.Id == bindingModel.Id);
foreach (var pc in bindingModel.ProcedureRecipes)
{
database.ProcedureRecipes.Add(new ProcedureRecipe
{
Procedure = Procedure,
Recipe = database.Recipes.First(x => x.Id == pc.Key)
});
}
_procedureRecipes = null;
}
} }
} }

View File

@ -3,6 +3,7 @@ using PolyclinicContracts.ViewModels;
using PolyclinicDataModels.Models; using PolyclinicDataModels.Models;
using SecuritySystemDatabaseImplement; using SecuritySystemDatabaseImplement;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PolyclinicDatabaseImplement.Models namespace PolyclinicDatabaseImplement.Models
{ {
@ -16,13 +17,35 @@ namespace PolyclinicDatabaseImplement.Models
[Required] [Required]
public string Comment { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty;
public static Recipe Create( RecipeBindingModel bindingModel) private Dictionary<int, IProcedureModel>? _recipeProcedures = null;
[ForeignKey("RecipeId")]
public virtual List<RecipeProcedure> Procedures { get; set; } = new();
[NotMapped]
public Dictionary<int, IProcedureModel> RecipeProcedures
{
get
{
if (_recipeProcedures == null)
{
_recipeProcedures = Procedures.ToDictionary(recPC => recPC.ProcedureId, recPC => (recPC.Procedure as IProcedureModel));
}
return _recipeProcedures;
}
}
public static Recipe Create(PolyclinicDatabase database, RecipeBindingModel bindingModel)
{ {
return new Recipe() return new Recipe()
{ {
Id = bindingModel.Id, Id = bindingModel.Id,
ProceduresCount = bindingModel.ProceduresCount, ProceduresCount = bindingModel.ProceduresCount,
Comment = bindingModel.Comment, Comment = bindingModel.Comment,
Procedures = bindingModel.RecipeProcedures.Select(x => new RecipeProcedure
{
Recipe = database.Recipes.First(y => y.Id == x.Key)
}).ToList()
}; };
} }
@ -38,5 +61,27 @@ namespace PolyclinicDatabaseImplement.Models
ProceduresCount = ProceduresCount, ProceduresCount = ProceduresCount,
Comment = Comment Comment = Comment
}; };
public void UpdateProcedures(PolyclinicDatabase database, RecipeBindingModel bindingModel)
{
var RecipeProcedures = database.RecipeProcedures.Where(x => x.ProcedureId == bindingModel.Id).ToList();
if (RecipeProcedures != null)
{
// удалили те, которых нет в модели
database.RecipeProcedures.RemoveRange(RecipeProcedures.Where(rec => !bindingModel.RecipeProcedures.ContainsKey(rec.RecipeId)));
database.SaveChanges();
}
var Procedure = database.Procedures.First(x => x.Id == bindingModel.Id);
foreach (var pc in bindingModel.RecipeProcedures)
{
database.RecipeProcedures.Add(new RecipeProcedure
{
Procedure = Procedure,
Recipe = database.Recipes.First(x => x.Id == pc.Key)
});
}
_recipeProcedures = null;
}
} }
} }

View File

@ -2,7 +2,7 @@
namespace PolyclinicDatabaseImplement.Models namespace PolyclinicDatabaseImplement.Models
{ {
public class ProcedureRecipe public class RecipeProcedure
{ {
public int Id { get; set; } public int Id { get; set; }
[Required] [Required]

View File

@ -18,7 +18,7 @@ namespace SecuritySystemDatabaseImplement
public virtual DbSet<Diagnose> Diagnoses { set; get; } public virtual DbSet<Diagnose> Diagnoses { set; get; }
public virtual DbSet<Medicament> Medicaments { set; get; } public virtual DbSet<Medicament> Medicaments { set; get; }
public virtual DbSet<Procedure> Procedures { set; get; } public virtual DbSet<Procedure> Procedures { set; get; }
public virtual DbSet<ProcedureRecipe> ProcedureRecipes { set; get; } public virtual DbSet<RecipeProcedure> RecipeProcedures { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; } public virtual DbSet<Recipe> Recipes { set; get; }
public virtual DbSet<Symptom> Symptomes { set; get; } public virtual DbSet<Symptom> Symptomes { set; get; }
public virtual DbSet<SymptomDiagnose> SymptomDiagnoses { set; get; } public virtual DbSet<SymptomDiagnose> SymptomDiagnoses { set; get; }