using FurnitureAssemblyContracts.BindingModels; using FurnitureAssemblyContracts.SearchModels; using FurnitureAssemblyContracts.StoragesContracts; using FurnitureAssemblyContracts.ViewModels; using FurnitureAssemblyFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FurnitureAssemblyFileImplement.Implements { public class ImplementerStorage : IImplementerStorage { private readonly DataFileSingleton source; public ImplementerStorage() { source = DataFileSingleton.GetInstance(); } public ImplementerViewModel? GetElement(ImplementerSearchModel model) { if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue && string.IsNullOrEmpty(model.Password)) { return null; } if (model.Id.HasValue) { return source.Implementers .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } if (model.ImplementerFIO != null && model.Password != null) { return source.Implementers .FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password)) ?.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.SaveWorkpieces(); 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.SaveWorkpieces(); return implementer.GetViewModel; } public ImplementerViewModel? Delete(ImplementerBindingModel model) { var element = source.Implementers.FirstOrDefault(x => x.Id == model.Id); if (element != null) { source.Implementers.Remove(element); source.SaveWorkpieces(); return element.GetViewModel; } return null; } } }