using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryListImplement.Models; using System.Collections.Generic; namespace ConfectioneryListImplement { public class MessageInfoStorage : IMessageInfoStorage { private readonly DataListSingleton _source; public MessageInfoStorage() { _source = DataListSingleton.GetInstance(); } public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) { foreach (var message in _source.Messages) { if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) return message.GetViewModel; } return null; } public List GetFilteredList(MessageInfoSearchModel model) { List result = new(); foreach (var item in _source.Messages) { if (item.ClientId.HasValue && item.ClientId == model.ClientId) { result.Add(item.GetViewModel); } } if (!(model.Page.HasValue && model.PageSize.HasValue)) { return result; } if (model.Page * model.PageSize >= result.Count) { return null; } List filteredResult = new(); for (var i = (model.Page.Value - 1) * model.PageSize.Value; i < model.Page.Value * model.PageSize.Value; i++) { filteredResult.Add(result[i]); } return filteredResult; } public List GetFullList() { List result = new(); foreach (var item in _source.Messages) { result.Add(item.GetViewModel); } return result; } public MessageInfoViewModel? Insert(MessageInfoBindingModel model) { var newMessage = MessageInfo.Create(model); if (newMessage == null) { return null; } _source.Messages.Add(newMessage); return newMessage.GetViewModel; } public MessageInfoViewModel? Update(MessageInfoBindingModel model) { foreach (var message in _source.Messages) { if (message.MessageId.Equals(model.MessageId)) { message.Update(model); return message.GetViewModel; } } return null; } } }