diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ComponentStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..e2d3450 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,92 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.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; + } + + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/FurnitureStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/FurnitureStorage.cs new file mode 100644 index 0000000..f9d26f2 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/FurnitureStorage.cs @@ -0,0 +1,88 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.Implements +{ + public class FurnitureStorage : IFurnitureStorage + { + private readonly DataFileSingleton source; + public FurnitureStorage() + { + source = DataFileSingleton.GetInstance(); + } + public FurnitureViewModel? GetElement(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName) && !model.Id.HasValue) + { + return null; + } + + return source.Furnitures.FirstOrDefault(x => + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(FurnitureSearchModel model) + { + if (string.IsNullOrEmpty(model.FurnitureName)) + { + return new(); + } + return source.Furnitures + .Where(x => x.FurnitureName.Contains(model.FurnitureName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Furnitures.Select(x => x.GetViewModel).ToList(); + } + + public FurnitureViewModel? Insert(FurnitureBindingModel model) + { + model.Id = source.Furnitures.Count > 0 ? source.Furnitures.Max(x => + x.Id) + 1 : 1; + var newFurniture = Furniture.Create(model); + if (newFurniture == null) + { + return null; + } + source.Furnitures.Add(newFurniture); + source.SaveFurnitures(); + return newFurniture.GetViewModel; + } + + public FurnitureViewModel? Update(FurnitureBindingModel model) + { + var furniture = source.Furnitures.FirstOrDefault(x => x.Id == model.Id); + if (furniture == null) + { + return null; + } + furniture.Update(model); + source.SaveFurnitures(); + return furniture.GetViewModel; + } + + public FurnitureViewModel? Delete(FurnitureBindingModel model) + { + var element = source.Furnitures.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + source.Furnitures.Remove(element); + source.SaveFurnitures(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FurnitureAssembly/FurnitureAssemFileImplement/Implements/OrderStorage.cs b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..39ed910 --- /dev/null +++ b/FurnitureAssembly/FurnitureAssemFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,89 @@ +using FurnitureAssemblyContracts.BindingModels; +using FurnitureAssemblyContracts.SearchModels; +using FurnitureAssemblyContracts.StoragesContracts; +using FurnitureAssemblyContracts.ViewModels; +using FurnitureAssemFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FurnitureAssemFileImplement.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; + } + + 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; + } + } +}