Rogashova_E.A._CourseWork_H.../HospitalDataBaseImplements/Models/Illness.cs

127 lines
4.8 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Models
{
public class Illness : IIllnessModel
{
public int Id { get; private set; }
[Required]
public string IllnessName { get; private set; } = string.Empty;
[Required]
public string Form { get; private set; } = string.Empty;
private Dictionary<int, IKurseModel>? _illnessKurses = null;
[NotMapped]
public Dictionary<int, IKurseModel> IllnessKurse
{
get
{
if (_illnessKurses == null)
{
_illnessKurses = Kurses.ToDictionary(recPC => recPC.KurseId, recPC => (recPC.Kurse as IKurseModel));
}
return _illnessKurses;
}
}
[ForeignKey("IllnessId")]
public virtual List<IllnessKurse> Kurses { get; set; } = new();
private Dictionary<int, ISymptomsModel>? _illnessSymptomses = null;
[NotMapped]
public Dictionary<int, ISymptomsModel> IllnessSymptoms
{
get
{
if (_illnessSymptomses == null)
{
_illnessSymptomses = Symptomses.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel));
}
return _illnessSymptomses;
}
}
[ForeignKey("IllnessId")]
public virtual List<IllnessSymptoms> Symptomses { get; set; } = new();
public static Illness Create(HospitalDatabase context, IllnessBindingModel model)
{
return new Illness()
{
Id = model.Id,
IllnessName = model.IllnessName,
Form = model.Form,
Kurses = model.IllnessKurse.Select(x => new IllnessKurse
{
Kurse = context.Kurse.First(y => y.Id == x.Key),
}).ToList(),
Symptomses = model.IllnessSymptoms.Select(x => new IllnessSymptoms
{
Symptoms = context.Symptomses.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(IllnessBindingModel model)
{
IllnessName = model.IllnessName;
Form = model.Form;
}
public IllnessViewModel GetViewModel => new()
{
Id = Id,
IllnessName = IllnessName,
Form = Form,
IllnessKurse = IllnessKurse,
IllnessSymptoms = IllnessSymptoms
};
public void UpdateDrugCourses(HospitalDatabase context, IllnessBindingModel model)
{
var illnessKurses = context.IllnessKurse.Where(rec => rec.IllnessId == model.Id).ToList();
if (illnessKurses != null && illnessKurses.Count > 0)
{ // удалили те, которых нет в модели
context.IllnessKurse.RemoveRange(illnessKurses.Where(rec
=> !model.IllnessKurse.ContainsKey(rec.KurseId)));
context.SaveChanges();
}
var illness = context.Illnesses.First(x => x.Id == Id);
foreach (var pc in model.IllnessKurse)
{
context.IllnessKurse.Add(new IllnessKurse
{
Illness = illness,
Kurse = context.Kurse.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_illnessKurses = null;
}
public void UpdateSymptomses(HospitalDatabase context, IllnessBindingModel model)
{
var illnessSymptomses = context.IllnessSymptomses.Where(rec => rec.IllnessId == model.Id).ToList();
if (illnessSymptomses != null && illnessSymptomses.Count > 0)
{ // удалили те, которых нет в модели
context.IllnessSymptomses.RemoveRange(illnessSymptomses.Where(rec
=> !model.IllnessSymptoms.ContainsKey(rec.SymptomsId)));
context.SaveChanges();
}
var illness = context.Illnesses.First(x => x.Id == Id);
foreach (var pc in model.IllnessSymptoms)
{
context.IllnessSymptomses.Add(new IllnessSymptoms
{
Illness = illness,
Symptoms = context.Symptomses.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_illnessSymptomses = null;
}
}
}