models done

This commit is contained in:
devil_1nc 2023-03-12 18:00:46 +04:00
parent 40fb3142eb
commit 3de75dc783
6 changed files with 317 additions and 1 deletions

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="..\AbstractSoftwareInstallationContracts\AbstractSoftwareInstallationContracts.csproj" />
<ProjectReference Include="..\AbstractSoftwareInstallationDataModels\AbstractSoftwareInstallationDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,50 @@
using AbstractSoftwareInstallationFileImplement.Models;
using System.Xml.Linq;
namespace AbstractSoftwareInstallationFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string SoftwareFileName = "Software.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string PackageFileName = "Package.xml";
public List<Software> Softwares { get; private set; }
public List<Order> Orders { get; private set; }
public List<Package> Packages { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveSoftwares() => SaveData(Softwares, SoftwareFileName, "Softwares", x => x.GetXElement);
public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Softwares = LoadData(SoftwareFileName, "Software", x => Software.Create(x)!)!;
Packages = LoadData(PackageFileName, "Package", x => Package.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
}
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,90 @@
using AbstractSoftwareInstallationContracts.BindingModels;
using AbstractSoftwareInstallationContracts.ViewModels;
using AbstractSoftwareInstallationDataModels;
using AbstractSoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractSoftwareInstallationFileImplement.Models
{
internal class Order : IOrderModel
{
public int PackageId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public int Id { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null) return null;
return new Order
{
PackageId = model.PackageId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Id = model.Id,
};
}
public static Order? Create(XElement element)
{
if (element == null) return null;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PackageId = Convert.ToInt32(element.Element("PackageId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.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 == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
PackageId = PackageId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("PackageId", PackageId.ToString()),
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,92 @@
using AbstractSoftwareInstallationContracts.BindingModels;
using AbstractSoftwareInstallationContracts.ViewModels;
using AbstractSoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractSoftwareInstallationFileImplement.Models
{
internal class Package : IPackageModel
{
public string PackageName { get; private set; } = String.Empty;
public int Id { get; private set; }
public double Price { get; private set; }
public Dictionary<int, int> Softwares { get; private set; } = new();
private Dictionary<int, (ISoftwareModel, int)> _packageSoftware = null;
public Dictionary<int, (ISoftwareModel, int)> PackageSoftware
{
get
{
if (_packageSoftware == null)
{
var source = DataFileSingleton.GetInstance();
_packageSoftware = Softwares.ToDictionary(
x => x.Key,
y => ((source.Softwares.FirstOrDefault(z => z.Id == y.Key) as ISoftwareModel)!, y.Value)
);
}
return _packageSoftware;
}
}
public static Package? Create(PackageBindingModel model)
{
if (model == null)
{
return null;
}
return new Package()
{
Id = model.Id,
PackageName = model.PackageName,
Price = model.Price,
Softwares = model.PackageSoftware.ToDictionary(x => x.Key,
x => x.Value.Item2)
};
}
public static Package? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Package()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
PackageName = element.Element("PackageName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Softwares = element.Element("PackageSoftwares")!.Elements("PackageSoftware").ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value)
)
};
}
public void Update(PackageBindingModel? model)
{
if (model == null)
{
return;
}
PackageName = model.PackageName;
Price = model.Price;
Softwares = model.PackageSoftware.ToDictionary(x => x.Key, x => x.Value.Item2);
}
public PackageViewModel GetViewModel => new()
{
Id = Id,
PackageName = PackageName,
Price = Price,
PackageSoftware = PackageSoftware
};
public XElement GetXElement => new("Software",
new XAttribute("Id", Id),
new XElement("PackageName", PackageName),
new XElement("Price", Price.ToString()));
}
}

View File

@ -0,0 +1,64 @@
using AbstractSoftwareInstallationContracts.BindingModels;
using AbstractSoftwareInstallationContracts.ViewModels;
using AbstractSoftwareInstallationDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractSoftwareInstallationFileImplement.Models
{
internal class Software : ISoftwareModel
{
public int Id { get; private set; }
public string SoftwareName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Software? Create(SoftwareBindingModel model)
{
if (model == null)
{
return null;
}
return new Software()
{
Id = model.Id,
SoftwareName = model.SoftwareName,
Cost = model.Cost
};
}
public static Software? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Software()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SoftwareName = element.Element("SoftwareName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(SoftwareBindingModel model)
{
if (model == null)
{
return;
}
SoftwareName = model.SoftwareName;
Cost = model.Cost;
}
public SoftwareViewModel GetViewModel => new()
{
Id = Id,
SoftwareName = SoftwareName,
Cost = Cost
};
public XElement GetXElement => new("Software",
new XAttribute("Id", Id),
new XElement("SoftwareName", SoftwareName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallatio
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallationBusinessLogic", "AbstractSoftwareInstallationBusinessLogic\AbstractSoftwareInstallationBusinessLogic.csproj", "{76E33F5D-6D55-4C28-B26B-9F33B10BA3EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractSoftwareInstallationListImplement", "AbstractSoftwareInstallationListImplement\AbstractSoftwareInstallationListImplement.csproj", "{31AD2872-9651-476A-9868-C4404FEEB0E4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractSoftwareInstallationListImplement", "AbstractSoftwareInstallationListImplement\AbstractSoftwareInstallationListImplement.csproj", "{31AD2872-9651-476A-9868-C4404FEEB0E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractSoftwareInstallationFileImplement", "AbstractSoftwareInstallationFileImplement\AbstractSoftwareInstallationFileImplement.csproj", "{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{31AD2872-9651-476A-9868-C4404FEEB0E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{31AD2872-9651-476A-9868-C4404FEEB0E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{31AD2872-9651-476A-9868-C4404FEEB0E4}.Release|Any CPU.Build.0 = Release|Any CPU
{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEE13C3F-1BB8-46B4-BC87-CFC367E52A77}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE