diff --git a/Pizzeria/Pizzeria/App.config b/Pizzeria/Pizzeria/App.config new file mode 100644 index 0000000..f7dfd6a --- /dev/null +++ b/Pizzeria/Pizzeria/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Pizzeria/Pizzeria/Program.cs b/Pizzeria/Pizzeria/Program.cs index 244a87e..6f922b8 100644 --- a/Pizzeria/Pizzeria/Program.cs +++ b/Pizzeria/Pizzeria/Program.cs @@ -7,6 +7,9 @@ using PizzeriaDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; +using DocumentFormat.OpenXml.Wordprocessing; +using PizzeriaBusinessLogic.MailWorker; +using PizzeriaContracts.BindingModels; namespace Pizzeria { @@ -15,57 +18,80 @@ namespace Pizzeria private static ServiceProvider? _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider; /// - /// The main entry point for the application. + /// The main entry point for the application. /// [STAThread] static void Main() { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - IServiceCollection services = new ServiceCollection(); + var services = new ServiceCollection(); ConfigureServices(services); - _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetService()); + try + { + var mailSender = _serviceProvider.GetService(); + mailSender?.MailConfig(new MailConfigBindingModel + { + MailLogin = System.Configuration.ConfigurationManager.AppSettings["MailLogin"] ?? string.Empty, + MailPassword = System.Configuration.ConfigurationManager.AppSettings["MailPassword"] ?? string.Empty, + SmtpClientHost = System.Configuration.ConfigurationManager.AppSettings["SmtpClientHost"] ?? string.Empty, + SmtpClientPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpClient Port"]), + PopHost = System.Configuration.ConfigurationManager.AppSettings["PopHost"] ?? string.Empty, + PopPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PopPort"]) + }); + // + var timer = new System.Threading.Timer(new + TimerCallback(MailCheck!), null, 0, 100000); + } + catch (Exception ex) + { + var logger = _serviceProvider.GetService(); + logger?.LogError(ex, " "); + } + Application.Run(_serviceProvider.GetRequiredService()); } - - private static void ConfigureServices(IServiceCollection services) + private static void ConfigureServices(ServiceCollection services) { services.AddLogging(option => { option.SetMinimumLevel(LogLevel.Information); option.AddNLog("nlog.config"); }); - - 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.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(); - 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.AddTransient(); + services.AddTransient(); } + private static void MailCheck(object obj) => + ServiceProvider?.GetService()?.MailCheck(); } } \ No newline at end of file diff --git a/Pizzeria/PizzeriaBusinessLogic/MailWorker/AbstractMailWorker.cs b/Pizzeria/PizzeriaBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..c2d3959 --- /dev/null +++ b/Pizzeria/PizzeriaBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,77 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; + +namespace PizzeriaBusinessLogic.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/Pizzeria/PizzeriaBusinessLogic/MailWorker/MailKitWorker.cs b/Pizzeria/PizzeriaBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..8c995e9 --- /dev/null +++ b/Pizzeria/PizzeriaBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,75 @@ +using Microsoft.Extensions.Logging; +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.BusinessLogicsContracts; +using System.Net.Mail; +using System.Net; +using System.Text; +using MailKit.Net.Pop3; +using MailKit.Security; + +namespace PizzeriaBusinessLogic.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; + } + } +} diff --git a/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj b/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj index 61876fd..71b9aae 100644 --- a/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj +++ b/Pizzeria/PizzeriaBusinessLogic/PizzeriaBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/Pizzeria/PizzeriaClientApp/Controllers/HomeController.cs b/Pizzeria/PizzeriaClientApp/Controllers/HomeController.cs index cc08177..caff2d2 100644 --- a/Pizzeria/PizzeriaClientApp/Controllers/HomeController.cs +++ b/Pizzeria/PizzeriaClientApp/Controllers/HomeController.cs @@ -3,6 +3,7 @@ using PizzeriaContracts.ViewModels; using PizzeriaClientApp.Models; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using Microsoft.Extensions.Logging; namespace PizzeriaClientApp.Controllers { @@ -146,5 +147,16 @@ namespace PizzeriaClientApp.Controllers ); 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}")); +} } } diff --git a/Pizzeria/PizzeriaClientApp/PizzeriaClientApp.csproj b/Pizzeria/PizzeriaClientApp/PizzeriaClientApp.csproj index 4f83ff7..6db08e3 100644 --- a/Pizzeria/PizzeriaClientApp/PizzeriaClientApp.csproj +++ b/Pizzeria/PizzeriaClientApp/PizzeriaClientApp.csproj @@ -7,6 +7,7 @@ + diff --git a/Pizzeria/PizzeriaClientApp/Views/Home/Mails.cshtml b/Pizzeria/PizzeriaClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..e8c3f35 --- /dev/null +++ b/Pizzeria/PizzeriaClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using PizzeriaContracts.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/Pizzeria/PizzeriaClientApp/Views/Shared/_Layout.cshtml b/Pizzeria/PizzeriaClientApp/Views/Shared/_Layout.cshtml index 4b3b4af..55dbda5 100644 --- a/Pizzeria/PizzeriaClientApp/Views/Shared/_Layout.cshtml +++ b/Pizzeria/PizzeriaClientApp/Views/Shared/_Layout.cshtml @@ -25,6 +25,9 @@ + diff --git a/Pizzeria/PizzeriaContracts/BindingModels/MailConfigBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..06303f4 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,12 @@ +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/BindingModels/MailSendInfoBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..4d8be63 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,9 @@ +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/BindingModels/MessageInfoBindingModel.cs b/Pizzeria/PizzeriaContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..f950171 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,14 @@ +using PizzeriaDataModels.Models; + +namespace PizzeriaContracts.BindingModels +{ + public class MessageInfoBindingModel : IMessageInfoModel + { + public string MessageId { get; set; } = string.Empty; + public int? ClientId { get; set; } + public string SenderName { get; set; } = string.Empty; + public DateTime DateDelivery { get; set; } + public string Subject { get; set; } = string.Empty; + public string Body { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..33b43e5 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,12 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} \ No newline at end of file diff --git a/Pizzeria/PizzeriaContracts/SearchModels/MessageInfoSearchModel.cs b/Pizzeria/PizzeriaContracts/SearchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..66c07a6 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/SearchModels/MessageInfoSearchModel.cs @@ -0,0 +1,8 @@ +namespace PizzeriaContracts.SearchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + } +} diff --git a/Pizzeria/PizzeriaContracts/StoragesContracts/IMessageInfoStorage.cs b/Pizzeria/PizzeriaContracts/StoragesContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..d3cbef9 --- /dev/null +++ b/Pizzeria/PizzeriaContracts/StoragesContracts/IMessageInfoStorage.cs @@ -0,0 +1,14 @@ +using PizzeriaContracts.BindingModels; +using PizzeriaContracts.SearchModels; +using PizzeriaContracts.ViewModels; + +namespace PizzeriaContracts.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/Pizzeria/PizzeriaContracts/ViewModels/MessageInfoViewModel.cs b/Pizzeria/PizzeriaContracts/ViewModels/MessageInfoViewModel.cs new file mode 100644 index 0000000..b0976da --- /dev/null +++ b/Pizzeria/PizzeriaContracts/ViewModels/MessageInfoViewModel.cs @@ -0,0 +1,20 @@ +using PizzeriaDataModels.Models; +using System.ComponentModel; + +namespace PizzeriaContracts.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/Pizzeria/PizzeriaDataModels/Models/IMessageInfoModel.cs b/Pizzeria/PizzeriaDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..74de8f7 --- /dev/null +++ b/Pizzeria/PizzeriaDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,12 @@ +namespace PizzeriaDataModels.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/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs b/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs index 7a6c686..7183fdc 100644 --- a/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs +++ b/Pizzeria/PizzeriaRestApi/Controllers/ClientController.cs @@ -11,10 +11,12 @@ namespace AbstractShopRestApi.Controllers { private readonly ILogger _logger; private readonly IClientLogic _logic; - public ClientController(IClientLogic logic, ILogger logger) + private readonly IMessageInfoLogic _mailLogic; + public ClientController(IClientLogic logic, ILogger logger, IMessageInfoLogic mailLogic) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] public ClientViewModel? Login(string login, string password) @@ -59,5 +61,20 @@ namespace AbstractShopRestApi.Controllers throw; } } + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } } } diff --git a/Pizzeria/PizzeriaRestApi/Program.cs b/Pizzeria/PizzeriaRestApi/Program.cs index ae9b484..6f122b0 100644 --- a/Pizzeria/PizzeriaRestApi/Program.cs +++ b/Pizzeria/PizzeriaRestApi/Program.cs @@ -3,6 +3,8 @@ using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.StoragesContracts; using PizzeriaDatabaseImplement.Implements; using Microsoft.OpenApi.Models; +using PizzeriaBusinessLogic.MailWorker; +using PizzeriaContracts.BindingModels; var builder = WebApplication.CreateBuilder(args); builder.Logging.SetMinimumLevel(LogLevel.Trace); @@ -30,6 +32,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/Pizzeria/PizzeriaRestApi/appsettings.json b/Pizzeria/PizzeriaRestApi/appsettings.json index 10f68b8..665f96f 100644 --- a/Pizzeria/PizzeriaRestApi/appsettings.json +++ b/Pizzeria/PizzeriaRestApi/appsettings.json @@ -5,5 +5,11 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" -} + "AllowedHosts": "*", + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "labwork15kafis@gmail.com", + "MailPassword": "passlab15" +} \ No newline at end of file