From 85e325491e0bd6b00d7772e607fa52ea809c7dc8 Mon Sep 17 00:00:00 2001 From: Yunusov_Niyaz Date: Sat, 4 May 2024 00:47:40 +0400 Subject: [PATCH] =?UTF-8?q?7=20=D0=BB=D0=B0=D0=B1=D0=B0=20=D0=B2=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=20=D1=87=D1=82=D0=BE-=D1=82=D0=BE=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CarRepairShop/CarRepairShop/App.config | 11 ++ .../CarRepairShop/FormMails.Designer.cs | 62 +++++++++ CarRepairShop/CarRepairShop/FormMails.cs | 39 ++++++ CarRepairShop/CarRepairShop/FormMails.resx | 120 ++++++++++++++++++ .../CarRepairShop/FormMain.Designer.cs | 19 ++- CarRepairShop/CarRepairShop/FormMain.cs | 8 ++ CarRepairShop/CarRepairShop/Program.cs | 42 +++++- .../BusinessLogics/ClientLogic.cs | 15 ++- .../BusinessLogics/MessageInfoLogic.cs | 41 ++++++ .../BusinessLogics/OrderLogic.cs | 45 +++++-- .../BusinessLogics/WorkModeling.cs | 3 +- .../CarRepairShopBusinessLogic.csproj | 1 + .../MailWorker/AbstractMailWorker.cs | 79 ++++++++++++ .../MailWorker/MailKitWorker.cs | 76 +++++++++++ .../BindingModels/MailConfigBindingModel.cs | 12 ++ .../BindingModels/MailSendInfoBindingModel.cs | 9 ++ .../BindingModels/MessageInfoBindingModel.cs | 19 +++ .../IMessageInfoLogic.cs | 13 ++ .../SearchModels/MessageInfoSearchModel.cs | 9 ++ .../StoragesContracts/IMessageInfoStorage.cs | 17 +++ .../ViewModels/MessageInfoViewModel.cs | 19 +++ .../IMessageInfoModel.cs | 12 ++ .../Implements/ClientStorage.cs | 4 +- .../Implements/MessageInfoStorage.cs | 51 ++++++++ .../Models/MessageInfo.cs | 52 ++++++++ .../RepairsShopDatabase.cs | 1 + .../DataFileSingleton.cs | 6 + .../Implements/MessageInfoStorage.cs | 53 ++++++++ .../Models/MessageInfo.cs | 75 +++++++++++ .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 61 +++++++++ .../Models/MessageInfo.cs | 48 +++++++ .../Controllers/ClientController.cs | 24 +++- CarRepairShop/CarRepairShopRestApi/Program.cs | 25 ++++ .../CarRepairShopRestApi/appsettings.json | 8 +- .../CarRepairShowClientApp.csproj | 16 +++ .../Controllers/HomeController.cs | 15 ++- .../Views/Home/Mails.cshtml | 54 ++++++++ .../Views/Shared/_Layout.cshtml | 3 + 39 files changed, 1139 insertions(+), 30 deletions(-) create mode 100644 CarRepairShop/CarRepairShop/App.config create mode 100644 CarRepairShop/CarRepairShop/FormMails.Designer.cs create mode 100644 CarRepairShop/CarRepairShop/FormMails.cs create mode 100644 CarRepairShop/CarRepairShop/FormMails.resx create mode 100644 CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs create mode 100644 CarRepairShop/CarRepairShopBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 CarRepairShop/CarRepairShopBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 CarRepairShop/CarRepairShopContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 CarRepairShop/CarRepairShopContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 CarRepairShop/CarRepairShopContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 CarRepairShop/CarRepairShopContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 CarRepairShop/CarRepairShopContracts/StoragesContracts/IMessageInfoStorage.cs create mode 100644 CarRepairShop/CarRepairShopContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 CarRepairShop/CarRepairShopDataModels/IMessageInfoModel.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 CarRepairShop/CarRepairShopDatabaseImplement/Models/MessageInfo.cs create mode 100644 CarRepairShop/CarRepairShopFileImplement/Implements/MessageInfoStorage.cs create mode 100644 CarRepairShop/CarRepairShopFileImplement/Models/MessageInfo.cs create mode 100644 CarRepairShop/CarRepairShopListImplement/Implements/MessageInfoStorage.cs create mode 100644 CarRepairShop/CarRepairShopListImplement/Models/MessageInfo.cs create mode 100644 CarRepairShop/CarRepairShowClientApp/Views/Home/Mails.cshtml diff --git a/CarRepairShop/CarRepairShop/App.config b/CarRepairShop/CarRepairShop/App.config new file mode 100644 index 0000000..290d9ca --- /dev/null +++ b/CarRepairShop/CarRepairShop/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/CarRepairShop/CarRepairShop/FormMails.Designer.cs b/CarRepairShop/CarRepairShop/FormMails.Designer.cs new file mode 100644 index 0000000..68bf727 --- /dev/null +++ b/CarRepairShop/CarRepairShop/FormMails.Designer.cs @@ -0,0 +1,62 @@ +namespace CarRepairShop +{ + partial class FormMails + { + /// + /// 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; + // + // FormMails + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(dataGridView); + Name = "Почта"; + Text = "FormMails"; + Load += ViewMailForm_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/CarRepairShop/CarRepairShop/FormMails.cs b/CarRepairShop/CarRepairShop/FormMails.cs new file mode 100644 index 0000000..4e8e782 --- /dev/null +++ b/CarRepairShop/CarRepairShop/FormMails.cs @@ -0,0 +1,39 @@ +using CarRepairShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace CarRepairShop +{ + public partial class FormMails : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + public FormMails(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void ViewMailForm_Load(object sender, EventArgs e) + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["MessageId"].Visible = false; + dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка списка писем"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки писем"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } +} diff --git a/CarRepairShop/CarRepairShop/FormMails.resx b/CarRepairShop/CarRepairShop/FormMails.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CarRepairShop/CarRepairShop/FormMails.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CarRepairShop/CarRepairShop/FormMain.Designer.cs b/CarRepairShop/CarRepairShop/FormMain.Designer.cs index 217d730..fb23cb4 100644 --- a/CarRepairShop/CarRepairShop/FormMain.Designer.cs +++ b/CarRepairShop/CarRepairShop/FormMain.Designer.cs @@ -45,6 +45,7 @@ buttonOrderReady = new Button(); buttonIssuedOrder = new Button(); buttonRefresh = new Button(); + почтаToolStripMenuItem = new ToolStripMenuItem(); menuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -52,7 +53,7 @@ // menuStrip // menuStrip.ImageScalingSize = new Size(20, 20); - menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, отчётыToolStripMenuItem, запускРаботToolStripMenuItem1 }); + menuStrip.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem, отчётыToolStripMenuItem, запускРаботToolStripMenuItem1, почтаToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; menuStrip.Size = new Size(1542, 28); @@ -69,28 +70,28 @@ // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(224, 26); + компонентыToolStripMenuItem.Size = new Size(185, 26); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click; // // ремонтToolStripMenuItem // ремонтToolStripMenuItem.Name = "ремонтToolStripMenuItem"; - ремонтToolStripMenuItem.Size = new Size(224, 26); + ремонтToolStripMenuItem.Size = new Size(185, 26); ремонтToolStripMenuItem.Text = "Ремонт"; ремонтToolStripMenuItem.Click += RepairToolStripMenuItem_Click; // // клиентыToolStripMenuItem // клиентыToolStripMenuItem.Name = "клиентыToolStripMenuItem"; - клиентыToolStripMenuItem.Size = new Size(224, 26); + клиентыToolStripMenuItem.Size = new Size(185, 26); клиентыToolStripMenuItem.Text = "Клиенты"; клиентыToolStripMenuItem.Click += ClientsToolStripMenuItem_Click; // // исполнителиToolStripMenuItem // исполнителиToolStripMenuItem.Name = "исполнителиToolStripMenuItem"; - исполнителиToolStripMenuItem.Size = new Size(224, 26); + исполнителиToolStripMenuItem.Size = new Size(185, 26); исполнителиToolStripMenuItem.Text = "Исполнители"; исполнителиToolStripMenuItem.Click += ImplementersToolStripMenuItem_Click; // @@ -191,6 +192,13 @@ buttonRefresh.UseVisualStyleBackColor = true; buttonRefresh.Click += ButtonRef_Click; // + // почтаToolStripMenuItem + // + почтаToolStripMenuItem.Name = "почтаToolStripMenuItem"; + почтаToolStripMenuItem.Size = new Size(65, 24); + почтаToolStripMenuItem.Text = "Почта"; + почтаToolStripMenuItem.Click += MailToolStripMenuItem_Click; + // // FormMain // AutoScaleDimensions = new SizeF(8F, 20F); @@ -234,5 +242,6 @@ private ToolStripMenuItem клиентыToolStripMenuItem; private ToolStripMenuItem исполнителиToolStripMenuItem; private ToolStripMenuItem запускРаботToolStripMenuItem1; + private ToolStripMenuItem почтаToolStripMenuItem; } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShop/FormMain.cs b/CarRepairShop/CarRepairShop/FormMain.cs index e70c8d1..5b9004d 100644 --- a/CarRepairShop/CarRepairShop/FormMain.cs +++ b/CarRepairShop/CarRepairShop/FormMain.cs @@ -216,5 +216,13 @@ namespace CarRepairShop { LoadData(); } + private void MailToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormMails)); + if (service is FormMails form) + { + form.ShowDialog(); + } + } } } diff --git a/CarRepairShop/CarRepairShop/Program.cs b/CarRepairShop/CarRepairShop/Program.cs index 6cbbd3a..a75e455 100644 --- a/CarRepairShop/CarRepairShop/Program.cs +++ b/CarRepairShop/CarRepairShop/Program.cs @@ -7,6 +7,8 @@ using CarRepairShopDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; +using CarRepairShopBusinessLogic.MailWorker; +using CarRepairShopContracts.BindingModels; namespace CarRepairShop { @@ -26,7 +28,33 @@ namespace CarRepairShop var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); + try + { + var mailSender = + _serviceProvider.GetService(); + mailSender?.MailConfig(new MailConfigBindingModel + { + MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? + string.Empty, + MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? + string.Empty, + SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? + string.Empty, + SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClient Port"]), + PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty, + PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"]) + }); + // + var timer = new System.Threading.Timer(new + TimerCallback(MailCheck!), null, 0, 100000); + } + catch (Exception ex) + { + var logger = _serviceProvider.GetService(); + logger?.LogError(ex, " "); + } + + Application.Run(_serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) { @@ -35,7 +63,8 @@ namespace CarRepairShop option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); - services.AddTransient(); + services.AddSingleton(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -47,8 +76,11 @@ namespace CarRepairShop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -63,5 +95,7 @@ namespace CarRepairShop services.AddTransient(); services.AddTransient(); } - } + private static void MailCheck(object obj) => + ServiceProvider?.GetService()?.MailCheck(); + } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ClientLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ClientLogic.cs index b17b259..94d2ce7 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/ClientLogic.cs @@ -4,6 +4,7 @@ using CarRepairShopContracts.SearchModels; using CarRepairShopContracts.StoragesContracts; using CarRepairShopContracts.ViewModels; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; namespace CarRepairShopBusinessLogic.BusinessLogics { @@ -96,14 +97,22 @@ namespace CarRepairShopBusinessLogic.BusinessLogics if (string.IsNullOrEmpty(model.Email)) { throw new ArgumentNullException("Нет Email клиента", - nameof(model.ClientFIO)); + nameof(model.Email)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля клиента", - nameof(model.ClientFIO)); + nameof(model.Password)); } - _logger.LogInformation("Client. ClientFIO:{ClientFIO}." + + if (!Regex.IsMatch(model.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) + { + throw new ArgumentException("Некорретно введен email клиента", nameof(model.Email)); + } + if (!Regex.IsMatch(model.Password, @"^(?=.*\d)(?=.*\W)(?=.*[^\d\s]).+$")) + { + throw new ArgumentException("Некорректно введен пароль клиента", nameof(model.Password)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}." + "Email:{ Email}. Password:{ Password}. Id: { Id} ", model.ClientFIO, model.Email, model.Password, model.Id); var element = _clientStorage.GetElement(new ClientSearchModel { diff --git a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs new file mode 100644 index 0000000..8fa65e1 --- /dev/null +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -0,0 +1,41 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.BusinessLogicsContracts; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using Microsoft.Extensions.Logging; + +namespace CarRepairShopBusinessLogic.BusinessLogics +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + private readonly IMessageInfoStorage _messageInfoStorage; + public MessageInfoLogic(ILogger 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/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs index 8930e3b..e52bfd2 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,4 +1,5 @@ -using CarRepairShopContracts.BindingModels; +using CarRepairShopBusinessLogic.MailWorker; +using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; using CarRepairShopContracts.SearchModels; using CarRepairShopContracts.StoragesContracts; @@ -12,13 +13,18 @@ namespace CarRepairShopBusinessLogic.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 +62,14 @@ namespace CarRepairShopBusinessLogic.BusinessLogics CheckModel(model); if (model.Status != OrderStatus.Неизвестен) return false; model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) - { + var res = _orderStorage.Insert(model); + if (res == null) + { _logger.LogWarning("Insert operation failed"); return false; } - return true; + SendOrderStatusMail(model.ClientId, $"Изменен статус заказа #{res.Id}", $"Заказ #{res.Id} изменен статус на {model.Status}"); + return true; } public bool ChangeStatus(OrderBindingModel model, OrderStatus status) @@ -83,7 +91,13 @@ namespace CarRepairShopBusinessLogic.BusinessLogics _orderStorage.Update(model); if (element.ImplementerId.HasValue) model.ImplementerId = element.ImplementerId; - _orderStorage.Update(model); + var result = _orderStorage.Update(model); + if (result == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); return true; } @@ -124,5 +138,20 @@ namespace CarRepairShopBusinessLogic.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/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/WorkModeling.cs b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/WorkModeling.cs index b02c6e7..b3bdb57 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/CarRepairShop/CarRepairShopBusinessLogic/BusinessLogics/WorkModeling.cs @@ -17,8 +17,7 @@ namespace CarRepairShopBusinessLogic.BusinessLogics _logger = logger; _rnd = new Random(1000); } - public void DoWork(IImplementerLogic implementerLogic, IOrderLogic - orderLogic) + public void DoWork(IImplementerLogic implementerLogic, IOrderLogic orderLogic) { _orderLogic = orderLogic; var implementers = implementerLogic.ReadList(null); diff --git a/CarRepairShop/CarRepairShopBusinessLogic/CarRepairShopBusinessLogic.csproj b/CarRepairShop/CarRepairShopBusinessLogic/CarRepairShopBusinessLogic.csproj index 093ff3a..24c10ba 100644 --- a/CarRepairShop/CarRepairShopBusinessLogic/CarRepairShopBusinessLogic.csproj +++ b/CarRepairShop/CarRepairShopBusinessLogic/CarRepairShopBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/CarRepairShop/CarRepairShopBusinessLogic/MailWorker/AbstractMailWorker.cs b/CarRepairShop/CarRepairShopBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..fe729ce --- /dev/null +++ b/CarRepairShop/CarRepairShopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,79 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace CarRepairShopBusinessLogic.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 ILogger _logger; + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + } + 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) + { + _messageInfoLogic.Create(mail); + } + } + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + protected abstract Task> + ReceiveMailAsync(); + } +} diff --git a/CarRepairShop/CarRepairShopBusinessLogic/MailWorker/MailKitWorker.cs b/CarRepairShop/CarRepairShopBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..408276c --- /dev/null +++ b/CarRepairShop/CarRepairShopBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,76 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System.Net.Mail; +using System.Net; +using System.Text; +using MailKit.Net.Pop3; +using MailKit.Security; + +namespace CarRepairShopBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, + IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { } + 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/CarRepairShop/CarRepairShopContracts/BindingModels/MailConfigBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..efdb2b9 --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,12 @@ +namespace CarRepairShopContracts.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/CarRepairShop/CarRepairShopContracts/BindingModels/MailSendInfoBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..8a091ac --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,9 @@ +namespace CarRepairShopContracts.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/CarRepairShop/CarRepairShopContracts/BindingModels/MessageInfoBindingModel.cs b/CarRepairShop/CarRepairShopContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..765e9dd --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,19 @@ +using CarRepairShopDataModels; + +namespace CarRepairShopContracts.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/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..3869e90 --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,13 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.ViewModels; + +namespace CarRepairShopContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + + bool Create(MessageInfoBindingModel model); + } +} diff --git a/CarRepairShop/CarRepairShopContracts/SearchModels/MessageInfoSearchModel.cs b/CarRepairShop/CarRepairShopContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..3c5a4ef --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,9 @@ +namespace CarRepairShopContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + + public string? MessageId { get; set; } + } +} diff --git a/CarRepairShop/CarRepairShopContracts/StoragesContracts/IMessageInfoStorage.cs b/CarRepairShop/CarRepairShopContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..4e8c80e --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,17 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.ViewModels; + +namespace CarRepairShopContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/CarRepairShop/CarRepairShopContracts/ViewModels/MessageInfoViewModel.cs b/CarRepairShop/CarRepairShopContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..724ad80 --- /dev/null +++ b/CarRepairShop/CarRepairShopContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,19 @@ +using CarRepairShopDataModels; +using System.ComponentModel; + +namespace CarRepairShopContracts.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/CarRepairShop/CarRepairShopDataModels/IMessageInfoModel.cs b/CarRepairShop/CarRepairShopDataModels/IMessageInfoModel.cs new file mode 100644 index 0000000..7a12450 --- /dev/null +++ b/CarRepairShop/CarRepairShopDataModels/IMessageInfoModel.cs @@ -0,0 +1,12 @@ +namespace CarRepairShopDataModels +{ + public interface IMessageInfoModel + { + string MessageId { get; } + int? ClientId { get; } + string SenderName { get; } + DateTime DateDelivery { get; } + string Subject { get; } + string Body { get; } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ClientStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ClientStorage.cs index 1536685..8ca0067 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ClientStorage.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/ClientStorage.cs @@ -23,9 +23,9 @@ namespace CarRepairShopDatabaseImplement.Implements } using var context = new RepairsShopDatabase(); return context.Clients - .Where(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO) && + .Where(x => (string.IsNullOrEmpty(model.ClientFIO) || x.ClientFIO.Contains(model.ClientFIO)) && (string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) && - (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))) + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password))) .Select(x => x.GetViewModel) .ToList(); } diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Implements/MessageInfoStorage.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..f703bc7 --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,51 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDatabaseImplement.Models; + +namespace CarRepairShopDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + using var context = new RepairsShopDatabase(); + if (model.MessageId != null) + { + return context.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new RepairsShopDatabase(); + return context.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new RepairsShopDatabase(); + return context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new RepairsShopDatabase(); + var newMessage = MessageInfo.Create(model); + if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId))) + { + return null; + } + context.Messages.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/Models/MessageInfo.cs b/CarRepairShop/CarRepairShopDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..dde776a --- /dev/null +++ b/CarRepairShop/CarRepairShopDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,52 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels; +using System.ComponentModel.DataAnnotations; + +namespace CarRepairShopDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + 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 Client? Client { get; private set; } + + 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, + }; + } +} diff --git a/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs b/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs index 35748e2..3d411a8 100644 --- a/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs +++ b/CarRepairShop/CarRepairShopDatabaseImplement/RepairsShopDatabase.cs @@ -20,5 +20,6 @@ namespace CarRepairShopDatabaseImplement public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { set; get; } } } diff --git a/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs b/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs index 5b534a8..7e5b3d4 100644 --- a/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs +++ b/CarRepairShop/CarRepairShopFileImplement/DataFileSingleton.cs @@ -11,11 +11,13 @@ namespace CarRepairShopFileImplement private readonly string RepairFileName = "Repair.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 Repairs { 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 CarRepairShopFileImplement "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 CarRepairShopFileImplement 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/CarRepairShop/CarRepairShopFileImplement/Implements/MessageInfoStorage.cs b/CarRepairShop/CarRepairShopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..707f76c --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,53 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopFileImplement.Models; + +namespace CarRepairShopFileImplement.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/CarRepairShop/CarRepairShopFileImplement/Models/MessageInfo.cs b/CarRepairShop/CarRepairShopFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..8b9011c --- /dev/null +++ b/CarRepairShop/CarRepairShopFileImplement/Models/MessageInfo.cs @@ -0,0 +1,75 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels; +using System.Xml.Linq; + +namespace CarRepairShopFileImplement.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/CarRepairShop/CarRepairShopListImplement/DataListSingleton.cs b/CarRepairShop/CarRepairShopListImplement/DataListSingleton.cs index 72d939f..3138329 100644 --- a/CarRepairShop/CarRepairShopListImplement/DataListSingleton.cs +++ b/CarRepairShop/CarRepairShopListImplement/DataListSingleton.cs @@ -10,6 +10,7 @@ namespace CarRepairShopListImplement public List Repairs { 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 CarRepairShopListImplement Repairs = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/CarRepairShop/CarRepairShopListImplement/Implements/MessageInfoStorage.cs b/CarRepairShop/CarRepairShopListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..5a3c046 --- /dev/null +++ b/CarRepairShop/CarRepairShopListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,61 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.SearchModels; +using CarRepairShopContracts.StoragesContracts; +using CarRepairShopContracts.ViewModels; +using CarRepairShopListImplement.Models; + +namespace CarRepairShopListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + 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 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 List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + 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/CarRepairShop/CarRepairShopListImplement/Models/MessageInfo.cs b/CarRepairShop/CarRepairShopListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..43a5b06 --- /dev/null +++ b/CarRepairShop/CarRepairShopListImplement/Models/MessageInfo.cs @@ -0,0 +1,48 @@ +using CarRepairShopContracts.BindingModels; +using CarRepairShopContracts.ViewModels; +using CarRepairShopDataModels; + +namespace CarRepairShopListImplement.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, + }; + } +} diff --git a/CarRepairShop/CarRepairShopRestApi/Controllers/ClientController.cs b/CarRepairShop/CarRepairShopRestApi/Controllers/ClientController.cs index 08ae606..6be7bb8 100644 --- a/CarRepairShop/CarRepairShopRestApi/Controllers/ClientController.cs +++ b/CarRepairShop/CarRepairShopRestApi/Controllers/ClientController.cs @@ -12,12 +12,14 @@ namespace CarRepairShopRestApi.Controllers { private readonly ILogger _logger; private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger + private readonly IMessageInfoLogic _mailLogic; + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; _logic = logic; - } + _mailLogic = mailLogic; + } [HttpGet] public ClientViewModel? Login(string login, string password) { @@ -35,7 +37,23 @@ namespace CarRepairShopRestApi.Controllers throw; } } - [HttpPost] + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + [HttpPost] public void Register(ClientBindingModel model) { try diff --git a/CarRepairShop/CarRepairShopRestApi/Program.cs b/CarRepairShop/CarRepairShopRestApi/Program.cs index f86ef1e..8b168f9 100644 --- a/CarRepairShop/CarRepairShopRestApi/Program.cs +++ b/CarRepairShop/CarRepairShopRestApi/Program.cs @@ -1,4 +1,6 @@ using CarRepairShopBusinessLogic.BusinessLogics; +using CarRepairShopBusinessLogic.MailWorker; +using CarRepairShopContracts.BindingModels; using CarRepairShopContracts.BusinessLogicsContracts; using CarRepairShopContracts.StoragesContracts; using CarRepairShopDatabaseImplement.Implements; @@ -11,9 +13,14 @@ builder.Logging.AddLog4Net("log4net.config"); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -28,6 +35,24 @@ builder.Services.AddSwaggerGen(c => }); }); var app = builder.Build(); +var mailSender = app.Services.GetService(); +mailSender?.MailConfig(new MailConfigBindingModel +{ + MailLogin = builder.Configuration?.GetSection("MailLogin")?.Value?.ToString() +?? string.Empty, + MailPassword = +builder.Configuration?.GetSection("MailPassword")?.Value?.ToString() ?? +string.Empty, + SmtpClientHost = +builder.Configuration?.GetSection("SmtpClientHost")?.Value?.ToString() ?? +string.Empty, + SmtpClientPort = +Convert.ToInt32(builder.Configuration?.GetSection("SmtpClientPort")?.Value?.ToString()), + PopHost = builder.Configuration?.GetSection("PopHost")?.Value?.ToString() ?? +string.Empty, + PopPort = +Convert.ToInt32(builder.Configuration?.GetSection("PopPort")?.Value?.ToString()) +}); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/CarRepairShop/CarRepairShopRestApi/appsettings.json b/CarRepairShop/CarRepairShopRestApi/appsettings.json index 10f68b8..ea66075 100644 --- a/CarRepairShop/CarRepairShopRestApi/appsettings.json +++ b/CarRepairShop/CarRepairShopRestApi/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "labwork73@gmail.com", + "MailPassword": "laba73000" } diff --git a/CarRepairShop/CarRepairShowClientApp/CarRepairShowClientApp.csproj b/CarRepairShop/CarRepairShowClientApp/CarRepairShowClientApp.csproj index d1795b9..52393c8 100644 --- a/CarRepairShop/CarRepairShowClientApp/CarRepairShowClientApp.csproj +++ b/CarRepairShop/CarRepairShowClientApp/CarRepairShowClientApp.csproj @@ -6,6 +6,10 @@ enable + + + + @@ -15,4 +19,16 @@ + + + + + + <_ContentIncludedByDefault Remove="Views\Home\Mails.cshtml" /> + + + + + + diff --git a/CarRepairShop/CarRepairShowClientApp/Controllers/HomeController.cs b/CarRepairShop/CarRepairShowClientApp/Controllers/HomeController.cs index 5859f3f..952e510 100644 --- a/CarRepairShop/CarRepairShowClientApp/Controllers/HomeController.cs +++ b/CarRepairShop/CarRepairShowClientApp/Controllers/HomeController.cs @@ -112,7 +112,8 @@ namespace CarRepairShowClientApp.Controllers [HttpGet] public IActionResult Create() { - ViewBag.Repairs = APIClient.GetRequest>("api/main/getrepairlist"); + var res = APIClient.GetRequest>("api/main/getrepairlist"); + ViewBag.Repairs = APIClient.GetRequest>("api/main/getrepairlist"); return View(); } [HttpPost] @@ -143,5 +144,15 @@ namespace CarRepairShowClientApp.Controllers ); return count * (rep?.Price ?? 1); } - } + [HttpGet] + public IActionResult Mails() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return + View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + } + } } \ No newline at end of file diff --git a/CarRepairShop/CarRepairShowClientApp/Views/Home/Mails.cshtml b/CarRepairShop/CarRepairShowClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..ca7133f --- /dev/null +++ b/CarRepairShop/CarRepairShowClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using CarRepairShopContracts.ViewModels + +@model List + +@{ + ViewData["Title"] = "Mails"; +} + +
+

Заказы

+
+ + +
+ @{ + if (Model == null) + { +

Авторизируйтесь

+ return; + } + + + + + + + + + + + @foreach (var item in Model) + { + + + + + + } + +
+ Дата письма + + Заголовок + + Текст +
+ @Html.DisplayFor(modelItem => item.DateDelivery) + + @Html.DisplayFor(modelItem => item.Subject) + + @Html.DisplayFor(modelItem => item.Body) +
+ } +
\ No newline at end of file diff --git a/CarRepairShop/CarRepairShowClientApp/Views/Shared/_Layout.cshtml b/CarRepairShop/CarRepairShowClientApp/Views/Shared/_Layout.cshtml index 4f7f945..699497d 100644 --- a/CarRepairShop/CarRepairShowClientApp/Views/Shared/_Layout.cshtml +++ b/CarRepairShop/CarRepairShowClientApp/Views/Shared/_Layout.cshtml @@ -26,6 +26,9 @@ +