This commit is contained in:
Игорь Гордеев 2024-03-01 21:57:02 +04:00
parent 4bff46b210
commit c748b420a1
8 changed files with 531 additions and 0 deletions

View File

@ -0,0 +1,53 @@
using SushiBarFileImplement.Models;
using System.Xml.Linq;
namespace SushiBarFileImplement
{
public class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string IngredientFileName = "Ingredient.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string SushiFileName = "Sushi.xml";
public List<Ingredient> Ingredients { get; private set; }
public List<Order> Orders { get; private set; }
public List<Sushi> ListSushi { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveIngredients() => SaveData(Ingredients, IngredientFileName,
"Ingredients", x => x.GetXElement);
public void SaveListSushi() => SaveData(ListSushi, SushiFileName,
"ListSushi", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
private DataFileSingleton()
{
Ingredients = LoadData(IngredientFileName, "Ingredient", x => Ingredient.Create(x)!)!;
ListSushi = LoadData(SushiFileName, "Sushi", x => Sushi.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,74 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
namespace SushiBarFileImplement.Implements
{
public class IngredientStorage : IIngredientStorage
{
private readonly DataFileSingleton source;
public IngredientStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<IngredientViewModel> GetFullList()
{
return source.Ingredients.Select(x => x.GetViewModel).ToList();
}
public List<IngredientViewModel> GetFilteredList(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName))
{
return new();
}
return source.Ingredients.Where(x =>
x.IngredientName.Contains(model.IngredientName)).Select(x => x.GetViewModel).ToList();
}
public IngredientViewModel? GetElement(IngredientSearchModel model)
{
if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue)
{
return null;
}
return source.Ingredients.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName ==
model.IngredientName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
}
public IngredientViewModel? Insert(IngredientBindingModel model)
{
model.Id = source.Ingredients.Count > 0 ? source.Ingredients.Max(x => x.Id) + 1 : 1;
var newIngredient = Ingredient.Create(model);
if (newIngredient == null)
{
return null;
}
source.Ingredients.Add(newIngredient);
source.SaveIngredients();
return newIngredient.GetViewModel;
}
public IngredientViewModel? Update(IngredientBindingModel model)
{
var ingredient = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (ingredient == null)
{
return null;
}
ingredient.Update(model);
source.SaveIngredients();
return ingredient.GetViewModel;
}
public IngredientViewModel? Delete(IngredientBindingModel model)
{
var element = source.Ingredients.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Ingredients.Remove(element);
source.SaveIngredients();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,91 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
namespace SushiBarFileImplement.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.Equals(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;
foreach (var sushi in source.ListSushi)
{
if (sushi.Id == order.SushiId)
{
viewModel.SushiName = sushi.SushiName;
break;
}
}
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,74 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarFileImplement.Models;
namespace SushiBarFileImplement.Implements
{
public class SushiStorage : ISushiStorage
{
private readonly DataFileSingleton source;
public SushiStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<SushiViewModel> GetFullList()
{
return source.ListSushi.Select(x => x.GetViewModel).ToList();
}
public List<SushiViewModel> GetFilteredList(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName))
{
return new();
}
return source.ListSushi.Where(x =>
x.SushiName.Contains(model.SushiName)).Select(x => x.GetViewModel).ToList();
}
public SushiViewModel? GetElement(SushiSearchModel model)
{
if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue)
{
return null;
}
return source.ListSushi.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SushiName) && x.SushiName ==
model.SushiName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public SushiViewModel? Insert(SushiBindingModel model)
{
model.Id = source.ListSushi.Count > 0 ? source.ListSushi.Max(x => x.Id) + 1 : 1;
var newSushi = Sushi.Create(model);
if (newSushi == null)
{
return null;
}
source.ListSushi.Add(newSushi);
source.SaveListSushi();
return newSushi.GetViewModel;
}
public SushiViewModel? Update(SushiBindingModel model)
{
var sushi = source.ListSushi.FirstOrDefault(x => x.Id == model.Id);
if (sushi == null)
{
return null;
}
sushi.Update(model);
source.SaveListSushi();
return sushi.GetViewModel;
}
public SushiViewModel? Delete(SushiBindingModel model)
{
var element = source.ListSushi.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.ListSushi.Remove(element);
source.SaveListSushi();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,59 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
public class Ingredient : IIngredientModel
{
public int Id { get; private set; }
public string IngredientName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static Ingredient? Create(IngredientBindingModel model)
{
if (model == null)
{
return null;
}
return new Ingredient()
{
Id = model.Id,
IngredientName = model.IngredientName,
Cost = model.Cost
};
}
public static Ingredient? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Ingredient()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
IngredientName = element.Element("IngredientName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(IngredientBindingModel model)
{
if (model == null)
{
return;
}
IngredientName = model.IngredientName;
Cost = model.Cost;
}
public IngredientViewModel GetViewModel => new()
{
Id = Id,
IngredientName = IngredientName,
Cost = Cost
};
public XElement GetXElement => new("Ingredient",
new XAttribute("Id", Id),
new XElement("IngredientName", IngredientName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -0,0 +1,84 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums;
using SushiBarDataModels.Models;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int SushiId { 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,
SushiId = model.SushiId,
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),
SushiId = Convert.ToInt32(element.Element("SushiId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.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;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
SushiId = SushiId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("SushiId", SushiId),
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

@ -0,0 +1,82 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Models;
using System.Xml.Linq;
namespace SushiBarFileImplement.Models
{
public class Sushi : ISushiModel
{
public int Id { get; private set; }
public string SushiName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Ingredients { get; private set; } = new();
private Dictionary<int, (IIngredientModel, int)>? _productIngredients = null;
public Dictionary<int, (IIngredientModel, int)> SushiIngredients
{
get
{
if (_productIngredients == null)
{
var source = DataFileSingleton.GetInstance();
_productIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value));
}
return _productIngredients;
}
}
public static Sushi? Create(SushiBindingModel model)
{
if (model == null)
{
return null;
}
return new Sushi()
{
Id = model.Id,
SushiName = model.SushiName,
Price = model.Price,
Ingredients = model.SushiIngredients.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Sushi? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Sushi()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SushiName = element.Element("SushiName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Ingredients = element.Element("SushiIngredients")!.Elements("SushiIngredient").ToDictionary(x =>
Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(SushiBindingModel model)
{
if (model == null)
{
return;
}
SushiName = model.SushiName;
Price = model.Price;
Ingredients = model.SushiIngredients.ToDictionary(x => x.Key, x =>
x.Value.Item2);
_productIngredients = null;
}
public SushiViewModel GetViewModel => new()
{
Id = Id,
SushiName = SushiName,
Price = Price,
SushiIngredients = SushiIngredients
};
public XElement GetXElement => new("Sushi",
new XAttribute("Id", Id),
new XElement("SushiName", SushiName),
new XElement("Price", Price.ToString()),
new XElement("SushiIngredients", Ingredients.Select(x => new XElement("SushiIngredient",
new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}

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="..\SushiBarContracts\SushiBarContracts.csproj" />
<ProjectReference Include="..\SushiBarDataModels\SushiBarDataModels.csproj" />
</ItemGroup>
</Project>