diff --git a/ConfectionaryFileImplement/ComponentStorage.cs b/ConfectionaryFileImplement/ComponentStorage.cs new file mode 100644 index 0000000..2293b9a --- /dev/null +++ b/ConfectionaryFileImplement/ComponentStorage.cs @@ -0,0 +1,79 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; + +namespace ConfectioneryFileImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataFileSingleton _source; + public ComponentStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return _source.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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; + } + } +} \ No newline at end of file diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs index 27c597d..71de12c 100644 --- a/ConfectionaryFileImplement/DataFileSingleton.cs +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -11,7 +11,8 @@ namespace ConfectioneryFileImplement private readonly string PastryFileName = "Pastry.xml"; public List Components { get; private set; } public List Orders { get; private set; } - public List Pastrys { get; private set; } + public List Pastries { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -21,14 +22,14 @@ namespace ConfectioneryFileImplement return instance; } public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); - public void SavePastrys() => SaveData(Pastrys, PastryFileName, "Pastrys", x => x.GetXElement); - public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", 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() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; - Pastrys = LoadData(PastryFileName, "Pastry", x => + Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; } @@ -44,8 +45,7 @@ namespace ConfectioneryFileImplement { if (data != null) { - new XDocument(new XElement(xmlNodeName, - data.Select(selectFunction).ToArray())).Save(filename); + new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename); } } } diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs new file mode 100644 index 0000000..5b79348 --- /dev/null +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement +{ + internal class OrderStorage + { + } +} diff --git a/ConfectionaryFileImplement/PastryStorage.cs b/ConfectionaryFileImplement/PastryStorage.cs new file mode 100644 index 0000000..9fbdedd --- /dev/null +++ b/ConfectionaryFileImplement/PastryStorage.cs @@ -0,0 +1,81 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement +{ + public class PastryStorage : IPastryStorage + { + private readonly DataFileSingleton _source; + public PastryStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public PastryViewModel? Delete(PastryBindingModel model) + { + var element = _source.Pastries.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + _source.Pastries.Remove(element); + _source.Savepastrys(); + return element.GetViewModel; + } + return null; + } + + 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 List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + return _source.Pastries + .Select(x => x.GetViewModel) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList(); + } + + public List GetFullList() + { + return _source.Pastries + .Select(x => x.GetViewModel) + .ToList(); + } + + public PastryViewModel? Insert(PastryBindingModel model) + { + throw new NotImplementedException(); + } + + 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; + } + } +}