71 lines
2.8 KiB
C#
71 lines
2.8 KiB
C#
using HospitalDatabaseImplement.Models;
|
|
using System.Xml.Linq;
|
|
|
|
namespace HospitalDatabaseImplement
|
|
{
|
|
public class LoaderFromXML
|
|
{
|
|
private static readonly string PatientFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Patient.xml");
|
|
private static readonly string TreatmentFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Treatment.xml");
|
|
private static readonly string ProcedureFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Procedure.xml");
|
|
|
|
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
|
|
{
|
|
if (File.Exists(filename))
|
|
{
|
|
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
|
|
}
|
|
return new List<T>();
|
|
}
|
|
/// <summary>
|
|
/// Чтение пациентов из XML-файла
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static void LoadPatients() // (запуск после загрузки лечений)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
if (context.Patients.ToList().Count > 0)
|
|
return;
|
|
var list = LoadData(PatientFileName, "Patient", x => Patient.Create(x)!)!;
|
|
list.ForEach(x =>
|
|
{
|
|
context.Patients.Add(x);
|
|
});
|
|
context.SaveChanges();
|
|
|
|
}
|
|
/// <summary>
|
|
/// Чтение лечений из XML-файла
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static void LoadTreatments() // (запуск после загрузки процедур)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
if (context.Treatments.ToList().Count > 0)
|
|
return;
|
|
var list = LoadData(TreatmentFileName, "Treatment", x => Treatment.Create(context, x)!)!;
|
|
list.ForEach(x =>
|
|
{
|
|
context.Treatments.Add(x);
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
/// <summary>
|
|
/// Чтение поцедур из XML-файла
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static void LoadProcedures()// (запуск после старта программы)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
if (context.Procedures.ToList().Count > 0)
|
|
return;
|
|
var list = LoadData(ProcedureFileName, "Procedure", x => Procedure.Create(x)!)!;
|
|
list.ForEach(x =>
|
|
{
|
|
context.Procedures.Add(x);
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
}
|