2024-04-29 20:47:31 +04:00
|
|
|
|
using PolyclinicContracts.BindingModels;
|
|
|
|
|
using PolyclinicContracts.ViewModels;
|
|
|
|
|
using PolyclinicDataModels.Models;
|
|
|
|
|
using SecuritySystemDatabaseImplement;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
2024-04-28 16:24:52 +04:00
|
|
|
|
|
|
|
|
|
namespace PolyclinicDatabaseImplement.Models
|
|
|
|
|
{
|
|
|
|
|
public class Symptom : ISymptomModel
|
|
|
|
|
{
|
2024-04-29 20:47:31 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public string Name { get; set; } = string.Empty;
|
2024-05-28 19:59:22 +04:00
|
|
|
|
public string? Comment { get; set; } = string.Empty;
|
2024-04-29 20:47:31 +04:00
|
|
|
|
[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; }
|
|
|
|
|
|
2024-05-28 19:59:22 +04:00
|
|
|
|
public static Symptom? Create(PolyclinicDatabase context, SymptomBindingModel model)
|
2024-04-29 20:47:31 +04:00
|
|
|
|
{
|
|
|
|
|
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();
|
2024-05-28 13:55:03 +04:00
|
|
|
|
|
2024-04-29 20:47:31 +04:00
|
|
|
|
if (symptomDiagnoses != null && symptomDiagnoses.Count > 0)
|
|
|
|
|
{
|
2024-05-28 13:55:03 +04:00
|
|
|
|
context.SymptomDiagnoses.RemoveRange(symptomDiagnoses);
|
2024-04-29 20:47:31 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
2024-05-28 13:55:03 +04:00
|
|
|
|
var symptom = context.Symptomes.First(x => x.Id == model.Id);
|
2024-04-29 20:47:31 +04:00
|
|
|
|
foreach (var pc in model.SymptomDiagnoses)
|
|
|
|
|
{
|
|
|
|
|
context.SymptomDiagnoses.Add(new SymptomDiagnose
|
|
|
|
|
{
|
2024-05-28 13:55:03 +04:00
|
|
|
|
Symptom = symptom,
|
2024-04-29 20:47:31 +04:00
|
|
|
|
Diagnose = context.Diagnoses.First(x => x.Id == pc.Key),
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_symptomDiagnoses = null;
|
|
|
|
|
}
|
2024-04-28 16:24:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|