using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using TravelCompanyContracts.StoragesContracts; using TravelCompanyContracts.BindingModels; using TravelCompanyContracts.SearchModels; using TravelCompanyContracts.ViewModels; using TravelCompanyFileImplement.Models; namespace TravelCompanyFileImplement.Implements { public class ConditionStorage : IConditionStorage { private readonly DataFileSingleton source; public ConditionStorage() { source = DataFileSingleton.GetInstance(); } public List GetFullList() { return source.Conditions .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(ConditionSearchModel model) { if (string.IsNullOrEmpty(model.ConditionName)) { return new(); } return source.Conditions .Where(x => x.ConditionName.Contains(model.ConditionName)) .Select(x => x.GetViewModel) .ToList(); } public ConditionViewModel? GetElement(ConditionSearchModel model) { if (string.IsNullOrEmpty(model.ConditionName) && !model.Id.HasValue) { return null; } return source.Conditions.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.ConditionName) && x.ConditionName ==model.ConditionName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public ConditionViewModel? Insert(ConditionBindingModel model) { model.Id = source.Conditions.Count > 0 ? source.Conditions.Max(x =>x.Id) + 1 : 1; var newCondition = Condition.Create(model); if (newCondition == null) { return null; } source.Conditions.Add(newCondition); source.SaveConditions(); return newCondition.GetViewModel; } public ConditionViewModel? Update(ConditionBindingModel model) { var condition = source.Conditions.FirstOrDefault(x => x.Id ==model.Id); if (condition == null) { return null; } condition.Update(model); source.SaveConditions(); return condition.GetViewModel; } public ConditionViewModel? Delete(ConditionBindingModel model) { var element = source.Conditions.FirstOrDefault(x => x.Id ==model.Id); if (element != null) { source.Conditions.Remove(element); source.SaveConditions(); return element.GetViewModel; } return null; } } }