using LawFirmContracts.BindingModels; using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using LawFirmFileImplement.Models; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LawFirmFileImplement.Implements { public class BlankStorage : IBlankStorage { private readonly DataFileSingleton source; public BlankStorage() { source = DataFileSingleton.GetInstance(); } public BlankViewModel? GetElement(BlankSearchModel model) { if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue) { return null; } return source.Blanks .FirstOrDefault(x => (!string.IsNullOrEmpty(model.BlankName) && x.BlankName == model.BlankName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } public List GetFilteredList(BlankSearchModel model) { if (string.IsNullOrEmpty(model.BlankName)) { return new(); } return source.Blanks .Where(x => x.BlankName.Contains(model.BlankName)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { return source.Blanks.Select(x => x.GetViewModel).ToList(); } public BlankViewModel? Insert(BlankBindingModel model) { model.Id = source.Blanks.Count > 0 ? source.Blanks.Max(x => x.Id) + 1 : 1; var newBlank = Blank.Create(model); if (newBlank == null) { return null; } source.Blanks.Add(newBlank); source.SaveBlanks(); return newBlank.GetViewModel; } public BlankViewModel? Update(BlankBindingModel model) { var blank = source.Blanks.FirstOrDefault(x => x.Id == model.Id); if (blank == null) { return null; } blank.Update(model); source.SaveBlanks(); return blank.GetViewModel; } public BlankViewModel? Delete(BlankBindingModel model) { var blank = source.Blanks.FirstOrDefault(x => x.Id == model.Id); if (blank == null) { return null; } source.Blanks.Remove(blank); source.SaveBlanks(); return blank.GetViewModel; } } }