using AircraftPlantContracts.BindingModels; using AircraftPlantContracts.SearchModels; using AircraftPlantContracts.StoragesContracts; using AircraftPlantContracts.ViewModels; using AircraftPlantFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AircraftPlantFileImplement.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 _source.Implementers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == model.ImplementerFIO) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } else if (!string.IsNullOrEmpty(model.ImplementerFIO) && !string.IsNullOrEmpty(model.Password)) return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO == model.ImplementerFIO && x.Password == model.Password)?.GetViewModel; return null; } /// /// Добавление элемента /// /// /// 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 implementer = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); if (implementer == null) { return null; } implementer.Update(model); _source.SaveImplementers(); return implementer.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; } } }