немного

This commit is contained in:
dex_moth 2024-02-20 21:52:07 +04:00
parent 30f244c552
commit 5a9a99b1b6
3 changed files with 76 additions and 6 deletions

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FishFactoryListImplement
{
internal class DataFileSingleton1
{
}
}

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FishFactoryListImplement.Models
{
@ -14,11 +15,21 @@ namespace FishFactoryListImplement.Models
public int Id { get; private set; }
public string CannedName { 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)>? _cannedComponents = null;
public Dictionary<int, (IComponentModel, int)> CannedComponents
{
get;
private set;
} = new Dictionary<int, (IComponentModel, int)>();
get
{
if (_cannedComponents == null)
{
var source = DataFileSingleton.GetInstance();
_cannedComponents = Components.ToDictionary(x => x.Key, y =>
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _cannedComponents;
}
}
public static Canned? Create(CannedBindingModel? model)
{
if (model == null)
@ -30,9 +41,26 @@ namespace FishFactoryListImplement.Models
Id = model.Id,
CannedName = model.CannedName,
Price = model.Price,
CannedComponents = model.CannedComponents
Components = model.CannedComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Canned? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Canned()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
CannedName = element.Element("CannedName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("CannedComponents")!.Elements("CannedComponent").ToDictionary
(x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value))};
}
public void Update(CannedBindingModel? model)
{
if (model == null)
@ -41,7 +69,9 @@ namespace FishFactoryListImplement.Models
}
CannedName = model.CannedName;
Price = model.Price;
CannedComponents = model.CannedComponents;
Components = model.CannedComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_cannedComponents = null;
}
public CannedViewModel GetViewModel => new()
{
@ -50,6 +80,13 @@ namespace FishFactoryListImplement.Models
Price = Price,
CannedComponents = CannedComponents
};
public XElement GetXElement => new("Canned",
new XAttribute("Id", Id),
new XElement("CannedName", CannedName),
new XElement("Price", Price.ToString()),
new XElement("CannedComponents", Components.Select(x =>
new XElement("CannedComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -1,6 +1,7 @@
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModel.Models;
using System.Xml.Linq;
namespace FishFactoryListImplement.Models
{
@ -22,6 +23,21 @@ namespace FishFactoryListImplement.Models
Cost = model.Cost
};
}
public static Component? Create(XElement element)
{
if (element == 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 == null)
@ -37,5 +53,10 @@ namespace FishFactoryListImplement.Models
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
}