using SewingDressesContracts.BindingModels; using SewingDressesContracts.ViewModels; using SewingDressesContracts.SearchModels; using SewingDressesContracts.StoragesContracts; using SewingDressesDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; namespace SewingDressesDatabaseImplement.Implements { public class MessageInfoStorage : IMessageInfoStorage { public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) { if (string.IsNullOrEmpty(model.MessageId)) { return null; } using var context = new SewingDressesDatabase(); return context.Messages.Include(x => x.Client).FirstOrDefault(x => x.MessageId.Equals(model.MessageId))?.GetViewModel; } public List GetFilteredList(MessageInfoSearchModel model) { if (!model.ClientId.HasValue) { return new(); } using var context = new SewingDressesDatabase(); return context.Messages.Include(x => x.Client).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); } public List GetFullList() { using var context = new SewingDressesDatabase(); return context.Messages.Include(x => x.Client).Select(x => x.GetViewModel).ToList(); } public MessageInfoViewModel? Insert(MessageInfoBindingModel model) { using var context = new SewingDressesDatabase(); 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; } } }