Реализовал хранение писем в БД
This commit is contained in:
parent
4d9d154821
commit
f3d1de0db8
@ -0,0 +1,50 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.SearchModels;
|
||||
using SecuritySystemContracts.StoragesContracts;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
using SecuritySystemDatabaseImplement.Models;
|
||||
|
||||
namespace SecuritySystemDatabaseImplement.Implements
|
||||
{
|
||||
public class MessageInfoStorage : IMessageInfoStorage
|
||||
{
|
||||
public MessageInfoViewModel? GetElement(MessageInfoSearchModel model)
|
||||
{
|
||||
return GetFilteredList(model).FirstOrDefault();
|
||||
}
|
||||
|
||||
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
|
||||
{
|
||||
var messageInfos = GetFullList();
|
||||
if (!string.IsNullOrEmpty(model.MessageId))
|
||||
{
|
||||
messageInfos = messageInfos.Where(x => x.MessageId == model.MessageId).ToList();
|
||||
}
|
||||
if (model.ClientId != null)
|
||||
{
|
||||
messageInfos = messageInfos.Where(x => x.ClientId == model.ClientId).ToList();
|
||||
}
|
||||
return messageInfos;
|
||||
}
|
||||
|
||||
public List<MessageInfoViewModel> GetFullList()
|
||||
{
|
||||
using var context = new SecuritySystemDatabase();
|
||||
return context.MessageInfos.Include(x => x.Client).Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public MessageInfoViewModel? Insert(MessageInfoBindingModel model)
|
||||
{
|
||||
var messageInfo = MessageInfo.Create(model);
|
||||
if (messageInfo == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new SecuritySystemDatabase();
|
||||
context.MessageInfos.Add(messageInfo);
|
||||
context.SaveChanges();
|
||||
return messageInfo.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
using SecuritySystemContracts.BindingModels;
|
||||
using SecuritySystemContracts.ViewModels;
|
||||
using SecuritySystemDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.MessageInfoModel.DataAnnotations.Schema;
|
||||
|
||||
namespace SecuritySystemDatabaseImplement.Models
|
||||
{
|
||||
public class MessageInfo : IMessageInfoModel
|
||||
{
|
||||
[Required]
|
||||
public string MessageId { get; set; } = string.Empty;
|
||||
public int? ClientId { get; set; }
|
||||
public virtual Client? Client { get; set; }
|
||||
[Required]
|
||||
public string SenderName { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public DateTime DateDelivery { get; set; } = DateTime.Now;
|
||||
[Required]
|
||||
public string Subject { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public string Body { get; set; } = string.Empty;
|
||||
|
||||
public static MessageInfo? Create(MessageInfoBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new MessageInfo()
|
||||
{
|
||||
MessageId = model.MessageId,
|
||||
ClientId = model.ClientId,
|
||||
SenderName = model.SenderName,
|
||||
DateDelivery = model.DateDelivery,
|
||||
Subject = model.Subject,
|
||||
Body = model.Body,
|
||||
};
|
||||
}
|
||||
public static MessageInfo Create(MessageInfoViewModel model)
|
||||
{
|
||||
return new MessageInfo
|
||||
{
|
||||
MessageId = model.MessageId,
|
||||
ClientId = model.ClientId,
|
||||
SenderName = model.SenderName,
|
||||
DateDelivery = model.DateDelivery,
|
||||
Subject = model.Subject,
|
||||
Body = model.Body,
|
||||
};
|
||||
}
|
||||
public void Update(MessageInfoBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
MessageId = model.MessageId;
|
||||
ClientId = model.ClientId;
|
||||
SenderName = model.SenderName;
|
||||
DateDelivery = model.DateDelivery;
|
||||
Subject = model.Subject;
|
||||
Body = model.Body;
|
||||
}
|
||||
public MessageInfoViewModel GetViewModel => new()
|
||||
{
|
||||
MessageId = MessageId,
|
||||
ClientId = ClientId,
|
||||
SenderName = SenderName,
|
||||
DateDelivery = DateDelivery,
|
||||
Subject = Subject,
|
||||
Body = Body,
|
||||
};
|
||||
}
|
||||
}
|
@ -21,6 +21,7 @@ namespace SecuritySystemDatabaseImplement
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<Client> Clients { set; get; }
|
||||
public virtual DbSet<Implementer> Implementers { set; get; }
|
||||
public virtual DbSet<MessageInfo> MessageInfos { set; get; }
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user