diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs index 9e20a35..7d4cab5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs @@ -4,6 +4,7 @@ using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopContracts.StoragesContracts; using CarpentryWorkshopContracts.ViewModels; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; namespace CarpentryWorkshopBusinessLogic.BusinessLogics { @@ -98,7 +99,17 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет логина(почты) клиента", nameof(model.Email)); } - if (string.IsNullOrEmpty(model.Password)) + if (!Regex.IsMatch(model.Email, @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|" + + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(? logger, IMessageInfoStorage MessageInfoStorage) + { + _logger = logger; + _messageInfoStorage = MessageInfoStorage; + } + + public bool Create(MessageInfoBindingModel model) + { + if (_messageInfoStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId} ", model?.MessageId, model?.ClientId); + var list = (model == null) ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 439f21e..184e4bc 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,4 +1,5 @@ -using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopBusinessLogic.MailWorker; +using CarpentryWorkshopContracts.BindingModels; using CarpentryWorkshopContracts.BusinessLogicsContracts; using CarpentryWorkshopContracts.SearchModels; using CarpentryWorkshopContracts.StoragesContracts; @@ -12,13 +13,17 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; static readonly object locker = new object(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; - } + _mailWorker = mailWorker; + _clientLogic = clientLogic; + } public OrderViewModel? ReadElement(OrderSearchModel model) { @@ -56,12 +61,16 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics CheckModel(model); if (model.Status != OrderStatus.Неизвестен) return false; model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) - { + var result = _orderStorage.Insert(model); + if (result == null) + { _logger.LogWarning("Insert operation failed"); return false; } - return true; + SendOrderStatusMail(result.ClientId, $"A new order has been created. " + + $"Order number #{result.Id}", $"Order #{result.Id} from {result.DateCreate} " + + $"for the amount {result.Sum:0.00} {result.Status}"); + return true; } public bool ChangeStatus(OrderBindingModel model, OrderStatus status) @@ -84,12 +93,15 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - if (_orderStorage.Update(model) == null) + var result = _orderStorage.Update(model); + if (result == null) { model.Status = oldStatus; _logger.LogWarning("Update operation failed"); return false; } + SendOrderStatusMail(result.ClientId, $"Status of order #{result.Id} has been changed", + $"Order #{result.Id} status has been changed to {result.Status}"); return true; } @@ -139,5 +151,21 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics } _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); } - } + + private bool SendOrderStatusMail(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + if (client == null) + { + return false; + } + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; + } + } } diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj index 9629fff..76aa008 100644 --- a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/CarpentryWorkshopBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..e4e5834 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,77 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace CarpentryWorkshopBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + protected string _mailPassword = string.Empty; + protected string _smtpClientHost = string.Empty; + protected int _smtpClientPort; + protected string _popHost = string.Empty; + protected int _popPort; + private readonly IMessageInfoLogic _messageInfoLogic; + private readonly IClientLogic _clientLogic; + private readonly ILogger _logger; + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + _clientLogic = clientLogic; + } + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + _logger.LogDebug("Config: {login}, {password}, {clientHost},{ clientPOrt}, { popHost}, { popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + { + return; + } + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + await SendMailAsync(info); + } + public async void MailCheck() + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + if (string.IsNullOrEmpty(_popHost) || _popPort == 0) + { + return; + } + if (_messageInfoLogic == null) + { + return; + } + var list = await ReceiveMailAsync(); + _logger.LogDebug("Check Mail: {Count} new mails", list.Count); + foreach (var mail in list) + { + mail.ClientId = _clientLogic.ReadElement(new() { Email = mail.SenderName })?.Id; + _messageInfoLogic.Create(mail); + } + } + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + protected abstract Task> + ReceiveMailAsync(); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/MailWorker/MailKitWorker.cs b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..e4a9f01 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,75 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using MailKit.Net.Pop3; +using MailKit.Security; +using System.Net.Mail; +using System.Net; +using System.Text; + +namespace CarpentryWorkshopBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) : base(logger, messageInfoLogic, clientLogic) { } + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + protected override async Task> + ReceiveMailAsync() + { + var list = new List(); + using var client = new Pop3Client(); + await Task.Run(() => + { + try + { + client.Connect(_popHost, _popPort, SecureSocketOptions.SslOnConnect); + client.Authenticate(_mailLogin, _mailPassword); + for (int i = 0; i < client.Count; i++) + { + var message = client.GetMessage(i); + foreach (var mail in message.From.Mailboxes) + { + list.Add(new MessageInfoBindingModel + { + DateDelivery = message.Date.DateTime, + MessageId = message.MessageId, + SenderName = mail.Address, + Subject = message.Subject, + Body = message.TextBody + }); + } + } + } + catch (MailKit.Security.AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + return list; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MailConfigBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..13cab1a --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,12 @@ +namespace CarpentryWorkshopContracts.BindingModels +{ + public class MailConfigBindingModel + { + public string MailLogin { get; set; } = string.Empty; + public string MailPassword { get; set; } = string.Empty; + public string SmtpClientHost { get; set; } = string.Empty; + public int SmtpClientPort { get; set; } + public string PopHost { get; set; } = string.Empty; + public int PopPort { get; set; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..b1d74cd --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,9 @@ +namespace CarpentryWorkshopContracts.BindingModels +{ + public class MailSendInfoBindingModel + { + public string MailAddress { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..62e9132 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,14 @@ +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopContracts.BindingModels +{ + public class MessageInfoBindingModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + public int? ClientId { get; set; } + public string SenderName { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Body { get; set; } = string.Empty; + public DateTime DateDelivery { get; set; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..da30c8b --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,12 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/MessageInfoSearchModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..31c4cbb --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,8 @@ +namespace CarpentryWorkshopContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..48b9e59 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,14 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.ViewModels; + +namespace CarpentryWorkshopContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + List GetFilteredList(MessageInfoSearchModel model); + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..b0cad95 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,24 @@ +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel; + +namespace CarpentryWorkshopContracts.ViewModels +{ + public class MessageInfoViewModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + + public int? ClientId { get; set; } + + [DisplayName("Отправитель")] + public string SenderName { get; set; } = string.Empty; + + [DisplayName("Дата письма")] + public DateTime DateDelivery { get; set; } + + [DisplayName("Заголовок")] + public string Subject { get; set; } = string.Empty; + + [DisplayName("Текст")] + public string Body { get; set; } = string.Empty; + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..7b4dac5 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,12 @@ +namespace CarpentryWorkshopDataModels.Models +{ + public interface IMessageInfoModel + { + string MessageId { get; } + int? ClientId { get; } + string SenderName { get; } + DateTime DateDelivery { get; } + string Subject { get; } + string Body { get; } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs index c9e29ba..62871ea 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/CarpentryWorkshopDatabase.cs @@ -19,5 +19,6 @@ namespace CarpentryWorkshopDatabaseImplement public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet MessageInfos { set; get; } } } diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..5ca4879 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,47 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDatabaseImplement.Models; + +namespace CarpentryWorkshopDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public List GetFullList() + { + using var context = new CarpentryWorkshopDatabase(); + return context.MessageInfos.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.MessageInfos.Where(x => x.ClientId.HasValue && x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return new(); + } + using var context = new CarpentryWorkshopDatabase(); + return context.MessageInfos.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + using var context = new CarpentryWorkshopDatabase(); + context.MessageInfos.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs index 6c5386b..95e95d8 100644 --- a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/Client.cs @@ -22,7 +22,10 @@ namespace CarpentryWorkshopDatabaseImplement.Models [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); - public static Client? Create(ClientBindingModel model) + [ForeignKey("ClientId")] + public virtual List Messages { get; set; } = new(); + + public static Client? Create(ClientBindingModel model) { if (model == null) { diff --git a/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..a043c40 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,59 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace CarpentryWorkshopDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + [DatabaseGenerated(DatabaseGeneratedOption.None)] + 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; } + + [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() + { + 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 + }; + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs index 5c2fcda..9f6e4a5 100644 --- a/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/DataFileSingleton.cs @@ -11,11 +11,13 @@ namespace CarpentryWorkshopFileImplement private readonly string WoodFileName = "Wood.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Woods { get; private set; } public List Clients { get; private set; } public List Implementers { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -34,6 +36,8 @@ namespace CarpentryWorkshopFileImplement "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Orders, ImplementerFileName, + "Messages", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => @@ -46,6 +50,8 @@ namespace CarpentryWorkshopFileImplement Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + Messages = LoadData(MessageInfoFileName, "MessageInfo", + x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..60fceaa --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,53 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopFileImplement.Models; + +namespace CarpentryWorkshopFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId != null) + { + return _source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + return _source.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return _source.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/MessageInfo.cs b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..63acd24 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopFileImplement/Models/MessageInfo.cs @@ -0,0 +1,75 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; +using System.Xml.Linq; + +namespace CarpentryWorkshopFileImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Body = element.Attribute("Body")!.Value, + Subject = element.Attribute("Subject")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + SenderName = element.Attribute("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Body", Body), + new XAttribute("Subject", Subject), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs index aed78f9..54b00e1 100644 --- a/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/DataListSingleton.cs @@ -10,6 +10,7 @@ namespace CarpentryWorkshopListImplement public List Woods { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); @@ -17,6 +18,7 @@ namespace CarpentryWorkshopListImplement Woods = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..a77281e --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,63 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.SearchModels; +using CarpentryWorkshopContracts.StoragesContracts; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopListImplement.Models; + +namespace CarpentryWorkshopListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + List result = new(); + foreach (var item in _source.Messages) + { + if (item.ClientId.HasValue && item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + return result; + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + foreach (var message in _source.Messages) + { + if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) + { + return message.GetViewModel; + } + } + return null; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/MessageInfo.cs b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..b445985 --- /dev/null +++ b/CarpentryWorkshop/CarpentryWorkshopListImplement/Models/MessageInfo.cs @@ -0,0 +1,48 @@ +using CarpentryWorkshopContracts.BindingModels; +using CarpentryWorkshopContracts.ViewModels; +using CarpentryWorkshopDataModels.Models; + +namespace CarpentryWorkshopListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + } +}