From 5805b378b4e8073730efab8843c343660b877835 Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Mon, 29 Apr 2024 20:47:31 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=B1=D0=B4=20=D0=BA=D1=83=D1=80=D1=81=D0=B0,=20=D1=81=D0=B8?= =?UTF-8?q?=D0=BC=D1=82=D0=BF=D0=BE=D0=BC=D0=B0=20=D0=B8=20=D1=81=D0=B8?= =?UTF-8?q?=D0=BC=D0=BF=D1=82=D0=BE=D0=BC=D0=B0=D0=94=D0=B8=D0=B0=D0=B3?= =?UTF-8?q?=D0=BD=D0=BE=D0=B7=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/Course.cs | 45 +++++----- .../Models/Symptom.cs | 85 +++++++++++++++++-- .../Models/SymptomDiagnose.cs | 11 ++- 3 files changed, 109 insertions(+), 32 deletions(-) diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs index 1dd6339..3f32268 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Course.cs @@ -72,32 +72,25 @@ namespace PolyclinicDatabaseImplement.Models public void UpdateDiagnoses(PolyclinicDatabase 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; + 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(); + } + 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), + }); + context.SaveChanges(); + } + _courseDiagnoses = null; } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs index ac28741..59f59ba 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/Symptom.cs @@ -1,12 +1,87 @@ -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 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(); + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public string Comment { get; set; } = string.Empty; + [ForeignKey("SymptomId")] + public virtual List Diagnoses { get; set; } = new(); + private Dictionary? _symptomDiagnoses = null; + [NotMapped] + public Dictionary SymptomDiagnoses + { + get + { + if (_symptomDiagnoses == null) + { + _symptomDiagnoses = Diagnoses.ToDictionary( + symptomDiagnose => symptomDiagnose.DiagnoseId, + symptomDiagnose => symptomDiagnose.Diagnose as IDiagnoseModel + ); + } + return _symptomDiagnoses; + } + } + public int Id { get; set; } + + public static Symptom Create(PolyclinicDatabase context, SymptomBindingModel model) + { + return new Symptom() + { + Id = model.Id, + Name = model.Name, + Comment = model.Comment, + Diagnoses = model.SymptomDiagnoses.Select(symptomDiagnose => new SymptomDiagnose + { + Diagnose = context.Diagnoses.First(diagnose => diagnose.Id == symptomDiagnose.Key) + }).ToList() + }; + } + + public void Update(SymptomBindingModel model) + { + Comment = model.Comment; + Name = model.Name; + } + + public SymptomViewModel GetViewModel => new() + { + Id = Id, + Comment = Comment, + Name = Name, + SymptomDiagnoses = SymptomDiagnoses + }; + + public void UpdateDiagnoses(PolyclinicDatabase context, SymptomBindingModel model) + { + var symptomDiagnoses = context.SymptomDiagnoses.Where(rec => rec.SymptomId == model.Id).ToList(); + if (symptomDiagnoses != null && symptomDiagnoses.Count > 0) + { + // удалили те, которых нет в модели + context.SymptomDiagnoses.RemoveRange(symptomDiagnoses + .Where(rec => !model.SymptomDiagnoses.ContainsKey(rec.DiagnoseId))); + context.SaveChanges(); + } + var course = context.Symptomes.First(x => x.Id == Id); + foreach (var pc in model.SymptomDiagnoses) + { + context.SymptomDiagnoses.Add(new SymptomDiagnose + { + Symptom = course, + Diagnose = context.Diagnoses.First(x => x.Id == pc.Key), + }); + context.SaveChanges(); + } + _symptomDiagnoses = null; + } } } diff --git a/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs b/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs index 6d75305..7d8a399 100644 --- a/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs +++ b/Polyclinic/PolyclinicDatabaseImplement/Models/SymptomDiagnose.cs @@ -1,6 +1,15 @@ -namespace PolyclinicDatabaseImplement.Models +using System.ComponentModel.DataAnnotations; + +namespace PolyclinicDatabaseImplement.Models { public class SymptomDiagnose { + public int Id { get; set; } + [Required] + public int SymptomId { get; set; } + [Required] + public int DiagnoseId { get; set; } + public virtual Symptom Symptom { get; set; } = new(); + public virtual Diagnose Diagnose { get; set; } = new(); } }