From 07dc91676ceddd71a241fa2fc5650a0de89828f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D1=8F?= Date: Wed, 19 Apr 2023 17:22:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AutomobilePlant/AutomobilePlant.csproj | 2 +- AutomobilePlant/AutomobilePlant/Program.cs | 2 +- .../Implements/CarStorage.cs | 94 +++++++++++++++++++ .../Implements/ComponentStorage.cs | 90 ++++++++++++++++++ .../Implements/OrderStorage.cs | 92 ++++++++++++++++++ 5 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs create mode 100644 AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs diff --git a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj index b9586a3..b635bae 100644 --- a/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj +++ b/AutomobilePlant/AutomobilePlant/AutomobilePlant.csproj @@ -24,7 +24,7 @@ - + \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlant/Program.cs b/AutomobilePlant/AutomobilePlant/Program.cs index f8f9e03..8a8efc6 100644 --- a/AutomobilePlant/AutomobilePlant/Program.cs +++ b/AutomobilePlant/AutomobilePlant/Program.cs @@ -1,7 +1,7 @@ using AutomobilePlantBusinessLogic.BusinessLogics; using AutomobilePlantContracts.BusinessLogicsContracts; using AutomobilePlantContracts.StoragesContracts; -using AutomobilePlantListImplement.Implements; +using AutomomilePlantFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs new file mode 100644 index 0000000..78d430f --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/CarStorage.cs @@ -0,0 +1,94 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.Implements +{ + public class CarStorage : ICarStorage + { + private readonly DataFileSingleton source; + public CarStorage() + { + source = DataFileSingleton.GetInstance(); + } + public CarViewModel? Delete(CarBindingModel model) + { + var element = source.Cars.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.Cars.Remove(element); + source.SaveCars(); + return element.GetViewModel; + } + return null; + } + + public CarViewModel? GetElement(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue) + { + return null; + } + return source.Cars + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.CarName) && x.CarName == + model.CarName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName)) + { + return new(); + } + return source.Cars + .Where(x => x.CarName.Contains(model.CarName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Cars + .Select(x => x.GetViewModel) + .ToList(); + } + + public CarViewModel? Insert(CarBindingModel model) + { + model.Id = source.Cars.Count > 0 ? source.Cars.Max(x => + x.Id) + 1 : 1; + var newCar = Car.Create(model); + if (newCar == null) + { + return null; + } + source.Cars.Add(newCar); + source.SaveCars(); + return newCar.GetViewModel; + } + + public CarViewModel? Update(CarBindingModel model) + { + var car = source.Cars.FirstOrDefault(x => x.Id == + model.Id); + if (car == null) + { + return null; + } + car.Update(model); + source.SaveCars(); + return car.GetViewModel; + } + } +} diff --git a/AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..f12c9c4 --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,90 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.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/AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs b/AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..e54c17a --- /dev/null +++ b/AutomobilePlant/AutomomilePlantFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,92 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModel; +using AutomobilePlantContracts.StoragesContracts; +using AutomobilePlantContracts.ViewModel; +using AutomomilePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomomilePlantFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + 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; + } + + 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 List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Orders + .Select(x => x.GetViewModel) + .ToList(); + } + + 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; + } + } +}