2024-05-02 22:08:35 +04:00
|
|
|
|
using PlumbingRepairContracts.BindingModels;
|
|
|
|
|
using PlumbingRepairContracts.SearchModels;
|
|
|
|
|
using PlumbingRepairContracts.StoragesContracts;
|
|
|
|
|
using PlumbingRepairContracts.ViewModels;
|
|
|
|
|
using PlumbingRepairDatabaseImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace PlumbingRepairDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class MessageInfoStorage : IMessageInfoStorage
|
|
|
|
|
{
|
|
|
|
|
public List<MessageInfoViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new PlumbingRepairDatabase();
|
|
|
|
|
return context.Messages
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PlumbingRepairDatabase();
|
|
|
|
|
return context.Messages
|
2024-05-11 19:41:48 +04:00
|
|
|
|
.Where(x => x.ClientId.HasValue && x.ClientId == model.ClientId)
|
2024-05-02 22:08:35 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
|
|
|
|
{
|
2024-05-11 19:41:48 +04:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.MessageId))
|
|
|
|
|
return null;
|
|
|
|
|
using var context = new PlumbingRepairDatabase();
|
2024-05-02 22:08:35 +04:00
|
|
|
|
return context.Messages
|
|
|
|
|
.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new PlumbingRepairDatabase();
|
2024-05-11 19:41:48 +04:00
|
|
|
|
model.ClientId = context.Clients.FirstOrDefault(x => x.Email.Equals(model.SenderName))?.Id;
|
2024-05-02 22:08:35 +04:00
|
|
|
|
var newMessageInfo = MessageInfo.Create(model);
|
|
|
|
|
|
2024-05-11 19:41:48 +04:00
|
|
|
|
if (newMessageInfo == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId)))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.Messages.Add(newMessageInfo);
|
2024-05-02 22:08:35 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newMessageInfo.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|