2024-05-05 00:23:11 +04:00
|
|
|
|
using AutomobilePlantContracts.BindingModels;
|
|
|
|
|
using AutomobilePlantContracts.SearchModels;
|
|
|
|
|
using AutomobilePlantContracts.StoragesContracts;
|
|
|
|
|
using AutomobilePlantContracts.ViewModels;
|
|
|
|
|
using AutomobilePlantDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace AutomobilePlantDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class MessageInfoStorage : IMessageInfoStorage
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new AutomobilePlantDatabase();
|
|
|
|
|
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 AutomobilePlantDatabase();
|
2024-05-05 23:46:21 +04:00
|
|
|
|
var messages = context.Messages
|
2024-05-06 10:30:28 +04:00
|
|
|
|
.Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId)
|
2024-05-05 00:23:11 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-05-05 23:46:21 +04:00
|
|
|
|
if (!model.Page.HasValue || !model.PageSize.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return messages.ToList();
|
|
|
|
|
}
|
|
|
|
|
return messages.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList();
|
2024-05-05 00:23:11 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<MessageInfoViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new AutomobilePlantDatabase();
|
|
|
|
|
return context.Messages
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new AutomobilePlantDatabase();
|
|
|
|
|
var newMessage = MessageInfo.Create(model);
|
|
|
|
|
if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId)))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Messages.Add(newMessage);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMessage.GetViewModel;
|
|
|
|
|
}
|
2024-05-05 23:46:21 +04:00
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? Update(MessageInfoBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new AutomobilePlantDatabase();
|
|
|
|
|
var message = context.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId));
|
|
|
|
|
if (message != null)
|
|
|
|
|
{
|
|
|
|
|
message.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
return message?.GetViewModel;
|
|
|
|
|
}
|
2024-05-05 00:23:11 +04:00
|
|
|
|
}
|
|
|
|
|
}
|