Реализация хранения данных в файлах - лаб№2.

This commit is contained in:
Артем Харламов 2023-03-20 15:05:31 +04:00
parent 44b015372f
commit f3b58bb66f
16 changed files with 618 additions and 7 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="..\AbstractFoodOrdersContracts\AbstractFoodOrdersContracts.csproj" />
<ProjectReference Include="..\AbstractFoodOrdersDataModels\AbstractFoodOrdersDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,63 @@
using AbstractFoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractFoodOrdersFileImplement
{
internal class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DishFileName = "Dish.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Dish> Dishes { 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 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)!)!;
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,81 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.SearchModels;
using AbstractFoodOrdersContracts.StoragesContracts;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFoodOrdersFileImplement.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,86 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.SearchModels;
using AbstractFoodOrdersContracts.StoragesContracts;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFoodOrdersFileImplement.Implements
{
public class DishStorage : IDishStorage
{
private readonly DataFileSingleton source;
public DishStorage()
{
source = DataFileSingleton.GetInstance();
}
public DishViewModel? Delete(DishBindingModel model)
{
var element = source.Dishes.FirstOrDefault(x => x.Id ==
model.Id);
if (element != null)
{
source.Dishes.Remove(element);
source.SaveDishes();
return element.GetViewModel;
}
return null;
}
public DishViewModel? GetElement(DishSearchModel model)
{
if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue)
{
return null;
}
return source.Dishes.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.DishName) && x.DishName ==model.DishName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<DishViewModel> GetFilteredList(DishSearchModel model)
{
if (string.IsNullOrEmpty(model.DishName))
{
return new();
}
return source.Dishes.Where(x => x.DishName.Contains(model.DishName)).Select(x => x.GetViewModel).ToList();
}
public List<DishViewModel> GetFullList()
{
return source.Dishes.Select(x => x.GetViewModel).ToList();
}
public DishViewModel? Insert(DishBindingModel model)
{
model.Id = source.Dishes.Count > 0 ? source.Dishes.Max(x =>
x.Id) + 1 : 1;
var newComponent = Dish.Create(model);
if (newComponent == null)
{
return null;
}
source.Dishes.Add(newComponent);
source.SaveDishes();
return newComponent.GetViewModel;
}
public DishViewModel? Update(DishBindingModel model)
{
var component = source.Dishes.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveDishes();
return component.GetViewModel;
}
}
}

View File

@ -0,0 +1,110 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.SearchModels;
using AbstractFoodOrdersContracts.StoragesContracts;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFoodOrdersFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => GetViewModel(x)).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).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;
}
//для загрузки названий изделия в заказе
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var manufacture = source.Dishes.FirstOrDefault(x => x.Id == order.DishId);
if (manufacture != null)
{
viewModel.DishName = manufacture.DishName;
}
return viewModel;
}
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 GetViewModel(newOrder);
}
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 GetViewModel(order);
}
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 GetViewModel(element);
}
return null;
}
}
}

View File

@ -0,0 +1,60 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersDataModels.Models;
using System.Xml.Linq;
namespace AbstractFoodOrdersFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
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)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
public XElement GetXElement => new("Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -0,0 +1,100 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractFoodOrdersFileImplement.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; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _dishComponents =
null;
public Dictionary<int, (IComponentModel, int)> DishComponents
{
get
{
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));
}
return _dishComponents;
}
}
public static Dish? Create(DishBindingModel model)
{
if (model == null)
{
return null;
}
return new Dish()
{
Id = model.Id,
DishName = model.DishName,
Price = model.Price,
Components = model.DishComponents.ToDictionary(x => x.Key, x
=> x.Value.Item2)
};
}
public static Dish? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Dish()
{
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))
};
}
public void Update(DishBindingModel model)
{
if (model == null)
{
return;
}
DishName = model.DishName;
Price = model.Price;
Components = model.DishComponents.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_dishComponents = null;
}
public DishViewModel GetViewModel => new()
{
Id = Id,
DishName = DishName,
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()));
}
}

View File

