diff --git a/.gitignore b/.gitignore index ca1c7a3..52737dc 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,9 @@ # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs +# dll файлы +*.dll + # Mono auto generated files mono_crash.* diff --git a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ComponentBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ComponentBindingModel.cs index 331ecc3..613d22c 100644 --- a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ComponentBindingModel.cs +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ComponentBindingModel.cs @@ -5,9 +5,7 @@ namespace SofrwareInstallationContracts.BindingModels public class ComponentBindingModel : IComponentModel { public string ComponentName { get; set; } = string.Empty; - public double Cost { get; set; } - public int Id { get; set; } } } diff --git a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ImplementerBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ImplementerBindingModel.cs index 3326d3a..44f1aa9 100644 --- a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ImplementerBindingModel.cs +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/ImplementerBindingModel.cs @@ -5,13 +5,9 @@ namespace SofrwareInstallationContracts.BindingModels public class ImplementerBindingModel : IImplementerModel { public int Id { get; set; } - public string ImplementerFIO { get; set; } = string.Empty; - public string Password { get; set; } = string.Empty; - public int WorkExperience { get; set; } - public int Qualification { get; set; } } } diff --git a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MailConfigBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..66d66e9 --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SofrwareInstallationContracts.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/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MailSendInfoBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..8ef8e71 --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,9 @@ +namespace SofrwareInstallationContracts.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/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MessageInfoBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..ad14e2e --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,21 @@ +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SofrwareInstallationContracts.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; } + public bool HasRead { get; set; } + public string? Reply { get; set; } + } +} diff --git a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/OrderBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/OrderBindingModel.cs index a1afb1d..2c07b3a 100644 --- a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/OrderBindingModel.cs +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/OrderBindingModel.cs @@ -6,22 +6,14 @@ namespace SofrwareInstallationContracts.BindingModels public class OrderBindingModel : IOrderModel { public int PackageId { get; set; } - public int ClientId { get; set; } - public int Count { get; set; } - public double Sum { get; set; } - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplement { get; set; } - public int Id { get; set; } public string PackageName { get; set; } = string.Empty; - public int? ImplementerId { get; set; } } } diff --git a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/PackageBindingModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/PackageBindingModel.cs index ab3f646..693e609 100644 --- a/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/PackageBindingModel.cs +++ b/SoftwareInstallation/SofrwareInstallationContracts/BindingModels/PackageBindingModel.cs @@ -5,11 +5,8 @@ namespace SofrwareInstallationContracts.BindingModels public class PackageBindingModel : IPackageModel { public string PackageName { get; set; } = string.Empty; - public double Price { get; set; } - public Dictionary PackageComponents { get; set; } = new(); - public int Id { get; set; } } } diff --git a/SoftwareInstallation/SofrwareInstallationContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/SoftwareInstallation/SofrwareInstallationContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..26bfa29 --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,17 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.ViewModels; + +namespace SofrwareInstallationContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + + bool Create(MessageInfoBindingModel model); + + bool Update(MessageInfoBindingModel model); + + MessageInfoViewModel? ReadElement(MessageInfoSearchModel model); + } +} diff --git a/SoftwareInstallation/SofrwareInstallationContracts/SearchModels/MessageInfoSearchModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..ce9510a --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,10 @@ +namespace SofrwareInstallationContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + public int? Page { get; set; } + public int? PageSize { get; set; } + } +} diff --git a/SoftwareInstallation/SofrwareInstallationContracts/StoragesContracts/IMessageInfoStorage.cs b/SoftwareInstallation/SofrwareInstallationContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..e9eb4df --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,19 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.ViewModels; + +namespace SofrwareInstallationContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + + MessageInfoViewModel? Update(MessageInfoBindingModel model); + } +} diff --git a/SoftwareInstallation/SofrwareInstallationContracts/ViewModels/MessageInfoViewModel.cs b/SoftwareInstallation/SofrwareInstallationContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..a8a4e5d --- /dev/null +++ b/SoftwareInstallation/SofrwareInstallationContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,30 @@ +using SoftwareInstallationDataModels.Models; +using System.ComponentModel; + +namespace SofrwareInstallationContracts.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; + + [DisplayName("Прочитано")] + public bool HasRead { get; set; } + + [DisplayName("Ответ")] + public string? Reply { get; set; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallation/App.config b/SoftwareInstallation/SoftwareInstallation/App.config new file mode 100644 index 0000000..00e7e59 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormMails.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormMails.Designer.cs new file mode 100644 index 0000000..fb6902b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormMails.Designer.cs @@ -0,0 +1,102 @@ +namespace SoftwareInstallationView +{ + 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() + { + this.DataGridView = new System.Windows.Forms.DataGridView(); + this.PrevPageButton = new System.Windows.Forms.Button(); + this.NextPageButton = new System.Windows.Forms.Button(); + this.labelInfoPages = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); + this.SuspendLayout(); + // + // DataGridView + // + this.DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.DataGridView.Location = new System.Drawing.Point(57, 3); + this.DataGridView.Name = "DataGridView"; + this.DataGridView.RowTemplate.Height = 25; + this.DataGridView.Size = new System.Drawing.Size(687, 411); + this.DataGridView.TabIndex = 0; + this.DataGridView.RowHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.DataGridView_RowHeaderMouseClick); + // + // PrevPageButton + // + this.PrevPageButton.Location = new System.Drawing.Point(12, 158); + this.PrevPageButton.Name = "PrevPageButton"; + this.PrevPageButton.Size = new System.Drawing.Size(38, 69); + this.PrevPageButton.TabIndex = 1; + this.PrevPageButton.Text = "<="; + this.PrevPageButton.UseVisualStyleBackColor = true; + this.PrevPageButton.Click += new System.EventHandler(this.PrevPageButton_Click); + // + // NextPageButton + // + this.NextPageButton.Location = new System.Drawing.Point(750, 159); + this.NextPageButton.Name = "NextPageButton"; + this.NextPageButton.Size = new System.Drawing.Size(38, 68); + this.NextPageButton.TabIndex = 2; + this.NextPageButton.Text = "=>"; + this.NextPageButton.UseVisualStyleBackColor = true; + this.NextPageButton.Click += new System.EventHandler(this.NextPageButton_Click); + // + // labelInfoPages + // + this.labelInfoPages.AutoSize = true; + this.labelInfoPages.Location = new System.Drawing.Point(387, 426); + this.labelInfoPages.Name = "labelInfoPages"; + this.labelInfoPages.Size = new System.Drawing.Size(75, 15); + this.labelInfoPages.TabIndex = 3; + this.labelInfoPages.Text = "{0} страница"; + // + // FormMails + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.labelInfoPages); + this.Controls.Add(this.NextPageButton); + this.Controls.Add(this.PrevPageButton); + this.Controls.Add(this.DataGridView); + this.Name = "FormMails"; + this.Text = "Эл. письма"; + this.Load += new System.EventHandler(this.FormMails_Load); + ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private DataGridView DataGridView; + private Button PrevPageButton; + private Button NextPageButton; + private Label labelInfoPages; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormMails.cs b/SoftwareInstallation/SoftwareInstallation/FormMails.cs new file mode 100644 index 0000000..153d2ba --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormMails.cs @@ -0,0 +1,103 @@ +using SofrwareInstallationContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System.Windows.Forms; +using SofrwareInstallationContracts.ViewModels; + +namespace SoftwareInstallationView +{ + public partial class FormMails : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + private int currentPage = 1; + public int pageSize = 5; + + public FormMails(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + PrevPageButton.Enabled = false; + LoadData(); + } + + private bool LoadData() + { + try + { + var list = _logic.ReadList(new() + { + Page = currentPage, + PageSize = pageSize, + }); + if (list != null) + { + DataGridView.DataSource = list; + DataGridView.Columns["ClientId"].Visible = false; + DataGridView.Columns["MessageId"].Visible = false; + DataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка писем"); + labelInfoPages.Text = $"{currentPage} страница"; + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки писем"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + return false; + } + } + + private void FormMails_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void PrevPageButton_Click(object sender, EventArgs e) + { + if (currentPage == 1) + { + _logger.LogWarning("Неккоректный номер страницы {page}", currentPage - 1); + return; + } + currentPage--; + if (LoadData()) + { + NextPageButton.Enabled = true; + if (currentPage == 1) + { + PrevPageButton.Enabled = false; + } + } + } + + private void NextPageButton_Click(object sender, EventArgs e) + { + currentPage++; + if (!LoadData() || ((List)DataGridView.DataSource).Count == 0) + { + _logger.LogWarning("Out of range messages"); + currentPage--; + LoadData(); + NextPageButton.Enabled = false; + } + else + { + PrevPageButton.Enabled = true; + } + } + + private void DataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormReplyMail)); + if (service is FormReplyMail form) + { + form.MessageId = (string)DataGridView.Rows[e.RowIndex].Cells["MessageId"].Value; + form.ShowDialog(); + LoadData(); + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallation/FormMails.resx b/SoftwareInstallation/SoftwareInstallation/FormMails.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/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/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs index 15fb6ce..3a385ac 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.Designer.cs @@ -33,6 +33,7 @@ this.ИзделияToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.КомпонентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.клиентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.исполнителиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.StoreToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.отчетыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.списокИзделийToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -45,6 +46,7 @@ this.OrderReadyButton = new System.Windows.Forms.Button(); this.IssuedOrderButton = new System.Windows.Forms.Button(); this.UpdateListButton = new System.Windows.Forms.Button(); + this.элПисьмаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.исполнителиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.клиентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.StoreReplenishment = new System.Windows.Forms.Button(); @@ -61,7 +63,8 @@ this.MenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.СправочникиToolStripMenuItem, this.отчетыToolStripMenuItem, - this.запускРаботToolStripMenuItem}); + this.запускРаботToolStripMenuItem, + this.элПисьмаToolStripMenuItem}); this.MenuStrip.Location = new System.Drawing.Point(0, 0); this.MenuStrip.Name = "MenuStrip"; this.MenuStrip.Size = new System.Drawing.Size(865, 24); @@ -84,24 +87,31 @@ // ИзделияToolStripMenuItem // this.ИзделияToolStripMenuItem.Name = "ИзделияToolStripMenuItem"; - this.ИзделияToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.ИзделияToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.ИзделияToolStripMenuItem.Text = "Изделия"; this.ИзделияToolStripMenuItem.Click += new System.EventHandler(this.ИзделияToolStripMenuItem_Click); // // КомпонентыToolStripMenuItem // this.КомпонентыToolStripMenuItem.Name = "КомпонентыToolStripMenuItem"; - this.КомпонентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.КомпонентыToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.КомпонентыToolStripMenuItem.Text = "Компоненты"; this.КомпонентыToolStripMenuItem.Click += new System.EventHandler(this.КомпонентыToolStripMenuItem_Click); // // клиентыToolStripMenuItem // this.клиентыToolStripMenuItem.Name = "клиентыToolStripMenuItem"; - this.клиентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.клиентыToolStripMenuItem.Size = new System.Drawing.Size(149, 22); this.клиентыToolStripMenuItem.Text = "Клиенты"; this.клиентыToolStripMenuItem.Click += new System.EventHandler(this.клиентыToolStripMenuItem_Click); // + // исполнителиToolStripMenuItem + // + this.исполнителиToolStripMenuItem.Name = "исполнителиToolStripMenuItem"; + this.исполнителиToolStripMenuItem.Size = new System.Drawing.Size(149, 22); + this.исполнителиToolStripMenuItem.Text = "Исполнители"; + this.исполнителиToolStripMenuItem.Click += new System.EventHandler(this.исполнителиToolStripMenuItem_Click); + // // StoreToolStripMenuItem // this.StoreToolStripMenuItem.Name = "StoreToolStripMenuItem"; @@ -209,12 +219,12 @@ this.UpdateListButton.UseVisualStyleBackColor = true; this.UpdateListButton.Click += new System.EventHandler(this.UpdateListButton_Click); // - // исполнителиToolStripMenuItem + // элПисьмаToolStripMenuItem // - this.исполнителиToolStripMenuItem.Name = "исполнителиToolStripMenuItem"; - this.исполнителиToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.исполнителиToolStripMenuItem.Text = "Исполнители"; - this.исполнителиToolStripMenuItem.Click += new System.EventHandler(this.исполнителиToolStripMenuItem_Click); + this.элПисьмаToolStripMenuItem.Name = "элПисьмаToolStripMenuItem"; + this.элПисьмаToolStripMenuItem.Size = new System.Drawing.Size(82, 20); + this.элПисьмаToolStripMenuItem.Text = "Эл. Письма"; + this.элПисьмаToolStripMenuItem.Click += new System.EventHandler(this.элПисьмаToolStripMenuItem_Click); // // StoreReplenishment // @@ -304,6 +314,7 @@ private ToolStripMenuItem клиентыToolStripMenuItem; private ToolStripMenuItem запускРаботToolStripMenuItem; private ToolStripMenuItem исполнителиToolStripMenuItem; + private ToolStripMenuItem элПисьмаToolStripMenuItem; private ToolStripMenuItem списокМагазиновToolStripMenuItem; private ToolStripMenuItem изделияПоМагазинамToolStripMenuItem; private ToolStripMenuItem списокЗаказовгруппировкаПоДатеToolStripMenuItem; diff --git a/SoftwareInstallation/SoftwareInstallation/FormMain.cs b/SoftwareInstallation/SoftwareInstallation/FormMain.cs index 5155a1e..e033cf9 100644 --- a/SoftwareInstallation/SoftwareInstallation/FormMain.cs +++ b/SoftwareInstallation/SoftwareInstallation/FormMain.cs @@ -267,6 +267,15 @@ namespace SoftwareInstallationView } } + private void элПисьмаToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormMails)); + if (service is FormMails form) + { + form.ShowDialog(); + } + } + private void списокМагазиновToolStripMenuItem_Click(object sender, EventArgs e) { using var dialog = new SaveFileDialog { Filter = "docx|*.docx" }; diff --git a/SoftwareInstallation/SoftwareInstallation/FormReplyMail.Designer.cs b/SoftwareInstallation/SoftwareInstallation/FormReplyMail.Designer.cs new file mode 100644 index 0000000..bcec740 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormReplyMail.Designer.cs @@ -0,0 +1,145 @@ +namespace SoftwareInstallationView +{ + partial class FormReplyMail + { + /// + /// 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() + { + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.HeadTextBox = new System.Windows.Forms.TextBox(); + this.MailTextBox = new System.Windows.Forms.TextBox(); + this.ReplyTextBox = new System.Windows.Forms.TextBox(); + this.ReplyButton = new System.Windows.Forms.Button(); + this.ButtonCancel = new System.Windows.Forms.Button(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(4, 19); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(40, 15); + this.label1.TabIndex = 0; + this.label1.Text = "Тема: "; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(4, 48); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(82, 15); + this.label2.TabIndex = 1; + this.label2.Text = "Содержание: "; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(4, 178); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(44, 15); + this.label3.TabIndex = 2; + this.label3.Text = "Ответ: "; + // + // HeadTextBox + // + this.HeadTextBox.Location = new System.Drawing.Point(92, 16); + this.HeadTextBox.Name = "HeadTextBox"; + this.HeadTextBox.ReadOnly = true; + this.HeadTextBox.Size = new System.Drawing.Size(469, 23); + this.HeadTextBox.TabIndex = 5; + // + // MailTextBox + // + this.MailTextBox.Location = new System.Drawing.Point(92, 45); + this.MailTextBox.Multiline = true; + this.MailTextBox.Name = "MailTextBox"; + this.MailTextBox.ReadOnly = true; + this.MailTextBox.Size = new System.Drawing.Size(469, 93); + this.MailTextBox.TabIndex = 6; + // + // ReplyTextBox + // + this.ReplyTextBox.Location = new System.Drawing.Point(92, 175); + this.ReplyTextBox.Multiline = true; + this.ReplyTextBox.Name = "ReplyTextBox"; + this.ReplyTextBox.Size = new System.Drawing.Size(469, 125); + this.ReplyTextBox.TabIndex = 7; + // + // ReplyButton + // + this.ReplyButton.Location = new System.Drawing.Point(351, 306); + this.ReplyButton.Name = "ReplyButton"; + this.ReplyButton.Size = new System.Drawing.Size(102, 36); + this.ReplyButton.TabIndex = 6; + this.ReplyButton.Text = "Ответить"; + this.ReplyButton.UseVisualStyleBackColor = true; + this.ReplyButton.Click += new System.EventHandler(this.ReplyButton_Click); + // + // ButtonCancel + // + this.ButtonCancel.Location = new System.Drawing.Point(459, 306); + this.ButtonCancel.Name = "ButtonCancel"; + this.ButtonCancel.Size = new System.Drawing.Size(102, 36); + this.ButtonCancel.TabIndex = 7; + this.ButtonCancel.Text = "Отмена"; + this.ButtonCancel.UseVisualStyleBackColor = true; + this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormReplyMail + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(573, 354); + this.Controls.Add(this.ButtonCancel); + this.Controls.Add(this.ReplyButton); + this.Controls.Add(this.ReplyTextBox); + this.Controls.Add(this.MailTextBox); + this.Controls.Add(this.HeadTextBox); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Name = "FormReplyMail"; + this.Text = "Ответ"; + this.Load += new System.EventHandler(this.FormReplyMail_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label label1; + private Label label2; + private Label label3; + private TextBox HeadTextBox; + private TextBox MailTextBox; + private TextBox ReplyTextBox; + private Button ReplyButton; + private Button ButtonCancel; + } +} \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallation/FormReplyMail.cs b/SoftwareInstallation/SoftwareInstallation/FormReplyMail.cs new file mode 100644 index 0000000..efcecee --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormReplyMail.cs @@ -0,0 +1,86 @@ +using Microsoft.Extensions.Logging; +using SofrwareInstallationContracts.BusinessLogicsContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationBusinessLogic.MailWorker; +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 SoftwareInstallationView +{ + public partial class FormReplyMail : Form + { + private readonly ILogger _logger; + private readonly AbstractMailWorker _mailWorker; + private readonly IMessageInfoLogic _logic; + private MessageInfoViewModel _message; + + public string MessageId { get; set; } = string.Empty; + + public FormReplyMail(ILogger logger, AbstractMailWorker mailWorker, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _mailWorker = mailWorker; + _logic = logic; + } + + private void ReplyButton_Click(object sender, EventArgs e) + { + _mailWorker.MailSendAsync(new() + { + MailAddress = _message.SenderName, + Subject = _message.Subject, + Text = ReplyTextBox.Text, + }); + + _logic.Update(new() + { + MessageId = MessageId, + Reply = ReplyTextBox.Text, + HasRead = true, + }); + + MessageBox.Show("Успешно отправлено письмо", "Отправка письма", MessageBoxButtons.OK); + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + + private void FormReplyMail_Load(object sender, EventArgs e) + { + try + { + _message = _logic.ReadElement(new() { MessageId = MessageId }); + + if (_message == null) + throw new ArgumentNullException("Письма с таким id не существует"); + + Text += $"для {_message.SenderName}"; + HeadTextBox.Text = _message.Subject; + MailTextBox.Text = _message.Body; + + if (_message.HasRead is false) + { + _logic.Update(new() { MessageId = MessageId, HasRead = true, Reply = _message.Reply }); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения собщения"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallation/FormReplyMail.resx b/SoftwareInstallation/SoftwareInstallation/FormReplyMail.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallation/FormReplyMail.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/SoftwareInstallation/SoftwareInstallation/Program.cs b/SoftwareInstallation/SoftwareInstallation/Program.cs index 9b06ab6..d9de629 100644 --- a/SoftwareInstallation/SoftwareInstallation/Program.cs +++ b/SoftwareInstallation/SoftwareInstallation/Program.cs @@ -7,6 +7,8 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using SoftwareInstallationBusinessLogic.OfficePackage; using SoftwareInstallationBusinessLogic.OfficePackage.Implements; +using SofrwareInstallationContracts.BindingModels; +using SoftwareInstallationBusinessLogic.MailWorker; namespace SoftwareInstallationView { @@ -22,6 +24,26 @@ namespace SoftwareInstallationView var services = new ServiceCollection(); ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); + 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, "Error"); + } Application.Run(_serviceProvider.GetRequiredService()); } @@ -33,12 +55,15 @@ namespace SoftwareInstallationView option.AddNLog("nlog.config"); }); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); @@ -65,11 +90,15 @@ namespace SoftwareInstallationView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); } + + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); } } diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ClientLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ClientLogic.cs index fc5ae1a..0a46a1d 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/ClientLogic.cs @@ -4,6 +4,7 @@ using SofrwareInstallationContracts.BusinessLogicsContracts; using SofrwareInstallationContracts.SearchModels; using SofrwareInstallationContracts.StoragesContracts; using SofrwareInstallationContracts.ViewModels; +using System.Text.RegularExpressions; namespace SoftwareInstallationBusinessLogic.BusinessLogic { @@ -86,27 +87,44 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic { throw new ArgumentNullException(nameof(model)); } + if (!withParams) { return; } + if (string.IsNullOrEmpty(model.ClientFIO)) { throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO)); } + if (string.IsNullOrEmpty(model.Email)) { throw new ArgumentNullException("Нет почты клиента", nameof(model.Email)); } + if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password)); } + + if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный email", nameof(model.Email)); + } + + if (!Regex.IsMatch(model.Password, @"^^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Неправильно введенный пароль", nameof(model.Password)); + } + _logger.LogInformation("Client. ClientFIO: {ClientFIO}. Email: {Email}. Id: {Id}", model.ClientFIO, model.Email, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel { Email = model.Email }); + if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Клиент с такой почтой уже есть"); diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/MessageInfoLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..34519e0 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/MessageInfoLogic.cs @@ -0,0 +1,76 @@ +using Microsoft.Extensions.Logging; +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.BusinessLogicsContracts; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; + +namespace SoftwareInstallationBusinessLogic.BusinessLogic +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + private readonly IMessageInfoStorage _messageStorage; + + public MessageInfoLogic(ILogger logger, IMessageInfoStorage messageStorage) + { + _logger = logger; + _messageStorage = messageStorage; + } + + public bool Create(MessageInfoBindingModel model) + { + if (_messageStorage.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 ? _messageStorage.GetFullList() : _messageStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + + return null; + } + + _logger.LogInformation("ReadList. Count: {Count}", list.Count); + + return list; + } + + public MessageInfoViewModel? ReadElement(MessageInfoSearchModel model) + { + var res = _messageStorage.GetElement(model); + + if (res == null) + { + _logger.LogWarning("Read element operation failed"); + + return null; + } + + return res; + } + + public bool Update(MessageInfoBindingModel model) + { + if (_messageStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + + return false; + } + + return true; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs index b26f93f..a4ec9c1 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/BusinessLogic/OrderLogic.cs @@ -5,6 +5,7 @@ using SofrwareInstallationContracts.StoragesContracts; using SofrwareInstallationContracts.ViewModels; using Microsoft.Extensions.Logging; using SoftwareInstallationDataModels.Enums; +using SoftwareInstallationBusinessLogic.MailWorker; namespace SoftwareInstallationBusinessLogic.BusinessLogic { @@ -12,13 +13,17 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; private readonly IStoreLogic _storeLogic; private readonly IPackageStorage _packageStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPackageStorage packageStorage,IStoreLogic storeLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPackageStorage packageStorage,IStoreLogic storeLogic, AbstractMailWorker mailWorker, IClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; _storeLogic = storeLogic; _packageStorage = packageStorage; } @@ -34,14 +39,17 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic } model.Status = OrderStatus.Принят; + var result = _orderStorage.Insert(model); - if (_orderStorage.Insert(model) == null) + if (result == null) { model.Status = OrderStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } + SendOrderMessage(result.ClientId, $"Установка ПО, Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); + return true; } @@ -82,11 +90,18 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic model.PackageId = vmodel.PackageId; model.Sum = vmodel.Sum; model.Count = vmodel.Count; + + var result = _orderStorage.Update(model); + + if (result == null) if (_orderStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } + + SendOrderMessage(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); + return true; } @@ -122,26 +137,29 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic return list; } - private bool CheckModel(OrderBindingModel model) + private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } + if (!withParams) + { + return; + } + if (model.PackageId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у изделия", nameof(model.PackageId)); + } if (model.Count <= 0) { - throw new ArgumentException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); + throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count)); } if (model.Sum <= 0) { - throw new ArgumentException("Суммарная стоимость заказа должна быть больше 0", nameof(model.Sum)); + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); } - if (model.DateCreate > model.DateImplement) - { - throw new ArgumentException("Время создания заказа не может быть больше времени его выполнения", nameof(model.DateImplement)); - } - - return true; + _logger.LogInformation("Order. OrderID:{Id}. Sum:{ Sum}. PackageId: { PackageId}", model.Id, model.Sum, model.PackageId); } public OrderViewModel? ReadElement(OrderSearchModel model) @@ -166,5 +184,24 @@ namespace SoftwareInstallationBusinessLogic.BusinessLogic return element; } + + private bool SendOrderMessage(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/SoftwareInstallation/SoftwareInstallationBusinessLogic/MailWorker/AbstractMailWorker.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..94c7488 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,89 @@ +using Microsoft.Extensions.Logging; +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.BusinessLogicsContracts; + +namespace SoftwareInstallationBusinessLogic.MailWorker +{ + public abstract class AbstractMailWorker + { + protected string _mailLogin = string.Empty; + protected string _mailPassword = string.Empty; + protected string _smtpClientHost = string.Empty; + protected int _smtpClientPort; + protected string _popHost = string.Empty; + protected int _popPort; + private readonly IMessageInfoLogic _messageInfoLogic; + private readonly IClientLogic _clientLogic; + private readonly ILogger _logger; + + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) + { + _logger = logger; + _messageInfoLogic = messageInfoLogic; + _clientLogic = clientLogic; + } + + public void MailConfig(MailConfigBindingModel config) + { + _mailLogin = config.MailLogin; + _mailPassword = config.MailPassword; + _smtpClientHost = config.SmtpClientHost; + _smtpClientPort = config.SmtpClientPort; + _popHost = config.PopHost; + _popPort = config.PopPort; + _logger.LogDebug("Config: {login}, {password}, {clientHost}, {clientPOrt}, {popHost}, {popPort}", _mailLogin, _mailPassword, _smtpClientHost, _smtpClientPort, _popHost, _popPort); + } + + public async void MailSendAsync(MailSendInfoBindingModel info) + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_smtpClientHost) || _smtpClientPort == 0) + { + return; + } + + if (string.IsNullOrEmpty(info.MailAddress) || string.IsNullOrEmpty(info.Subject) || string.IsNullOrEmpty(info.Text)) + { + return; + } + + _logger.LogDebug("Send Mail: {To}, {Subject}", info.MailAddress, info.Subject); + + await SendMailAsync(info); + } + + public async void MailCheck() + { + if (string.IsNullOrEmpty(_mailLogin) || string.IsNullOrEmpty(_mailPassword)) + { + return; + } + + if (string.IsNullOrEmpty(_popHost) || _popPort == 0) + { + return; + } + + if (_messageInfoLogic == null) + { + return; + } + + var list = await ReceiveMailAsync(); + _logger.LogDebug("Check Mail: {Count} new mails", list.Count); + + foreach (var mail in list) + { + mail.ClientId = _clientLogic.ReadElement(new() { Email = mail.SenderName })?.Id; + _messageInfoLogic.Create(mail); + } + } + + protected abstract Task SendMailAsync(MailSendInfoBindingModel info); + protected abstract Task> ReceiveMailAsync(); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/MailWorker/MailKitWorker.cs b/SoftwareInstallation/SoftwareInstallationBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..ac5084e --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,78 @@ +using Microsoft.Extensions.Logging; +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.BusinessLogicsContracts; +using System.Net.Mail; +using System.Net; +using System.Text; +using MailKit.Net.Pop3; +using MailKit.Security; + +namespace SoftwareInstallationBusinessLogic.MailWorker +{ + public class MailKitWorker : AbstractMailWorker + { + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) : base(logger, messageInfoLogic, clientLogic) {} + + protected override async Task SendMailAsync(MailSendInfoBindingModel info) + { + using var objMailMessage = new MailMessage(); + using var objSmtpClient = new SmtpClient(_smtpClientHost, _smtpClientPort); + + try + { + objMailMessage.From = new MailAddress(_mailLogin); + objMailMessage.To.Add(new MailAddress(info.MailAddress)); + objMailMessage.Subject = info.Subject; + objMailMessage.Body = info.Text; + objMailMessage.SubjectEncoding = Encoding.UTF8; + objMailMessage.BodyEncoding = Encoding.UTF8; + objSmtpClient.UseDefaultCredentials = false; + objSmtpClient.EnableSsl = true; + objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; + objSmtpClient.Credentials = new NetworkCredential(_mailLogin, _mailPassword); + + await Task.Run(() => objSmtpClient.Send(objMailMessage)); + } + catch (Exception) + { + throw; + } + } + + protected override async Task> ReceiveMailAsync() + { + var list = new List(); + using var client = new Pop3Client(); + await Task.Run(() => + { + try + { + client.Connect(_popHost, _popPort, SecureSocketOptions.SslOnConnect); + client.Authenticate(_mailLogin, _mailPassword); + for (int i = 0; i < client.Count; i++) + { + var message = client.GetMessage(i); + foreach (var mail in message.From.Mailboxes) + { + list.Add(new MessageInfoBindingModel + { + DateDelivery = message.Date.DateTime, + MessageId = message.MessageId, + SenderName = mail.Address, + Subject = message.Subject, + Body = message.TextBody + }); + } + } + } + catch (AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + return list; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj b/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj index ebfbb91..1f3f49c 100644 --- a/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj +++ b/SoftwareInstallation/SoftwareInstallationBusinessLogic/SoftwareInstallationBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/SoftwareInstallation/SoftwareInstallationClientApp/APIClient.cs b/SoftwareInstallation/SoftwareInstallationClientApp/APIClient.cs index bff558d..97132c6 100644 --- a/SoftwareInstallation/SoftwareInstallationClientApp/APIClient.cs +++ b/SoftwareInstallation/SoftwareInstallationClientApp/APIClient.cs @@ -8,7 +8,7 @@ namespace SoftwareInstallationClientApp public class APIClient { private static readonly HttpClient _client = new(); - + public static int CurrentPage { get; set; } = 1; public static ClientViewModel? Client { get; set; } = null; public static void Connect(IConfiguration configuration) diff --git a/SoftwareInstallation/SoftwareInstallationClientApp/Controllers/HomeController.cs b/SoftwareInstallation/SoftwareInstallationClientApp/Controllers/HomeController.cs index 552c267..499d1b7 100644 --- a/SoftwareInstallation/SoftwareInstallationClientApp/Controllers/HomeController.cs +++ b/SoftwareInstallation/SoftwareInstallationClientApp/Controllers/HomeController.cs @@ -3,6 +3,9 @@ using SoftwareInstallationClientApp.Models; using SofrwareInstallationContracts.BindingModels; using SofrwareInstallationContracts.ViewModels; using System.Diagnostics; +using System.Text; +using SofrwareInstallationContracts.SearchModels; +using Microsoft.AspNetCore.Mvc.Rendering; namespace SoftwareInstallationClientApp.Controllers { @@ -143,5 +146,53 @@ namespace SoftwareInstallationClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getpackage?packageId={package}"); return count * (prod?.Price ?? 1); } + + [HttpGet] + public IActionResult Mails() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(); + } + + [HttpGet] + public Tuple? SwitchPage(bool isNext) + { + if (isNext) + { + APIClient.CurrentPage++; + } + else + { + if (APIClient.CurrentPage == 1) + { + return null; + } + APIClient.CurrentPage--; + } + + var res = APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client!.Id}&page={APIClient.CurrentPage}"); + + if (isNext && (res == null || res.Count == 0)) + { + APIClient.CurrentPage--; + return Tuple.Create(null, null, APIClient.CurrentPage != 1, false); + } + + StringBuilder htmlTable = new(); + foreach (var mail in res) + { + htmlTable.Append("" + + $"{mail.DateDelivery}" + + $"{mail.Subject}" + + $"{mail.Body}" + + "" + (mail.HasRead ? "Прочитано" : "Непрочитано") + "" + + $"{mail.Reply}" + + ""); + } + return Tuple.Create(htmlTable.ToString(), APIClient.CurrentPage.ToString(), APIClient.CurrentPage != 1, true); + } } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationClientApp/Views/Home/Mails.cshtml b/SoftwareInstallation/SoftwareInstallationClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..b0087cf --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,79 @@ +@{ + ViewData["Title"] = "Mails"; +} +
+

