From abc25205ce1dee24639a73fb23e5cedd89e4bbca Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:10:44 +0400 Subject: [PATCH] step 1 --- .../DataFileSingleton.cs | 9 +- .../Implements/ShopStorage.cs | 79 ++++++++++++++++ .../PizzeriaShopFileImplement/models/Shop.cs | 93 +++++++++++++++++++ 3 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs create mode 100644 Pizzeria/PizzeriaShopFileImplement/models/Shop.cs diff --git a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs index d09c25a..3e309f1 100644 --- a/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs +++ b/Pizzeria/PizzeriaShopFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace PizzeriaFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PizzaFileName = "Pizza.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pizzas { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -25,10 +27,9 @@ namespace PizzeriaFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); private DataFileSingleton() { - Components = LoadData(ComponentFileName, "Component", x => - Component.Create(x)!)!; - Pizzas = LoadData(PizzaFileName, "Pizza", x => - Pizza.Create(x)!)!; + Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; + Pizzas = LoadData(PizzaFileName, "Pizza", x => Pizza.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..8605fca --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/Implements/ShopStorage.cs @@ -0,0 +1,79 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.StoragesContracts; +using PizzeriaContracts.ViewModels; +using PizzeriaFileImplement.models; + +namespace PizzeriaFileImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton source; + public ShopStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Shops + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + return source.Shops + .Where(x => x.ShopName.Contains(model.ShopName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + return source.Shops + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1; + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + source.Shops.Add(newShop); + source.SaveData(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + source.SaveData(); + return shop.GetViewModel; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + var element = source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Shops.Remove(element); + source.SaveData(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs new file mode 100644 index 0000000..0090f9d --- /dev/null +++ b/Pizzeria/PizzeriaShopFileImplement/models/Shop.cs @@ -0,0 +1,93 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.ViewModels; +using PizzeriaDataModels; +using System.Xml.Linq; + +namespace PizzeriaFileImplement.models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + public string ShopName { get; set; } = string.Empty; + public string Addres { get; set; } = string.Empty; + public DateTime OpenTime { get; set; } + public Dictionary Pizzas { get; private set; } = new(); + private Dictionary? _shopPizzas = null; + public Dictionary ShopPizzas + { + get + { + if (_shopPizzas == null) + { + var source = DataFileSingleton.GetInstance(); + _shopPizzas = Pizzas.ToDictionary(x => x.Key, y => + ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!, + y.Value)); + } + return _shopPizzas; + } + } + + public static Shop? Create(ShopBindingModel model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Addres = model.Addres, + OpenTime = model.OpenTime, + Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Shop() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ShopName = element.Element("ShopName")!.Value, + Addres = element.Element("Price")!.Value, + Pizzas = element.Element("ShopPizzas")!.Elements("ShopPizza") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + public void Update(ShopBindingModel model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + Addres = model.Addres; + OpenTime = model.OpenTime; + Pizzas = model.ShopPizzas.ToDictionary(x => x.Key, x => x.Value.Item2); + _shopPizzas = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Addres = Addres, + OpenTime = OpenTime, + ShopPizzas = ShopPizzas + }; + public XElement GetXElement => new("Shop", + new XAttribute("Id", Id), + new XElement("ShopName", ShopName), + new XElement("Addres", Addres), + new XElement("ShopPizzas", Pizzas.Select(x => + new XElement("ShopPizza", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +}