diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index d917771..d12e142 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -34,12 +34,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics } model.Status = OrderStatus.Принят; model.DateCreate = DateTime.Now; - if (_orderStorage.Insert(model) == null) + var result = _orderStorage.Insert(model); + if (result == null) { _logger.LogWarning("Insert operation failed"); return false; } - SendOrderStatusMail(model, $"Новый заказ создан. Номер заказа #{model.Id}", $"Заказ #{model.Id} от {model.DateCreate} на сумму {model.Sum:.02f} принят"); + SendOrderStatusMail(result.ClientId, $"Новый заказ создан. Номер заказа #{result.Id}", $"Заказ #{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); return true; } @@ -110,12 +111,13 @@ namespace ConfectioneryBusinessLogic.BusinessLogics model.PastryId = vmodel.PastryId; model.Sum = vmodel.Sum; model.Count= vmodel.Count; - if (_orderStorage.Update(model) == null) + var result = _orderStorage.Update(model); + if (result == null) { _logger.LogWarning("Update operation failed"); return false; } - SendOrderStatusMail(model, $"Изменен статус заказа #{model.Id}", $"Заказ #{model.Id} изменен статус на {model.Status}"); + SendOrderStatusMail(result.ClientId, $"Изменен статус заказа #{result.Id}", $"Заказ #{model.Id} изменен статус на {result.Status}"); return true; } @@ -136,9 +138,9 @@ namespace ConfectioneryBusinessLogic.BusinessLogics return element; } - private bool SendOrderStatusMail(OrderBindingModel model, string subject, string text) + private bool SendOrderStatusMail(int clientId, string subject, string text) { - var client = _clientLogic.ReadElement(new() { Id = model.ClientId }); + var client = _clientLogic.ReadElement(new() { Id = clientId }); if (client == null) { return false; diff --git a/Confectionery/App.config b/Confectionery/App.config index 347c78c..400d322 100644 --- a/Confectionery/App.config +++ b/Confectionery/App.config @@ -1,11 +1,11 @@  - + - + - + \ No newline at end of file diff --git a/ConfectioneryClientApp/Controllers/HomeController.cs b/ConfectioneryClientApp/Controllers/HomeController.cs index 83f23d6..c48230a 100644 --- a/ConfectioneryClientApp/Controllers/HomeController.cs +++ b/ConfectioneryClientApp/Controllers/HomeController.cs @@ -144,5 +144,15 @@ namespace ConfectioneryClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getpastry?pastryId={pastry}"); 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/ConfectioneryClientApp/Views/Home/Mails.cshtml b/ConfectioneryClientApp/Views/Home/Mails.cshtml new file mode 100644 index 0000000..e881061 --- /dev/null +++ b/ConfectioneryClientApp/Views/Home/Mails.cshtml @@ -0,0 +1,54 @@ +@using ConfectioneryContracts.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) +
+ } +
diff --git a/ConfectioneryClientApp/Views/Shared/_Layout.cshtml b/ConfectioneryClientApp/Views/Shared/_Layout.cshtml index 8801497..b329d8b 100644 --- a/ConfectioneryClientApp/Views/Shared/_Layout.cshtml +++ b/ConfectioneryClientApp/Views/Shared/_Layout.cshtml @@ -28,6 +28,9 @@ + diff --git a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs index bd25a7c..0e5a4bf 100644 --- a/ConfectioneryDatabaseImplement/MessageInfoStorage.cs +++ b/ConfectioneryDatabaseImplement/MessageInfoStorage.cs @@ -40,7 +40,7 @@ namespace ConfectioneryDatabaseImplement { using var context = new ConfectioneryDatabase(); var newMessage = MessageInfo.Create(model); - if (newMessage == null) + if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId))) { return null; } diff --git a/ConfectioneryRestApi/Controllers/ClientController.cs b/ConfectioneryRestApi/Controllers/ClientController.cs index 6e165ca..70ab65a 100644 --- a/ConfectioneryRestApi/Controllers/ClientController.cs +++ b/ConfectioneryRestApi/Controllers/ClientController.cs @@ -13,11 +13,13 @@ namespace ConfectioneryRestApi.Controllers private readonly ILogger _logger; private readonly IClientLogic _logic; + private readonly IMessageInfoLogic _mailLogic; - public ClientController(IClientLogic logic, ILogger logger) + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; _logic = logic; + _mailLogic = mailLogic; } [HttpGet] @@ -66,5 +68,22 @@ namespace ConfectioneryRestApi.Controllers throw; } } - } + + [HttpGet] + public List? GetMessages(int clientId) + { + try + { + return _mailLogic.ReadList(new MessageInfoSearchModel + { + ClientId = clientId + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения писем клиента"); + throw; + } + } + } } diff --git a/ConfectioneryRestApi/Program.cs b/ConfectioneryRestApi/Program.cs index da7b812..865a728 100644 --- a/ConfectioneryRestApi/Program.cs +++ b/ConfectioneryRestApi/Program.cs @@ -1,7 +1,10 @@ using ConfectioneryBusinessLogic; using ConfectioneryBusinessLogic.BusinessLogics; +using ConfectioneryBusinessLogic.MailWorker; +using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; +using ConfectioneryDatabaseImplement; using ConfectioneryDatabaseImplement.Implements; using Microsoft.OpenApi.Models; @@ -14,7 +17,10 @@ builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddTransient(); +builder.Services.AddSingleton(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddControllers(); @@ -31,6 +37,19 @@ 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/ConfectioneryRestApi/appsettings.json b/ConfectioneryRestApi/appsettings.json index 10f68b8..697acfb 100644 --- a/ConfectioneryRestApi/appsettings.json +++ b/ConfectioneryRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.mail.ru", + "SmtpClientPort": "587", + "PopHost": "pop.mail.ru", + "PopPort": "995", + "MailLogin": "ordersender228@mail.ru", + "MailPassword": "v8czsQ8zztJc5wEHxKPN" }