From a932838d22e647cd4a074e0f65e60f0fb3355e6d Mon Sep 17 00:00:00 2001 From: eegov Date: Sun, 26 Feb 2023 00:05:47 +0400 Subject: [PATCH] Mails --- .../AbstractShopBusinessLogic.csproj | 1 + .../BusinessLogics/MessageInfoLogic.cs | 20 ++++ .../MailWorker/AbstractMailWorker.cs | 92 +++++++++++++++++++ .../MailWorker/MailKitWorker.cs | 78 ++++++++++++++++ .../BindingModels/MailConfigBindingModel.cs | 17 ++++ .../BindingModels/MailSendInfoBindingModel.cs | 11 +++ .../BindingModels/MessageInfoBindingModel.cs | 19 ++++ .../IMessageInfoLogic.cs | 13 +++ .../SearchModels/MessageInfoSearchModel.cs | 9 ++ .../StoragesContracts/IMessageInfoStorage.cs | 17 ++++ .../ViewModels/MessageInfoViewModel.cs | 24 +++++ .../Models/IMessageInfoModel.cs | 17 ++++ .../Implements/MessageInfoStorage.cs | 30 ++++++ .../Implements/MessageInfoStorage.cs | 30 ++++++ .../Implements/MessageInfoStorage.cs | 30 ++++++ .../Controllers/ClientController.cs | 22 ++++- AbstractShop/AbstractShopRestApi/Program.cs | 17 ++++ .../AbstractShopRestApi/appsettings.json | 9 +- .../AbstractShopView/AbstractShopView.csproj | 6 ++ AbstractShop/AbstractShopView/App.config | 11 +++ AbstractShop/AbstractShopView/Program.cs | 38 +++++++- .../Controllers/HomeController.cs | 10 ++ .../Views/Home/Mails.cshtml | 54 +++++++++++ .../Views/Shared/_Layout.cshtml | 3 + 24 files changed, 574 insertions(+), 4 deletions(-) create mode 100644 AbstractShop/AbstractShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs create mode 100644 AbstractShop/AbstractShopBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 AbstractShop/AbstractShopBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 AbstractShop/AbstractShopContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 AbstractShop/AbstractShopContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 AbstractShop/AbstractShopContracts/SearchModels/MessageInfoSearchModel.cs create mode 100644 AbstractShop/AbstractShopContracts/StoragesContracts/IMessageInfoStorage.cs create mode 100644 AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs create mode 100644 AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs create mode 100644 AbstractShop/AbstractShopDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 AbstractShop/AbstractShopFileImplement/Implements/MessageInfoStorage.cs create mode 100644 AbstractShop/AbstractShopListImplement/Implements/MessageInfoStorage.cs create mode 100644 AbstractShop/AbstractShopView/App.config create mode 100644 AbstractShop/AbstractShowClientApp/Views/Home/Mails.cshtml diff --git a/AbstractShop/AbstractShopBusinessLogic/AbstractShopBusinessLogic.csproj b/AbstractShop/AbstractShopBusinessLogic/AbstractShopBusinessLogic.csproj index e95ce3c..6d8f7c8 100644 --- a/AbstractShop/AbstractShopBusinessLogic/AbstractShopBusinessLogic.csproj +++ b/AbstractShop/AbstractShopBusinessLogic/AbstractShopBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/AbstractShop/AbstractShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs b/AbstractShop/AbstractShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs new file mode 100644 index 0000000..44c72a2 --- /dev/null +++ b/AbstractShop/AbstractShopBusinessLogic/BusinessLogics/MessageInfoLogic.cs @@ -0,0 +1,20 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.BusinessLogicsContracts; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopBusinessLogic.BusinessLogics +{ + public class MessageInfoLogic : IMessageInfoLogic + { + public bool Create(MessageInfoBindingModel model) + { + throw new NotImplementedException(); + } + + public List? ReadList(MessageInfoSearchModel? model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopBusinessLogic/MailWorker/AbstractMailWorker.cs b/AbstractShop/AbstractShopBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..3659ebb --- /dev/null +++ b/AbstractShop/AbstractShopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,92 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; + +namespace AbstractShopBusinessLogic.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(); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopBusinessLogic/MailWorker/MailKitWorker.cs b/AbstractShop/AbstractShopBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..5540f79 --- /dev/null +++ b/AbstractShop/AbstractShopBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,78 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.BusinessLogicsContracts; +using MailKit.Net.Pop3; +using MailKit.Security; +using Microsoft.Extensions.Logging; +using System.Net; +using System.Net.Mail; +using System.Text; + +namespace AbstractShopBusinessLogic.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 (AuthenticationException) + { } + finally + { + client.Disconnect(true); + } + }); + return list; + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BindingModels/MailConfigBindingModel.cs b/AbstractShop/AbstractShopContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..251a295 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,17 @@ +namespace AbstractShopContracts.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; } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BindingModels/MailSendInfoBindingModel.cs b/AbstractShop/AbstractShopContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..a026b08 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,11 @@ +namespace AbstractShopContracts.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; + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs b/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..16397de --- /dev/null +++ b/AbstractShop/AbstractShopContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,19 @@ +using AbstractShopDataModels.Models; + +namespace AbstractShopContracts.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; } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..c145d1a --- /dev/null +++ b/AbstractShop/AbstractShopContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,13 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + + bool Create(MessageInfoBindingModel model); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/SearchModels/MessageInfoSearchModel.cs b/AbstractShop/AbstractShopContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..5a14c7a --- /dev/null +++ b/AbstractShop/AbstractShopContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,9 @@ +namespace AbstractShopContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + + public string? MessageId { get; set; } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/StoragesContracts/IMessageInfoStorage.cs b/AbstractShop/AbstractShopContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..f531757 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,17 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopContracts.StoragesContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + + List GetFilteredList(MessageInfoSearchModel model); + + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs b/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..d84d6d5 --- /dev/null +++ b/AbstractShop/AbstractShopContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,24 @@ +using AbstractShopDataModels.Models; +using System.ComponentModel; + +namespace AbstractShopContracts.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; + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs b/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..e51db32 --- /dev/null +++ b/AbstractShop/AbstractShopDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,17 @@ +namespace AbstractShopDataModels.Models +{ + public interface IMessageInfoModel + { + string MessageId { get; } + + int? ClientId { get; } + + string SenderName { get; } + + DateTime DateDelivery { get; } + + string Subject { get; } + + string Body { get; } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopDatabaseImplement/Implements/MessageInfoStorage.cs b/AbstractShop/AbstractShopDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..c974fbd --- /dev/null +++ b/AbstractShop/AbstractShopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,30 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopFileImplement/Implements/MessageInfoStorage.cs b/AbstractShop/AbstractShopFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..8bd4194 --- /dev/null +++ b/AbstractShop/AbstractShopFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,30 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopListImplement/Implements/MessageInfoStorage.cs b/AbstractShop/AbstractShopListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..7ab1bbf --- /dev/null +++ b/AbstractShop/AbstractShopListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,30 @@ +using AbstractShopContracts.BindingModels; +using AbstractShopContracts.SearchModels; +using AbstractShopContracts.StoragesContracts; +using AbstractShopContracts.ViewModels; + +namespace AbstractShopListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/AbstractShop/AbstractShopRestApi/Controllers/ClientController.cs b/AbstractShop/AbstractShopRestApi/Controllers/ClientController.cs index aea3a03..29c57c7 100644 --- a/AbstractShop/AbstractShopRestApi/Controllers/ClientController.cs +++ b/AbstractShop/AbstractShopRestApi/Controllers/ClientController.cs @@ -14,10 +14,13 @@ namespace AbstractShopRestApi.Controllers private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger logger) + private readonly IMessageInfoLogic _mailLogic; + + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] @@ -65,5 +68,22 @@ namespace AbstractShopRestApi.Controllers throw; } } + + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } } } \ No newline at end of file diff --git a/AbstractShop/AbstractShopRestApi/Program.cs b/AbstractShop/AbstractShopRestApi/Program.cs index ab43f08..1898ab8 100644 --- a/AbstractShop/AbstractShopRestApi/Program.cs +++ b/AbstractShop/AbstractShopRestApi/Program.cs @@ -1,4 +1,6 @@ using AbstractShopBusinessLogic.BusinessLogics; +using AbstractShopBusinessLogic.MailWorker; +using AbstractShopContracts.BindingModels; using AbstractShopContracts.BusinessLogicsContracts; using AbstractShopContracts.StoragesContracts; using AbstractShopDatabaseImplement.Implements; @@ -14,11 +16,15 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); + +builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -30,6 +36,17 @@ 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/AbstractShop/AbstractShopRestApi/appsettings.json b/AbstractShop/AbstractShopRestApi/appsettings.json index 10f68b8..d252f8b 100644 --- a/AbstractShop/AbstractShopRestApi/appsettings.json +++ b/AbstractShop/AbstractShopRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "labwork15kafis@gmail.com", + "MailPassword": "xlmo gisw flzo kvam" } diff --git a/AbstractShop/AbstractShopView/AbstractShopView.csproj b/AbstractShop/AbstractShopView/AbstractShopView.csproj index acf0e06..383dddb 100644 --- a/AbstractShop/AbstractShopView/AbstractShopView.csproj +++ b/AbstractShop/AbstractShopView/AbstractShopView.csproj @@ -38,4 +38,10 @@ + + + Always + + + \ No newline at end of file diff --git a/AbstractShop/AbstractShopView/App.config b/AbstractShop/AbstractShopView/App.config new file mode 100644 index 0000000..4a2acbe --- /dev/null +++ b/AbstractShop/AbstractShopView/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/AbstractShop/AbstractShopView/Program.cs b/AbstractShop/AbstractShopView/Program.cs index 8c28ab0..b662e57 100644 --- a/AbstractShop/AbstractShopView/Program.cs +++ b/AbstractShop/AbstractShopView/Program.cs @@ -1,6 +1,8 @@ using AbstractShopBusinessLogic.BusinessLogics; +using AbstractShopBusinessLogic.MailWorker; using AbstractShopBusinessLogic.OfficePackage; using AbstractShopBusinessLogic.OfficePackage.Implements; +using AbstractShopContracts.BindingModels; using AbstractShopContracts.BusinessLogicsContracts; using AbstractShopContracts.StoragesContracts; using AbstractShopDatabaseImplement.Implements; @@ -24,8 +26,34 @@ namespace AbstractShopView // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - // Application.Run(new Form1()); - } + 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, " "); + } + + // Application.Run(new Form1()); + } private static void ConfigureServices(ServiceCollection services) { @@ -39,6 +67,7 @@ namespace AbstractShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -46,7 +75,10 @@ namespace AbstractShopView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); @@ -62,5 +94,7 @@ namespace AbstractShopView services.AddTransient(); services.AddTransient(); } + + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); } } \ No newline at end of file diff --git a/AbstractShop/AbstractShowClientApp/Controllers/HomeController.cs b/AbstractShop/AbstractShowClientApp/Controllers/HomeController.cs index 1adf044..b391bb2 100644 --- a/AbstractShop/AbstractShowClientApp/Controllers/HomeController.cs +++ b/AbstractShop/AbstractShowClientApp/Controllers/HomeController.cs @@ -143,5 +143,15 @@ namespace AbstractShowClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getproduct?productId={product}"); return count * (prod?.Price ?? 1); } + + [HttpGet] + public IActionResult Mails() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + } } } \ No newline at end of file diff --git a/AbstractShop/AbstractShowClientApp/Views/Home/Mails.cshtml b/AbstractShop/AbstractShowClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..075226b --- /dev/null +++ b/AbstractShop/AbstractShowClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using AbstractShopContracts.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/AbstractShop/AbstractShowClientApp/Views/Shared/_Layout.cshtml b/AbstractShop/AbstractShowClientApp/Views/Shared/_Layout.cshtml index 77df392..fd3bd95 100644 --- a/AbstractShop/AbstractShowClientApp/Views/Shared/_Layout.cshtml +++ b/AbstractShop/AbstractShowClientApp/Views/Shared/_Layout.cshtml @@ -25,6 +25,9 @@ +