Base.01
This commit is contained in:
parent
ab7d28ebc2
commit
2e5afa7f85
@ -5,13 +5,15 @@ VisualStudioVersion = 17.7.34221.43
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerView", "DinerView\DinerView.csproj", "{7FC291E6-FE0A-4D25-B46F-724FB599F26F}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerDataModels", "DinerDataModels\DinerDataModels.csproj", "{2AFAC265-13C0-432E-8F33-ABF764FD0877}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerDataModels", "DinerDataModels\DinerDataModels.csproj", "{2AFAC265-13C0-432E-8F33-ABF764FD0877}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerContracts", "DinerContracts\DinerContracts.csproj", "{89BA6B4F-1F08-4327-B88B-E48335742088}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerContracts", "DinerContracts\DinerContracts.csproj", "{89BA6B4F-1F08-4327-B88B-E48335742088}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerListImplement", "DinerListImplement\DinerListImplement.csproj", "{33BCF269-AF60-4E7E-9F3B-2ECEDCEBF8F9}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "DinerListImplement\DinerListImplement.csproj", "{33BCF269-AF60-4E7E-9F3B-2ECEDCEBF8F9}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DineryBusinessLogic", "DineryBusinessLogic\DineryBusinessLogic.csproj", "{2920D9E3-AE18-4914-BD43-C04D9123FBD7}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DineryBusinessLogic", "DineryBusinessLogic\DineryBusinessLogic.csproj", "{2920D9E3-AE18-4914-BD43-C04D9123FBD7}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerShopImplement\DinerFileImplement.csproj", "{E46007E3-F3EC-4551-A203-19361E7B1B8D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -39,6 +41,10 @@ Global
|
||||
{2920D9E3-AE18-4914-BD43-C04D9123FBD7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{2920D9E3-AE18-4914-BD43-C04D9123FBD7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{2920D9E3-AE18-4914-BD43-C04D9123FBD7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E46007E3-F3EC-4551-A203-19361E7B1B8D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E46007E3-F3EC-4551-A203-19361E7B1B8D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E46007E3-F3EC-4551-A203-19361E7B1B8D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E46007E3-F3EC-4551-A203-19361E7B1B8D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
59
Diner/DinerShopImplement/DataFileSingleton.cs
Normal file
59
Diner/DinerShopImplement/DataFileSingleton.cs
Normal file
@ -0,0 +1,59 @@
|
||||
using DinerFileImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace DinerFileImplement
|
||||
{
|
||||
internal class DataFileSingleton
|
||||
{
|
||||
private static DataFileSingleton? instance;
|
||||
|
||||
private readonly string FoodFileName = "Food.xml";
|
||||
|
||||
private readonly string OrderFileName = "Order.xml";
|
||||
|
||||
private readonly string SnackFileName = "Snack.xml";
|
||||
|
||||
public List<Food> Foods { get; set; }
|
||||
public List<Order> Orders { get; private set; }
|
||||
public List<Snack> Snacks { get; private set; }
|
||||
|
||||
private DataFileSingleton() {
|
||||
Foods = LoadData(FoodFileName, "Food", x => Food.Create(x)!)!;
|
||||
Snacks = LoadData(OrderFileName, "Order", x => Snack.Create(x)!)!;
|
||||
Orders = new List<Order>();
|
||||
}
|
||||
public static DataFileSingleton GetSingleton() {
|
||||
if (instance == null) instance = new DataFileSingleton();
|
||||
return instance;
|
||||
}
|
||||
public void SaveFoods() => SaveData(Foods, FoodFileName, "Foods",
|
||||
x => x.GetXElement);
|
||||
public void SaveSnacks() => SaveData(Snacks, SnackFileName, "Snacks",
|
||||
x => x.GetXElement);
|
||||
public void SaveOrder() => SaveData(Orders, OrderFileName, "Orders",
|
||||
x => x.GetXElement);
|
||||
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);
|
||||
}
|
||||
}
|
||||
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>();
|
||||
}
|
||||
}
|
||||
}
|
14
Diner/DinerShopImplement/DinerFileImplement.csproj
Normal file
14
Diner/DinerShopImplement/DinerFileImplement.csproj
Normal file
@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DinerContracts\DinerContracts.csproj" />
|
||||
<ProjectReference Include="..\DinerDataModels\DinerDataModels.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
57
Diner/DinerShopImplement/Models/Food.cs
Normal file
57
Diner/DinerShopImplement/Models/Food.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using DinerContracts.BindingModels;
|
||||
using DinerContracts.ViewModels;
|
||||
using DinerDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace DinerFileImplement.Models
|
||||
{
|
||||
internal class Food : IFoodModel
|
||||
{
|
||||
public string ComponentName { get; private set; } = string.Empty;
|
||||
|
||||
public double Price { get; set; }
|
||||
|
||||
public int ID { get; private set; }
|
||||
|
||||
public static Food? Create(FoodBindingModel? model)
|
||||
{
|
||||
if (model == null) return null;
|
||||
return new Food()
|
||||
{
|
||||
ID = model.ID,
|
||||
ComponentName = model.ComponentName,
|
||||
Price = model.Price,
|
||||
};
|
||||
}
|
||||
public static Food? Create(XElement element) {
|
||||
if (element == null) return null;
|
||||
return new Food()
|
||||
{
|
||||
ID = Convert.ToInt32(element.Attribute("ID")!.Value),
|
||||
ComponentName = element.Element("ComponentName")!.Value,
|
||||
Price = Convert.ToDouble(element.Element("Price")!.Value)
|
||||
};
|
||||
}
|
||||
public void Update(FoodBindingModel? model)
|
||||
{
|
||||
if (model == null) return;
|
||||
ComponentName = model.ComponentName;
|
||||
Price = model.Price;
|
||||
}
|
||||
public FoodViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
ComponentName = ComponentName,
|
||||
Price = Price,
|
||||
};
|
||||
|
||||
public XElement GetXElement => new("Food", new XAttribute("ID", ID),
|
||||
new XElement("CompoenntName", ComponentName),
|
||||
new XElement("Price", Price.ToString()));
|
||||
}
|
||||
}
|
82
Diner/DinerShopImplement/Models/Order.cs
Normal file
82
Diner/DinerShopImplement/Models/Order.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using DinerContracts.BindingModels;
|
||||
using DinerContracts.ViewModels;
|
||||
using DinerDataModels.Enums;
|
||||
using DinerDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace DinerFileImplement.Models
|
||||
{
|
||||
internal class Order : IOrderModel
|
||||
{
|
||||
public int ProductID { get; private set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
|
||||
public double Sum { get; private set; }
|
||||
|
||||
public OrderStatus Status { get; set; }
|
||||
|
||||
public DateTime DateCreate { get; private set; }
|
||||
|
||||
public DateTime? DateImplement { get; set; }
|
||||
|
||||
public int ID { get; private set; }
|
||||
|
||||
public static Order? Create(OrderBindingModel? model)
|
||||
{
|
||||
if (model == null) return null;
|
||||
return new Order()
|
||||
{
|
||||
ID = model.ID,
|
||||
ProductID = model.ProductID,
|
||||
Count = model.Count,
|
||||
Sum = model.Sum,
|
||||
Status = model.Status,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
};
|
||||
}
|
||||
public static Order? Create(XElement element) {
|
||||
if (element == null) return null;
|
||||
return new Order()
|
||||
{
|
||||
ID = Convert.ToInt32(element.Attribute("ID")!.Value),
|
||||
ProductID = Convert.ToInt32(element.Element("ProductName")!.Value),
|
||||
Count = Convert.ToInt32(element.Element("Price")!.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 = 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,
|
||||
ProductID = ProductID,
|
||||
Count = Count,
|
||||
Sum = Sum,
|
||||
Status = Status,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
};
|
||||
public XElement GetXElement => new("Order", new XAttribute("ID", ID),
|
||||
new XElement("ProductID", ProductID),
|
||||
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()));
|
||||
|
||||
}
|
||||
}
|
79
Diner/DinerShopImplement/Models/Snack.cs
Normal file
79
Diner/DinerShopImplement/Models/Snack.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using DinerContracts.BindingModels;
|
||||
using DinerContracts.ViewModels;
|
||||
using DinerDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.XPath;
|
||||
|
||||
namespace DinerFileImplement.Models
|
||||
{
|
||||
internal class Snack : ISnackModel
|
||||
{
|
||||
public string ProductName { get; private set; } = string.Empty;
|
||||
|
||||
public double Price { get; private set; }
|
||||
|
||||
public Dictionary<int, int> Components { get; private set; } = new();
|
||||
private Dictionary<int, (IFoodModel, int)>? _productComponents = null;
|
||||
public Dictionary<int, (IFoodModel, int)> ProductComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_productComponents == null) {
|
||||
}
|
||||
return _productComponents;
|
||||
}
|
||||
}
|
||||
public int ID { get; private set; }
|
||||
|
||||
public static Snack? Create(SnackBindingModel? model)
|
||||
{
|
||||
if (model == null) return null;
|
||||
return new Snack()
|
||||
{
|
||||
ID = model.ID,
|
||||
ProductName = model.ProductName,
|
||||
Price = model.Price,
|
||||
Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
|
||||
};
|
||||
}
|
||||
public static Snack? Create(XElement element)
|
||||
{
|
||||
if (element == null) return null;
|
||||
return new Snack()
|
||||
{
|
||||
ID = Convert.ToInt32(element.Attribute("ID")!.Value),
|
||||
ProductName = element.Element("ProductName")!.Value,
|
||||
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
||||
Components = element.Element("ProductComponents")!.Elements("ProductComponent")
|
||||
.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
|
||||
};
|
||||
}
|
||||
public void Update(SnackBindingModel? model)
|
||||
{
|
||||
if (model == null) return;
|
||||
ProductName = model.ProductName;
|
||||
Price = model.Price;
|
||||
Components = model.ProductComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||||
_productComponents = null;
|
||||
}
|
||||
public SnackViewModel GetViewModel => new()
|
||||
{
|
||||
ID = ID,
|
||||
ProductName = ProductName,
|
||||
Price = Price,
|
||||
ProductComponents = ProductComponents
|
||||
};
|
||||
public XElement GetXElement => new("Sncak", new XAttribute("ID", ID),
|
||||
new XElement("ProductName", ProductName),
|
||||
new XElement("Price", Price.ToString()),
|
||||
new XElement("ProductComponents", Components.Select(x => new XElement("ProductComponent",
|
||||
new XElement("Key", x.Key),
|
||||
new XElement("Value", x.Value))).ToArray()));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user