diff --git a/SecuritySystem/SecuritySystemFileImplement/Implements/ComponentStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..d63c549 --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/ComponentStorage.cs @@ -0,0 +1,72 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemFileImplement.Models; + +namespace SecuritySystemFileImplement.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/SecuritySystem/SecuritySystemFileImplement/Implements/OrderStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..1e12c9b --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/OrderStorage.cs @@ -0,0 +1,92 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; +using SecuritySystemFileImplement.Models; + +namespace SecuritySystemFileImplement.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 car = source.Secures.FirstOrDefault(x => x.Id == order.SecureId); + if (car != null) + { + viewModel.SecureName = car.SecureName; + } + return viewModel; + } + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs b/SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs new file mode 100644 index 0000000..68a0a84 --- /dev/null +++ b/SecuritySystem/SecuritySystemFileImplement/Implements/SecureStorage.cs @@ -0,0 +1,80 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; + +namespace SecuritySystemFileImplement.Implements +{ + public class SecureStorage : ISecureStorage + { + private readonly DataFileSingleton source; + + public SecureStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public SecureViewModel? GetElement(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName) && !model.Id.HasValue) + { + return null; + } + return source.Secures + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.SecureName) && x.SecureName == model.SecureName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(SecureSearchModel model) + { + if (string.IsNullOrEmpty(model.SecureName)) + { + return new(); + } + return source.Secures + .Where(x => x.SecureName.Contains(model.SecureName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Secures.Select(x => x.GetViewModel).ToList(); + } + + public SecureViewModel? Insert(SecureBindingModel model) + { + model.Id = source.Secures.Count > 0 ? source.Secures.Max(x => x.Id) + 1 : 1; + var newSecure = Secure.Create(model); + if (newSecure == null) + { + return null; + } + source.Secures.Add(newSecure); + source.SaveSecures(); + return newSecure.GetViewModel; + } + + public SecureViewModel? Update(SecureBindingModel model) + { + var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id); + if (secure == null) + { + return null; + } + secure.Update(model); + source.SaveSecures(); + return secure.GetViewModel; + } + public SecureViewModel? Delete(SecureBindingModel model) + { + var secure = source.Secures.FirstOrDefault(x => x.Id == model.Id); + if (secure == null) + { + return null; + } + source.Secures.Remove(secure); + source.SaveSecures(); + return secure.GetViewModel; + } + } +} diff --git a/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj b/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj index 97a8a90..7037095 100644 --- a/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj +++ b/SecuritySystem/SecuritySystemFileImplement/SecuritySystemFileImplement.csproj @@ -11,8 +11,4 @@ - - - - diff --git a/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj b/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj index 726d3b3..3937b06 100644 --- a/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj +++ b/SecuritySystem/SecuritySystemView/SecuritySystemView.csproj @@ -29,6 +29,7 @@ +