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(); } }