From 82ce660a86aba83759a4e123a5a025b8c68f73f1 Mon Sep 17 00:00:00 2001 From: Oleg Shabunov Date: Tue, 19 Mar 2024 22:22:43 +0400 Subject: [PATCH] AutoWorkshopFileImplement/Implements --- .../Implements/ComponentStorage.cs | 82 ++++++++++++++++ .../Implements/OrderStorage.cs | 96 +++++++++++++++++++ .../Implements/RepairStorage.cs | 80 ++++++++++++++++ AutoWorkshopFileImplement/Models/Repair.cs | 9 +- AutoWorkshopView/Program.cs | 2 +- 5 files changed, 265 insertions(+), 4 deletions(-) create mode 100644 AutoWorkshopFileImplement/Implements/ComponentStorage.cs create mode 100644 AutoWorkshopFileImplement/Implements/OrderStorage.cs create mode 100644 AutoWorkshopFileImplement/Implements/RepairStorage.cs diff --git a/AutoWorkshopFileImplement/Implements/ComponentStorage.cs b/AutoWorkshopFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..752a95a --- /dev/null +++ b/AutoWorkshopFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,82 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopFileImplement.Models; + +namespace AutoWorkshopFileImplement.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 Component = _source.Components.FirstOrDefault(x => x.Id == Model.Id); + + if (Component == null) + return null; + + _source.Components.Remove(Component); + _source.SaveComponents(); + + return Component.GetViewModel; + } + } +} diff --git a/AutoWorkshopFileImplement/Implements/OrderStorage.cs b/AutoWorkshopFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..54f0d0b --- /dev/null +++ b/AutoWorkshopFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,96 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopFileImplement.Models; +using System.Reflection; + +namespace AutoWorkshopFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton _source; + + public OrderStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _source.Orders.Select(x => AddRepairName(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 => AddRepairName(x.GetViewModel)).ToList(); + } + + 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 AddRepairName(Element.GetViewModel); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel Model) + { + if (!Model.Id.HasValue) + return null; + + var Order = _source.Orders.FirstOrDefault(x => (Model.Id.HasValue && x.Id == Model.Id)); + + if (Order == null) + return null; + + return AddRepairName(Order.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 AddRepairName(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 AddRepairName(Order.GetViewModel); + } + + private OrderViewModel AddRepairName(OrderViewModel Model) + { + var SelectedRepair = _source.Repairs.FirstOrDefault(x => x.Id == Model.RepairId); + + Model.RepairName = SelectedRepair?.RepairName ?? string.Empty; + return Model; + } + } +} diff --git a/AutoWorkshopFileImplement/Implements/RepairStorage.cs b/AutoWorkshopFileImplement/Implements/RepairStorage.cs new file mode 100644 index 0000000..0fcd3da --- /dev/null +++ b/AutoWorkshopFileImplement/Implements/RepairStorage.cs @@ -0,0 +1,80 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopFileImplement.Models; + +namespace AutoWorkshopFileImplement.Implements +{ + public class RepairStorage : IRepairStorage + { + private readonly DataFileSingleton _source; + + public RepairStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _source.Repairs.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(RepairSearchModel Model) + { + if (string.IsNullOrEmpty(Model.RepairName)) + return new(); + + return _source.Repairs.Where(x => x.RepairName.Contains(Model.RepairName)).Select(x => x.GetViewModel).ToList(); + } + + public RepairViewModel? GetElement(RepairSearchModel Model) + { + if (string.IsNullOrEmpty(Model.RepairName) && !Model.Id.HasValue) + return null; + + return _source.Repairs.FirstOrDefault(x => (!string.IsNullOrEmpty(Model.RepairName) && x.RepairName == Model.RepairName) || (Model.Id.HasValue && x.Id == Model.Id))?.GetViewModel; + } + + public RepairViewModel? Insert(RepairBindingModel Model) + { + Model.Id = _source.Repairs.Count > 0 ? _source.Repairs.Max(x => x.Id) + 1 : 1; + + var NewRepair = Repair.Create(Model); + + if (NewRepair == null) + return null; + + _source.Repairs.Add(NewRepair); + _source.SaveRepairs(); + + return NewRepair.GetViewModel; + } + + public RepairViewModel? Update(RepairBindingModel Model) + { + var Repair = _source.Repairs.FirstOrDefault(x => x.Id == Model.Id); + + if (Repair == null) + return null; + + Repair.Update(Model); + _source.SaveRepairs(); + + return Repair.GetViewModel; + } + + public RepairViewModel? Delete(RepairBindingModel Model) + { + var Repair = _source.Repairs.FirstOrDefault(x => x.Id == Model.Id); + + if (Repair == null) + return null; + + _source.Repairs.Remove(Repair); + _source.SaveRepairs(); + + return Repair.GetViewModel; + } + } +} diff --git a/AutoWorkshopFileImplement/Models/Repair.cs b/AutoWorkshopFileImplement/Models/Repair.cs index 7907ac6..ee7b2f3 100644 --- a/AutoWorkshopFileImplement/Models/Repair.cs +++ b/AutoWorkshopFileImplement/Models/Repair.cs @@ -24,7 +24,8 @@ namespace AutoWorkshopFileImplement.Models if (_RepairComponents == null) { var source = DataFileSingleton.GetInstance(); - _RepairComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); + _RepairComponents = Components.ToDictionary(x => + x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value)); } return _RepairComponents; } @@ -53,7 +54,8 @@ namespace AutoWorkshopFileImplement.Models Id = Convert.ToInt32(Element.Attribute("Id")!.Value), RepairName = Element.Element("RepairName")!.Value, Price = Convert.ToDouble(Element.Element("Price")!.Value), - Components = Element.Element("RepairComponents")!.Elements("RepairComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) + Components = Element.Element("RepairComponents")!.Elements("RepairComponent").ToDictionary(x => + Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)) }; } @@ -81,7 +83,8 @@ namespace AutoWorkshopFileImplement.Models new XAttribute("Id", Id), new XElement("RepairName", RepairName), new XElement("Price", Price.ToString()), - new XElement("RepairComponents", Components.Select(x => new XElement("RepairComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()) + new XElement("RepairComponents", Components.Select(x => + new XElement("RepairComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()) ); } } diff --git a/AutoWorkshopView/Program.cs b/AutoWorkshopView/Program.cs index c16b8be..bad6fb6 100644 --- a/AutoWorkshopView/Program.cs +++ b/AutoWorkshopView/Program.cs @@ -1,7 +1,7 @@ using AutoWorkshopBusinessLogic.BusinessLogics; using AutoWorkshopContracts.BusinessLogicContracts; using AutoWorkshopContracts.StoragesContracts; -using AutoWorkshopListImplement.Implements; +using AutoWorkshopFileImplement.Implements; using AutoWorkshopView.Forms; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging;