76 lines
2.8 KiB
C#
76 lines
2.8 KiB
C#
using HospitalDataBaseImplements.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
|
|
namespace HospitalDataBaseImplements
|
|
{
|
|
public class LoaderFromXML
|
|
{
|
|
// private static readonly string IllnessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Illness.xml");
|
|
private static readonly string SymptomsFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Symptoms.xml");
|
|
private static readonly string KursesFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Kurses.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 LoadIllness() // (запуск после загрузки курсов)
|
|
//{
|
|
// using var context = new HospitalDatabase();
|
|
// if (context.Illnesses.ToList().Count > 0)
|
|
// return;
|
|
// var list = LoadData(IllnessFileName, "Illness", x => Illness.Create(x)!)!;
|
|
// list.ForEach(x =>
|
|
// {
|
|
// context.Illnesses.Add(x);
|
|
// });
|
|
// context.SaveChanges();
|
|
|
|
//}
|
|
/// <summary>
|
|
/// Чтение курсов из XML-файла
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static void LoadKurses() // (запуск после загрузки симптомов)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
if (context.Kurse.ToList().Count > 0)
|
|
return;
|
|
var list = LoadData(KursesFileName, "Kurses", x => Kurses.Create(context, x)!)!;
|
|
list.ForEach(x =>
|
|
{
|
|
context.Kurse.Add(x);
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
/// <summary>
|
|
/// Чтение симптомов из XML-файла
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static void LoadSymptoms()// (запуск после старта программы)
|
|
{
|
|
using var context = new HospitalDatabase();
|
|
if (context.Symptomses.ToList().Count > 0)
|
|
return;
|
|
var list = LoadData(SymptomsFileName, "Symptoms", x => Symptoms.Create(x)!)!;
|
|
list.ForEach(x =>
|
|
{
|
|
context.Symptomses.Add(x);
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
}
|