From ac9f4b87ce635effe07b1bed73f7dbebc0ff926c Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Sun, 28 Apr 2024 16:24:52 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=20=D0=B2?= =?UTF-8?q?=D1=81=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D1=8B=20=D0=BC?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=D0=BB=D0=B5=D0=B9=20=D0=B1=D0=B4,=20=D0=BD?= =?UTF-8?q?=D1=83=D0=B6=D0=BD=D0=BE=20=D0=B8=D1=85=20=D1=80=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/Course.cs | 101 ++++++++++++++++++ .../Models/CourseDiagnose.cs | 15 +++ .../Models/Diagnose.cs | 2 +- .../Models/Medicament.cs | 13 +++ .../Models/Procedure.cs | 12 +++ .../Models/ProcedureRecipe.cs | 6 ++ .../Models/Recipe.cs | 12 +++ .../Models/Symptom.cs | 12 +++ .../Models/SymptomDiagnose.cs | 6 ++ .../Models/SymptomRecipe.cs | 6 ++ 10 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/CourseDiagnose.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs create mode 100644 Polyclinic/PolyclinicDatabaseImplement/Models/SymptomRecipe.cs diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs new file mode 100644 index 0000000..9df375c --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs @@ -0,0 +1,101 @@ +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 Course : ICourseModel + { + public int Id { get; set; } + [Required] + public int DaysCount { get; set; } + [Required] + public int PillsPerDay { get; set; } + public string Comment { get; set; } = string.Empty; + [Required] + public int RecipeId { get; set; } + public virtual Recipe Recipe { get; set; } = new(); + [ForeignKey("CourseId")] + public virtual List Diagnoses { get; set; } = new(); + private Dictionary? _courseDiagnoses = null; + public Dictionary CourseDiagnoses + { + get + { + if (_courseDiagnoses == null) + { + _courseDiagnoses = Diagnoses.ToDictionary( + courseDiagnose => courseDiagnose.DiagnoseId, + courseDiagnose => courseDiagnose.Diagnose as IDiagnoseModel + ); + } + return _courseDiagnoses; + } + } + + public static Course Create(SecuritySystemDatabase context, CourseBindingModel model) + { + return new Course() + { + Id = model.Id, + DaysCount = model.DaysCount, + PillsPerDay = model.PillsPerDay, + Comment = model.Comment, + RecipeId = model.RecipeId, + Diagnoses = model.CourseDiagnoses.Select(courseDiagnose => new CourseDiagnose + { + Diagnose = context.Diagnoses.First(diagnose => diagnose.Id == courseDiagnose.Key) + }).ToList() + }; + } + + public void Update(CourseBindingModel model) + { + DaysCount = model.DaysCount; + PillsPerDay = model.PillsPerDay; + Comment = model.Comment; + RecipeId = model.RecipeId; + } + + public CourseViewModel GetViewModel => new() + { + Id = Id, + CourseName = CourseName, + Price = Price, + CourseDiagnoses = CourseDiagnoses + }; + + public void UpdateDiagnoses(SecuritySystemDatabase context, CourseBindingModel model) + { + var courseDiagnoses = context.CourseDiagnoses.Where(rec => rec.CourseId == model.Id).ToList(); + if (courseDiagnoses != null && courseDiagnoses.Count > 0) + { + // удалили те, которых нет в модели + context.CourseDiagnoses.RemoveRange(courseDiagnoses.Where(rec => !model.CourseDiagnoses.ContainsKey(rec.DiagnoseId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateDiagnose in courseDiagnoses) + { + updateDiagnose.Count = model.CourseDiagnoses[updateDiagnose.DiagnoseId].Item2; + model.CourseDiagnoses.Remove(updateDiagnose.DiagnoseId); + } + context.SaveChanges(); + } + var course = context.Courses.First(x => x.Id == Id); + foreach (var pc in model.CourseDiagnoses) + { + context.CourseDiagnoses.Add(new CourseDiagnose + { + Course = course, + Diagnose = context.Diagnoses.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _courseDiagnoses = null; + } + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/CourseDiagnose.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/CourseDiagnose.cs new file mode 100644 index 0000000..5c8809f --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/CourseDiagnose.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace PolyclinicDatabaseImplement.Models +{ + public class CourseDiagnose + { + public int Id { get; set; } + [Required] + public int CourseId { get; set; } + [Required] + public int DiagnoseId { get; set; } + public virtual Course Course { get; set; } = new(); + public virtual Diagnose Diagnose { get; set; } = new(); + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Diagnose.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Diagnose.cs index 24716bb..3366fa2 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Diagnose.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Diagnose.cs @@ -14,7 +14,7 @@ namespace PolyclinicDatabaseImplement.Models [Required] public int UserId { get; set; } public int Id { get; set; } - public virtual User? User { get; set; } + public virtual User User { get; set; } = new(); public static Diagnose? Create(DiagnoseBindingModel? model) { diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs new file mode 100644 index 0000000..c2244fa --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Medicament.cs @@ -0,0 +1,13 @@ +using PolyclinicDataModels.Models; + +namespace PolyclinicDatabaseImplement.Models +{ + public class Medicament : IMedicamentModel + { + public string Name => throw new NotImplementedException(); + public string Comment => throw new NotImplementedException(); + public int ProcedureId => throw new NotImplementedException(); + public int SymptomId => throw new NotImplementedException(); + public int Id => throw new NotImplementedException(); + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs new file mode 100644 index 0000000..f12efe8 --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Procedure.cs @@ -0,0 +1,12 @@ +using PolyclinicDataModels.Models; + +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(); + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs new file mode 100644 index 0000000..ab430d1 --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/ProcedureRecipe.cs @@ -0,0 +1,6 @@ +namespace PolyclinicDatabaseImplement.Models +{ + public class ProcedureRecipe + { + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs new file mode 100644 index 0000000..48ab046 --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Recipe.cs @@ -0,0 +1,12 @@ +using PolyclinicDataModels.Models; + +namespace PolyclinicDatabaseImplement.Models +{ + public class Recipe : IRecipeModel + { + public int ProceduresCount { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public string Comment { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public Dictionary ProcedureRecipes => throw new NotImplementedException(); + public int Id => throw new NotImplementedException(); + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs new file mode 100644 index 0000000..ac28741 --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs @@ -0,0 +1,12 @@ +using PolyclinicDataModels.Models; + +namespace PolyclinicDatabaseImplement.Models +{ + public class Symptom : ISymptomModel + { + public string Name => throw new NotImplementedException(); + public string Comment => throw new NotImplementedException(); + public Dictionary SymptomDiagnoses => throw new NotImplementedException(); + public int Id => throw new NotImplementedException(); + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs new file mode 100644 index 0000000..6d75305 --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs @@ -0,0 +1,6 @@ +namespace PolyclinicDatabaseImplement.Models +{ + public class SymptomDiagnose + { + } +} diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomRecipe.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomRecipe.cs new file mode 100644 index 0000000..2ada977 --- /dev/null +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomRecipe.cs @@ -0,0 +1,6 @@ +namespace PolyclinicDatabaseImplement.Models +{ + public class SymptomRecipe + { + } +}