изменила 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

@ -8,7 +8,5 @@ namespace PolyclinicContracts.BindingModels
public int UserId { get; set; }
public string Name { 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 ProceduresCount { get; set; }
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;
[DisplayName("Комментарий")]
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("Комментарий")]
public string Comment { get; set; } = string.Empty;
public Dictionary<int, IProcedureModel> RecipeProcedures { get; } = new();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -153,7 +153,27 @@ namespace PolyclinicDatabaseImplement.Migrations
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")
.ValueGeneratedOnAdd()
@ -173,27 +193,7 @@ namespace PolyclinicDatabaseImplement.Migrations
b.HasIndex("RecipeId");
b.ToTable("ProcedureRecipes");
});
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");
b.ToTable("RecipeProcedures");
});
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.Symptom", b =>
@ -351,16 +351,16 @@ namespace PolyclinicDatabaseImplement.Migrations
b.Navigation("Symptom");
});
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.ProcedureRecipe", b =>
modelBuilder.Entity("PolyclinicDatabaseImplement.Models.RecipeProcedure", b =>
{
b.HasOne("PolyclinicDatabaseImplement.Models.Procedure", "Procedure")
.WithMany("Recipes")
.WithMany()
.HasForeignKey("ProcedureId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PolyclinicDatabaseImplement.Models.Recipe", "Recipe")
.WithMany()
.WithMany("Procedures")
.HasForeignKey("RecipeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@ -413,9 +413,9 @@ namespace PolyclinicDatabaseImplement.Migrations
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 =>

View File

@ -3,7 +3,6 @@ using PolyclinicContracts.ViewModels;
using PolyclinicDataModels.Models;
using SecuritySystemDatabaseImplement;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PolyclinicDatabaseImplement.Models
{
@ -20,25 +19,7 @@ namespace PolyclinicDatabaseImplement.Models
[Required]
public string Comment { get; set; } = string.Empty;
private Dictionary<int, IRecipeModel>? _procedureRecipes = null;
[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)
public static Procedure Create(ProcedureBindingModel bindingModel)
{
return new Procedure()
{
@ -46,10 +27,6 @@ namespace PolyclinicDatabaseImplement.Models
UserId = bindingModel.UserId,
Name = bindingModel.Name,
Comment = bindingModel.Comment,
Recipes = bindingModel.ProcedureRecipes.Select(x => new ProcedureRecipe
{
Recipe = database.Recipes.First(y => y.Id == x.Key)
}).ToList()
};
}
@ -66,27 +43,5 @@ namespace PolyclinicDatabaseImplement.Models
UserId = UserId,
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 SecuritySystemDatabaseImplement;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PolyclinicDatabaseImplement.Models
{
@ -16,13 +17,35 @@ namespace PolyclinicDatabaseImplement.Models
[Required]
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()
{
Id = bindingModel.Id,
ProceduresCount = bindingModel.ProceduresCount,
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,
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
{
public class ProcedureRecipe
public class RecipeProcedure
{
public int Id { get; set; }
[Required]

View File

@ -18,7 +18,7 @@ namespace SecuritySystemDatabaseImplement
public virtual DbSet<Diagnose> Diagnoses { set; get; }
public virtual DbSet<Medicament> Medicaments { 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<Symptom> Symptomes { set; get; }
public virtual DbSet<SymptomDiagnose> SymptomDiagnoses { set; get; }