using FishFactoryFileImplement.Models; using FishFactoryContracts.BindingModels; using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; using FishFactoryContracts.ViewModels; namespace FishFactoryFileImplement.Implements { public class CannedStorage : ICannedStorage { private readonly DataFileSingleton source; public CannedStorage() { source = DataFileSingleton.GetInstance(); } public List GetFullList() { return source.Canneds.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(CannedSearchModel model) { if (string.IsNullOrEmpty(model.CannedName)) { return new(); } return source.Canneds.Where(x => x.CannedName.Contains(model.CannedName)).Select(x => x.GetViewModel).ToList(); } public CannedViewModel? GetElement(CannedSearchModel model) { if (string.IsNullOrEmpty(model.CannedName) && !model.Id.HasValue) { return null; } return source.Canneds.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) && x.CannedName == model.CannedName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public CannedViewModel? Insert(CannedBindingModel model) { model.Id = source.Canneds.Count > 0 ? source.Canneds.Max(x => x.Id) + 1 : 1; var newCanned = Canned.Create(model); if (newCanned == null) { return null; } source.Canneds.Add(newCanned); source.SaveCanneds(); return newCanned.GetViewModel; } public CannedViewModel? Update(CannedBindingModel model) { var Canned = source.Canneds.FirstOrDefault(x => x.Id == model.Id); if (Canned == null) { return null; } Canned.Update(model); source.SaveCanneds(); return Canned.GetViewModel; } public CannedViewModel? Delete(CannedBindingModel model) { var Canned = source.Canneds.FirstOrDefault(x => x.Id == model.Id); if (Canned != null) { source.Canneds.Remove(Canned); source.SaveCanneds(); return Canned.GetViewModel; } return null; } } }