PIbd-23_Elatomtsev_L.K._Con.../Confectionery/ConfectioneryDatabaseImplement/Implements/MessageInfoStorage.cs

54 lines
1.8 KiB
C#
Raw Normal View History

2024-05-22 07:00:31 +04:00
using ConfectioneryContracts.BindingModels;
using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContracts;
using ConfectioneryContracts.ViewModels;
using ConfectioneryDatabaseImplement.Models;
namespace ConfectioneryDatabaseImplement.Implements
{
public class MessageInfoStorage : IMessageInfoStorage
{
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
{
using var context = new ConfectioneryDatabase();
if (model.MessageId != null)
{
return context.Messages.FirstOrDefault(
x => x.MessageId == model.MessageId)?.GetViewModel;
}
return null;
}
public List<MessageInfoViewModel> GetFilteredList(
MessageInfoSearchModel model)
{
using var context = new ConfectioneryDatabase();
return context.Messages
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageInfoViewModel> GetFullList()
{
using var context = new ConfectioneryDatabase();
return context.Messages
.Select(x => x.GetViewModel)
.ToList();
}
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
{
using var context = new ConfectioneryDatabase();
var newMessage = MessageInfo.Create(context, model);
if (newMessage == null || context.Messages.Any(
x => x.MessageId.Equals(model.MessageId)))
{
return null;
}
context.Messages.Add(newMessage);
context.SaveChanges();
return newMessage.GetViewModel;
}
}
}