using PrecastConcretePlantContracts.BindingModels; using PrecastConcretePlantContracts.SearchModels; using PrecastConcretePlantContracts.StoragesContracts; using PrecastConcretePlantContracts.ViewModels; using PrecastConcretePlantFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PrecastConcretePlantFileImplement.Implements { public class ReinforcedStorage : IReinforcedStorage { private readonly DataFileSingleton source; public ReinforcedStorage() { source = DataFileSingleton.GetInstance(); } public ReinforcedViewModel? Delete(ReinforcedBindingModel model) { var element = source.Reinforceds.FirstOrDefault(x => x.Id == model.Id); if (element != null) { source.Reinforceds.Remove(element); source.SaveReinforceds(); return element.GetViewModel; } return null; } public ReinforcedViewModel? GetElement(ReinforcedSearchModel model) { if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue) { return null; } return source.Reinforceds.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(ReinforcedSearchModel model) { if (string.IsNullOrEmpty(model.ReinforcedName)) { return new(); } return source.Reinforceds.Where(x => x.ReinforcedName.Contains(model.ReinforcedName)).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { return source.Reinforceds.Select(x => x.GetViewModel).ToList(); } public ReinforcedViewModel? Insert(ReinforcedBindingModel model) { model.Id = source.Reinforceds.Count > 0 ? source.Reinforceds.Max(x => x.Id) + 1 : 1; var newReinforced = Reinforced.Create(model); if (newReinforced == null) { return null; } source.Reinforceds.Add(newReinforced); source.SaveReinforceds(); return newReinforced.GetViewModel; } public ReinforcedViewModel? Update(ReinforcedBindingModel model) { var reinforced = source.Reinforceds.FirstOrDefault(x => x.Id == model.Id); if (reinforced == null) { return null; } reinforced.Update(model); source.SaveReinforceds(); return reinforced.GetViewModel; } } }