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.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace AircraftPlantFileImplement.Implements { /// /// Реализация интерфейса хранилища для изделий /// public class PlaneStorage : IPlaneStorage { /// /// Хранилище /// private readonly DataFileSingleton _source; /// /// Конструктор /// public PlaneStorage() { _source = DataFileSingleton.GetInstance(); } /// /// Получение полного списка /// /// public List GetFullList() { return _source.Planes .Select(x => x.GetViewModel) .ToList(); } /// /// Получение фильтрованного списка /// /// /// public List GetFilteredList(PlaneSearchModel model) { if (string.IsNullOrEmpty(model.PlaneName)) { return new(); } return _source.Planes .Where(x => x.PlaneName.Contains(model.PlaneName)) .Select(x => x.GetViewModel) .ToList(); } /// /// Получение элемента /// /// /// public PlaneViewModel? GetElement(PlaneSearchModel model) { if (string.IsNullOrEmpty(model.PlaneName) && !model.Id.HasValue) { return null; } return _source.Planes .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaneName) && x.PlaneName == model.PlaneName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } /// /// Добавление элемента /// /// /// public PlaneViewModel? Insert(PlaneBindingModel model) { model.Id = _source.Planes.Count > 0 ? _source.Planes.Max(x => x.Id) + 1 : 1; var newPlane = Plane.Create(model); if (newPlane == null) { return null; } _source.Planes.Add(newPlane); _source.SavePlanes(); return newPlane.GetViewModel; } /// /// Редактирование элемента /// /// /// public PlaneViewModel? Update(PlaneBindingModel model) { var plane = _source.Planes.FirstOrDefault(x => x.Id == model.Id); if (plane == null) { return null; } plane.Update(model); _source.SavePlanes(); return plane.GetViewModel; } /// /// Удаление элемента /// /// /// public PlaneViewModel? Delete(PlaneBindingModel model) { var element = _source.Planes.FirstOrDefault(x => x.Id == model.Id); if (element != null) { _source.Planes.Remove(element); _source.SavePlanes(); return element.GetViewModel; } return null; } } }