From a7f5e9f99fb63cc00cdb8c75338ec197dc92594d Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Wed, 15 May 2024 22:47:25 +0400 Subject: [PATCH] lab7 --- .../BusinessLogics/ClientLogic.cs | 11 +- .../BusinessLogics/MessageInfoLogic.cs | 48 +++ .../BusinessLogics/OrderLogic.cs | 61 +++- .../FishFactoryBusinessLogic.csproj | 1 + .../MailWorker/AbstractMailWorker.cs | 85 +++++ .../MailWorker/MailKitWorker.cs | 83 +++++ .../Controllers/HomeController.cs | 12 +- .../Views/Home/Mails.cshtml | 54 ++++ .../Views/Shared/_Layout.cshtml | 3 + .../BindingModels/MailConfigBindingModel.cs | 18 ++ .../BindingModels/MailSendInfoBindingModel.cs | 15 + .../BindingModels/MessageInfoBindingModel.cs | 19 ++ .../IMessageInfoLogic.cs | 17 + .../SearchModels/MessageInfoSearchModel.cs | 14 + .../StoragesContracts/IMessageInfoStorage.cs | 19 ++ .../ViewModels/MessageInfoViewModel.cs | 24 ++ .../Models/IMessageInfoModel.cs | 18 ++ .../FishFactoryDatabase.cs | 1 + .../Implements/ClientStorage.cs | 2 +- .../Implements/MessageInfoStorage.cs | 63 ++++ .../20240502185406_mail.Designer.cs | 296 ++++++++++++++++++ .../Migrations/20240502185406_mail.cs | 48 +++ .../FishFactoryDatabaseModelSnapshot.cs | 39 +++ .../Models/Client.cs | 2 + .../Models/MessageInfo.cs | 50 +++ .../DataFileSingleton.cs | 4 + .../Implements/MessageInfoStorage.cs | 58 ++++ .../Models/MessageInfo.cs | 80 +++++ .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 73 +++++ .../Models/MessageInfo.cs | 52 +++ .../Controllers/ClientController.cs | 22 +- FishFactory/FishFactoryRestApi/Program.cs | 18 ++ .../FishFactoryRestApi/appsettings.json | 10 +- FishFactory/FishFactoryView/App.config | 11 + .../FishFactoryView/FormViewMail.Designer.cs | 61 ++++ FishFactory/FishFactoryView/FormViewMail.cs | 47 +++ FishFactory/FishFactoryView/FormViewMail.resx | 120 +++++++ .../FishFactoryView/MainForm.Designer.cs | 59 ++-- FishFactory/FishFactoryView/MainForm.cs | 8 + FishFactory/FishFactoryView/Program.cs | 34 +- 41 files changed, 1621 insertions(+), 41 deletions(-) create mode 100644 FishFactory/FishFactoryBusinessLogic/BusinessLogics/MessageInfoLogic.cs create mode 100644 FishFactory/FishFactoryBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 FishFactory/FishFactoryBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 FishFactory/FishFactoryClientApp/Views/Home/Mails.cshtml create mode 100644 FishFactory/FishFactoryContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 FishFactory/FishFactoryContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 FishFactory/FishFactoryContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 FishFactory/FishFactoryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 FishFactory/FishFactoryContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 FishFactory/FishFactoryContracts/StoragesContracts/IMessageInfoStorage.cs create mode 100644 FishFactory/FishFactoryContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 FishFactory/FishFactoryDataModels/Models/IMessageInfoModel.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.Designer.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/MessageInfo.cs create mode 100644 FishFactory/FishFactoryFileImplement/Implements/MessageInfoStorage.cs create mode 100644 FishFactory/FishFactoryFileImplement/Models/MessageInfo.cs create mode 100644 FishFactory/FishFactoryListImplement/Implements/MessageInfoStorage.cs create mode 100644 FishFactory/FishFactoryListImplement/Models/MessageInfo.cs create mode 100644 FishFactory/FishFactoryView/App.config create mode 100644 FishFactory/FishFactoryView/FormViewMail.Designer.cs create mode 100644 FishFactory/FishFactoryView/FormViewMail.cs create mode 100644 FishFactory/FishFactoryView/FormViewMail.resx diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ClientLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ClientLogic.cs index 4a171bf..2263a06 100644 --- a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/ClientLogic.cs @@ -4,6 +4,7 @@ using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; using FishFactoryContracts.ViewModels; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; using System; using System.Collections.Generic; using System.Linq; @@ -109,7 +110,15 @@ namespace FishFactoryBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет пароля пользователя", nameof(model.Password)); } - _logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Id:{Id}", model.ClientFIO, model.Email, model.Id); + if (string.IsNullOrEmpty(model.Email) || !Regex.IsMatch(model.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", RegexOptions.IgnoreCase)) + { + throw new ArgumentException("Некорретно введен email пользователя", nameof(model.Email)); + } + if (string.IsNullOrEmpty(model.Email) || !Regex.IsMatch(model.Password, @"^(?=.*\d)(?=.*\W)(?=.*[^\d\s]).+$")) + { + throw new ArgumentException("Некорректно введен пароль пользователя", nameof(model.Password)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}.Email:{Email}.Password:{Password}.Id:{Id}", model.ClientFIO, model.Email, model.Password, model.Id); var element = _clientStorage.GetElement(new ClientSearchModel { Email = model.Email diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/MessageInfoLogic.cs new file mode 100644 index 0000000..ae593ac --- /dev/null +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -0,0 +1,48 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryBusinessLogic.BusinessLogics +{ + public class MessageInfoLogic : IMessageInfoLogic + { + private readonly ILogger _logger; + private readonly IMessageInfoStorage _messageInfoStorage; + public MessageInfoLogic(ILogger logger, IMessageInfoStorage MessageInfoStorage) + { + _logger = logger; + _messageInfoStorage = MessageInfoStorage; + } + + public bool Create(MessageInfoBindingModel model) + { + if (_messageInfoStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public List? ReadList(MessageInfoSearchModel? model) + { + _logger.LogInformation("ReadList. MessageId:{MessageId}.ClientId:{ClientId} ", model?.MessageId, model?.ClientId); + var list = (model == null) ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + } +} diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs index bbb1cca..d103da6 100644 --- a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -1,4 +1,5 @@ -using FishFactoryContracts.BindingModels; +using FishFactoryBusinessLogic.MailWorker; +using FishFactoryContracts.BindingModels; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryContracts.SearchModels; using FishFactoryContracts.StoragesContracts; @@ -17,12 +18,16 @@ namespace FishFactoryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientStorage _clientStorage; static readonly object locker = new object(); - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientStorage clientStorage) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientStorage = clientStorage; } public OrderViewModel? ReadElement(OrderSearchModel model) { @@ -59,11 +64,14 @@ namespace FishFactoryBusinessLogic.BusinessLogics CheckModel(model); if (model.Status != OrderStatus.Неизвестен) return false; model.Status = OrderStatus.Принят; - if (_orderStorage.Insert(model) == null) + var order = _orderStorage.Insert(model); + if (order == null) { _logger.LogWarning("Insert operation failed"); return false; } + var clientView = _clientStorage.GetElement(new() { Id = order!.ClientId }); + SendEmail(clientView, order); return true; } @@ -83,8 +91,15 @@ namespace FishFactoryBusinessLogic.BusinessLogics } model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; - _orderStorage.Update(model); - return true; + var result = _orderStorage.Update(model); + if (result == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + var clientView = _clientStorage.GetElement(new() { Id = result!.ClientId }); + SendEmail(clientView, result); + return true; } public bool TakeOrderInWork(OrderBindingModel model) @@ -105,8 +120,7 @@ namespace FishFactoryBusinessLogic.BusinessLogics return ChangeStatus(model, OrderStatus.Выдан); } - private void CheckModel(OrderBindingModel model, bool withParams = -true) + private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { @@ -126,5 +140,36 @@ true) } _logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id); } - } + public void SendEmail(ClientViewModel clientModel, OrderViewModel orderModel) + { + if (clientModel == null && orderModel == null) + { + return; + } + + MailSendInfoBindingModel mailModel; + + if (orderModel.Status == OrderStatus.Принят) + { + mailModel = new MailSendInfoBindingModel + { + MailAddress = clientModel.Email, + Subject = $"Order №{orderModel.Id}", + Text = $"Your order №{orderModel.Id} by {orderModel.DateCreate} on {orderModel.Sum} was accepted!" + }; + } + else + { + mailModel = new MailSendInfoBindingModel + { + MailAddress = clientModel.Email, + Subject = $"Order №{orderModel.Id}", + Text = $"Order №{orderModel.Id} status was changed to {orderModel.Status}" + }; + } + + + _mailWorker.MailSendAsync(mailModel); + } + } } diff --git a/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj b/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj index 785273a..6071b3b 100644 --- a/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj +++ b/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/FishFactory/FishFactoryBusinessLogic/MailWorker/AbstractMailWorker.cs b/FishFactory/FishFactoryBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..953aa13 --- /dev/null +++ b/FishFactory/FishFactoryBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,85 @@ +using FishFactoryBusinessLogic.BusinessLogics; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryBusinessLogic.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/FishFactory/FishFactoryBusinessLogic/MailWorker/MailKitWorker.cs b/FishFactory/FishFactoryBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..feae477 --- /dev/null +++ b/FishFactory/FishFactoryBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,83 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using MailKit.Net.Pop3; +using MailKit.Security; + +namespace FishFactoryBusinessLogic.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/FishFactory/FishFactoryClientApp/Controllers/HomeController.cs b/FishFactory/FishFactoryClientApp/Controllers/HomeController.cs index 718ca73..0deaaf7 100644 --- a/FishFactory/FishFactoryClientApp/Controllers/HomeController.cs +++ b/FishFactory/FishFactoryClientApp/Controllers/HomeController.cs @@ -140,5 +140,15 @@ namespace FishFactoryClientApp.Controllers var _canned = APIClient.GetRequest($"api/main/getcanned?cannedid={canned}"); return Math.Round(count * (_canned?.Price ?? 1), 2); } - } + [HttpGet] + public IActionResult Mails() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return + View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + } + } } \ No newline at end of file diff --git a/FishFactory/FishFactoryClientApp/Views/Home/Mails.cshtml b/FishFactory/FishFactoryClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..9b7a49d --- /dev/null +++ b/FishFactory/FishFactoryClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using FishFactoryContracts.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/FishFactory/FishFactoryClientApp/Views/Shared/_Layout.cshtml b/FishFactory/FishFactoryClientApp/Views/Shared/_Layout.cshtml index 48ee3b8..c0a94d8 100644 --- a/FishFactory/FishFactoryClientApp/Views/Shared/_Layout.cshtml +++ b/FishFactory/FishFactoryClientApp/Views/Shared/_Layout.cshtml @@ -25,6 +25,9 @@ + diff --git a/FishFactory/FishFactoryContracts/BindingModels/MailConfigBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..1b2f2dc --- /dev/null +++ b/FishFactory/FishFactoryContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryContracts/BindingModels/MailSendInfoBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..f6855b4 --- /dev/null +++ b/FishFactory/FishFactoryContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryContracts/BindingModels/MessageInfoBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..3ae0bfa --- /dev/null +++ b/FishFactory/FishFactoryContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,19 @@ +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.BindingModels +{ + public class MessageInfoBindingModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + public int? ClientId { get; set; } + public string SenderName { get; set; } = string.Empty; + public string Subject { get; set; } = string.Empty; + public string Body { get; set; } = string.Empty; + public DateTime DateDelivery { get; set; } + } +} diff --git a/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..89f528a --- /dev/null +++ b/FishFactory/FishFactoryContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,17 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} diff --git a/FishFactory/FishFactoryContracts/SearchModels/MessageInfoSearchModel.cs b/FishFactory/FishFactoryContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..b9227ca --- /dev/null +++ b/FishFactory/FishFactoryContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + } +} diff --git a/FishFactory/FishFactoryContracts/StoragesContracts/IMessageInfoStorage.cs b/FishFactory/FishFactoryContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..51124ee --- /dev/null +++ b/FishFactory/FishFactoryContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,19 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + List GetFilteredList(MessageInfoSearchModel model); + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/FishFactory/FishFactoryContracts/ViewModels/MessageInfoViewModel.cs b/FishFactory/FishFactoryContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..e1cf599 --- /dev/null +++ b/FishFactory/FishFactoryContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,24 @@ +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryContracts.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/FishFactory/FishFactoryDataModels/Models/IMessageInfoModel.cs b/FishFactory/FishFactoryDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..5732ac9 --- /dev/null +++ b/FishFactory/FishFactoryDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDataModels.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/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs index d037fba..8abe1a3 100644 --- a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -24,5 +24,6 @@ namespace FishFactoryDatabaseImplement public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } public virtual DbSet Implementers { set; get; } + public virtual DbSet Messages { set; get; } } } diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/ClientStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/ClientStorage.cs index b92c705..37ce54f 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Implements/ClientStorage.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/ClientStorage.cs @@ -37,7 +37,7 @@ namespace FishFactoryDatabaseImplement.Implements } using var context = new FishFactoryDatabase(); return context.Clients.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Email) && model.Email == x.Email) || (model.Id.HasValue && model.Id == x.Id))?.GetViewModel; + (!string.IsNullOrEmpty(model.Email) && model.Email.Equals(x.Email)) || (model.Id.HasValue && model.Id == x.Id))?.GetViewModel; } public ClientViewModel? Insert(ClientBindingModel model) diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/MessageInfoStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..271aec3 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,63 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Messages.Include(x => x.Client).FirstOrDefault(x => x.MessageId.Equals(model.MessageId))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Messages.Include(x => x.Client).Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Messages.Include(x => x.Client).Select(x => x.GetViewModel).ToList(); + } + + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new FishFactoryDatabase(); + model.ClientId = context.Clients.FirstOrDefault(x => x.Email.Equals(model.SenderName))?.Id; + var message = MessageInfo.Create(model); + if (message == null) + return null; + context.Messages.Add(message); + try + { + context.SaveChanges(); + return message.GetViewModel; + } + catch (Exception ex) + { + return null; + } + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.Designer.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.Designer.cs new file mode 100644 index 0000000..8ecacdb --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.Designer.cs @@ -0,0 +1,296 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + [Migration("20240502185406_mail")] + partial class mail + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.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("FishFactoryDatabaseImplement.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("FishFactoryDatabaseImplement.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("FishFactoryDatabaseImplement.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("Messages"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + 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("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.Navigation("Canned"); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.cs new file mode 100644 index 0000000..76bc551 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240502185406_mail.cs @@ -0,0 +1,48 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class mail : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Messages", + 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_Messages", x => x.MessageId); + table.ForeignKey( + name: "FK_Messages_Clients_ClientId", + column: x => x.ClientId, + principalTable: "Clients", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Messages_ClientId", + table: "Messages", + column: "ClientId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Messages"); + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs index 59e1033..95a6196 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs @@ -140,6 +140,36 @@ namespace FishFactoryDatabaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("FishFactoryDatabaseImplement.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("Messages"); + }); + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -202,6 +232,15 @@ namespace FishFactoryDatabaseImplement.Migrations b.Navigation("Component"); }); + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.MessageInfo", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Client", "Client") + .WithMany() + .HasForeignKey("ClientId"); + + b.Navigation("Client"); + }); + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => { b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Client.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Client.cs index 0a3be24..a8c58b5 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Models/Client.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Client.cs @@ -24,6 +24,8 @@ namespace FishFactoryDatabaseImplement.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) { diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/MessageInfo.cs b/FishFactory/FishFactoryDatabaseImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..71f0062 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/MessageInfo.cs @@ -0,0 +1,50 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryDatabaseImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + public string MessageId { get; private set; } = string.Empty; + public int? ClientId { get; private set; } + public string SenderName { get; private set; } = string.Empty; + public DateTime DateDelivery { get; private set; } = DateTime.Now; + public string Subject { get; private set; } = string.Empty; + public string Body { get; private set; } = string.Empty; + public virtual Client? Client { get; private set; } + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new MessageInfo + { + MessageId = model.MessageId, + ClientId = model.ClientId, + SenderName = model.SenderName, + Body = model.Body, + DateDelivery = model.DateDelivery, + Subject = model.Subject, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + ClientId = ClientId, + SenderName = SenderName, + Body = Body, + DateDelivery = DateDelivery, + Subject = Subject, + }; + } +} diff --git a/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs b/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs index c824b0e..9018a31 100644 --- a/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs +++ b/FishFactory/FishFactoryFileImplement/DataFileSingleton.cs @@ -16,11 +16,13 @@ namespace FishFactoryFileImplement private readonly string CannedFileName = "Canned.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 Canneds { get; private set; } public List Clients { get; private set; } public List Implementers { get; private set; } + public List Messages { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -50,6 +52,7 @@ namespace FishFactoryFileImplement 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 SaveMessages() => SaveData(Messages, MessageInfoFileName, "Messages", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; @@ -57,6 +60,7 @@ namespace FishFactoryFileImplement Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; Implementers = LoadData(ImplementerFileName, "Implementer", x => Implementer.Create(x)!)!; + Messages = LoadData(MessageInfoFileName, "MessageInfo", x => MessageInfo.Create(x)!)!; } } } diff --git a/FishFactory/FishFactoryFileImplement/Implements/MessageInfoStorage.cs b/FishFactory/FishFactoryFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..61da87f --- /dev/null +++ b/FishFactory/FishFactoryFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,58 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + return _source.Messages.FirstOrDefault(x => x.MessageId.Equals(model.MessageId))?.GetViewModel; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + if (!model.ClientId.HasValue) + { + return new(); + } + return _source.Messages.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + return _source.Messages.Select(x => x.GetViewModel).ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + _source.SaveMessages(); + return newMessage.GetViewModel; + } + } +} diff --git a/FishFactory/FishFactoryFileImplement/Models/MessageInfo.cs b/FishFactory/FishFactoryFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..d476504 --- /dev/null +++ b/FishFactory/FishFactoryFileImplement/Models/MessageInfo.cs @@ -0,0 +1,80 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace FishFactoryFileImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + ClientId = model.ClientId, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public static MessageInfo? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Body = element.Attribute("Body")!.Value, + Subject = element.Attribute("Subject")!.Value, + ClientId = Convert.ToInt32(element.Attribute("ClientId")!.Value), + MessageId = element.Attribute("MessageId")!.Value, + SenderName = element.Attribute("SenderName")!.Value, + DateDelivery = Convert.ToDateTime(element.Attribute("DateDelivery")!.Value), + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + public XElement GetXElement => new("MessageInfo", + new XAttribute("Body", Body), + new XAttribute("Subject", Subject), + new XAttribute("ClientId", ClientId), + new XAttribute("MessageId", MessageId), + new XAttribute("SenderName", SenderName), + new XAttribute("DateDelivery", DateDelivery) + ); + } +} diff --git a/FishFactory/FishFactoryListImplement/DataListSingleton.cs b/FishFactory/FishFactoryListImplement/DataListSingleton.cs index dcd496a..060ed70 100644 --- a/FishFactory/FishFactoryListImplement/DataListSingleton.cs +++ b/FishFactory/FishFactoryListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace FishFactoryListImplement public List Canneds { 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 FishFactoryListImplement Canneds = new List(); Clients = new List(); Implementers = new List(); + Messages = new List(); } public static DataListSingleton GetInstance() { diff --git a/FishFactory/FishFactoryListImplement/Implements/MessageInfoStorage.cs b/FishFactory/FishFactoryListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..6e24a13 --- /dev/null +++ b/FishFactory/FishFactoryListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,73 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage(DataListSingleton source) + { + _source = DataListSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (string.IsNullOrEmpty(model.MessageId)) + { + return null; + } + + foreach (var msg in _source.Messages) + { + if (msg.MessageId.Equals(model.MessageId)) + { + return msg.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + var list = new List(); + foreach (var msg in _source.Messages) + { + if (msg.ClientId.HasValue && model.ClientId == msg.ClientId) + { + list.Add(msg.GetViewModel); + } + } + return list; + } + + public List GetFullList() + { + var list = new List(); + foreach (var msg in _source.Messages) + { + list.Add(msg.GetViewModel); + } + return list; + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + var newMessage = MessageInfo.Create(model); + if (newMessage == null) + { + return null; + } + _source.Messages.Add(newMessage); + return newMessage.GetViewModel; + } + } +} diff --git a/FishFactory/FishFactoryListImplement/Models/MessageInfo.cs b/FishFactory/FishFactoryListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..b30888c --- /dev/null +++ b/FishFactory/FishFactoryListImplement/Models/MessageInfo.cs @@ -0,0 +1,52 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FishFactoryListImplement.Models +{ + public class MessageInfo : IMessageInfoModel + { + public string MessageId { get; private set; } = string.Empty; + + public int? ClientId { get; private set; } + + public string SenderName { get; private set; } = string.Empty; + + public DateTime DateDelivery { get; private set; } = DateTime.Now; + + public string Subject { get; private set; } = string.Empty; + + public string Body { get; private set; } = string.Empty; + + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Body = model.Body, + Subject = model.Subject, + MessageId = model.MessageId, + SenderName = model.SenderName, + DateDelivery = model.DateDelivery, + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + + } +} diff --git a/FishFactory/FishFactoryRestApi/Controllers/ClientController.cs b/FishFactory/FishFactoryRestApi/Controllers/ClientController.cs index 5a442d7..a3108d4 100644 --- a/FishFactory/FishFactoryRestApi/Controllers/ClientController.cs +++ b/FishFactory/FishFactoryRestApi/Controllers/ClientController.cs @@ -12,10 +12,12 @@ namespace FishFactoryRestApi.Controllers { private readonly ILogger _logger; private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger logger) + private readonly IMessageInfoLogic _messageLogic; + public ClientController(IClientLogic logic, ILogger logger, IMessageInfoLogic messageLogic) { _logger = logger; _logic = logic; + _messageLogic = messageLogic; } [HttpGet] public ClientViewModel? Login(string login, string password) @@ -34,7 +36,23 @@ namespace FishFactoryRestApi.Controllers throw; } } - [HttpPost] + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _messageLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + [HttpPost] public void Register(ClientBindingModel model) { try diff --git a/FishFactory/FishFactoryRestApi/Program.cs b/FishFactory/FishFactoryRestApi/Program.cs index b6cc610..5708ba0 100644 --- a/FishFactory/FishFactoryRestApi/Program.cs +++ b/FishFactory/FishFactoryRestApi/Program.cs @@ -1,4 +1,6 @@ using FishFactoryBusinessLogic.BusinessLogics; +using FishFactoryBusinessLogic.MailWorker; +using FishFactoryContracts.BindingModels; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryContracts.StoragesContracts; using FishFactoryDatabaseImplement.Implements; @@ -13,11 +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(); +builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "FishFactoryRestApi", Version = "v1" }); @@ -28,6 +36,16 @@ if (app.Environment.IsDevelopment()) app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "FishFactoryRestApi v1")); } +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()) +}); app.UseHttpsRedirection(); app.UseAuthentication(); diff --git a/FishFactory/FishFactoryRestApi/appsettings.json b/FishFactory/FishFactoryRestApi/appsettings.json index 10f68b8..730807f 100644 --- a/FishFactory/FishFactoryRestApi/appsettings.json +++ b/FishFactory/FishFactoryRestApi/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" -} + "AllowedHosts": "*", + "SmtpClientHost": "smtp.mail.ru", + "SmtpClientPort": "587", + "PopHost": "pop.mail.ru", + "PopPort": "995", + "MailLogin": "laba7_done@mail.ru", + "MailPassword": "wK8vxFXG567zus51ftEJ" +} \ No newline at end of file diff --git a/FishFactory/FishFactoryView/App.config b/FishFactory/FishFactoryView/App.config new file mode 100644 index 0000000..c91908e --- /dev/null +++ b/FishFactory/FishFactoryView/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormViewMail.Designer.cs b/FishFactory/FishFactoryView/FormViewMail.Designer.cs new file mode 100644 index 0000000..90746d9 --- /dev/null +++ b/FishFactory/FishFactoryView/FormViewMail.Designer.cs @@ -0,0 +1,61 @@ +namespace FishFactoryView +{ + partial class FormViewMail + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + dataGridView = new DataGridView(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(12, 12); + dataGridView.Name = "dataGridView"; + dataGridView.RowTemplate.Height = 25; + dataGridView.Size = new Size(776, 426); + dataGridView.TabIndex = 0; + // + // FormViewMail + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(dataGridView); + Name = "FormViewMail"; + Text = "Список отчетов"; + Load += ViewMailForm_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/FishFactory/FishFactoryView/FormViewMail.cs b/FishFactory/FishFactoryView/FormViewMail.cs new file mode 100644 index 0000000..d53566c --- /dev/null +++ b/FishFactory/FishFactoryView/FormViewMail.cs @@ -0,0 +1,47 @@ +using FishFactoryContracts.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 FishFactoryView +{ + public partial class FormViewMail : Form + { + private readonly ILogger _logger; + private readonly IMessageInfoLogic _logic; + public FormViewMail(ILogger logger, IMessageInfoLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void ViewMailForm_Load(object sender, EventArgs e) + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["ClientId"].Visible = false; + dataGridView.Columns["MessageId"].Visible = false; + dataGridView.Columns["Body"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка списка писем"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки писем"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } +} diff --git a/FishFactory/FishFactoryView/FormViewMail.resx b/FishFactory/FishFactoryView/FormViewMail.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/FishFactory/FishFactoryView/FormViewMail.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/FishFactory/FishFactoryView/MainForm.Designer.cs b/FishFactory/FishFactoryView/MainForm.Designer.cs index ba77f05..88cbfd8 100644 --- a/FishFactory/FishFactoryView/MainForm.Designer.cs +++ b/FishFactory/FishFactoryView/MainForm.Designer.cs @@ -43,6 +43,7 @@ ComponentCannedToolStripMenuItem = new ToolStripMenuItem(); OrdersToolStripMenuItem = new ToolStripMenuItem(); StartingWorkToolStripMenuItem = new ToolStripMenuItem(); + mailToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); menuStrip1.SuspendLayout(); SuspendLayout(); @@ -50,18 +51,20 @@ // dataGridView // dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; - dataGridView.Location = new Point(-1, 30); + dataGridView.Location = new Point(-1, 22); + dataGridView.Margin = new Padding(3, 2, 3, 2); dataGridView.Name = "dataGridView"; dataGridView.RowHeadersWidth = 51; dataGridView.RowTemplate.Height = 29; - dataGridView.Size = new Size(871, 457); + dataGridView.Size = new Size(762, 343); dataGridView.TabIndex = 1; // // button1 // - button1.Location = new Point(885, 55); + button1.Location = new Point(774, 41); + button1.Margin = new Padding(3, 2, 3, 2); button1.Name = "button1"; - button1.Size = new Size(179, 29); + button1.Size = new Size(157, 22); button1.TabIndex = 2; button1.Text = "Создать заказ"; button1.UseVisualStyleBackColor = true; @@ -69,9 +72,10 @@ // // button4 // - button4.Location = new Point(885, 133); + button4.Location = new Point(774, 100); + button4.Margin = new Padding(3, 2, 3, 2); button4.Name = "button4"; - button4.Size = new Size(179, 29); + button4.Size = new Size(157, 22); button4.TabIndex = 5; button4.Text = "Заказ выдан"; button4.UseVisualStyleBackColor = true; @@ -79,9 +83,10 @@ // // button5 // - button5.Location = new Point(885, 206); + button5.Location = new Point(774, 154); + button5.Margin = new Padding(3, 2, 3, 2); button5.Name = "button5"; - button5.Size = new Size(179, 29); + button5.Size = new Size(157, 22); button5.TabIndex = 6; button5.Text = "Обновить список"; button5.UseVisualStyleBackColor = true; @@ -90,10 +95,11 @@ // menuStrip1 // menuStrip1.ImageScalingSize = new Size(20, 20); - menuStrip1.Items.AddRange(new ToolStripItem[] { spravochnikToolStripMenuItem, otchetToolStripMenuItem, StartingWorkToolStripMenuItem }); + menuStrip1.Items.AddRange(new ToolStripItem[] { spravochnikToolStripMenuItem, otchetToolStripMenuItem, StartingWorkToolStripMenuItem, mailToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Size = new Size(1076, 28); + menuStrip1.Padding = new Padding(5, 2, 0, 2); + menuStrip1.Size = new Size(942, 24); menuStrip1.TabIndex = 7; menuStrip1.Text = "menuStrip1"; // @@ -101,34 +107,34 @@ // spravochnikToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { компонентыToolStripMenuItem, консервыToolStripMenuItem, ClientsToolStripMenuItem, ImplementerToolStripMenuItem }); spravochnikToolStripMenuItem.Name = "spravochnikToolStripMenuItem"; - spravochnikToolStripMenuItem.Size = new Size(117, 24); + spravochnikToolStripMenuItem.Size = new Size(94, 20); spravochnikToolStripMenuItem.Text = "Справочники"; // // компонентыToolStripMenuItem // компонентыToolStripMenuItem.Name = "компонентыToolStripMenuItem"; - компонентыToolStripMenuItem.Size = new Size(185, 26); + компонентыToolStripMenuItem.Size = new Size(149, 22); компонентыToolStripMenuItem.Text = "Компоненты"; компонентыToolStripMenuItem.Click += ComponentToolStripMenuItem_Click; // // консервыToolStripMenuItem // консервыToolStripMenuItem.Name = "консервыToolStripMenuItem"; - консервыToolStripMenuItem.Size = new Size(185, 26); + консервыToolStripMenuItem.Size = new Size(149, 22); консервыToolStripMenuItem.Text = "Консервы"; консервыToolStripMenuItem.Click += CannedToolStripMenuItem_Click; // // ClientsToolStripMenuItem // ClientsToolStripMenuItem.Name = "ClientsToolStripMenuItem"; - ClientsToolStripMenuItem.Size = new Size(185, 26); + ClientsToolStripMenuItem.Size = new Size(149, 22); ClientsToolStripMenuItem.Text = "Клиенты"; ClientsToolStripMenuItem.Click += ClientsToolStripMenuItem_Click; // // ImplementerToolStripMenuItem // ImplementerToolStripMenuItem.Name = "ImplementerToolStripMenuItem"; - ImplementerToolStripMenuItem.Size = new Size(185, 26); + ImplementerToolStripMenuItem.Size = new Size(149, 22); ImplementerToolStripMenuItem.Text = "Исполнители"; ImplementerToolStripMenuItem.Click += ImplementerToolStripMenuItem_Click; // @@ -136,48 +142,56 @@ // otchetToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { ComponentsToolStripMenuItem, ComponentCannedToolStripMenuItem, OrdersToolStripMenuItem }); otchetToolStripMenuItem.Name = "otchetToolStripMenuItem"; - otchetToolStripMenuItem.Size = new Size(73, 24); + otchetToolStripMenuItem.Size = new Size(60, 20); otchetToolStripMenuItem.Text = "Отчёты"; // // ComponentsToolStripMenuItem // ComponentsToolStripMenuItem.Name = "ComponentsToolStripMenuItem"; - ComponentsToolStripMenuItem.Size = new Size(276, 26); + ComponentsToolStripMenuItem.Size = new Size(218, 22); ComponentsToolStripMenuItem.Text = "Список компонентов"; ComponentsToolStripMenuItem.Click += ComponentsToolStripMenuItem_Click; // // ComponentCannedToolStripMenuItem // ComponentCannedToolStripMenuItem.Name = "ComponentCannedToolStripMenuItem"; - ComponentCannedToolStripMenuItem.Size = new Size(276, 26); + ComponentCannedToolStripMenuItem.Size = new Size(218, 22); ComponentCannedToolStripMenuItem.Text = "Компоненты по изделиям"; ComponentCannedToolStripMenuItem.Click += ComponentCannedToolStripMenuItem_Click; // // OrdersToolStripMenuItem // OrdersToolStripMenuItem.Name = "OrdersToolStripMenuItem"; - OrdersToolStripMenuItem.Size = new Size(276, 26); + OrdersToolStripMenuItem.Size = new Size(218, 22); OrdersToolStripMenuItem.Text = "Список заказов"; OrdersToolStripMenuItem.Click += OrdersToolStripMenuItem_Click; // // StartingWorkToolStripMenuItem // StartingWorkToolStripMenuItem.Name = "StartingWorkToolStripMenuItem"; - StartingWorkToolStripMenuItem.Size = new Size(114, 24); + StartingWorkToolStripMenuItem.Size = new Size(92, 20); StartingWorkToolStripMenuItem.Text = "Запуск работ"; StartingWorkToolStripMenuItem.Click += StartingWorkToolStripMenuItem_Click; // + // mailToolStripMenuItem + // + mailToolStripMenuItem.Name = "mailToolStripMenuItem"; + mailToolStripMenuItem.Size = new Size(102, 20); + mailToolStripMenuItem.Text = "Отчет на почту"; + mailToolStripMenuItem.Click += mailToolStripMenuItem_Click; + // // MainForm // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1076, 487); + ClientSize = new Size(942, 365); Controls.Add(button5); Controls.Add(button4); Controls.Add(button1); Controls.Add(dataGridView); Controls.Add(menuStrip1); MainMenuStrip = menuStrip1; + Margin = new Padding(3, 2, 3, 2); Name = "MainForm"; Text = "Рыбный завод"; Load += FormMain_Load; @@ -204,5 +218,6 @@ private ToolStripMenuItem ClientsToolStripMenuItem; private ToolStripMenuItem StartingWorkToolStripMenuItem; private ToolStripMenuItem ImplementerToolStripMenuItem; + private ToolStripMenuItem mailToolStripMenuItem; } } \ No newline at end of file diff --git a/FishFactory/FishFactoryView/MainForm.cs b/FishFactory/FishFactoryView/MainForm.cs index 9479486..4acf0e3 100644 --- a/FishFactory/FishFactoryView/MainForm.cs +++ b/FishFactory/FishFactoryView/MainForm.cs @@ -164,5 +164,13 @@ namespace FishFactoryView MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } + private void mailToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormViewMail)); + if (service is FormViewMail form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/FishFactory/FishFactoryView/Program.cs b/FishFactory/FishFactoryView/Program.cs index 824c20f..20019a3 100644 --- a/FishFactory/FishFactoryView/Program.cs +++ b/FishFactory/FishFactoryView/Program.cs @@ -1,4 +1,4 @@ -using FishFactoryBusinessLogic.BusinessLogics; +using FishFactoryBusinessLogic.BusinessLogics; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryContracts.StoragesContracts; using FishFactoryDatabaseImplement.Implements; @@ -7,7 +7,8 @@ using FishFactoryBusinessLogic.OfficePackage; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; -using System; +using FishFactoryBusinessLogic.MailWorker; +using FishFactoryContracts.BindingModels; namespace FishFactoryView { @@ -27,8 +28,28 @@ namespace FishFactoryView 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) { services.AddLogging(option => @@ -40,14 +61,17 @@ namespace FishFactoryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -63,6 +87,8 @@ namespace FishFactoryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); } } \ No newline at end of file