PIbd-23-Radaev-A.V.-GiftShop/GiftShop/GiftShopDatabaseImplement/Implements/MessageInfoStorage.cs
Arkadiy Radaev 44fa88599e res8
2024-05-17 13:28:57 +04:00

53 lines
1.5 KiB
C#

using GiftShopContracts.BindingModels;
using GiftShopContracts.SearchModels;
using GiftShopContracts.StoragesContracts;
using GiftShopContracts.ViewModels;
using GiftShopDatabaseImplement.Models;
namespace GiftShopDatabaseImplement.Implements
{
public class MessageInfoStorage : IMessageInfoStorage
{
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
{
using var context = new GiftShopDatabase();
if (string.IsNullOrEmpty(model.MessageId))
{
return null;
}
return context.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel;
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
//if (!model.ClientId.HasValue) return new();
using var context = new GiftShopDatabase();
return context.Messages
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MessageInfoViewModel> GetFullList()
{
using var context = new GiftShopDatabase();
return context.Messages
.Select(x => x.GetViewModel)
.ToList();
}
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
{
using var context = new GiftShopDatabase();
var newMessage = Message.Create(model,context);
if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId)))
{
return null;
}
context.Messages.Add(newMessage);
context.SaveChanges();
return newMessage.GetViewModel;
}
}
}