2024-03-19 22:01:33 +04:00
|
|
|
|
using AutoWorkshopFileImplement.Models;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
namespace AutoWorkshopFileImplement
|
|
|
|
|
{
|
|
|
|
|
public class DataFileSingleton
|
|
|
|
|
{
|
|
|
|
|
private static DataFileSingleton? _instance;
|
|
|
|
|
|
|
|
|
|
private readonly string ComponentFileName = "Component.xml";
|
|
|
|
|
private readonly string OrderFileName = "Order.xml";
|
|
|
|
|
private readonly string RepairFileName = "Repair.xml";
|
2024-04-09 23:44:27 +04:00
|
|
|
|
private readonly string ShopFileName = "Shop.xml";
|
|
|
|
|
|
|
|
|
|
public List<Component> Components { get; private set; }
|
|
|
|
|
public List<Order> Orders { get; private set; }
|
2024-03-19 22:01:33 +04:00
|
|
|
|
public List<Repair> Repairs { get; private set; }
|
2024-04-09 23:44:27 +04:00
|
|
|
|
public List<Shop> Shops { get; private set; }
|
|
|
|
|
|
2024-03-19 22:01:33 +04:00
|
|
|
|
private DataFileSingleton()
|
|
|
|
|
{
|
|
|
|
|
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
|
|
|
|
|
Repairs = LoadData(RepairFileName, "Repair", x => Repair.Create(x)!)!;
|
|
|
|
|
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
|
2024-04-10 21:49:56 +04:00
|
|
|
|
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
|
2024-03-19 22:01:33 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DataFileSingleton GetInstance()
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new DataFileSingleton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
|
|
|
|
|
public void SaveRepairs() => SaveData(Repairs, RepairFileName, "Repairs", x => x.GetXElement);
|
|
|
|
|
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
|
2024-04-09 23:44:27 +04:00
|
|
|
|
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
|
2024-03-19 22:01:33 +04:00
|
|
|
|
|
|
|
|
|
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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SaveData<T>(List<T> Data, string FileName, string XmlNodeName, Func<T, XElement> SelectFunction)
|
|
|
|
|
{
|
|
|
|
|
if (Data != null)
|
|
|
|
|
{
|
|
|
|
|
new XDocument(new XElement(XmlNodeName, Data.Select(SelectFunction).ToArray())).Save(FileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|