PIbd-21_Bakalskaya_E.D._Sus.../SushiBarDatabaseImplement/Implements/MessageInfoStorage.cs

72 lines
2.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using SushiBarContracts.BindingModel;
using SushiBarContracts.SearchModel;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarDatabaseImplement.Implements
{
public class MessageInfoStorage : IMessageInfoStorage
{
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
{
using var context = new SushiBarDatabase();
if(string.IsNullOrEmpty(model.MessageId)) return null;
return context.Messages
.Include(x => x.Client)
.FirstOrDefault(x => x.MessageId == model.MessageId)?
.GetViewModel;
}
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{
using var context = new SushiBarDatabase();
if (model.ClientId.HasValue)
{
return context.Messages
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public List<MessageInfoViewModel> GetFullList()
{
using var context = new SushiBarDatabase();
return context.Messages
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
{
using var context = new SushiBarDatabase();
var newMessage = MessageInfo.Create(model);
if (newMessage == null) return null;
context.Messages.Add(newMessage);
context.SaveChanges();
return newMessage.GetViewModel;
/* using var context = new SushiBarDatabase();
model.ClientId = context.Clients.FirstOrDefault(x => x.Email.Equals(model.SenderName))?.Id;
var message = MessageInfo.Create(model);
if (message == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId)))
{
return null;
}
context.Messages.Add(message);
context.SaveChanges();
return message.GetViewModel;*/
}
}
}