using LawFirmContracts.BindingModels; using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using LawFirmFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LawFirmFileImplement.Implements { public class DocumentStorage : IDocumentStorage { private readonly DataFileSingleton source; public DocumentStorage() { source = DataFileSingleton.GetInstance(); } public DocumentViewModel? GetElement(DocumentSearchModel model) { if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue) { return null; } return source.Documents .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(DocumentSearchModel model) { if (string.IsNullOrEmpty(model.DocumentName)) { return new(); } return source.Documents .Where(x => x.DocumentName.Contains(model.DocumentName)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { return source.Documents.Select(x => x.GetViewModel).ToList(); } public DocumentViewModel? Insert(DocumentBindingModel model) { model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1; var newDoc = Document.Create(model); if (newDoc == null) { return null; } source.Documents.Add(newDoc); source.SaveDocuments(); return newDoc.GetViewModel; } public DocumentViewModel? Update(DocumentBindingModel model) { var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); if (document == null) { return null; } document.Update(model); source.SaveDocuments(); return document.GetViewModel; } public DocumentViewModel? Delete(DocumentBindingModel model) { var document = source.Documents.FirstOrDefault(x => x.Id == model.Id); if (document == null) { return null; } source.Documents.Remove(document); source.SaveDocuments(); return document.GetViewModel; } } }