Реализовал модели бд курса, симтпома и симптомаДиагноза

This commit is contained in:
Никита Потапов 2024-04-29 20:47:31 +04:00
parent a0ecb7b222
commit 5805b378b4
3 changed files with 109 additions and 32 deletions

View File

@ -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;
}
}
}

View File

@ -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<int, IDiagnoseModel> 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<SymptomDiagnose> Diagnoses { get; set; } = new();
private Dictionary<int, IDiagnoseModel>? _symptomDiagnoses = null;
[NotMapped]
public Dictionary<int, IDiagnoseModel> 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;
}
}
}

View File

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