ISEbd-21_Agliullov.D.A._Con.../ConfectionaryListImplement/MessageInfoStorage.cs

76 lines
1.8 KiB
C#
Raw Normal View History

2023-03-13 19:08:51 +04:00
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels;
using ConfectioneryListImplement.Models;
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)
{
2023-03-13 19:37:04 +04:00
if (model.MessageId != null && model.MessageId.Equals(message.MessageId))
2023-03-13 19:08:51 +04:00
return message.GetViewModel;
}
return null;
}
2023-03-17 19:15:58 +04:00
public List<MessageInfoViewModel>? GetListOnPage(int page, int pageSize)
{
if (page * pageSize >= _source.Messages.Count)
{
return null;
}
List<MessageInfoViewModel> result = new();
for (var i = (page - 1) * pageSize; i < page * pageSize; i++)
{
result.Add(_source.Messages[i].GetViewModel);
}
return result;
}
2023-03-13 19:08:51 +04:00
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
List<MessageInfoViewModel> result = new();
foreach (var item in _source.Messages)
{
if (item.ClientId.HasValue && item.ClientId == model.ClientId)
{
result.Add(item.GetViewModel);
}
}
return result;
}
public List<MessageInfoViewModel> GetFullList()
{
List<MessageInfoViewModel> 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;
}
}
}