diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index 2c453a2..dcd8aca 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -7,11 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Confectionery", "Confection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryDataModels", "ConfectioneryDataModels\ConfectioneryDataModels.csproj", "{22A053DC-3DC2-4375-9563-BF647A1092A4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{6C4950D1-D788-4D5A-8C15-0A376274F2DD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryContracts", "ConfectioneryContracts\ConfectioneryContracts.csproj", "{6C4950D1-D788-4D5A-8C15-0A376274F2DD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{30F4AE5A-2C90-4C7F-BBE9-2FA56BE26C63}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic", "ConfectioneryBusinessLogic\ConfectioneryBusinessLogic.csproj", "{30F4AE5A-2C90-4C7F-BBE9-2FA56BE26C63}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{85992074-656D-4F79-AB23-60DE4F9ABF30}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{85992074-656D-4F79-AB23-60DE4F9ABF30}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplements\ConfectioneryFileImplement.csproj", "{48A8592F-EF58-4FC4-9C9E-B401836FF21E}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -39,6 +41,10 @@ Global {85992074-656D-4F79-AB23-60DE4F9ABF30}.Debug|Any CPU.Build.0 = Debug|Any CPU {85992074-656D-4F79-AB23-60DE4F9ABF30}.Release|Any CPU.ActiveCfg = Release|Any CPU {85992074-656D-4F79-AB23-60DE4F9ABF30}.Release|Any CPU.Build.0 = Release|Any CPU + {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/Confectionery/Confectionery.csproj b/Confectionery/Confectionery/Confectionery.csproj index 0e7fb26..09d809e 100644 --- a/Confectionery/Confectionery/Confectionery.csproj +++ b/Confectionery/Confectionery/Confectionery.csproj @@ -31,6 +31,7 @@ + diff --git a/Confectionery/Confectionery/Program.cs b/Confectionery/Confectionery/Program.cs index d812df3..03d458e 100644 --- a/Confectionery/Confectionery/Program.cs +++ b/Confectionery/Confectionery/Program.cs @@ -1,7 +1,7 @@ using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; -using ConfectioneryListImplement.Implements; +using ConfectioneryFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/Confectionery/ConfectioneryFileImplements/ConfectioneryFileImplement.csproj b/Confectionery/ConfectioneryFileImplements/ConfectioneryFileImplement.csproj new file mode 100644 index 0000000..68fca7e --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/ConfectioneryFileImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/Confectionery/ConfectioneryFileImplements/DataFileSingleton.cs b/Confectionery/ConfectioneryFileImplements/DataFileSingleton.cs new file mode 100644 index 0000000..53fc38f --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/DataFileSingleton.cs @@ -0,0 +1,54 @@ +using ConfectioneryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement +{ + public class DataFileSingleton + { + private static DataFileSingleton? instance; + private readonly string IngredientFileName = "Ingredient.xml"; + private readonly string OrderFileName = "Order.xml"; + private readonly string PastryFileName = "Pastry.xml"; + public List Ingredients { get; private set; } + public List Orders { get; private set; } + public List Pastries { 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 SavePastries() => SaveData(Pastries, PastryFileName, "Pastries", x => x.GetXElement); + public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + private DataFileSingleton() + { + Ingredients = LoadData(IngredientFileName, "Ingredient", x => Ingredient.Create(x)!)!; + Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.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); + } + } + } +} diff --git a/Confectionery/ConfectioneryFileImplements/Implements/IngredientStorage.cs b/Confectionery/ConfectioneryFileImplements/Implements/IngredientStorage.cs new file mode 100644 index 0000000..721efda --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/Implements/IngredientStorage.cs @@ -0,0 +1,86 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement.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; + } + } +} diff --git a/Confectionery/ConfectioneryFileImplements/Implements/OrderStorage.cs b/Confectionery/ConfectioneryFileImplements/Implements/OrderStorage.cs new file mode 100644 index 0000000..499f021 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/Implements/OrderStorage.cs @@ -0,0 +1,95 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Orders + .Select(x => AccessPastryStorage(x.GetViewModel)) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessPastryStorage(x.GetViewModel)) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return AccessPastryStorage(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 AccessPastryStorage(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 AccessPastryStorage(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 AccessPastryStorage(element.GetViewModel); + } + return null; + } + + public OrderViewModel AccessPastryStorage(OrderViewModel model) + { + string? pastryName = source.Pastries.FirstOrDefault(x => x.Id == model?.PastryId)?.PastryName; + if (pastryName != null) model.PastryName = pastryName; + return model; + } + } +} diff --git a/Confectionery/ConfectioneryFileImplements/Implements/PastryStorage.cs b/Confectionery/ConfectioneryFileImplements/Implements/PastryStorage.cs new file mode 100644 index 0000000..acda54c --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/Implements/PastryStorage.cs @@ -0,0 +1,89 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + private readonly DataFileSingleton source; + public PastryStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Pastries + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + return source.Pastries + .Where(x => x.PastryName.Contains(model.PastryName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue) + { + return null; + } + return source.Pastries + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public PastryViewModel? Insert(PastryBindingModel model) + { + model.Id = source.Pastries.Count > 0 ? source.Pastries.Max(x => x.Id) + 1 : 1; + var newPastry = Pastry.Create(model); + if (newPastry == null) + { + return null; + } + source.Pastries.Add(newPastry); + source.SavePastries(); + return newPastry.GetViewModel; + } + + public PastryViewModel? Update(PastryBindingModel model) + { + var pastry = source.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (pastry == null) + { + return null; + } + pastry.Update(model); + source.SavePastries(); + return pastry.GetViewModel; + } + + public PastryViewModel? Delete(PastryBindingModel model) + { + var element = source.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Pastries.Remove(element); + source.SavePastries(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryFileImplements/Models/Ingredient.cs b/Confectionery/ConfectioneryFileImplements/Models/Ingredient.cs new file mode 100644 index 0000000..ed8c0bd --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/Models/Ingredient.cs @@ -0,0 +1,62 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.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/Confectionery/ConfectioneryFileImplements/Models/Order.cs b/Confectionery/ConfectioneryFileImplements/Models/Order.cs new file mode 100644 index 0000000..702e13a --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/Models/Order.cs @@ -0,0 +1,91 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + public int PastryId { get; private set; } + public int Count { get; private set; } + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } + + public DateTime DateCreate { get; private set; } + + public DateTime? DateImplement { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PastryId = model.PastryId, + 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), + PastryId = Convert.ToInt32(element.Element("PastryId")!.Value), + Count = Convert.ToInt32(element.Element("Count")!.Value), + Sum = Convert.ToDouble(element.Element("Sum")!.Value), + Status = (OrderStatus) Convert.ToInt32(element.Element("Status")!.Value), + DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value), + DateImplement = Convert.ToDateTime(String.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : element.Element("DateImplement")!.Value) + }; + } + + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + if (model.DateImplement.HasValue) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("PastryId", PastryId), + new XElement("Count", Count.ToString()), + new XElement("Sum", Sum.ToString()), + new XElement("Status", ((int)Status).ToString()), + new XElement("DateCreate", DateCreate.ToString()), + new XElement("DateImplement", DateImplement.ToString())); + } +} diff --git a/Confectionery/ConfectioneryFileImplements/Models/Pastry.cs b/Confectionery/ConfectioneryFileImplements/Models/Pastry.cs new file mode 100644 index 0000000..2bb7093 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplements/Models/Pastry.cs @@ -0,0 +1,89 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.Models +{ + public class Pastry + { + public int Id { get; private set; } + public string PastryName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary Ingredients { get; private set; } = new(); + private Dictionary? _pastryIngredients = null; + public Dictionary PastryIngredients + { + get + { + if (_pastryIngredients == null) + { + var source = DataFileSingleton.GetInstance(); + _pastryIngredients = Ingredients.ToDictionary(x => x.Key, y => ((source.Ingredients.FirstOrDefault(z => z.Id == y.Key) as IIngredientModel)!, y.Value)); + } + return _pastryIngredients; + } + } + public static Pastry? Create(PastryBindingModel model) + { + if (model == null) + { + return null; + } + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Ingredients = model.PastryIngredients.ToDictionary(x => x.Key, x => x.Value.Item2) + }; + } + + public static Pastry? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Pastry() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + PastryName = element.Element("PastryName")!.Value, + Price = Convert.ToDouble(element.Element("Price")!.Value), + Ingredients = element.Element("PastryIngredients")!.Elements("PastryIngredient").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + }; + } + + public void Update(PastryBindingModel model) + { + if (model == null) + { + return; + } + PastryName = model.PastryName; + Price = model.Price; + Ingredients = model.PastryIngredients.ToDictionary(x => x.Key, x => x.Value.Item2); + _pastryIngredients = null; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryIngredients = PastryIngredients + }; + public XElement GetXElement => new("Pastry", + new XAttribute("Id", Id), + new XElement("PastryName", PastryName), + new XElement("Price", Price.ToString()), + new XElement("PastryIngredients", Ingredients.Select(x => new XElement("PastryIngredient", + new XElement("Key", x.Key), + new XElement("Value", x.Value))) + .ToArray())); + } +}