70 lines
2.5 KiB
C#
70 lines
2.5 KiB
C#
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();
|
|
var messages = context.Messages
|
|
.Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
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();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|