2024-05-22 10:46:39 +04:00
|
|
|
|
using SushiBarContracts.BindingModels;
|
2024-05-21 21:48:45 +04:00
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class MessageInfoStorage : IMessageInfoStorage
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
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 SushiBarDatabase();
|
|
|
|
|
return context.Messages
|
|
|
|
|
.Where(x => x.ClientId == model.ClientId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<MessageInfoViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
return context.Messages
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new SushiBarDatabase();
|
|
|
|
|
var newMessage = MessageInfo.Create(context, model);
|
2024-05-22 10:46:39 +04:00
|
|
|
|
if (newMessage == null || newMessage.Body == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId)))
|
2024-05-21 21:48:45 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Messages.Add(newMessage);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMessage.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|