using PlumbingRepairContracts.BindingModels; using PlumbingRepairContracts.SearchModels; using PlumbingRepairContracts.StoragesContracts; using PlumbingRepairContracts.ViewModels; using PlumbingRepairFileImplement.Models; using System.Net; namespace PlumbingRepairFileImplement.Implements { public class ImplementerStorage : IImplementerStorage { private readonly DataFileSingleton source; public ImplementerStorage() { source = DataFileSingleton.GetInstance(); } public List GetFullList() { return source.Implementers .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(ImplementerSearchModel model) { if (string.IsNullOrEmpty(model.ImplementerFIO)) { return new(); } return source.Implementers .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue) { return null; } return source.Implementers .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == model.ImplementerFIO) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public ImplementerViewModel? Insert(ImplementerBindingModel model) { model.Id = source.Implementers.Count > 0 ? source.Implementers.Max(x => x.Id) + 1 : 1; var newImplementer = Implementer.Create(model); if (newImplementer == null) { return null; } source.Implementers.Add(newImplementer); source.SaveImplementers(); return newImplementer.GetViewModel; } public ImplementerViewModel? Update(ImplementerBindingModel model) { var client = source.Implementers.FirstOrDefault(x => x.Id == model.Id); if (client == null) { return null; } client.Update(model); source.SaveImplementers(); return client.GetViewModel; } public ImplementerViewModel? Delete(ImplementerBindingModel model) { var element = source.Implementers.FirstOrDefault(x => x.Id == model.Id); if (element != null) { source.Implementers.Remove(element); source.SaveImplementers(); return element.GetViewModel; } return null; } } }