fix
This commit is contained in:
parent
c4d28586d0
commit
4b938e90ca
@ -19,37 +19,28 @@ namespace FoodOrdersFileImplement
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
public void SaveComponents() => SaveData(Components, ComponentFileName,
|
||||
"Components", x => x.GetXElement);
|
||||
public void SaveDishes() => SaveData(Dishes, DishFileName,
|
||||
"Dishes", x => x.GetXElement);
|
||||
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x
|
||||
=> x.GetXElement);
|
||||
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
|
||||
public void SaveDishes() => SaveData(Dishes, DishFileName, "Dishes", x => x.GetXElement);
|
||||
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
|
||||
private DataFileSingleton()
|
||||
{
|
||||
Components = LoadData(ComponentFileName, "Component", x =>
|
||||
Component.Create(x)!)!;
|
||||
Dishes = LoadData(DishFileName, "Dish", x =>
|
||||
Dish.Create(x)!)!;
|
||||
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
|
||||
Dishes = LoadData(DishFileName, "Dish", x => Dish.Create(x)!)!;
|
||||
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
|
||||
}
|
||||
private static List<T>? LoadData<T>(string filename, string xmlNodeName,
|
||||
Func<XElement, T> selectFunction)
|
||||
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 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)
|
||||
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);
|
||||
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,7 @@ namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
return _source.Components.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel
|
||||
model)
|
||||
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ComponentName))
|
||||
{
|
||||
@ -27,6 +26,8 @@ namespace FoodOrdersFileImplement.Implements
|
||||
}
|
||||
return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
//FirstOrDefault выбирается первый или ничего, то есть вернёт первое совпадение или null
|
||||
public ComponentViewModel? GetElement(ComponentSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
|
||||
@ -37,8 +38,7 @@ namespace FoodOrdersFileImplement.Implements
|
||||
}
|
||||
public ComponentViewModel? Insert(ComponentBindingModel model)
|
||||
{
|
||||
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x =>
|
||||
x.Id) + 1 : 1;
|
||||
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1;
|
||||
var newComponent = Component.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
@ -50,8 +50,7 @@ namespace FoodOrdersFileImplement.Implements
|
||||
}
|
||||
public ComponentViewModel? Update(ComponentBindingModel model)
|
||||
{
|
||||
var component = _source.Components.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
var component = _source.Components.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
@ -62,8 +61,7 @@ namespace FoodOrdersFileImplement.Implements
|
||||
}
|
||||
public ComponentViewModel? Delete(ComponentBindingModel model)
|
||||
{
|
||||
var element = _source.Components.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
var element = _source.Components.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
_source.Components.Remove(element);
|
||||
|
@ -32,8 +32,7 @@ namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _source.Dishes
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
return _source.Dishes.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
public DishViewModel? Insert(DishBindingModel model)
|
||||
{
|
||||
|
@ -32,8 +32,7 @@ namespace FoodOrdersFileImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _source.Orders
|
||||
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
return _source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
|
||||
private OrderViewModel GetViewModel(Order order)
|
||||
|
@ -50,9 +50,11 @@ namespace FoodOrdersFileImplement.Models
|
||||
ComponentName = ComponentName,
|
||||
Cost = Cost
|
||||
};
|
||||
public XElement GetXElement => new("Component",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("ComponentName", ComponentName),
|
||||
new XElement("Cost", Cost.ToString()));
|
||||
public XElement GetXElement => new(
|
||||
"Component",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("ComponentName", ComponentName),
|
||||
new XElement("Cost", Cost.ToString())
|
||||
);
|
||||
}
|
||||
}
|
@ -8,11 +8,16 @@ namespace FoodOrdersFileImplement.Models
|
||||
public class Dish : IDishModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
|
||||
public string DishName { get; private set; } = string.Empty;
|
||||
|
||||
public double Price { get; private set; }
|
||||
|
||||
//словарь для файла, так как нам в файле нужно хранить просто id компонента и его количество
|
||||
public Dictionary<int, int> Components { get; private set; } = new();
|
||||
private Dictionary<int, (IComponentModel, int)>? _dishComponents =
|
||||
null;
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
|
||||
|
||||
public Dictionary<int, (IComponentModel, int)> DishComponents
|
||||
{
|
||||
get
|
||||
@ -20,9 +25,7 @@ namespace FoodOrdersFileImplement.Models
|
||||
if (_dishComponents == null)
|
||||
{
|
||||
var _source = DataFileSingleton.GetInstance();
|
||||
_dishComponents = Components.ToDictionary(x => x.Key, y =>
|
||||
((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!,
|
||||
y.Value));
|
||||
_dishComponents = Components.ToDictionary(x => x.Key, y => ((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
|
||||
}
|
||||
return _dishComponents;
|
||||
}
|
||||
@ -38,8 +41,7 @@ namespace FoodOrdersFileImplement.Models
|
||||
Id = model.Id,
|
||||
DishName = model.DishName,
|
||||
Price = model.Price,
|
||||
Components = model.DishComponents.ToDictionary(x => x.Key, x
|
||||
=> x.Value.Item2)
|
||||
Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
|
||||
};
|
||||
}
|
||||
public static Dish? Create(XElement element)
|
||||
@ -53,11 +55,7 @@ namespace FoodOrdersFileImplement.Models
|
||||
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
|
||||
DishName = element.Element("DishName")!.Value,
|
||||
Price = Convert.ToDouble(element.Element("Price")!.Value),
|
||||
Components =
|
||||
element.Element("DishComponents")!.Elements("DishComponent")
|
||||
.ToDictionary(x =>
|
||||
Convert.ToInt32(x.Element("Key")?.Value), x =>
|
||||
Convert.ToInt32(x.Element("Value")?.Value))
|
||||
Components = element.Element("DishComponents")!.Elements("DishComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
|
||||
};
|
||||
}
|
||||
public void Update(DishBindingModel model)
|
||||
@ -68,8 +66,9 @@ namespace FoodOrdersFileImplement.Models
|
||||
}
|
||||
DishName = model.DishName;
|
||||
Price = model.Price;
|
||||
Components = model.DishComponents.ToDictionary(x => x.Key, x =>
|
||||
x.Value.Item2);
|
||||
Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
|
||||
//обнуляем словарь, чтобы в случае обновления, у нас был в дальнейшем сформирован актуальный словарь
|
||||
// с помощью get метода
|
||||
_dishComponents = null;
|
||||
}
|
||||
public DishViewModel GetViewModel => new()
|
||||
@ -79,17 +78,17 @@ namespace FoodOrdersFileImplement.Models
|
||||
Price = Price,
|
||||
DishComponents = DishComponents
|
||||
};
|
||||
public XElement GetXElement => new("Dish",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("DishName", DishName),
|
||||
new XElement("Price", Price.ToString()),
|
||||
new XElement("DishComponents", Components.Select(x =>
|
||||
new XElement("DishComponent",
|
||||
|
||||
new XElement("Key", x.Key),
|
||||
|
||||
new XElement("Value", x.Value)))
|
||||
|
||||
.ToArray()));
|
||||
public XElement GetXElement => new(
|
||||
"Dish",
|
||||
new XAttribute("Id", Id),
|
||||
new XElement("DishName", DishName),
|
||||
new XElement("Price", Price.ToString()),
|
||||
new XElement("DishComponents", Components.Select(x =>
|
||||
new XElement("DishComponent",
|
||||
new XElement("Key", x.Key),
|
||||
new XElement("Value", x.Value))).ToArray()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user