using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.StoragesContracts; using TravelCompanyContracts.ViewModels; using TravelCompanyFileImplement.Models; namespace TravelCompanyFileImplement.Implements { public class OrderStorage : IOrderStorage { private readonly DataFileSingleton _source; public OrderStorage() { _source = DataFileSingleton.GetInstance(); } public List GetFullList() { return _source.Orders .Select(x => InsertTravelName(x.GetViewModel)) .ToList(); } public List GetFilteredList(OrderSearchModel model) { if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue) { return new(); } if (!model.DateFrom.HasValue || !model.DateTo.HasValue) { return _source.Orders .Where(x => x.Id == model.Id) .Select(x => InsertTravelName(x.GetViewModel)) .ToList(); } else { return _source.Orders .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) .Select(x => InsertTravelName(x.GetViewModel)) .ToList(); } } public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } return InsertTravelName(_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 InsertTravelName(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 InsertTravelName(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 InsertTravelName(element.GetViewModel); } return null; } public OrderViewModel InsertTravelName(OrderViewModel fullModel) { string? travelName = _source.Travels.FirstOrDefault(x => x.Id == fullModel?.TravelId)?.TravelName; if (travelName != null) fullModel.TravelName = travelName; return fullModel; } } }