using DocumentFormat.OpenXml.Bibliography; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.StoragesContracts; using TravelCompanyContracts.ViewModels; using TravelCompanyListImplement.Models; namespace TravelCompanyFileImplement.Implements { public class ImplementerStorage : IImplementerStorage { private readonly DataFileSingleton _source; public ImplementerStorage() { _source = DataFileSingleton.GetInstance(); } public ImplementerViewModel? Delete(ImplementerBindingModel model) { var element = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); if (element != null) { _source.Implementers.Remove(element); _source.SaveConditions(); return element.GetViewModel; } return null; } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue && string.IsNullOrEmpty(model.Password)) { return null; } if (model.ImplementerFIO != null && model.Password != null) { return _source.Implementers .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) ?.GetViewModel; } if (model.Id.HasValue) { return _source.Implementers .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } return _source.Implementers .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) ?.GetViewModel; } public List GetFilteredList(ImplementerSearchModel model) { if (string.IsNullOrEmpty(model.ImplementerFIO)) { return new(); } return _source.Implementers .Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { return _source.Implementers .Select(x => x.GetViewModel) .ToList(); } public ImplementerViewModel? Insert(ImplementerBindingModel model) { model.Id = _source.Implementers.Count > 0 ? _source.Implementers.Max(x => x.Id) + 1 : 1; var newImpl = Implementer.Create(model); if (newImpl == null) { return null; } _source.Implementers.Add(newImpl); _source.SaveConditions(); return newImpl.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.SaveConditions(); return implementer.GetViewModel; } } }