PIbd-21_RazubaevSM_Plumbing.../PlumbingRepair/PlumbingRepairFileImplement/Implements/MessageInfoStorage.cs

72 lines
2.1 KiB
C#
Raw Normal View History

2024-05-02 22:08:35 +04:00
using PlumbingRepairContracts.BindingModels;
using PlumbingRepairContracts.SearchModels;
using PlumbingRepairContracts.StoragesContracts;
using PlumbingRepairContracts.ViewModels;
using PlumbingRepairFileImplement.Models;
namespace PlumbingRepairFileImplement.Implements
{
public class MessageInfoStorage : IMessageInfoStorage
{
private readonly DataFileSingleton source;
public MessageInfoStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<MessageInfoViewModel> GetFullList()
{
return source.Messages
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
var res = source.Messages
2024-05-02 22:08:35 +04:00
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
if (!(model.Page.HasValue && model.PageSize.HasValue))
{
return res.ToList();
}
return res.Skip(model.PageSize.Value * model.Page.Value).Take(model.PageSize.Value).ToList();
}
2024-05-02 22:08:35 +04:00
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
{
if (model.MessageId != null)
{
return null;
}
return source.Messages
.FirstOrDefault(x => x.MessageId == model.MessageId)
?.GetViewModel;
}
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
{
var newMessageInfo = MessageInfo.Create(model);
if (newMessageInfo == null)
{
return null;
}
source.Messages.Add(newMessageInfo);
source.SaveMessages();
return newMessageInfo.GetViewModel;
}
public MessageInfoViewModel? Update(MessageInfoBindingModel model)
{
var message = source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId);
if (message == null)
{
return null;
}
message.Update(model);
source.SaveMessages();
return message.GetViewModel;
}
}
2024-05-02 22:08:35 +04:00
}