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 @@ <ItemGroup> <ProjectReference Include="..\AbstractAutoBusinessLogic\AutomobilePlantBusinessLogic.csproj" /> - <ProjectReference Include="..\AbstractAutoListImplement\AutomobilePlantListImplement.csproj" /> + <ProjectReference Include="..\AutomomilePlantFileImplement\AutomomilePlantFileImplement.csproj" /> </ItemGroup> </Project> \ 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<CarViewModel> 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<CarViewModel> 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<ComponentViewModel> GetFullList() + { + return source.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List<ComponentViewModel> 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<OrderViewModel> 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<OrderViewModel> 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; + } + } +}