From 0b7cbc388f182edad4d218a0ef92e9ea88e4899b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2?= Date: Tue, 18 Jun 2024 01:30:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20x2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MessageInfoLogic.cs | 67 +++++++++++++--- .../Implements/ClientStorage.cs | 1 + .../Models/Client.cs | 2 + .../DataFileSingleton.cs | 7 ++ .../Implements/MessageInfoStorage.cs | 63 +++++++++++++++ .../Models/MessageInfo.cs | 80 +++++++++++++++++++ .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 77 ++++++++++++++++++ .../Models/MessageInfo.cs | 53 ++++++++++++ .../ConfectioneryView/FormMail.Designer.cs | 62 ++++++++++++++ .../{FormViewMail.cs => FormMail.cs} | 6 +- .../{FormViewMail.resx => FormMail.resx} | 0 Confectionery/ConfectioneryView/FormMain.cs | 4 +- .../FormViewMail.Designer.cs | 62 -------------- Confectionery/ConfectioneryView/Program.cs | 2 +- 15 files changed, 410 insertions(+), 78 deletions(-) create mode 100644 Confectionery/ConfectioneryFileImplement/Implements/MessageInfoStorage.cs create mode 100644 Confectionery/ConfectioneryFileImplement/Models/MessageInfo.cs create mode 100644 Confectionery/ConfectioneryListImplement/Implements/MessageInfoStorage.cs create mode 100644 Confectionery/ConfectioneryListImplement/Models/MessageInfo.cs create mode 100644 Confectionery/ConfectioneryView/FormMail.Designer.cs rename Confectionery/ConfectioneryView/{FormViewMail.cs => FormMail.cs} (86%) rename Confectionery/ConfectioneryView/{FormViewMail.resx => FormMail.resx} (100%) delete mode 100644 Confectionery/ConfectioneryView/FormViewMail.Designer.cs diff --git a/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs b/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs index 2e1f3bf..c53ea3a 100644 --- a/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/MessageInfoLogic.cs @@ -15,15 +15,34 @@ namespace ConfectioneryBusinessLogic public class MessageInfoLogic : IMessageInfoLogic { private readonly ILogger _logger; + private readonly IMessageInfoStorage _messageInfoStorage; - public MessageInfoLogic(ILogger logger, IMessageInfoStorage MessageInfoStorage) + + private readonly IClientStorage _clientStorage; + + public MessageInfoLogic(ILogger logger, IMessageInfoStorage messageInfoStorage, IClientStorage clientStorage) { _logger = logger; - _messageInfoStorage = MessageInfoStorage; + _messageInfoStorage = messageInfoStorage; + _clientStorage = clientStorage; + } + + 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; } public bool Create(MessageInfoBindingModel model) { + CheckModel(model); if (_messageInfoStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); @@ -32,17 +51,45 @@ namespace ConfectioneryBusinessLogic return true; } - public List? ReadList(MessageInfoSearchModel? model) + private void CheckModel(MessageInfoBindingModel model, bool withParams = true) { - _logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId} ", model?.MessageId, model?.ClientId); - var list = (model == null) ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); - if (list == null) + if (model == null) { - _logger.LogWarning("ReadList return null list"); - return null; + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.MessageId)) + { + throw new ArgumentNullException("Не указан id сообщения", nameof(model.MessageId)); + } + if (string.IsNullOrEmpty(model.SenderName)) + { + throw new ArgumentNullException("Не указана почта отправителя", nameof(model.SenderName)); + } + if (string.IsNullOrEmpty(model.Subject)) + { + throw new ArgumentNullException("Не указан заголовок", nameof(model.Subject)); + } + if (string.IsNullOrEmpty(model.Body)) + { + throw new ArgumentNullException("Не указан текст сообщения", nameof(model.Subject)); + } + _logger.LogInformation("MessageInfo. MessageId: {MessageId}. SenderName: {SenderName}. DateDelivery: {DateDelivery} Subject: {Subject}. Body: {Body}", model.MessageId, model.SenderName, model.DateDelivery, model.Subject, model.Body); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.SenderName + }); + if (element == null) + { + _logger.LogWarning("Не удалось найти клиента, отправившего письмо с адреса Email: {Email}", model.SenderName); + } + else + { + model.ClientId = element.Id; } - _logger.LogInformation("ReadList. Count:{Count}", list.Count); - return list; } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs index 5611024..d2bf54c 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ClientStorage.cs @@ -43,6 +43,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) + || (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && string.IsNullOrEmpty(model.Password)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs index e72ae76..7e03e5a 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Client.cs @@ -26,6 +26,8 @@ namespace ConfectioneryDatabaseImplement.Models [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List ClientMessages { get; set; } = new(); public static Client? Create(ClientBindingModel model) { diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs index 4cc4c9d..429dcfc 100644 --- a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs +++ b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs @@ -22,6 +22,8 @@ namespace ConfectioneryFileImplement private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; + public List Components { get; private set; } public List Orders { get; private set; } @@ -32,6 +34,8 @@ namespace ConfectioneryFileImplement public List Implementers { get; private set; } + public List Messages { get; private set; } + public static DataFileSingleton GetInstance() { if (instance == null) @@ -51,6 +55,8 @@ namespace ConfectioneryFileImplement 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 => Component.Create(x)!)!; @@ -58,6 +64,7 @@ namespace ConfectioneryFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => 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/Confectionery/ConfectioneryFileImplement/Implements/MessageInfoStorage.cs b/Confectionery/ConfectioneryFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..b154cda --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,63 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return _source.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + return _source.Messages + .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 null; + } + return _source.Messages.FirstOrDefault(x => model.MessageId.Equals(x.MessageId))?.GetViewModel; + } + + 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/Confectionery/ConfectioneryFileImplement/Models/MessageInfo.cs b/Confectionery/ConfectioneryFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..9a0af9c --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Models/MessageInfo.cs @@ -0,0 +1,80 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement.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() + { + MessageId = model.MessageId, + ClientId = model.ClientId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + Body = model.Body + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + MessageId = element.Attribute("MessageId")!.Value, + ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + SenderName = element.Element("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Element("DateDelivery")!.Value), + Subject = element.Element("Subject")!.Value, + Body = element.Element("Body")!.Value + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("MessageId", MessageId), + new XElement("ClientId", ClientId), + new XElement("SenderName", SenderName), + new XElement("DateDelivery", DateDelivery), + new XElement("Subject", Subject), + new XElement("Body", Body) + ); + } +} diff --git a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs index d4931f6..bda2cc2 100644 --- a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs +++ b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace ConfectioneryListImplement public List Pastries { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } private DataListSingleton() { Components = new List(); @@ -22,6 +23,7 @@ namespace ConfectioneryListImplement Pastries = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() diff --git a/Confectionery/ConfectioneryListImplement/Implements/MessageInfoStorage.cs b/Confectionery/ConfectioneryListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..58ee527 --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,77 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var message in _source.Messages) + { + result.Add(message.GetViewModel); + } + return result; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + var result = new List(); + if (!model.ClientId.HasValue) + { + return result; + } + foreach (var message in _source.Messages) + { + if (message.ClientId.HasValue && message.ClientId == model.ClientId) + { + result.Add(message.GetViewModel); + } + } + return result; + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + foreach (var message in _source.Messages) + { + if (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/Confectionery/ConfectioneryListImplement/Models/MessageInfo.cs b/Confectionery/ConfectioneryListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..36bb35a --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Models/MessageInfo.cs @@ -0,0 +1,53 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryListImplement.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() + { + 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/Confectionery/ConfectioneryView/FormMail.Designer.cs b/Confectionery/ConfectioneryView/FormMail.Designer.cs new file mode 100644 index 0000000..c7f883d --- /dev/null +++ b/Confectionery/ConfectioneryView/FormMail.Designer.cs @@ -0,0 +1,62 @@ +namespace ConfectioneryView +{ + partial class FormMail + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + dataGridView = new DataGridView(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 12); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 51; + dataGridView.RowTemplate.Height = 29; + dataGridView.Size = new Size(776, 426); + dataGridView.TabIndex = 0; + // + // FormMail + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(dataGridView); + Name = "FormMail"; + Text = "FormMail"; + Load += FormMail_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryView/FormViewMail.cs b/Confectionery/ConfectioneryView/FormMail.cs similarity index 86% rename from Confectionery/ConfectioneryView/FormViewMail.cs rename to Confectionery/ConfectioneryView/FormMail.cs index 52a8c17..b5c72a4 100644 --- a/Confectionery/ConfectioneryView/FormViewMail.cs +++ b/Confectionery/ConfectioneryView/FormMail.cs @@ -12,18 +12,18 @@ using System.Windows.Forms; namespace ConfectioneryView { - public partial class FormViewMail : Form + public partial class FormMail : Form { private readonly ILogger _logger; private readonly IMessageInfoLogic _logic; - public FormViewMail(ILogger logger, IMessageInfoLogic logic) + public FormMail(ILogger logger, IMessageInfoLogic logic) { InitializeComponent(); _logger = logger; _logic = logic; } - private void ViewMailForm_Load(object sender, EventArgs e) + private void FormMail_Load(object sender, EventArgs e) { try { diff --git a/Confectionery/ConfectioneryView/FormViewMail.resx b/Confectionery/ConfectioneryView/FormMail.resx similarity index 100% rename from Confectionery/ConfectioneryView/FormViewMail.resx rename to Confectionery/ConfectioneryView/FormMail.resx diff --git a/Confectionery/ConfectioneryView/FormMain.cs b/Confectionery/ConfectioneryView/FormMain.cs index 0350fd6..7d36da8 100644 --- a/Confectionery/ConfectioneryView/FormMain.cs +++ b/Confectionery/ConfectioneryView/FormMain.cs @@ -166,8 +166,8 @@ namespace ConfectioneryView private void ПочтаToolStripMenuItem_Click(object sender, EventArgs e) { - var service = Program.ServiceProvider?.GetService(typeof(FormViewMail)); - if (service is FormViewMail form) + var service = Program.ServiceProvider?.GetService(typeof(FormMail)); + if (service is FormMail form) { form.ShowDialog(); } diff --git a/Confectionery/ConfectioneryView/FormViewMail.Designer.cs b/Confectionery/ConfectioneryView/FormViewMail.Designer.cs deleted file mode 100644 index dcc1ba6..0000000 --- a/Confectionery/ConfectioneryView/FormViewMail.Designer.cs +++ /dev/null @@ -1,62 +0,0 @@ -namespace ConfectioneryView -{ - partial class FormViewMail - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - dataGridView = new DataGridView(); - ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); - SuspendLayout(); - // - // dataGridView - // - dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Location = new Point(12, 12); - dataGridView.Name = "dataGridView"; - dataGridView.RowHeadersWidth = 51; - dataGridView.RowTemplate.Height = 29; - dataGridView.Size = new Size(776, 426); - dataGridView.TabIndex = 0; - // - // ViewMailForm - // - AutoScaleDimensions = new SizeF(8F, 20F); - AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); - Controls.Add(dataGridView); - Name = "ViewMailForm"; - Text = "ViewMailForm"; - Load += ViewMailForm_Load; - ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); - ResumeLayout(false); - } - - #endregion - - private DataGridView dataGridView; - } -} \ No newline at end of file diff --git a/Confectionery/ConfectioneryView/Program.cs b/Confectionery/ConfectioneryView/Program.cs index 27bbabb..bdd1d59 100644 --- a/Confectionery/ConfectioneryView/Program.cs +++ b/Confectionery/ConfectioneryView/Program.cs @@ -102,7 +102,7 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); } private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck();