using PlumbingRepairContracts.BindingModels; using PlumbingRepairContracts.SearchModels; using PlumbingRepairContracts.StoragesContracts; using PlumbingRepairContracts.ViewModels; using PlumbingRepairFileImplement.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PlumbingRepairFileImplement.Implements { public class MessageInfoStorage : IMessageInfoStorage { private readonly DataFileSingleton source; public MessageInfoStorage() { source = DataFileSingleton.GetInstance(); } public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) { if (model.MessageId == null) return null; return source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; } public List GetFilteredList(MessageInfoSearchModel model) { if (!model.ClientId.HasValue) return new(); return source.Messages .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { return source.Messages .Select(x => x.GetViewModel) .ToList(); } public MessageInfoViewModel? Insert(MessageInfoBindingModel model) { var newMessage = Message.Create(model); if (newMessage == null) { return null; } source.Messages.Add(newMessage); source.SaveMessages(); return newMessage.GetViewModel; } } }