diff --git a/SushiBar/SushiBar.sln b/SushiBar/SushiBar.sln index d25d4a8..6fc6147 100644 --- a/SushiBar/SushiBar.sln +++ b/SushiBar/SushiBar.sln @@ -5,13 +5,15 @@ VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarView", "SushiBar\SushiBarView.csproj", "{021299FA-4932-4367-A9A7-378EB340D8BC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarDataModels", "SushiBarDataModels\SushiBarDataModels.csproj", "{8DD9F541-A4FA-4C0B-8E7D-8525C0D77E1C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarDataModels", "SushiBarDataModels\SushiBarDataModels.csproj", "{8DD9F541-A4FA-4C0B-8E7D-8525C0D77E1C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarContracts", "SushiBarContracts\SushiBarContracts.csproj", "{1B31E006-79D1-4376-AEEB-55AF60806AB1}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarContracts", "SushiBarContracts\SushiBarContracts.csproj", "{1B31E006-79D1-4376-AEEB-55AF60806AB1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarBusinessLogic", "SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{2755BF17-8728-4D7E-A4EE-BC8810C508F6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarBusinessLogic", "SushiBarBusinessLogic\SushiBarBusinessLogic.csproj", "{2755BF17-8728-4D7E-A4EE-BC8810C508F6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarListImplement", "SushiBarListImplement\SushiBarListImplement.csproj", "{81A91405-3721-40EF-A47C-CEBD92C0A4D0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SushiBarListImplement", "SushiBarListImplement\SushiBarListImplement.csproj", "{81A91405-3721-40EF-A47C-CEBD92C0A4D0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SushiBarFileImplement", "SushiBarFileImplement\SushiBarFileImplement.csproj", "{A26B0D9A-31CB-4845-ABC3-D84E60BECE46}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {81A91405-3721-40EF-A47C-CEBD92C0A4D0}.Debug|Any CPU.Build.0 = Debug|Any CPU {81A91405-3721-40EF-A47C-CEBD92C0A4D0}.Release|Any CPU.ActiveCfg = Release|Any CPU {81A91405-3721-40EF-A47C-CEBD92C0A4D0}.Release|Any CPU.Build.0 = Release|Any CPU + {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A26B0D9A-31CB-4845-ABC3-D84E60BECE46}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SushiBar/SushiBar/Program.cs b/SushiBar/SushiBar/Program.cs index 07ff1b6..35f5754 100644 --- a/SushiBar/SushiBar/Program.cs +++ b/SushiBar/SushiBar/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using SushiBarBusinessLogic.BusinessLogics; using SushiBarContracts.BusinessLogicsContracts; using SushiBarContracts.StoragesContracts; -using SushiBarListImplement.Implements; +using SushiBarFileImplement.Implements; using SushiBarView; namespace SushiBar diff --git a/SushiBar/SushiBar/SushiBarView.csproj b/SushiBar/SushiBar/SushiBarView.csproj index dceb0d9..237037b 100644 --- a/SushiBar/SushiBar/SushiBarView.csproj +++ b/SushiBar/SushiBar/SushiBarView.csproj @@ -17,6 +17,7 @@ + diff --git a/SushiBar/SushiBarFileImplement/DataFileSingleton.cs b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs new file mode 100644 index 0000000..7461a1d --- /dev/null +++ b/SushiBar/SushiBarFileImplement/DataFileSingleton.cs @@ -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 Ingredients { get; private set; } + public List Orders { get; private set; } + public List 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? LoadData(string filename, string xmlNodeName, + Func selectFunction) + { + if (File.Exists(filename)) + { + return + XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList(); + } + return new List(); + } + private static void SaveData(List data, string filename, string + xmlNodeName, Func selectFunction) + { + if (data != null) + { + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); + } + } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/Implements/IngredientStorage.cs b/SushiBar/SushiBarFileImplement/Implements/IngredientStorage.cs new file mode 100644 index 0000000..5366cf1 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/IngredientStorage.cs @@ -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 GetFullList() + { + return source.Ingredients.Select(x => x.GetViewModel).ToList(); + } + public List 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; + } + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..b2770d1 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/OrderStorage.cs @@ -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 GetFullList() + { + return source.Orders.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders.Where(x => x.Id.Equals(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; + } + 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 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; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs b/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs new file mode 100644 index 0000000..a4a8add --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Implements/SushiStorage.cs @@ -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 GetFullList() + { + return source.ListSushi.Select(x => x.GetViewModel).ToList(); + } + public List 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; + } + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Ingredient.cs b/SushiBar/SushiBarFileImplement/Models/Ingredient.cs new file mode 100644 index 0000000..d940c5a --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Ingredient.cs @@ -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())); + } +} diff --git a/SushiBar/SushiBarFileImplement/Models/Order.cs b/SushiBar/SushiBarFileImplement/Models/Order.cs new file mode 100644 index 0000000..6431e5b --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Order.cs @@ -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 = 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())); + } +} \ No newline at end of file diff --git a/SushiBar/SushiBarFileImplement/Models/Sushi.cs b/SushiBar/SushiBarFileImplement/Models/Sushi.cs new file mode 100644 index 0000000..d697e76 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/Models/Sushi.cs @@ -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 Ingredients { get; private set; } = new(); + private Dictionary? _productIngredients = null; + public Dictionary 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())); + } +} diff --git a/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj new file mode 100644 index 0000000..00e2c96 --- /dev/null +++ b/SushiBar/SushiBarFileImplement/SushiBarFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + +