From 233c432ede4599fd53667647b0b2c3a528b676ba Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sun, 19 Feb 2023 21:21:08 +0400 Subject: [PATCH] Component Storage --- .../Implements/ComponentStorage .cs | 76 ++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/JewelryStoreFileImplement/Implements/ComponentStorage .cs b/JewelryStoreFileImplement/Implements/ComponentStorage .cs index a016dd5..a882b1b 100644 --- a/JewelryStoreFileImplement/Implements/ComponentStorage .cs +++ b/JewelryStoreFileImplement/Implements/ComponentStorage .cs @@ -11,8 +11,80 @@ using System.Threading.Tasks; namespace JewelryStoreFileImplement.Implements { - public class ComponentStorage : IComponentStorage // TODO Lab_2 + 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; + + } } }