From c1fdf0b803af9cba72cf098bdbace2d5d6480a51 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sun, 19 Feb 2023 21:20:30 +0400 Subject: [PATCH] Comonent Storage TODO --- .../Implements/ComponentStorage .cs | 76 +----------- .../Implements/JewelStorage.cs | 112 ++++++++++++++++++ 2 files changed, 114 insertions(+), 74 deletions(-) create mode 100644 JewelryStoreFileImplement/Implements/JewelStorage.cs diff --git a/JewelryStoreFileImplement/Implements/ComponentStorage .cs b/JewelryStoreFileImplement/Implements/ComponentStorage .cs index a882b1b..a016dd5 100644 --- a/JewelryStoreFileImplement/Implements/ComponentStorage .cs +++ b/JewelryStoreFileImplement/Implements/ComponentStorage .cs @@ -11,80 +11,8 @@ using System.Threading.Tasks; namespace JewelryStoreFileImplement.Implements { - public class ComponentStorage : IComponentStorage + public class ComponentStorage : IComponentStorage // TODO Lab_2 { - 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/JewelryStoreFileImplement/Implements/JewelStorage.cs b/JewelryStoreFileImplement/Implements/JewelStorage.cs new file mode 100644 index 0000000..e09b4ab --- /dev/null +++ b/JewelryStoreFileImplement/Implements/JewelStorage.cs @@ -0,0 +1,112 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreListImplement.Implements +{ + public class JewelStorage : IJewelStorage // TODO реализовать интерфейс + { + private readonly DataListSingleton _source; + public JewelStorage() + { + _source = DataListSingleton.GetInstance(); + } + public JewelViewModel? Delete(JewelBindingModel model) + { + for (int i = 0; i < _source.Jewels.Count; ++i) + { + if (_source.Jewels[i].Id == model.Id) + { + var element = _source.Jewels[i]; + _source.Jewels.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public JewelViewModel? GetElement(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue) + { + return null; + } + foreach (var jewel in _source.Jewels) + { + if ((!string.IsNullOrEmpty(model.JewelName) && + jewel.JewelName == model.JewelName) || + (model.Id.HasValue && jewel.Id == model.Id)) + { + return jewel.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(JewelSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.JewelName)) + { + return result; + } + foreach (var jewel in _source.Jewels) + { + if (jewel.JewelName.Contains(model.JewelName)) + { + result.Add(jewel.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var jewel in _source.Jewels) + { + result.Add(jewel.GetViewModel); + } + return result; + } + + public JewelViewModel? Insert(JewelBindingModel model) + { + model.Id = 1; + foreach (var jewel in _source.Jewels) + { + if (model.Id <= jewel.Id) + { + model.Id = jewel.Id + 1; + } + } + var newJewel = Jewel.Create(model); + if (newJewel == null) + { + return null; + } + _source.Jewels.Add(newJewel); + return newJewel.GetViewModel; + } + + public JewelViewModel? Update(JewelBindingModel model) + { + foreach (var jewel in _source.Jewels) + { + if (jewel.Id == model.Id) + { + jewel.Update(model); + return jewel.GetViewModel; + } + } + return null; + } + } +}