From b3ac22d499e55e3fb86a468b213ef5160a0fc2fd Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Sat, 22 Apr 2023 19:59:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=93=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 --- .../BlacksmithWorkshop/App.config | 11 + .../BlacksmithWorkshop/FormMails.Designer.cs | 61 ++++ .../BlacksmithWorkshop/FormMails.cs | 52 +++ .../BlacksmithWorkshop/FormMails.resx | 60 ++++ .../BlacksmithWorkshop/FormMain.Designer.cs | 23 +- .../BlacksmithWorkshop/FormMain.cs | 20 +- .../BlacksmithWorkshop/Program.cs | 40 ++- .../BlacksmithWorkshopBusinessLogic.csproj | 1 + .../BusinessLogics/ClientLogic.cs | 10 +- .../BusinessLogics/MessageInfoLogic.cs | 50 +++ .../BusinessLogics/OrderLogic.cs | 64 ++-- .../BusinessLogics/WorkModeling.cs | 51 +-- .../MailWorker/AbstractMailWorker.cs | 85 +++++ .../MailWorker/MailKitWorker.cs | 83 +++++ .../Controllers/HomeController.cs | 11 + .../Views/Home/Mails.cshtml | 54 ++++ .../Views/Shared/_Layout.cshtml | 2 + .../BindingModels/MailConfigBindingModel.cs | 19 ++ .../BindingModels/MailSendInfoBindingModel.cs | 15 + .../BindingModels/MessageInfoBindingModel.cs | 24 ++ .../IMessageInfoLogic.cs | 18 ++ .../SearchModels/MessageInfoSearchModel.cs | 14 + .../StoragesContracts/IMessageInfoStorage.cs | 22 ++ .../ViewModels/MessageInfoViewModel.cs | 29 ++ .../Models/IMessageInfoModel.cs | 18 ++ .../BlacksmithWorkshopDatabase.cs | 4 +- .../Implements/MessageInfoStorage.cs | 65 ++++ .../Implements/OrderStorage.cs | 4 +- .../20230420221009_Lab7Migra.Designer.cs | 285 +++++++++++++++++ .../Migrations/20230420221009_Lab7Migra.cs | 38 +++ .../20230422144647_Laba7NewMigra.Designer.cs | 295 ++++++++++++++++++ .../20230422144647_Laba7NewMigra.cs | 56 ++++ ...BlacksmithWorkshopDatabaseModelSnapshot.cs | 39 +++ .../Models/MessageInfo.cs | 56 ++++ .../Models/Order.cs | 7 +- .../DataFileSingleton.cs | 5 +- .../Implements/ImplementerStorage.cs | 86 ++++- .../Implements/MessageInfoStorage.cs | 12 + .../Models/Implementer.cs | 74 ++++- .../Models/MessageInfo.cs | 12 + .../DataListSingleton.cs | 2 + .../Implements/ImplementerStorage.cs | 100 +++++- .../Implements/MessageInfoStorage.cs | 35 +++ .../Models/Implementer.cs | 51 ++- .../Models/MessageInfo.cs | 12 + .../Controllers/ClientController.cs | 25 +- .../BlacksmithWorkshopRestApi/Program.cs | 17 + .../appsettings.json | 10 +- 48 files changed, 2048 insertions(+), 79 deletions(-) create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/App.config create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormMails.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormMails.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshop/FormMails.resx create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDataModel/Models/IMessageInfoModel.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.Designer.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs create mode 100644 BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/App.config b/BlacksmithWorkshop/BlacksmithWorkshop/App.config new file mode 100644 index 0000000..3d237bc --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.Designer.cs new file mode 100644 index 0000000..f7b64aa --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.Designer.cs @@ -0,0 +1,61 @@ +namespace BlacksmithWorkshop +{ + 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 = "FormMails"; + Text = "Сообщения"; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.cs new file mode 100644 index 0000000..7045e30 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.cs @@ -0,0 +1,52 @@ +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace BlacksmithWorkshop +{ + public partial class FormMails : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + public FormMails(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + LoadData(); + + } + private void LoadData() + { + 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/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.resx b/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMails.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs index 2721765..a6cfeca 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.Designer.cs @@ -38,13 +38,14 @@ ComponentsToolStripMenuItem = new ToolStripMenuItem(); ComponentsManufactueToolStripMenuItem = new ToolStripMenuItem(); OrdersToolStripMenuItem = new ToolStripMenuItem(); + запускРаботыToolStripMenuItem = new ToolStripMenuItem(); dataGridView = new DataGridView(); buttonCreateOrder = new Button(); buttonnTakeOrderInWork = new Button(); buttonOrderReady = new Button(); buttonIssuedOrder = new Button(); buttonRef = new Button(); - запускРаботыToolStripMenuItem = new ToolStripMenuItem(); + письмаToolStripMenuItem = new ToolStripMenuItem(); menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); @@ -52,7 +53,7 @@ // menuStrip1 // menuStrip1.ImageScalingSize = new Size(20, 20); - menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem1, отчётыToolStripMenuItem, запускРаботыToolStripMenuItem }); + menuStrip1.Items.AddRange(new ToolStripItem[] { справочникиToolStripMenuItem1, отчётыToolStripMenuItem, запускРаботыToolStripMenuItem, письмаToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; menuStrip1.Size = new Size(920, 28); @@ -122,6 +123,13 @@ OrdersToolStripMenuItem.Text = "Список заказов"; OrdersToolStripMenuItem.Click += OrdersToolStripMenuItem_Click; // + // запускРаботыToolStripMenuItem + // + запускРаботыToolStripMenuItem.Name = "запускРаботыToolStripMenuItem"; + запускРаботыToolStripMenuItem.Size = new Size(125, 24); + запускРаботыToolStripMenuItem.Text = "Запуск работы"; + запускРаботыToolStripMenuItem.Click += ЗапускРаботыToolStripMenuItem_Click; + // // dataGridView // dataGridView.BackgroundColor = SystemColors.ButtonHighlight; @@ -183,12 +191,12 @@ buttonRef.UseVisualStyleBackColor = true; buttonRef.Click += ButtonRef_Click; // - // запускРаботыToolStripMenuItem + // письмаToolStripMenuItem // - запускРаботыToolStripMenuItem.Name = "запускРаботыToolStripMenuItem"; - запускРаботыToolStripMenuItem.Size = new Size(125, 24); - запускРаботыToolStripMenuItem.Text = "Запуск работы"; - запускРаботыToolStripMenuItem.Click += ЗапускРаботыToolStripMenuItem_Click; + письмаToolStripMenuItem.Name = "письмаToolStripMenuItem"; + письмаToolStripMenuItem.Size = new Size(77, 24); + письмаToolStripMenuItem.Text = "Письма"; + письмаToolStripMenuItem.Click += ПисьмаToolStripMenuItem_Click; // // FormMain // @@ -231,5 +239,6 @@ private ToolStripMenuItem клиентыToolStripMenuItem; private ToolStripMenuItem исполнителиToolStripMenuItem; private ToolStripMenuItem запускРаботыToolStripMenuItem; + private ToolStripMenuItem письмаToolStripMenuItem; } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs index 16d5e0d..7b8bd7c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/FormMain.cs @@ -2,6 +2,8 @@ using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.BusinessLogicsContracts; using BlacksmithWorkshopDataModel.Enums; +using Microsoft.Data.SqlClient; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -112,7 +114,7 @@ namespace BlacksmithWorkshop DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), ClientId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value), ImplementerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ImplementerId"].Value), - + @@ -120,15 +122,17 @@ namespace BlacksmithWorkshop }); if (!operationResult) { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); } LoadData(); } + + catch (Exception ex) { - _logger.LogError(ex, "Ошибка передачи заказа в работу"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); @@ -197,7 +201,7 @@ namespace BlacksmithWorkshop DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()), ClientId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value), ImplementerId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["ImplementerId"].Value), - + }); if (!operationResult) @@ -285,5 +289,15 @@ Program.ServiceProvider?.GetService(typeof(FormImplementers)); MessageBoxButtons.OK, MessageBoxIcon.Information); } + + private void ПисьмаToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = +Program.ServiceProvider?.GetService(typeof(FormMails)); + if (service is FormMails form) + { + form.ShowDialog(); + } + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index e4eb66c..bfc515d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -1,6 +1,8 @@ using BlacksmithWorkshopBusinessLogic.BusinessLogics; +using BlacksmithWorkshopBusinessLogic.MailWorker; using BlacksmithWorkshopBusinessLogic.OfficePackage; using BlacksmithWorkshopBusinessLogic.OfficePackage.Implements; +using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.BusinessLogicsContracts; using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopDatabaseImplement.Implements; @@ -26,7 +28,28 @@ namespace BlacksmithWorkshop 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["SmtpClientPort"]), + 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) { @@ -40,7 +63,7 @@ namespace BlacksmithWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); - + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -48,9 +71,11 @@ namespace BlacksmithWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); - - - services.AddTransient(); + services.AddTransient(); + + services.AddSingleton(); + + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -67,6 +92,9 @@ namespace BlacksmithWorkshop services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + } - } + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); + } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BlacksmithWorkshopBusinessLogic.csproj b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BlacksmithWorkshopBusinessLogic.csproj index 4603062..ee9568d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BlacksmithWorkshopBusinessLogic.csproj +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BlacksmithWorkshopBusinessLogic.csproj @@ -8,6 +8,7 @@ + all diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs index 00900c6..af81b51 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/ClientLogic.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace BlacksmithWorkshopBusinessLogic.BusinessLogics @@ -114,7 +115,14 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics throw new ArgumentNullException("Введите пароль", nameof(model.Password)); } - + if (!Regex.IsMatch(model.Email, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) + { + 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. Client:{ClientFIO}. Email:{ Email}. Id: { Id}", model.ClientFIO, model.Email, model.Id); var element = _clientStorage.GetElement(new ClientSearchModel { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs new file mode 100644 index 0000000..4c99eb7 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -0,0 +1,50 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopBusinessLogic.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. MessageInfoName:{MessageInfoName}.Id:{ Id}", model?.MessageId, model?.MessageId); + 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/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs index 474755c..7b0ad01 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,4 +1,5 @@ -using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopBusinessLogic.MailWorker; +using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.BusinessLogicsContracts; using BlacksmithWorkshopContracts.SearchModels; using BlacksmithWorkshopContracts.StoragesContracts; @@ -17,10 +18,14 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + private readonly AbstractMailWorker _abstractMailWorker; + private readonly IClientLogic _clientLogic; + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker abstractMailWorker, IClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; + _abstractMailWorker = abstractMailWorker; + _clientLogic = clientLogic; } public bool CreateOrder(OrderBindingModel model) { @@ -35,13 +40,16 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics model.Status = OrderStatus.Принят; model.DateCreate = DateTime.Now; - - if (_orderStorage.Insert(model) == null) + var order = _orderStorage.Insert(model); + if (order == null) { + model.Status = OrderStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } + + MailSend(model.ClientId, $"Заказ {order.Id} создан", $"Заказ {order.Id} был создан {model.DateCreate}. Сумма {model.Sum}"); return true; } @@ -66,21 +74,20 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics _orderStorage.GetFilteredList(model); if (list == null) { - + _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } - private bool UpdateStatus(OrderBindingModel model, OrderStatus newStatus) + private bool UpdateStatus(OrderBindingModel newmodel, OrderStatus newStatus) { var viewModel = _orderStorage.GetElement(new OrderSearchModel { - Id = model.Id + Id = newmodel.Id }); - if (viewModel == null) { @@ -88,7 +95,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics return false; } - OrderBindingModel model1 = new OrderBindingModel + OrderBindingModel model = new OrderBindingModel { Id = viewModel.Id, ManufactureId = viewModel.ManufactureId, @@ -96,29 +103,30 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics DateCreate = viewModel.DateCreate, DateImplement = viewModel.DateImplement, Count = viewModel.Count, - Sum = viewModel.Sum, - + Sum = viewModel.Sum }; - if (model.ImplementerId.HasValue) + if (newmodel.ImplementerId.HasValue) { - model1.ImplementerId = model.ImplementerId; - + model.ImplementerId = newmodel.ImplementerId; } - CheckModel(model1, false); - if (model1.Status + 1 != newStatus) + CheckModel(model, false); + if (model.Status + 1 != newStatus) { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } - model1.Status = newStatus; - if (model1.Status == OrderStatus.Готов) model1.DateImplement = DateTime.Now; - if (_orderStorage.Update(model1) == null) + model.Status = newStatus; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + var result = _orderStorage.Update(model); + if (result == null) { - model1.Status--; + model.Status--; _logger.LogWarning("Update operation failed"); return false; } + MailSend(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{result.Id} изменен статус на {result.Status}"); return true; } @@ -176,6 +184,22 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } + public bool MailSend(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + if (client == null) + { + _logger.LogWarning("Email sending failed. Client with id {Id} not found", clientId); + return false; + } + _abstractMailWorker.MailSendAsync(new MailSendInfoBindingModel() + { + Subject = subject, + Text = text, + MailAddress = client.Email + }); + return true; + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs index 3424475..5e781e1 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogics/WorkModeling.cs @@ -73,22 +73,24 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics try { _logger.LogDebug("DoWork. Worker {Id} try get order { Order}", implementer.Id, order.Id); - // пытаемся назначить заказ на исполнителя -_orderLogic.TakeOrderInWork(new OrderBindingModel -{ - Id = order.Id, - ImplementerId = implementer.Id -}); - - // делаем работу - Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, - 1000) * order.Count); - _logger.LogDebug("DoWork. Worker {Id} finish order { Order} ", implementer.Id, order.Id); - _orderLogic.FinishOrder(new OrderBindingModel - { - Id = order.Id, - ImplementerId = implementer.Id - }); + // пытаемся назначить заказ на исполнителя + if (_orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + })) + { + + // делаем работу + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, + 1000) * order.Count); + _logger.LogDebug("DoWork. Worker {Id} finish order { Order} ", implementer.Id, order.Id); + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + }); + } } // кто-то мог уже перехватить заказ, игнорируем ошибку catch (InvalidOperationException ex) @@ -115,14 +117,21 @@ _orderLogic.TakeOrderInWork(new OrderBindingModel { if (_orderLogic == null || implementer == null || orders == null || orders.Count == 0) { - return; - } try { - var runOrder = await Task.Run(() => orders.FirstOrDefault(x => x.ImplementerId == implementer.Id && x.Status == OrderStatus.Выполняется)); - + // Выбираем из всех заказов тот, который выполняется данным исполнителем + var runOrder = await Task.Run(() => _orderLogic.ReadElement(new OrderSearchModel + { + ImplementerId = implementer.Id, + Status = OrderStatus.Выполняется + })); + if (runOrder == null) + { + return; + } + if (runOrder == null) { @@ -136,7 +145,7 @@ _orderLogic.TakeOrderInWork(new OrderBindingModel runOrder.Count); _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", implementer.Id, runOrder.Id); - _orderLogic.DeliveryOrder(new OrderBindingModel + _orderLogic.FinishOrder(new OrderBindingModel { Id = runOrder.Id }); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..1b5e922 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,85 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopBusinessLogic.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/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..a060443 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,83 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using MailKit.Net.Pop3; +using MailKit.Security; + +namespace BlacksmithWorkshopBusinessLogic.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/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs index 148207a..e942df2 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs @@ -3,6 +3,7 @@ using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.SearchModels; using BlacksmithWorkshopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.Diagnostics; @@ -150,6 +151,16 @@ namespace BlacksmithWorkshopClientApp.Controllers ); return count * (manu?.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}")); +} } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..c424e05 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using BlacksmithWorkshopContracts.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/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml index 2bc9a6b..1469ecb 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml @@ -23,6 +23,8 @@ + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailConfigBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..297106b --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.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/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..6770bdf --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.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/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MessageInfoBindingModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..d419730 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,24 @@ +using BlacksmithWorkshopDataModel.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.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 DateTime DateDelivery { get; set; } + + public string Subject { get; set; } = string.Empty; + + public string Body { get; set; } = string.Empty; + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..e04098a --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,18 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + + bool Create(MessageInfoBindingModel model); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..f5890d4 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..2620eef --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,22 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..fdcff50 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,29 @@ +using BlacksmithWorkshopDataModel.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopContracts.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/BlacksmithWorkshop/BlacksmithWorkshopDataModel/Models/IMessageInfoModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopDataModel/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..9923f60 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDataModel/Models/IMessageInfoModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDataModel.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/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs index 9608e88..6af8077 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -25,7 +25,9 @@ optionsBuilder) public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } - } + public virtual DbSet MessagesInfo { set; get; } + + } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..1531c58 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,65 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + using var context = new BlacksmithWorkshopDatabase(); + return context.MessagesInfo + .FirstOrDefault(x => + x.MessageId.Equals(model.MessageId))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + using var context = new BlacksmithWorkshopDatabase(); + return context.MessagesInfo + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + return context.MessagesInfo + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new BlacksmithWorkshopDatabase(); + var newMessage = MessageInfo.Create(model); + + if (newMessage == null ) + { + + return null; + } + + context.MessagesInfo.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs index 1a03a1b..847462d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/OrderStorage.cs @@ -58,7 +58,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements } using var context = new BlacksmithWorkshopDatabase(); - if (model.Id.HasValue) + if (model.ClientId.HasValue) { return context.Orders @@ -80,7 +80,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements } return context.Orders - .Include(x => x.ManufactureName).Include(x => x.Manufacture).Include(x => x.Implementer) + .Include(x => x.Manufacture).Include(x => x.Implementer).Include(x => x.Client) .Where(x => x.Id == model.Id || (model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo)) .Select(x => x.GetViewModel) .ToList(); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.Designer.cs new file mode 100644 index 0000000..4372272 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.Designer.cs @@ -0,0 +1,285 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + [Migration("20230420221009_Lab7Migra")] + partial class Lab7Migra + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .IsRequired() + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.ToTable("MessagesInfo"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ImplementerId") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("ManufactureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Components") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Orders") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.cs new file mode 100644 index 0000000..5bcd5da --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230420221009_Lab7Migra.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + /// + public partial class Lab7Migra : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "MessagesInfo", + columns: table => new + { + MessageId = table.Column(type: "nvarchar(450)", nullable: false), + ClientId = table.Column(type: "int", nullable: true), + SenderName = table.Column(type: "nvarchar(max)", nullable: false), + DateDelivery = table.Column(type: "datetime2", nullable: false), + Subject = table.Column(type: "nvarchar(max)", nullable: false), + Body = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_MessagesInfo", x => x.MessageId); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "MessagesInfo"); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.Designer.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.Designer.cs new file mode 100644 index 0000000..cef6455 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.Designer.cs @@ -0,0 +1,295 @@ +// +using System; +using BlacksmithWorkshopDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + [DbContext(typeof(BlacksmithWorkshopDatabase))] + [Migration("20230422144647_Laba7NewMigra")] + partial class Laba7NewMigra + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Manufactures"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("MessagesInfo"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ImplementerId") + .HasColumnType("int"); + + b.Property("ManufactureId") + .HasColumnType("int"); + + b.Property("ManufactureName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("ManufactureId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component") + .WithMany("ManufactureComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Components") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture") + .WithMany("Orders") + .HasForeignKey("ManufactureId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Manufacture"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b => + { + b.Navigation("ManufactureComponents"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.cs new file mode 100644 index 0000000..d796001 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/20230422144647_Laba7NewMigra.cs @@ -0,0 +1,56 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace BlacksmithWorkshopDatabaseImplement.Migrations +{ + /// + public partial class Laba7NewMigra : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "ClientId", + table: "MessagesInfo", + type: "int", + nullable: true, + oldClrType: typeof(int), + oldType: "int"); + + migrationBuilder.CreateIndex( + name: "IX_MessagesInfo_ClientId", + table: "MessagesInfo", + column: "ClientId"); + + migrationBuilder.AddForeignKey( + name: "FK_MessagesInfo_Clients_ClientId", + table: "MessagesInfo", + column: "ClientId", + principalTable: "Clients", + principalColumn: "Id"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_MessagesInfo_Clients_ClientId", + table: "MessagesInfo"); + + migrationBuilder.DropIndex( + name: "IX_MessagesInfo_ClientId", + table: "MessagesInfo"); + + migrationBuilder.AlterColumn( + name: "ClientId", + table: "MessagesInfo", + type: "int", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "int", + oldNullable: true); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs index f1c1fb6..14a6ba5 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Migrations/BlacksmithWorkshopDatabaseModelSnapshot.cs @@ -140,6 +140,36 @@ namespace BlacksmithWorkshopDatabaseImplement.Migrations b.ToTable("ManufactureComponents"); }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("nvarchar(450)"); + + b.Property("Body") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("DateDelivery") + .HasColumnType("datetime2"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("MessagesInfo"); + }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -206,6 +236,15 @@ namespace BlacksmithWorkshopDatabaseImplement.Migrations b.Navigation("Manufacture"); }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b => { b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Client", "Client") diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..dbb4cae --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,56 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModel.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopDatabaseImplement.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; } + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + public virtual Client? Client { get; private set; } + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new MessageInfo() + { + ClientId = model.ClientId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + Body = model.Body, + MessageId = model.MessageId + + }; + } + public MessageInfoViewModel GetViewModel => new() + { + ClientId = ClientId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body, + MessageId = MessageId + }; + + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs index a1aeb82..0929e6d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Order.cs @@ -83,12 +83,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models return; } - ManufactureId = model.ManufactureId; - ManufactureName = model.ManufactureName; - Count = model.Count; - Sum = model.Sum; + Status = model.Status; - DateCreate = model.DateCreate; + DateImplement = model.DateImplement; ImplementerId = model.ImplementerId; ; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs index e3f7494..377517d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/DataFileSingleton.cs @@ -16,11 +16,13 @@ namespace BlacksmithWorkshopFileImplement private readonly string ManufactureFileName = "Manufacture.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 Manufactures { get; private set; } public List Clients { get; private set; } public List Implementers { get; private set; } + public List MessagesInfo { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -36,6 +38,7 @@ namespace BlacksmithWorkshopFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessagesInfo() => SaveData(MessagesInfo, ImplementerFileName, "MessagesInfo", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => @@ -45,7 +48,7 @@ namespace BlacksmithWorkshopFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; - + MessagesInfo = LoadData(MessageInfoFileName, "MessageInfo", x => MessageInfo.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ImplementerStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ImplementerStorage.cs index b3ae5cc..0e14e9d 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ImplementerStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/ImplementerStorage.cs @@ -1,4 +1,9 @@ -using System; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopFileImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,84 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopFileImplement.Implements { - internal class ImplementerStorage + public class ImplementerStorage : IImplementerStorage { + private readonly DataFileSingleton source; + public ImplementerStorage() + { + source = DataFileSingleton.GetInstance(); + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var element = source.Implementers.FirstOrDefault(x => x.Id == + model.Id); + if (element != null) + { + source.Implementers.Remove(element); + source.SaveImplementers(); + return element.GetViewModel; + } + return null; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue) + { + return null; + } + return source.Implementers + .FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ImplementerFIO) && x.ImplementerFIO == + model.ImplementerFIO) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return new(); + } + return source.Implementers + .Where(x => x.ImplementerFIO.Contains(model.ImplementerFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + return source.Implementers + .Select(x => x.GetViewModel) + .ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = source.Implementers.Count > 0 ? source.Implementers.Max(x => + x.Id) + 1 : 1; + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + source.Implementers.Add(newImplementer); + source.SaveImplementers(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var component = source.Implementers.FirstOrDefault(x => x.Id == + model.Id); + if (component == null) + { + return null; + } + component.Update(model); + source.SaveImplementers(); + return component.GetViewModel; + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..f609d12 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopFileImplement.Implements +{ + internal class MessageInfoStorage + { + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Implementer.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Implementer.cs index 5b4fb9d..669bfa0 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Implementer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/Implementer.cs @@ -1,12 +1,82 @@ -using System; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModel.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Xml.Linq; namespace BlacksmithWorkshopFileImplement.Models { - internal class Implementer + public class Implementer : IImplementerModel { + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public int Id { get; private set; } + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new Implementer() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + Qualification = model.Qualification, + WorkExperience = model.WorkExperience, + }; + } + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Implementer() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ImplementerFIO = element.Element("FIO")!.Value, + Password = element.Element("Password")!.Value, + Qualification = Convert.ToInt32(element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32(element.Element("WorkExperience")!.Value), + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + Qualification = model.Qualification; + WorkExperience = model.WorkExperience; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + Qualification = Qualification, + }; + public XElement GetXElement => new("Implementer", + new XAttribute("Id", Id), + new XElement("FIO", ImplementerFIO), + new XElement("Password", Password), + new XElement("Qualification", Qualification), + new XElement("WorkExperience", WorkExperience) + ); + } } + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..de7e225 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopFileImplement/Models/MessageInfo.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopFileImplement.Models +{ + internal class MessageInfo + { + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs index e351c92..052e980 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace BlacksmithWorkshopListImplement public List Manufactures { 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 BlacksmithWorkshopListImplement Manufactures = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs index f2e72ed..6248a6f 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/ImplementerStorage.cs @@ -1,4 +1,9 @@ -using System; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopListImplement.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +11,98 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopListImplement.Implements { - internal class ImplementerStorage + public class ImplementerStorage:IImplementerStorage { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Implementers) + { + result.Add(component.GetViewModel); + } + return result; + } + public List GetFilteredList(ImplementerSearchModel + model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + return result; + } + foreach (var component in _source.Implementers) + { + if (component.ImplementerFIO.Contains(model.ImplementerFIO)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Implementers) + { + if ((!string.IsNullOrEmpty(model.ImplementerFIO) && + component.ImplementerFIO == model.ImplementerFIO) || + (model.Id.HasValue && component.Id == model.Id)) + { + return component.GetViewModel; + } + } + return null; + } + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Implementers) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + _source.Implementers.Add(newImplementer); + return newImplementer.GetViewModel; + } + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var component in _source.Implementers) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..099b0b0 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,35 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.SearchModels; +using BlacksmithWorkshopContracts.StoragesContracts; +using BlacksmithWorkshopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs index 896b589..033bdee 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/Implementer.cs @@ -1,4 +1,7 @@ -using System; +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModel.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,51 @@ using System.Threading.Tasks; namespace BlacksmithWorkshopListImplement.Models { - internal class Implementer + public class Implementer: IImplementerModel { + public string ImplementerFIO { get; private set; } = string.Empty; + + public string Password { get; private set; } = string.Empty; + + public int WorkExperience { get; private set; } + + public int Qualification { get; private set; } + + public int Id { get; private set; } + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + }; } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..5e61d46 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopListImplement/Models/MessageInfo.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BlacksmithWorkshopListImplement.Models +{ + internal class MessageInfo + { + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Controllers/ClientController.cs b/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Controllers/ClientController.cs index e8b4d8d..b61ef76 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Controllers/ClientController.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Controllers/ClientController.cs @@ -12,11 +12,13 @@ namespace BlacksmithWorkshopRestApi.Controllers { private readonly ILogger _logger; private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger - logger) + private readonly IMessageInfoLogic _mailLogic; + public ClientController(IClientLogic logic, ILogger + logger, IMessageInfoLogic mailLogic) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] public ClientViewModel? Login(string login, string password) @@ -61,5 +63,22 @@ namespace BlacksmithWorkshopRestApi.Controllers throw; } } - } + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + + } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Program.cs index 690dac2..8d4578e 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopRestApi/Program.cs @@ -1,4 +1,6 @@ using BlacksmithWorkshopBusinessLogic.BusinessLogics; +using BlacksmithWorkshopBusinessLogic.MailWorker; +using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.BusinessLogicsContracts; using BlacksmithWorkshopContracts.StoragesContracts; using BlacksmithWorkshopDatabaseImplement.Implements; @@ -12,10 +14,15 @@ 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.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -27,6 +34,16 @@ builder.Services.AddSwaggerGen(c => c.SwaggerDoc("v1", new OpenApiInfo })); 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/BlacksmithWorkshop/BlacksmithWorkshopRestApi/appsettings.json b/BlacksmithWorkshop/BlacksmithWorkshopRestApi/appsettings.json index 10f68b8..5990d80 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopRestApi/appsettings.json +++ b/BlacksmithWorkshop/BlacksmithWorkshopRestApi/appsettings.json @@ -5,5 +5,13 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "labwork15kafis@gmail.com", + "MailPassword": "passlab15" + + }