AutoWorkshopFileImplements/Models

This commit is contained in:
ShabOl 2024-03-19 22:01:33 +04:00
parent e9691f2f63
commit e3a1f15b7c
8 changed files with 322 additions and 0 deletions

View File

@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoWorkshopBusinessLogic",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoWorkshopListImplement", "AutoWorkshopImplement\AutoWorkshopListImplement.csproj", "{B564F5E8-2F14-4816-8481-1F9649F1F414}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoWorkshopFileImplement", "AutoWorkshopFileImplement\AutoWorkshopFileImplement.csproj", "{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,10 @@ Global
{B564F5E8-2F14-4816-8481-1F9649F1F414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B564F5E8-2F14-4816-8481-1F9649F1F414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B564F5E8-2F14-4816-8481-1F9649F1F414}.Release|Any CPU.Build.0 = Release|Any CPU
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{862B0F3D-1B88-45B8-9526-AD21A6D6FA81}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AutoWorkshopContracts\AutoWorkshopContracts.csproj" />
<ProjectReference Include="..\AutoWorkshopDataModels\AutoWorkshopDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,59 @@
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";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Repair> Repairs { get; private set; }
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)!)!;
}
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);
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);
}
}
}
}

View File

@ -0,0 +1,65 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.Xml.Linq;
namespace AutoWorkshopFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel Model)
{
if (Model is null)
return null;
return new Component()
{
Id = Model.Id,
ComponentName = Model.ComponentName,
Cost = Model.Cost
};
}
public static Component? Create(XElement Element)
{
if (Element is null)
return null;
return new Component()
{
Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
ComponentName = Element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(Element.Element("Cost")!.Value)
};
}
public void Update(ComponentBindingModel Model)
{
if (Model is null)
return;
ComponentName = Model.ComponentName;
Cost = Model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new(
"Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString())
);
}
}

View File

@ -0,0 +1,90 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Enums;
using AutoWorkshopDataModels.Models;
using System.Xml.Linq;
namespace AutoWorkshopFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int RepairId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; }
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? Model)
{
if (Model is null)
return null;
return new Order()
{
Id = Model.Id,
RepairId = Model.RepairId,
Count = Model.Count,
Sum = Model.Sum,
Status = Model.Status,
DateCreate = Model.DateCreate,
DateImplement = Model.DateImplement
};
}
public static Order? Create(XElement Element)
{
if (Element is null)
return null;
return new Order()
{
Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
RepairId = Convert.ToInt32(Element.Element("RepairId")!.Value),
Count = Convert.ToInt32(Element.Element("Count")!.Value),
Sum = Convert.ToDouble(Element.Element("Sum")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), Element.Element("Status")!.Value),
DateCreate = Convert.ToDateTime(Element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(Element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(Element.Element("DateImplement")!.Value)
};
}
public void Update(OrderBindingModel? Model)
{
if (Model is null)
return;
Status = Model.Status;
DateImplement = Model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
RepairId = RepairId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("RepairId", RepairId),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString())
);
}
}

View File

@ -0,0 +1,87 @@
using AutoWorkshopContracts.BindingModels;
using AutoWorkshopContracts.ViewModels;
using AutoWorkshopDataModels.Models;
using System.Xml.Linq;
namespace AutoWorkshopFileImplement.Models
{
public class Repair : IRepairModel
{
public int Id { get; private set; }
public string RepairName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _RepairComponents = null;
public Dictionary<int, (IComponentModel, int)> RepairComponents
{
get
{
if (_RepairComponents == null)
{
var source = DataFileSingleton.GetInstance();
_RepairComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _RepairComponents;
}
}
public static Repair? Create(RepairBindingModel Model)
{
if (Model is null)
return null;
return new Repair()
{
Id = Model.Id,
RepairName = Model.RepairName,
Price = Model.Price,
Components = Model.RepairComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Repair? Create(XElement Element)
{
if (Element is null)
return null;
return new Repair()
{
Id = Convert.ToInt32(Element.Attribute("Id")!.Value),
RepairName = Element.Element("RepairName")!.Value,
Price = Convert.ToDouble(Element.Element("Price")!.Value),
Components = Element.Element("RepairComponents")!.Elements("RepairComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(RepairBindingModel Model)
{
if (Model is null)
return;
RepairName = Model.RepairName;
Price = Model.Price;
Components = Model.RepairComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_RepairComponents = null;
}
public RepairViewModel GetViewModel => new()
{
Id = Id,
RepairName = RepairName,
Price = Price,
RepairComponents = RepairComponents
};
public XElement GetXElement => new(
"Repair",
new XAttribute("Id", Id),
new XElement("RepairName", RepairName),
new XElement("Price", Price.ToString()),
new XElement("RepairComponents", Components.Select(x => new XElement("RepairComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray())
);
}
}

View File

@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\AutoWorkshopBusinessLogic\AutoWorkshopBusinessLogic.csproj" />
<ProjectReference Include="..\AutoWorkshopFileImplement\AutoWorkshopFileImplement.csproj" />
<ProjectReference Include="..\AutoWorkshopImplement\AutoWorkshopListImplement.csproj" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog" Version="5.2.8" />

BIN
Задание.pdf Normal file

Binary file not shown.