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 OrderStorage : IOrderStorage { /// /// Хранилище /// private readonly DataFileSingleton _source; /// /// Конструктор /// public OrderStorage() { _source = DataFileSingleton.GetInstance(); } /// /// Получение полного списка /// /// public List GetFullList() { return _source.Orders .Select(x => x.GetViewModel) .ToList(); } /// /// Получение фильтрованного списка /// /// /// public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue) { return new(); } return _source.Orders .Where(x => x.Id.Equals(model.Id)) .Select(x => x.GetViewModel) .ToList(); } /// /// Получение элемента /// /// /// public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } return _source.Orders .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } /// /// Добавление элемента /// /// /// public OrderViewModel? Insert(OrderBindingModel model) { model.Id = _source.Orders.Count > 0 ? _source.Orders.Max(x => x.Id) + 1 : 1; var newOrder = Order.Create(model); if (newOrder == null) { return null; } _source.Orders.Add(newOrder); _source.SaveOrders(); return newOrder.GetViewModel; } /// /// Редактирование элемента /// /// /// public OrderViewModel? Update(OrderBindingModel model) { var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; } order.Update(model); _source.SaveOrders(); return order.GetViewModel; } /// /// Удаление элемента /// /// /// public OrderViewModel? Delete(OrderBindingModel model) { var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id); if (element != null) { _source.Orders.Remove(element); _source.SaveOrders(); return element.GetViewModel; } return null; } } }