Письма

+
+ +
+ + + + + + + + + + + + +
+ Дата письма + + Тема + + Содержание + + Статус + + Ответ +
+ +
+ + \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationClientApp/Views/Shared/_Layout.cshtml b/SoftwareInstallation/SoftwareInstallationClientApp/Views/Shared/_Layout.cshtml index 219e65e..c518ca5 100644 --- a/SoftwareInstallation/SoftwareInstallationClientApp/Views/Shared/_Layout.cshtml +++ b/SoftwareInstallation/SoftwareInstallationClientApp/Views/Shared/_Layout.cshtml @@ -7,6 +7,7 @@ +
@@ -25,6 +26,9 @@ + diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/MessageInfoStorage.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..b52ff4e --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,72 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataBaseImplement.Models; + +namespace SoftwareInstallationDataBaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId == null) + return null; + using var context = new SoftwareInstallationDataBase(); + return context.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new SoftwareInstallationDataBase(); + + var res = context.Messages + .Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId) + .Select(x => x.GetViewModel); + + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return res.ToList(); + } + + return res.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); + } + + public List GetFullList() + { + using var context = new SoftwareInstallationDataBase(); + return context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + var newMessage = Message.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; + } + + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + using var context = new SoftwareInstallationDataBase(); + var res = context.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId)); + + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + + return res?.GetViewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230504171714_InitDb.Designer.cs similarity index 86% rename from SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs rename to SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230504171714_InitDb.Designer.cs index aeb1982..11df286 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.Designer.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230504171714_InitDb.Designer.cs @@ -12,7 +12,7 @@ using SoftwareInstallationDataBaseImplement; namespace SoftwareInstallationDataBaseImplement.Migrations { [DbContext(typeof(SoftwareInstallationDataBase))] - [Migration("20230503134657_InitDb")] + [Migration("20230504171714_InitDb")] partial class InitDb { /// @@ -97,6 +97,43 @@ namespace SoftwareInstallationDataBaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Message", 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("HasRead") + .HasColumnType("bit"); + + b.Property("Reply") + .HasColumnType("nvarchar(max)"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => { b.Property("Id") @@ -243,6 +280,17 @@ namespace SoftwareInstallationDataBaseImplement.Migrations b.ToTable("StorePackages"); }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Message", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Client", "Client") + .WithMany("Messages") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => { b.HasOne("SoftwareInstallationDataBaseImplement.Models.Client", "Client") @@ -308,6 +356,8 @@ namespace SoftwareInstallationDataBaseImplement.Migrations modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Client", b => { + b.Navigation("Messages"); + b.Navigation("Orders"); }); diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230504171714_InitDb.cs similarity index 87% rename from SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.cs rename to SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230504171714_InitDb.cs index 5a9be04..f4e3357 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230503134657_InitDb.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/20230504171714_InitDb.cs @@ -86,6 +86,30 @@ namespace SoftwareInstallationDataBaseImplement.Migrations table.PrimaryKey("PK_Stores", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + MessageId = table.Column(type: "nvarchar(450)", nullable: false), + ClientId = table.Column(type: "int", nullable: false), + 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), + HasRead = table.Column(type: "bit", nullable: false), + Reply = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.MessageId); + table.ForeignKey( + name: "FK_Messages_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -178,6 +202,11 @@ namespace SoftwareInstallationDataBaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "IX_Messages_ClientId", + table: "Messages", + column: "ClientId"); + migrationBuilder.CreateIndex( name: "IX_Orders_ClientId", table: "Orders", @@ -217,6 +246,9 @@ namespace SoftwareInstallationDataBaseImplement.Migrations /// protected override void Down(MigrationBuilder migrationBuilder) { + migrationBuilder.DropTable( + name: "Messages"); + migrationBuilder.DropTable( name: "Orders"); diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs index ae1125d..d57f607 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Migrations/SoftwareInstallationDataBaseModelSnapshot.cs @@ -94,6 +94,43 @@ namespace SoftwareInstallationDataBaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Message", 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("HasRead") + .HasColumnType("bit"); + + b.Property("Reply") + .HasColumnType("nvarchar(max)"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("MessageId"); + + b.HasIndex("ClientId"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => { b.Property("Id") @@ -240,6 +277,17 @@ namespace SoftwareInstallationDataBaseImplement.Migrations b.ToTable("StorePackages"); }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Message", b => + { + b.HasOne("SoftwareInstallationDataBaseImplement.Models.Client", "Client") + .WithMany("Messages") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + }); + modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Order", b => { b.HasOne("SoftwareInstallationDataBaseImplement.Models.Client", "Client") @@ -305,6 +353,8 @@ namespace SoftwareInstallationDataBaseImplement.Migrations modelBuilder.Entity("SoftwareInstallationDataBaseImplement.Models.Client", b => { + b.Navigation("Messages"); + b.Navigation("Orders"); }); diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Client.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Client.cs index 2d1a8ad..f8a7864 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Client.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Client.cs @@ -18,6 +18,9 @@ namespace SoftwareInstallationDataBaseImplement.Models [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); + + [ForeignKey("ClientId")] + public virtual List Messages { get; set; } = new(); public static Client? Create(ClientBindingModel model) { if (model == null) diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Message.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Message.cs new file mode 100644 index 0000000..831383e --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/Models/Message.cs @@ -0,0 +1,69 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace SoftwareInstallationDataBaseImplement.Models +{ + public class Message : IMessageInfoModel + { + [Key] + public string MessageId { get; private set; } = string.Empty; + public int? ClientId { get; private set; } + [Required] + public string SenderName { get; private set; } = string.Empty; + [Required] + public DateTime DateDelivery { get; private set; } = DateTime.Now; + [Required] + public string Subject { get; private set; } = string.Empty; + [Required] + public string Body { get; private set; } = string.Empty; + [Required] + public bool HasRead { get; private set; } + + public string? Reply { get; private set; } + + public virtual Client Client { get; set; } + + public static Message? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Reply = model.Reply, + HasRead = model.HasRead, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + Reply = model.Reply; + HasRead = model.HasRead; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Reply = Reply, + HasRead = HasRead, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + } +} diff --git a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs index c027421..f60996f 100644 --- a/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs +++ b/SoftwareInstallation/SoftwareInstallationDataBaseImplement/SoftwareInstallationDataBase.cs @@ -9,7 +9,7 @@ namespace SoftwareInstallationDataBaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-VG5USAH\SQLEXPRESS;Initial Catalog=SoftwareInstallationDataBaseHardFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=COMP-AVZH\SQLEXPRESS;Initial Catalog=SoftwareInstallationDataBaseHardFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -21,5 +21,6 @@ namespace SoftwareInstallationDataBaseImplement public virtual DbSet Stores { set; get; } public virtual DbSet StorePackages { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { set; get; } } } \ No newline at end of file diff --git a/SoftwareInstallation/SoftwareInstallationDataModels/Models/IMessageInfoModel.cs b/SoftwareInstallation/SoftwareInstallationDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..27dcdbb --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,14 @@ +namespace SoftwareInstallationDataModels.Models +{ + public interface IMessageInfoModel + { + string MessageId { get; } + int? ClientId { get; } + string SenderName { get; } + DateTime DateDelivery { get; } + string Subject { get; } + string Body { get; } + public bool HasRead { get; } + public string? Reply { get; } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs index f645127..7163b91 100644 --- a/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/DataFileSingleton.cs @@ -12,6 +12,7 @@ namespace SoftwareInstallationFileImplement private readonly string PackageFileName = "Package.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageFileName = "Message.xml"; private readonly string StoreFileName = "Store.xml"; public List Components { get; private set; } @@ -19,6 +20,7 @@ namespace SoftwareInstallationFileImplement public List Packages { get; private set; } public List Clients { get; private set; } public List Implementers { get; private set; } + public List Messages { get; private set; } public List Stores { get; private set; } public static DataFileSingleton GetInstance() @@ -33,8 +35,9 @@ namespace SoftwareInstallationFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName,"Components", x => x.GetXElement); public void SavePackages() => SaveData(Packages, PackageFileName, "Packages", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); - public void SaveClients() => SaveData(Clients, OrderFileName, "Clients", x => x.GetXElement); - public void SaveImplementers() => SaveData(Implementers, OrderFileName, "Implementers", 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 SaveMessages() => SaveData(Messages, MessageFileName, "Messages", x => x.GetXElement); public void SaveStores() => SaveData(Stores, StoreFileName, "Stores", x => x.GetXElement); private DataFileSingleton() @@ -44,6 +47,7 @@ namespace SoftwareInstallationFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + Messages = LoadData(MessageFileName, "Message", x => Message.Create(x)!)!; Stores = LoadData(StoreFileName, "Store", x => Store.Create(x)!)!; } diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/MessageInfoStorage.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..5e3fda2 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,65 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationFileImplement.Models; + +namespace SoftwareInstallationFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton source; + public MessageInfoStorage() + { + source = DataFileSingleton.GetInstance(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId == null) + return null; + return source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + var res = source.Messages + .Where(x => !model.ClientId.HasValue || x.ClientId == model.ClientId) + .Select(x => x.GetViewModel); + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return res.ToList(); + } + return res.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); + } + + public List GetFullList() + { + return source.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = Message.Create(model); + if (newMessage == null) + { + return null; + } + source.Messages.Add(newMessage); + source.SaveMessages(); + return newMessage.GetViewModel; + } + + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + var res = source.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId)); + if (res != null) + { + res.Update(model); + source.SaveMessages(); + } + return res?.GetViewModel; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Message.cs b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Message.cs new file mode 100644 index 0000000..7431ba0 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationFileImplement/Models/Message.cs @@ -0,0 +1,97 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System.Xml.Linq; + +namespace SoftwareInstallationFileImplement.Models +{ + public class Message : 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 bool HasRead { get; private set; } + + public string? Reply { get; private set; } + + public static Message? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Reply = model.Reply, + HasRead = model.HasRead, + Body = model.Body, + Subject = model.Subject, + DateDelivery = model.DateDelivery, + SenderName = model.SenderName, + ClientId = model.ClientId, + MessageId = model.MessageId + }; + } + + public static Message? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Reply = element.Attribute("Reply")!.Value, + HasRead = Convert.ToBoolean(element.Attribute("HasRead")!.Value), + Body = element.Attribute("Body")!.Value, + Subject = element.Attribute("Subject")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + SenderName = element.Attribute("SenderName")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + }; + } + + public void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + Reply = model.Reply; + HasRead = model.HasRead; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + Reply = Reply, + HasRead = HasRead, + DateDelivery = DateDelivery, + SenderName = SenderName, + ClientId = ClientId, + MessageId = MessageId + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Subject", Subject), + new XAttribute("Body", Body), + new XAttribute("Reply", Reply), + new XAttribute("HasRead", HasRead), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs b/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs index a6a6012..8fefbc0 100644 --- a/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs +++ b/SoftwareInstallation/SoftwareInstallationListImplement/DataListSingleton.cs @@ -11,6 +11,7 @@ namespace SoftwareInstallationListImplement public List Packages { get; set; } public List Clients { get; set; } public List Implementers { get; set; } + public List Messages { get; set; } public List Stores { get; set; } private DataListSingleton() @@ -20,6 +21,7 @@ namespace SoftwareInstallationListImplement Packages = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); Stores = new List(); } diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Implements/MessageInfoStorage.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..8e05295 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,95 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.SearchModels; +using SofrwareInstallationContracts.StoragesContracts; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationListImplement.Models; + +namespace SoftwareInstallationListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId == null) + { + return null; + } + foreach (var message in _source.Messages) + { + if (model.MessageId.Equals(message.MessageId)) + return message.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + var result = new List(); + foreach (var item in _source.Messages) + { + if (item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return result; + } + if (model.Page * model.PageSize >= result.Count) + { + return null; + } + List filteredResult = new(); + for (var i = (model.Page.Value - 1) * model.PageSize.Value; i < model.Page.Value * model.PageSize.Value; i++) + { + filteredResult.Add(result[i]); + } + return filteredResult; + } + + public List GetFullList() + { + var result = new List(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = Message.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + + public MessageInfoViewModel? Update(MessageInfoBindingModel model) + { + foreach (var message in _source.Messages) + { + if (message.MessageId.Equals(model.MessageId)) + { + message.Update(model); + return message.GetViewModel; + } + } + return null; + } + } +} diff --git a/SoftwareInstallation/SoftwareInstallationListImplement/Models/Message.cs b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Message.cs new file mode 100644 index 0000000..a0d1068 --- /dev/null +++ b/SoftwareInstallation/SoftwareInstallationListImplement/Models/Message.cs @@ -0,0 +1,71 @@ +using SofrwareInstallationContracts.BindingModels; +using SofrwareInstallationContracts.ViewModels; +using SoftwareInstallationDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SoftwareInstallationListImplement.Models +{ + public class Message : 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 bool HasRead { get; private set; } + + public string? Reply { get; private set; } + + public static Message? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Reply = model.Reply, + HasRead = model.HasRead, + Body = model.Body, + Subject = model.Subject, + DateDelivery = model.DateDelivery, + SenderName = model.SenderName, + ClientId = model.ClientId, + MessageId = model.MessageId + }; + } + + public void Update(MessageInfoBindingModel model) + { + if (model == null) + { + return; + } + Reply = model.Reply; + HasRead = model.HasRead; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Reply = Reply, + HasRead = HasRead, + Subject = Subject, + DateDelivery = DateDelivery, + SenderName = SenderName, + ClientId = ClientId, + MessageId = MessageId + }; + } +} diff --git a/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ClientController.cs b/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ClientController.cs index 1bfc8d9..8185ccb 100644 --- a/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ClientController.cs +++ b/SoftwareInstallation/SoftwareInstallationRestApi/Controllers/ClientController.cs @@ -13,11 +13,35 @@ namespace SoftwareInstallationRestApi.Controllers { private readonly ILogger _logger; private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger logger) + private readonly IMessageInfoLogic _mailLogic; + public int pageSize = 3; + + public ClientController(IClientLogic logic, ILogger logger, IMessageInfoLogic mailLogic) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } + + [HttpGet] + public List? GetMessages(int clientId, int page) + { + try + { + return _mailLogic.ReadList(new() + { + ClientId = clientId, + Page = page, + PageSize = pageSize + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + [HttpGet] public ClientViewModel? Login(string login, string password) { @@ -63,6 +87,5 @@ namespace SoftwareInstallationRestApi.Controllers throw; } } - } } diff --git a/SoftwareInstallation/SoftwareInstallationRestApi/Program.cs b/SoftwareInstallation/SoftwareInstallationRestApi/Program.cs index d7da893..c9b9a7d 100644 --- a/SoftwareInstallation/SoftwareInstallationRestApi/Program.cs +++ b/SoftwareInstallation/SoftwareInstallationRestApi/Program.cs @@ -3,6 +3,8 @@ using SofrwareInstallationContracts.BusinessLogicsContracts; using SofrwareInstallationContracts.StoragesContracts; using SoftwareInstallationDataBaseImplement.Implements; using Microsoft.OpenApi.Models; +using SoftwareInstallationBusinessLogic.MailWorker; +using SofrwareInstallationContracts.BindingModels; var builder = WebApplication.CreateBuilder(args); @@ -13,13 +15,17 @@ 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.AddSingleton(); + builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -33,6 +39,18 @@ 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/SoftwareInstallation/SoftwareInstallationRestApi/appsettings.json b/SoftwareInstallation/SoftwareInstallationRestApi/appsettings.json index 10f68b8..d2cd417 100644 --- a/SoftwareInstallation/SoftwareInstallationRestApi/appsettings.json +++ b/SoftwareInstallation/SoftwareInstallationRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "labwork7zhimol@gmail.com", + "MailPassword": "nnsz pcnf xezb gxtc" }