@ -0,0 +1,92 @@
using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.ViewModels;
using AbstractFoodOrdersDataModels.Enums;
using AbstractFoodOrdersDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace AbstractFoodOrdersFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int DishId { 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 static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
DishId = model.DishId,
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),
DishId = Convert.ToInt32(element.Element("DishId")!.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 = 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;
}
DishId = model.DishId;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
DishId = DishId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("DishId", DishId),
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

@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersBusinessL
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersListImplement", "AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj", "{275ED134-536F-4025-BE10-2718F66FBD10}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFoodOrdersListImplement", "AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj", "{275ED134-536F-4025-BE10-2718F66FBD10}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AbstractFoodOrdersFileImplement", "AbstractShopFileImplement\AbstractFoodOrdersFileImplement.csproj", "{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,10 @@ Global
{275ED134-536F-4025-BE10-2718F66FBD10}.Debug|Any CPU.Build.0 = Debug|Any CPU {275ED134-536F-4025-BE10-2718F66FBD10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{275ED134-536F-4025-BE10-2718F66FBD10}.Release|Any CPU.ActiveCfg = Release|Any CPU {275ED134-536F-4025-BE10-2718F66FBD10}.Release|Any CPU.ActiveCfg = Release|Any CPU
{275ED134-536F-4025-BE10-2718F66FBD10}.Release|Any CPU.Build.0 = Release|Any CPU {275ED134-536F-4025-BE10-2718F66FBD10}.Release|Any CPU.Build.0 = Release|Any CPU
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0FC0C67-FFF7-4D2A-9194-A1466C82EDAB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -18,6 +18,7 @@
<ProjectReference Include="..\AbstractFoodOrdersContracts\AbstractFoodOrdersContracts.csproj" /> <ProjectReference Include="..\AbstractFoodOrdersContracts\AbstractFoodOrdersContracts.csproj" />
<ProjectReference Include="..\AbstractFoodOrdersDataModels\AbstractFoodOrdersDataModels.csproj" /> <ProjectReference Include="..\AbstractFoodOrdersDataModels\AbstractFoodOrdersDataModels.csproj" />
<ProjectReference Include="..\AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj" /> <ProjectReference Include="..\AbstractFoodOrdersListImplement\AbstractFoodOrdersListImplement.csproj" />
<ProjectReference Include="..\AbstractShopFileImplement\AbstractFoodOrdersFileImplement.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,4 @@
namespace AbstractFoodOrdersView namespace FoodOrders
{ {
partial class FormComponent partial class FormComponent
{ {

View File

@ -12,7 +12,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace AbstractFoodOrdersView namespace FoodOrders
{ {
public partial class FormComponent : Form public partial class FormComponent : Form
{ {

View File

@ -1,4 +1,4 @@
namespace AbstractFoodOrdersView namespace FoodOrders
{ {
partial class FormComponents partial class FormComponents
{ {

View File

@ -12,7 +12,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
namespace AbstractFoodOrdersView namespace FoodOrders
{ {
public partial class FormComponents : Form public partial class FormComponents : Form
{ {

View File

@ -1,6 +1,5 @@
using AbstractFoodOrdersContracts.BindingModels; using AbstractFoodOrdersContracts.BindingModels;
using AbstractFoodOrdersContracts.BusinessLogicsContracts; using AbstractFoodOrdersContracts.BusinessLogicsContracts;
using AbstractFoodOrdersView;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;

View File

@ -1,8 +1,7 @@
using AbstractFoodOrdersBusinessLogic.BusinessLogics; using AbstractFoodOrdersBusinessLogic.BusinessLogics;
using AbstractFoodOrdersContracts.BusinessLogicsContracts; using AbstractFoodOrdersContracts.BusinessLogicsContracts;
using AbstractFoodOrdersContracts.StoragesContracts; using AbstractFoodOrdersContracts.StoragesContracts;
using AbstractFoodOrdersListImplement.Implements; using AbstractFoodOrdersFileImplement.Implements;
using AbstractFoodOrdersView;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging; using NLog.Extensions.Logging;