using PlumbingRepairContracts.BindingModels; using PlumbingRepairContracts.SearchModels; using PlumbingRepairContracts.StoragesContracts; using PlumbingRepairContracts.ViewModels; using PlumbingRepairListImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PlumbingRepairListImplement.Implements { public class WorkStorage : IWorkStorage { private readonly DataListSingleton _source; public WorkStorage() { _source = DataListSingleton.GetInstance(); } public List GetFullList() { var result = new List(); foreach (var work in _source.Works) { result.Add(work.GetViewModel); } return result; } public List GetFilteredList(WorkSearchModel model) { var result = new List(); if (string.IsNullOrEmpty(model.WorkName)) { return result; } foreach (var work in _source.Works) { if (work.WorkName.Contains(model.WorkName)) { result.Add(work.GetViewModel); } } return result; } public WorkViewModel? GetElement(WorkSearchModel model) { if (string.IsNullOrEmpty(model.WorkName) && !model.Id.HasValue) { return null; } foreach (var work in _source.Works) { if ((!string.IsNullOrEmpty(model.WorkName) && work.WorkName == model.WorkName) || (model.Id.HasValue && work.Id == model.Id)) { return work.GetViewModel; } } return null; } public WorkViewModel? Insert(WorkBindingModel model) { model.Id = 1; foreach (var work in _source.Works) { if (model.Id <= work.Id) { model.Id = work.Id + 1; } } var newWork = Work.Create(model); if (newWork == null) { return null; } _source.Works.Add(newWork); return newWork.GetViewModel; } public WorkViewModel? Update(WorkBindingModel model) { foreach (var work in _source.Works) { if (work.Id == model.Id) { work.Update(model); return work.GetViewModel; } } return null; } public WorkViewModel? Delete(WorkBindingModel model) { for (int i = 0; i < _source.Works.Count; ++i) { if (_source.Works[i].Id == model.Id) { var element = _source.Works[i]; _source.Works.RemoveAt(i); return element.GetViewModel; } } return null; } } }