using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; using SushiBarListImplement.Models; namespace SushiBarListImplement.Implements { public class SushiStorage : ISushiStorage { private readonly DataListSingleton _source; public SushiStorage() { _source = DataListSingleton.GetInsance(); } public List GetFullList() { var result = new List(); foreach (var sushi in _source.Sushis) { result.Add(sushi.GetViewModel); } return result; } public List GetFilteredList(SushiSearchModel model) { var result = new List(); if (string.IsNullOrEmpty(model.SushiName)) { return result; } foreach (var sushi in _source.Sushis) { if (sushi.SushiName.Contains(model.SushiName)) { result.Add(sushi.GetViewModel); } } return result; } public SushiViewModel? GetElement(SushiSearchModel model) { if (string.IsNullOrEmpty(model.SushiName) && !model.Id.HasValue) { return null; } foreach (var sushi in _source.Sushis) { if ((!string.IsNullOrEmpty(model.SushiName) && sushi.SushiName == model.SushiName) || (model.Id.HasValue && sushi.Id == model.Id)) { return sushi.GetViewModel; } } return null; } public SushiViewModel? Insert(SushiBindingModel model) { model.Id = 1; foreach (var sushi in _source.Sushis) { if (model.Id <= sushi.Id) { model.Id = sushi.Id + 1; } } var newSushi = Sushi.Create(model); if (newSushi == null) { return null; } _source.Sushis.Add(newSushi); return newSushi.GetViewModel; } public SushiViewModel? Update(SushiBindingModel model) { foreach (var sushi in _source.Sushis) { if (sushi.Id == model.Id) { sushi.Update(model); return sushi.GetViewModel; } } return null; } public SushiViewModel? Delete(SushiBindingModel model) { for (int i = 0; i < _source.Sushis.Count; ++i) { if (_source.Sushis[i].Id == model.Id) { var element = _source.Sushis[i]; _source.Sushis.RemoveAt(i); return element.GetViewModel; } } return null; } } }