using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using SecuritySystemContracts.BindingModels; using SecuritySystemContracts.SearchModels; using SecuritySystemContracts.StoragesContracts; using SecuritySystemContracts.ViewModels; using SecuritySystemFileImplement.Models; using SecuritySystemFileImplement; namespace SecuritySystemFileImplement.Implements { public class SecureStorage : ISecureStorage { private readonly DataFileSingleton source; public SecureStorage() { source = DataFileSingleton.GetInstance(); } public SecureViewModel? GetElement(SecureSearchModel model) { if (string.IsNullOrEmpty(model.SecureName) && !model.Id.HasValue) { return null; } return source.Secures.FirstOrDefault(x => (!string.IsNullOrEmpty(model.SecureName) && x.SecureName == model.SecureName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(SecureSearchModel model) { if (string.IsNullOrEmpty(model.SecureName)) { return new(); } return source.Secures .Where(x => x.SecureName.Contains(model.SecureName)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { return source.Secures.Select(x => x.GetViewModel).ToList(); } public SecureViewModel? Insert(SecureBindingModel model) { model.Id = source.Secures.Count > 0 ? source.Secures.Max(x => x.Id) + 1 : 1; var newSecure =Secure.Create(model); if (newSecure == null) { return null; } source.Secures.Add(newSecure); source.SaveSecures(); return newSecure.GetViewModel; } public SecureViewModel? Update(SecureBindingModel model) { var Secure = source.Secures.FirstOrDefault(x => x.Id == model.Id); if (Secure == null) { return null; } Secure.Update(model); source.SaveSecures(); return Secure.GetViewModel; } public SecureViewModel? Delete(SecureBindingModel model) { var Secure = source.Secures.FirstOrDefault(x => x.Id == model.Id); if (Secure == null) { return null; } Secure.Update(model); source.SaveSecures(); return Secure.GetViewModel; } } }