diff --git a/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/OrderStorage.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..6b6ba76 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,91 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.SearchModels; +using AbstractSoftwareInstallationContracts.StoragesContracts; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationFileImplement.Models; + +namespace AbstractSoftwareInstallationFileImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataFileSingleton source; + + public OrderStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + return GetViewModel(source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + return source.Orders + .Where(x => x.Id == model.Id) + .Select(x => GetViewModel(x)) + .ToList(); + } + + public List GetFullList() + { + return source.Orders.Select(x => GetViewModel(x)).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 GetViewModel(newOrder); + } + + 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 GetViewModel(order); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + var order = source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + source.Orders.Remove(order); + source.SaveOrders(); + return GetViewModel(order); + } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + var package = source.Packages.FirstOrDefault(x => x.Id == order.PackageId); + if (package != null) + { + viewModel.PackageName = package.PackageName; + } + return viewModel; + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/PackageStorage.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/PackageStorage.cs new file mode 100644 index 0000000..1a82ee7 --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/PackageStorage.cs @@ -0,0 +1,86 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.SearchModels; +using AbstractSoftwareInstallationContracts.StoragesContracts; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbstractSoftwareInstallationFileImplement.Implements +{ + public class PackageStorage : IPackageStorage + { + private readonly DataFileSingleton source; + + public PackageStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public PackageViewModel? GetElement(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName) && !model.Id.HasValue) + { + return null; + } + return source.Packages + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PackageName) && x.PackageName == model.PackageName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(PackageSearchModel model) + { + if (string.IsNullOrEmpty(model.PackageName)) + { + return new(); + } + return source.Packages + .Where(x => x.PackageName.Contains(model.PackageName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Packages.Select(x => x.GetViewModel).ToList(); + } + + public PackageViewModel? Insert(PackageBindingModel model) + { + model.Id = source.Packages.Count > 0 ? source.Packages.Max(x => x.Id) + 1 : 1; + var newPackage = Package.Create(model); + if (newPackage == null) + { + return null; + } + source.Packages.Add(newPackage); + source.SavePackages(); + return newPackage.GetViewModel; + } + + public PackageViewModel? Update(PackageBindingModel model) + { + var Package = source.Packages.FirstOrDefault(x => x.Id == model.Id); + if (Package == null) + { + return null; + } + Package.Update(model); + source.SavePackages(); + return Package.GetViewModel; + } + public PackageViewModel? Delete(PackageBindingModel model) + { + var Package = source.Packages.FirstOrDefault(x => x.Id == model.Id); + if (Package == null) + { + return null; + } + source.Packages.Remove(Package); + source.SavePackages(); + return Package.GetViewModel; + } + } +} diff --git a/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/SoftwareStorage.cs b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/SoftwareStorage.cs new file mode 100644 index 0000000..9480f6f --- /dev/null +++ b/SoftwareInstallation/AbstractSoftwareInstallationFileImplement/Implements/SoftwareStorage.cs @@ -0,0 +1,75 @@ +using AbstractSoftwareInstallationContracts.BindingModels; +using AbstractSoftwareInstallationContracts.SearchModels; +using AbstractSoftwareInstallationContracts.StoragesContracts; +using AbstractSoftwareInstallationContracts.ViewModels; +using AbstractSoftwareInstallationFileImplement.Models; + +namespace AbstractSoftwareInstallationFileImplement.Implements +{ + public class SoftwareStorage : ISoftwareStorage + { + private readonly DataFileSingleton _source; + public SoftwareStorage() + { + _source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return _source.Softwares.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(SoftwareSearchModel + model) + { + if (string.IsNullOrEmpty(model.SoftwareName)) + { + return new(); + } + return _source.Softwares.Where(x => x.SoftwareName.Contains(model.SoftwareName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public SoftwareViewModel? GetElement(SoftwareSearchModel model) + { + if (string.IsNullOrEmpty(model.SoftwareName) && !model.Id.HasValue) + { + return null; + } + return _source.Softwares.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SoftwareName) && x.SoftwareName == model.SoftwareName) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public SoftwareViewModel? Insert(SoftwareBindingModel model) + { + model.Id = _source.Softwares.Count > 0 ? _source.Softwares.Max(x => x.Id) + 1 : 1; + var newSoftware = Software.Create(model); + if (newSoftware == null) + { + return null; + } + _source.Softwares.Add(newSoftware); + _source.SaveSoftwares(); + return newSoftware.GetViewModel; + } + public SoftwareViewModel? Update(SoftwareBindingModel model) + { + var software = _source.Softwares.FirstOrDefault(x => x.Id == model.Id); + if (software == null) + { + return null; + } + software.Update(model); + _source.SaveSoftwares(); + return software.GetViewModel; + } + public SoftwareViewModel? Delete(SoftwareBindingModel model) + { + var software = _source.Softwares.FirstOrDefault(x => x.Id == model.Id); + if (software == null) + { + return null; + } + _source.Softwares.Remove(software); + _source.SaveSoftwares(); + return software.GetViewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallation/Program.cs b/SoftwareInstallation/SoftwareInstallation/Program.cs index 5f1471b..1b6b0b8 100644 --- a/SoftwareInstallation/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/SoftwareInstallation/Program.cs @@ -1,10 +1,8 @@ -using AbstractOrderInstallationListImplement.Implements; -using AbstractPackageInstallationListImplement.Implements; +using AbstractSoftwareInstallationFileImplement.Implements; using AbstractSoftwareInstallationBusinessLogic; using AbstractSoftwareInstallationBusinessLogic.BusinessLogic; using AbstractSoftwareInstallationContracts.BusinessLogicsContracts; using AbstractSoftwareInstallationContracts.StoragesContracts; -using AbstractSoftwareInstallationListImplement.Implements; using SoftwareInstallationView; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj index 3783142..3bbfa37 100644 --- a/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj +++ b/SoftwareInstallation/SoftwareInstallation/SoftwareInstallationView.csproj @@ -16,6 +16,8 @@ + +