This commit is contained in:
Данила 2023-04-04 12:54:46 +04:00
parent 1004237bb7
commit 87fbd6f7ae
10 changed files with 415 additions and 15 deletions

View File

@ -0,0 +1,61 @@
using FoodOrderFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FoodOrderFileImplement
{
public class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string FoodFileName = "Food.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Food> Foods { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveFoods() => SaveData(Foods, FoodFileName, "Foods", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Foods = LoadData(FoodFileName, "Food", x => Food.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,91 @@
using FoodOrderFileImplement.Models;
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoodOrderFileImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
private readonly DataFileSingleton source;
public ComponentStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return source.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
return source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
return source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = source.Components.Count > 0 ? source.Components.Max(x => x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
source.Components.Add(newComponent);
source.SaveComponents();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveComponents();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Components.Remove(element);
source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,99 @@
using FoodOrderFileImplement.Models;
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoodOrderFileImplement.Implements
{
public class FoodStorage : IFoodStorage
{
private readonly DataFileSingleton source;
public FoodStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<FoodViewModel> GetFullList()
{
return source.Foods.Select(x => x.GetViewModel).ToList();
}
public List<FoodViewModel> GetFilteredList(FoodSearchModel model)
{
if (string.IsNullOrEmpty(model.FoodName))
{
return new();
}
return source.Foods.Where(x => x.FoodName.Contains(model.FoodName)).Select(x => x.GetViewModel).ToList();
}
public FoodViewModel? GetElement(FoodSearchModel model)
{
if (string.IsNullOrEmpty(model.FoodName) && !model.Id.HasValue)
{
return null;
}
return source.Foods.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FoodName) && x.FoodName == model.FoodName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public FoodViewModel? Insert(FoodBindingModel model)
{
model.Id = source.Foods.Count > 0 ? source.Foods.Max(x => x.Id) + 1 : 1;
var newFood = Food.Create(model);
if (newFood == null)
{
return null;
}
source.Foods.Add(newFood);
source.SaveFoods();
return newFood.GetViewModel;
}
public FoodViewModel? Update(FoodBindingModel model)
{
var food = source.Foods.FirstOrDefault(x => x.Id == model.Id);
if (food == null)
{
return null;
}
food.Update(model);
source.SaveFoods();
return food.GetViewModel;
}
public FoodViewModel? Delete(FoodBindingModel model)
{
var element = source.Foods.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Foods.Remove(element);
source.SaveFoods();
return element.GetViewModel;
}
return null;
}
List<FoodViewModel> IFoodStorage.GetFullList()
{
throw new NotImplementedException();
}
}
}

View File

@ -0,0 +1,94 @@
using FoodOrderFileImplement.Models;
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoodOrderFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
source.Orders.Add(newOrder);
source.SaveOrders();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
source.SaveOrders();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var element = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Orders.Remove(element);
source.SaveOrders();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -10,7 +10,7 @@ using System.Xml.Linq;
namespace FoodOrderFileImplement.Models
{
internal class Component : IComponentModel
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;

View File

@ -16,14 +16,25 @@ namespace FoodOrderFileImplement.Models
public int Id { get; private set; }
public string FoodName { 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)> _foodComponents = null;
public Dictionary<int, (IComponentModel, int)> _foodComponents = null;
public Dictionary<int, (IComponentModel, int)> FoodComponents
{
get;
private set;
} = new Dictionary<int, (IComponentModel, int)>();
public static Food? Create(FoodBindingModel? model)
get
{
if (_foodComponents == null)
{
var source = DataFileSingleton.GetInstance();
_foodComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _foodComponents;
}
}
public static Food? Create(FoodBindingModel model)
{
if (model == null)
{
@ -34,12 +45,14 @@ namespace FoodOrderFileImplement.Models
Id = model.Id,
FoodName = model.FoodName,
Price = model.Price,
Components = model.FoodComponents.ToDictionary(x=>x.Key, x=>x.Value.Item2)
Components = model.FoodComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Food? Create(XElement element)
{
if(element == null)
if (element == null)
{
return null;
}
@ -51,7 +64,8 @@ namespace FoodOrderFileImplement.Models
Components = element.Element("FoodComponents")!.Elements("FoodComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(FoodBindingModel? model)
public void Update(FoodBindingModel model)
{
if (model == null)
{
@ -59,8 +73,10 @@ namespace FoodOrderFileImplement.Models
}
FoodName = model.FoodName;
Price = model.Price;
FoodComponents = model.FoodComponents;
Components = model.FoodComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_foodComponents = null;
}
public FoodViewModel GetViewModel => new()
{
Id = Id,
@ -68,5 +84,14 @@ namespace FoodOrderFileImplement.Models
Price = Price,
FoodComponents = FoodComponents
};
public XElement GetXElement => new("Food",
new XAttribute("Id", Id),
new XElement("FoodName", FoodName),
new XElement("Price", Price.ToString()),
new XElement("FoodComponents", Components.Select(x =>
new XElement("FoodComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()));
}
}

View File

@ -12,7 +12,7 @@ using System.Xml.Linq;
namespace FoodOrderFileImplement.Models
{
internal class Order : IOrderModel
public class Order : IOrderModel
{
public int Id { get; private set; }
public int FoodId { get; private set; }
@ -43,7 +43,26 @@ namespace FoodOrderFileImplement.Models
}
public static Order? Create(XElement element)
{
return new Order();
if (element == null)
{
return null;
}
var order = new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
FoodId = Convert.ToInt32(element.Element("FoodId")!.Value),
FoodName = element.Element("FoodName")!.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 = DateTime.ParseExact(element.Element("DateCreate")!.Value, "G", null)
};
DateTime.TryParse(element.Element("DateImplement")!.Value, out DateTime dateImpl);
order.DateImplement = dateImpl;
return order;
}
public void Update(OrderBindingModel? model)
{
@ -71,6 +90,14 @@ namespace FoodOrderFileImplement.Models
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new("Order");
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("FoodName", FoodName),
new XElement("FoodId", FoodId.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

@ -14,9 +14,9 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FoodOrderFileImplement\FoodOrderFileImplement.csproj" />
<ProjectReference Include="..\FoodOrdersBusinessLogic\FoodOrderBusinessLogic.csproj" />
<ProjectReference Include="..\FoodOrdersContracts\FoodOrdersContracts.csproj" />
<ProjectReference Include="..\FoodOrdersListImplement\FoodOrdersListImplement.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -60,4 +60,7 @@
<metadata name="menuStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>25</value>
</metadata>
</root>

View File

@ -2,7 +2,7 @@ using FoodOrdersBusinessLogic.BusinessLogic;
using FoodOrdersBusinessLogic.BusinessLogics;
using FoodOrdersContracts.BusinessLogicsContracts;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersListImplement.Implements;
using FoodOrderFileImplement.Implements;
using FoodOrdersView;
using FoodOrdersView;
using Microsoft.Extensions.DependencyInjection;