From a7c7600073b4e2abf7ec0d759a2ccc607b1f1936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=91=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D0=BA=D0=B0=D1=8F?= Date: Sun, 28 Apr 2024 18:13:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=8E=D0=B7=D0=B5=D1=80=D0=B0=20=D0=B2=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D1=86=D0=B5=D0=B4=D1=83=D1=80=D1=83,=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D0=BB=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=D0=BB=D1=8C=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?UTF-8?q?=D0=B4=D1=83=D1=80=D0=B0=20=D0=B8=20=D0=BF=D1=80=D0=BE=D1=86?= =?UTF-8?q?=D0=B5=D0=B4=D1=83=D1=80=D0=B0=D0=A0=D0=B5=D1=86=D0=B5=D0=BF?= =?UTF-8?q?=D1=82=20=D0=B4=D0=BB=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ProcedureBindingModel.cs | 3 +- .../ViewModels/ProcedureViewModel.cs | 4 +- .../Models/IProcedureModel.cs | 3 +- .../Models/Procedure.cs | 90 +++++++++++++++++-- .../Models/ProcedureRecipe.cs | 13 ++- .../PolyclinicDatabase.cs | 3 + 6 files changed, 106 insertions(+), 10 deletions(-) diff --git a/Polyclinic/PolyclinicContracts/BindingModels/ProcedureBindingModel.cs b/Polyclinic/PolyclinicContracts/BindingModels/ProcedureBindingModel.cs index c6d8b1f..344ab1c 100644 --- a/Polyclinic/PolyclinicContracts/BindingModels/ProcedureBindingModel.cs +++ b/Polyclinic/PolyclinicContracts/BindingModels/ProcedureBindingModel.cs @@ -5,9 +5,10 @@ namespace PolyclinicContracts.BindingModels public class ProcedureBindingModel : IProcedureModel { public int Id { get; set; } + public int UserId { get; set; } public string Name { get; set; } = string.Empty; public string Comment { get; set; } = string.Empty; - public Dictionary ProcedureCourses { get; set; } = new(); + public Dictionary ProcedureRecipes { get; set; } = new(); } } \ No newline at end of file diff --git a/Polyclinic/PolyclinicContracts/ViewModels/ProcedureViewModel.cs b/Polyclinic/PolyclinicContracts/ViewModels/ProcedureViewModel.cs index f10e28c..eb70d53 100644 --- a/Polyclinic/PolyclinicContracts/ViewModels/ProcedureViewModel.cs +++ b/Polyclinic/PolyclinicContracts/ViewModels/ProcedureViewModel.cs @@ -6,11 +6,11 @@ namespace PolyclinicContracts.ViewModels public class ProcedureViewModel : IProcedureModel { public int Id { get; set; } + public int UserId { get; set; } [DisplayName("Название процедуры")] public string Name { get; set; } = string.Empty; [DisplayName("Комментарий")] public string Comment { get; set; } = string.Empty; - public Dictionary ProcedureCourses { get; set; } = new(); - + public Dictionary ProcedureRecipes { get; set; } = new(); } } \ No newline at end of file diff --git a/Polyclinic/PolyclinicDataModels/Models/IProcedureModel.cs b/Polyclinic/PolyclinicDataModels/Models/IProcedureModel.cs index 3c1d743..47827ad 100644 --- a/Polyclinic/PolyclinicDataModels/Models/IProcedureModel.cs +++ b/Polyclinic/PolyclinicDataModels/Models/IProcedureModel.cs @@ -4,6 +4,7 @@ { string Name { get; } string Comment { get; } - Dictionary ProcedureCourses { get; } + int UserId { get; } + Dictionary ProcedureRecipes { get; } } } \ No newline at end of file diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs index f12efe8..acb2cb3 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs @@ -1,12 +1,92 @@ -using PolyclinicDataModels.Models; +using PolyclinicContracts.BindingModels; +using PolyclinicContracts.ViewModels; +using PolyclinicDataModels.Models; +using SecuritySystemDatabaseImplement; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace PolyclinicDatabaseImplement.Models { public class Procedure : IProcedureModel { - public string Name => throw new NotImplementedException(); - public string Comment => throw new NotImplementedException(); - public Dictionary ProcedureCourses => throw new NotImplementedException(); - public int Id => throw new NotImplementedException(); + public int Id { get; set; } + + [Required] + public int UserId { get; set; } + + [Required] + public string Name { get; set; } = string.Empty; + + [Required] + public string Comment { get; set; } = string.Empty; + + private Dictionary? _procedureRecipes = null; + + [ForeignKey("ProcedureId")] + public virtual List Recipes { get; set; } = new(); + + [NotMapped] + public Dictionary 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() + { + Id = bindingModel.Id, + 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() + }; + } + + public void Update(ProcedureBindingModel bindingModel) + { + Name = bindingModel.Name; + Comment = bindingModel.Comment; + } + + public ProcedureViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + 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; + } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs index ab430d1..c44a4fd 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs @@ -1,6 +1,17 @@ -namespace PolyclinicDatabaseImplement.Models +using System.ComponentModel.DataAnnotations; + +namespace PolyclinicDatabaseImplement.Models { public class ProcedureRecipe { + public int Id { get; set; } + + [Required] + public int ProcedureId { get; set; } + + [Required] + public int RecipeId { get; set; } + public virtual Procedure Procedure { get; set; } + public virtual Recipe Recipe { get; set; } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs b/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs index 7e6f892..79eaaaf 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/PolyclinicDatabase.cs @@ -17,5 +17,8 @@ namespace SecuritySystemDatabaseImplement public virtual DbSet Diagnoses { set; get; } public virtual DbSet Courses { set; get; } public virtual DbSet CourseDiagnoses { set; get; } + public virtual DbSet Recipes { set; get; } + public virtual DbSet Procedures { set; get; } + public virtual DbSet ProcedureRecipes { set; get; } } }