2023-03-13 20:02:18 +04:00
|
|
|
|
using ConfectioneryContracts.BindingModels;
|
|
|
|
|
using ConfectioneryContracts.SearchModels;
|
|
|
|
|
using ConfectioneryContracts.StoragesContract;
|
|
|
|
|
using ConfectioneryContracts.ViewModels;
|
|
|
|
|
using ConfectioneryDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace ConfectioneryDatabaseImplement
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 19:15:58 +04:00
|
|
|
|
public List<MessageInfoViewModel>? GetListOnPage(int page, int pageSize)
|
|
|
|
|
{
|
|
|
|
|
using var context = new ConfectioneryDatabase();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return context.Messages.Skip((page - 1) * pageSize).Take(pageSize)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-13 20:02:18 +04:00
|
|
|
|
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(model);
|
2023-03-13 22:59:37 +04:00
|
|
|
|
if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId)))
|
2023-03-13 20:02:18 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Messages.Add(newMessage);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMessage.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|