From 1f141851ea9a6351e26869eb02b2ab9eb93ed80f Mon Sep 17 00:00:00 2001 From: Ismailov_Rovshan Date: Thu, 4 May 2023 21:06:20 +0400 Subject: [PATCH] 7 --- .../BusinessLogic/ClientLogic.cs | 6 + .../BusinessLogic/MessageInfoLogic.cs | 53 ++++ .../BusinessLogic/OrderLogic.cs | 32 +- .../MailWorker/AbstractMailWorker.cs | 97 ++++++ .../MailWorker/MailKitWorker.cs | 84 ++++++ .../RenovationWorkBusinessLogic.csproj | 1 + .../Controllers/HomeController.cs | 10 + .../Views/Home/Create.cshtml | 48 +-- .../Views/Shared/Mails.cshtml | 54 ++++ .../Views/Shared/_Layout.cshtml | 3 + .../BindingModels/MailConfigBindingModel.cs | 23 ++ .../BindingModels/MailSendInfoBindingModel.cs | 17 ++ .../BindingModels/MessageInfoBindingModel.cs | 19 ++ .../IMessageInfoLogic.cs | 17 ++ .../SeatchModels/MessageInfoSearchModel.cs | 14 + .../StorageContracts/IMessageInfoStorage.cs | 19 ++ .../ViewModels/MessageInfoModel.cs | 25 ++ .../Models/IMessageInfoModel.cs | 18 ++ .../Implements/MessageInfoStorage.cs | 58 ++++ .../Implements/OrderStorage.cs | 47 ++- .../20230503184114_Third.Designer.cs | 283 ++++++++++++++++++ .../Migrations/20230503184114_Third.cs | 38 +++ .../RenovationWorkDatabaseModelSnapshot.cs | 28 ++ .../Model/MessageInfo.cs | 53 ++++ .../RenovationWorkDatabase.cs | 2 +- .../DataFileSingleton.cs | 6 + .../Implements/MessageInfoStorage.cs | 59 ++++ .../Models/MessageInfo.cs | 81 +++++ .../DataListSingleton.cs | 2 + .../Implements/MessageInfoStorage.cs | 67 +++++ .../Models/MessageInfo.cs | 46 +++ .../Controllers/ClientController.cs | 23 +- .../RenovationWorkRestApi/Program.cs | 19 ++ .../RenovationWorkRestApi/appsettings.json | 9 +- RenovationWork/RenovationWorkView/App.config | 11 + .../RenovationWorkView/FormMain.Designer.cs | 10 +- RenovationWork/RenovationWorkView/FormMain.cs | 1 + RenovationWork/RenovationWorkView/Program.cs | 34 +++ .../RenovationWorkView.csproj | 3 + 39 files changed, 1381 insertions(+), 39 deletions(-) create mode 100644 RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/MessageInfoLogic.cs create mode 100644 RenovationWork/RenovationWorkBusinessLogic/MailWorker/AbstractMailWorker.cs create mode 100644 RenovationWork/RenovationWorkBusinessLogic/MailWorker/MailKitWorker.cs create mode 100644 RenovationWork/RenovationWorkClientApp/Views/Shared/Mails.cshtml create mode 100644 RenovationWork/RenovationWorkContracts/BindingModels/MailConfigBindingModel.cs create mode 100644 RenovationWork/RenovationWorkContracts/BindingModels/MailSendInfoBindingModel.cs create mode 100644 RenovationWork/RenovationWorkContracts/BindingModels/MessageInfoBindingModel.cs create mode 100644 RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IMessageInfoLogic.cs create mode 100644 RenovationWork/RenovationWorkContracts/SeatchModels/MessageInfoSearchModel.cs create mode 100644 RenovationWork/RenovationWorkContracts/StorageContracts/IMessageInfoStorage.cs create mode 100644 RenovationWork/RenovationWorkContracts/ViewModels/MessageInfoModel.cs create mode 100644 RenovationWork/RenovationWorkDataModels/Models/IMessageInfoModel.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Implements/MessageInfoStorage.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.Designer.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.cs create mode 100644 RenovationWork/RenovationWorkDatabaseImplement/Model/MessageInfo.cs create mode 100644 RenovationWork/RenovationWorkFileImplement/Implements/MessageInfoStorage.cs create mode 100644 RenovationWork/RenovationWorkFileImplement/Models/MessageInfo.cs create mode 100644 RenovationWork/RenovationWorkListImplement/Implements/MessageInfoStorage.cs create mode 100644 RenovationWork/RenovationWorkListImplement/Models/MessageInfo.cs create mode 100644 RenovationWork/RenovationWorkView/App.config diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/ClientLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/ClientLogic.cs index b20da80..276edb3 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/ClientLogic.cs +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/ClientLogic.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace RenovationWorkBusinessLogic.BusinessLogic @@ -111,6 +112,11 @@ namespace RenovationWorkBusinessLogic.BusinessLogic { Email = model.Email }); + if (!Regex.IsMatch(model.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$")) + { + throw new ArgumentException("Некорретно введенный email", nameof(model.Email)); + } + if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такой клиент уже существует"); diff --git a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/MessageInfoLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/MessageInfoLogic.cs new file mode 100644 index 0000000..b1d5fae --- /dev/null +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/MessageInfoLogic.cs @@ -0,0 +1,53 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkBusinessLogic.BusinessLogic +{ + 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/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs index f766b8d..c07729a 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs +++ b/RenovationWork/RenovationWorkBusinessLogic/BusinessLogic/OrderLogic.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using RenovationWorkBusinessLogic.MailWorker; using RenovationWorkContracts.BindingModels; using RenovationWorkContracts.BusinessLogicsContracts; using RenovationWorkContracts.SeatchModels; @@ -17,32 +18,34 @@ namespace RenovationWorkBusinessLogic.BusinessLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly AbstractMailWorker _mailWorker; + private readonly IClientLogic _clientLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic) { _logger = logger; _orderStorage = orderStorage; + _mailWorker = mailWorker; + _clientLogic = clientLogic; } public bool CreateOrder(OrderBindingModel model) { CheckModel(model); - if (model.Status != OrderStatus.Неизвестен) { _logger.LogWarning("Insert operation failed. Order status incorrect."); return false; } - model.Status = OrderStatus.Принят; - - if (_orderStorage.Insert(model) == null) + var result = _orderStorage.Insert(model); + if (result == null) { model.Status = OrderStatus.Неизвестен; _logger.LogWarning("Insert operation failed"); return false; } - + SendOrderMessage(result.ClientId, $"Заказ №{result.Id}", $"Заказ №{result.Id} от {result.DateCreate} на сумму {result.Sum:0.00} принят"); return true; } @@ -74,12 +77,14 @@ namespace RenovationWorkBusinessLogic.BusinessLogic model.DateImplement = viewModel.DateImplement; } CheckModel(model); + var result = _orderStorage.Update(model); if (_orderStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } + SendOrderMessage(result.ClientId, $"Заказ №{result.Id}", $"Заказ №{model.Id} изменен статус на {result.Status}"); return true; } public bool TakeOrderInWork(OrderBindingModel model) @@ -149,6 +154,21 @@ namespace RenovationWorkBusinessLogic.BusinessLogic _logger.LogInformation("ReadElement. OrderId:{Id}", order.Id); return order; } + private bool SendOrderMessage(int clientId, string subject, string text) + { + var client = _clientLogic.ReadElement(new() { Id = clientId }); + if (client == null) + { + return false; + } + _mailWorker.MailSendAsync(new() + { + MailAddress = client.Email, + Subject = subject, + Text = text + }); + return true; + } } } diff --git a/RenovationWork/RenovationWorkBusinessLogic/MailWorker/AbstractMailWorker.cs b/RenovationWork/RenovationWorkBusinessLogic/MailWorker/AbstractMailWorker.cs new file mode 100644 index 0000000..f6da45e --- /dev/null +++ b/RenovationWork/RenovationWorkBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -0,0 +1,97 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkBusinessLogic.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/RenovationWork/RenovationWorkBusinessLogic/MailWorker/MailKitWorker.cs b/RenovationWork/RenovationWorkBusinessLogic/MailWorker/MailKitWorker.cs new file mode 100644 index 0000000..298b74c --- /dev/null +++ b/RenovationWork/RenovationWorkBusinessLogic/MailWorker/MailKitWorker.cs @@ -0,0 +1,84 @@ +using Microsoft.Extensions.Logging; +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.BusinessLogicsContracts; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Security.Authentication; +using System.Text; +using System.Threading.Tasks; +using MailKit.Net.Pop3; +using MailKit.Security; +using AuthenticationException = MailKit.Security.AuthenticationException; + +namespace RenovationWorkBusinessLogic.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/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj b/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj index 83ba93c..6c4aabe 100644 --- a/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj +++ b/RenovationWork/RenovationWorkBusinessLogic/RenovationWorkBusinessLogic.csproj @@ -8,6 +8,7 @@ + diff --git a/RenovationWork/RenovationWorkClientApp/Controllers/HomeController.cs b/RenovationWork/RenovationWorkClientApp/Controllers/HomeController.cs index aace84d..891ab66 100644 --- a/RenovationWork/RenovationWorkClientApp/Controllers/HomeController.cs +++ b/RenovationWork/RenovationWorkClientApp/Controllers/HomeController.cs @@ -143,6 +143,16 @@ namespace RenovationWorkClientApp.Controllers var prod = APIClient.GetRequest($"api/main/getrepair?repairId={repair}"); 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/RenovationWork/RenovationWorkClientApp/Views/Home/Create.cshtml b/RenovationWork/RenovationWorkClientApp/Views/Home/Create.cshtml index 20736a2..51665d9 100644 --- a/RenovationWork/RenovationWorkClientApp/Views/Home/Create.cshtml +++ b/RenovationWork/RenovationWorkClientApp/Views/Home/Create.cshtml @@ -8,7 +8,7 @@
Изделие:
- +
@@ -25,26 +25,28 @@
- + function check() { + var count = $('#count').val(); + var repair = $('#repair').val(); + if (count && repair) { + $.ajax({ + method: "POST", + url: "/Home/Calc", + data: { count: count, repair: repair }, + success: function (result) { + $("#sum").val(result); + } + }); + }; + } + +} diff --git a/RenovationWork/RenovationWorkClientApp/Views/Shared/Mails.cshtml b/RenovationWork/RenovationWorkClientApp/Views/Shared/Mails.cshtml new file mode 100644 index 0000000..11c1058 --- /dev/null +++ b/RenovationWork/RenovationWorkClientApp/Views/Shared/Mails.cshtml @@ -0,0 +1,54 @@ +@using RenovationWorkContracts.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/RenovationWork/RenovationWorkClientApp/Views/Shared/_Layout.cshtml b/RenovationWork/RenovationWorkClientApp/Views/Shared/_Layout.cshtml index 8df796b..b61c848 100644 --- a/RenovationWork/RenovationWorkClientApp/Views/Shared/_Layout.cshtml +++ b/RenovationWork/RenovationWorkClientApp/Views/Shared/_Layout.cshtml @@ -25,6 +25,9 @@ + diff --git a/RenovationWork/RenovationWorkContracts/BindingModels/MailConfigBindingModel.cs b/RenovationWork/RenovationWorkContracts/BindingModels/MailConfigBindingModel.cs new file mode 100644 index 0000000..4ebe829 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BindingModels/MailConfigBindingModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.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/RenovationWork/RenovationWorkContracts/BindingModels/MailSendInfoBindingModel.cs b/RenovationWork/RenovationWorkContracts/BindingModels/MailSendInfoBindingModel.cs new file mode 100644 index 0000000..ad991b9 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BindingModels/MailSendInfoBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.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/RenovationWork/RenovationWorkContracts/BindingModels/MessageInfoBindingModel.cs b/RenovationWork/RenovationWorkContracts/BindingModels/MessageInfoBindingModel.cs new file mode 100644 index 0000000..fac13a2 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BindingModels/MessageInfoBindingModel.cs @@ -0,0 +1,19 @@ +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.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; + } +} diff --git a/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IMessageInfoLogic.cs b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IMessageInfoLogic.cs new file mode 100644 index 0000000..3678c17 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/BusinessLogicsContracts/IMessageInfoLogic.cs @@ -0,0 +1,17 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.BusinessLogicsContracts +{ + public interface IMessageInfoLogic + { + List? ReadList(MessageInfoSearchModel? model); + bool Create(MessageInfoBindingModel model); + } +} diff --git a/RenovationWork/RenovationWorkContracts/SeatchModels/MessageInfoSearchModel.cs b/RenovationWork/RenovationWorkContracts/SeatchModels/MessageInfoSearchModel.cs new file mode 100644 index 0000000..0dbfc20 --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/SeatchModels/MessageInfoSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.SeatchModels +{ + public class MessageInfoSearchModel + { + public int? ClientId { get; set; } + public string? MessageId { get; set; } + } +} diff --git a/RenovationWork/RenovationWorkContracts/StorageContracts/IMessageInfoStorage.cs b/RenovationWork/RenovationWorkContracts/StorageContracts/IMessageInfoStorage.cs new file mode 100644 index 0000000..ba63c9d --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/StorageContracts/IMessageInfoStorage.cs @@ -0,0 +1,19 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.StorageContracts +{ + public interface IMessageInfoStorage + { + List GetFullList(); + List GetFilteredList(MessageInfoSearchModel model); + MessageInfoViewModel? GetElement(MessageInfoSearchModel model); + MessageInfoViewModel? Insert(MessageInfoBindingModel model); + } +} diff --git a/RenovationWork/RenovationWorkContracts/ViewModels/MessageInfoModel.cs b/RenovationWork/RenovationWorkContracts/ViewModels/MessageInfoModel.cs new file mode 100644 index 0000000..61c415e --- /dev/null +++ b/RenovationWork/RenovationWorkContracts/ViewModels/MessageInfoModel.cs @@ -0,0 +1,25 @@ +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkContracts.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/RenovationWork/RenovationWorkDataModels/Models/IMessageInfoModel.cs b/RenovationWork/RenovationWorkDataModels/Models/IMessageInfoModel.cs new file mode 100644 index 0000000..bea1c89 --- /dev/null +++ b/RenovationWork/RenovationWorkDataModels/Models/IMessageInfoModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDataModels.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/RenovationWork/RenovationWorkDatabaseImplement/Implements/MessageInfoStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..f2a1eaf --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,58 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDatabaseImplement.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + using var context = new RenovationWorkDatabase(); + + if (model.MessageId != null) + { + return context.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + using var context = new RenovationWorkDatabase(); + return context.Messages + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new RenovationWorkDatabase(); + return context.Messages + .Select(x => x.GetViewModel) + .ToList(); + } + + public MessageInfoViewModel? Insert(MessageInfoBindingModel model) + { + using var context = new RenovationWorkDatabase(); + var newMessage = MessageInfo.Create(model); + if (newMessage == null || context.Messages.Any(x => x.MessageId.Equals(model.MessageId))) + { + return null; + } + context.Messages.Add(newMessage); + context.SaveChanges(); + return newMessage.GetViewModel; + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs index a2033f2..62a41e0 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Implements/OrderStorage.cs @@ -53,16 +53,51 @@ namespace RenovationWorkDatabaseImplement.Implements public List GetFilteredList(OrderSearchModel model) { using var context = new RenovationWorkDatabase(); + if (model.Id.HasValue) + { + return context.Orders + .Include(x => x.Repair) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.DateFrom != null && model.DateTo != null) + { + return context.Orders + .Include(x => x.Repair) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ClientId.HasValue) + { + return context.Orders + .Include(x => x.Repair) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.ImplementerId.HasValue) + { + return context.Orders + .Include(x => x.Repair) + .Include(x => x.Client) + .Include(x => x.Implementer) + .Where(x => x.ImplementerId == model.ImplementerId) + .Select(x => x.GetViewModel) + .ToList(); + } return context.Orders .Include(x => x.Repair) .Include(x => x.Client) .Include(x => x.Implementer) - .Where(x => ( - (!model.Id.HasValue || x.Id == model.Id) && - (!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) && - (!model.DateTo.HasValue || x.DateCreate <= model.DateTo) - ) - ) + .Where(x => model.Status == x.Status) .Select(x => x.GetViewModel) .ToList(); diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.Designer.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.Designer.cs new file mode 100644 index 0000000..0fa3918 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.Designer.cs @@ -0,0 +1,283 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RenovationWorkDatabaseImplement; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + [DbContext(typeof(RenovationWorkDatabase))] + [Migration("20230503184114_Third")] + partial class Third + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("text"); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.Property("Qualification") + .HasColumnType("integer"); + + b.Property("WorkExperience") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("text"); + + b.Property("Body") + .IsRequired() + .HasColumnType("text"); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("DateDelivery") + .HasColumnType("timestamp with time zone"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("MessageId"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("ImplementerId") + .HasColumnType("integer"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ImplementerId"); + + b.HasIndex("RepairId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Repair", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("double precision"); + + b.Property("RepairName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Repairs"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.RepairComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("RepairId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("RepairId"); + + b.ToTable("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Order", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Model.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Model.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + + b.HasOne("RenovationWorkDatabaseImplement.Model.Repair", "Repair") + .WithMany() + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Implementer"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.RepairComponent", b => + { + b.HasOne("RenovationWorkDatabaseImplement.Model.Component", "Component") + .WithMany("RepairComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("RenovationWorkDatabaseImplement.Model.Repair", "Repair") + .WithMany("Components") + .HasForeignKey("RepairId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Repair"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Component", b => + { + b.Navigation("RepairComponents"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Implementer", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Repair", b => + { + b.Navigation("Components"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.cs new file mode 100644 index 0000000..aca76d8 --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/20230503184114_Third.cs @@ -0,0 +1,38 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace RenovationWorkDatabaseImplement.Migrations +{ + /// + public partial class Third : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + MessageId = table.Column(type: "text", nullable: false), + ClientId = table.Column(type: "integer", nullable: true), + SenderName = table.Column(type: "text", nullable: false), + DateDelivery = table.Column(type: "timestamp with time zone", nullable: false), + Subject = table.Column(type: "text", nullable: false), + Body = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.MessageId); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Messages"); + } + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs index 14a3ebf..23f8520 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/Migrations/RenovationWorkDatabaseModelSnapshot.cs @@ -94,6 +94,34 @@ namespace RenovationWorkDatabaseImplement.Migrations b.ToTable("Implementers"); }); + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.MessageInfo", b => + { + b.Property("MessageId") + .HasColumnType("text"); + + b.Property("Body") + .IsRequired() + .HasColumnType("text"); + + b.Property("ClientId") + .HasColumnType("integer"); + + b.Property("DateDelivery") + .HasColumnType("timestamp with time zone"); + + b.Property("SenderName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Subject") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("MessageId"); + + b.ToTable("Messages"); + }); + modelBuilder.Entity("RenovationWorkDatabaseImplement.Model.Order", b => { b.Property("Id") diff --git a/RenovationWork/RenovationWorkDatabaseImplement/Model/MessageInfo.cs b/RenovationWork/RenovationWorkDatabaseImplement/Model/MessageInfo.cs new file mode 100644 index 0000000..e5f41ca --- /dev/null +++ b/RenovationWork/RenovationWorkDatabaseImplement/Model/MessageInfo.cs @@ -0,0 +1,53 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkDatabaseImplement.Model +{ + public class MessageInfo : IMessageInfoModel + { + [Key] + public string MessageId { get; set; } = string.Empty; + public int? ClientId { get; set; } + [Required] + public string SenderName { get; set; } = string.Empty; + [Required] + public DateTime DateDelivery { get; set; } + [Required] + public string Subject { get; set; } = string.Empty; + [Required] + public string Body { get; set; } = string.Empty; + public static MessageInfo? Create(MessageInfoBindingModel model) + { + if (model == null) + { + return null; + } + + return new() + { + MessageId = model.MessageId, + SenderName = model.SenderName, + ClientId = model.ClientId, + DateDelivery = DateTime.SpecifyKind(model.DateDelivery, DateTimeKind.Utc), + Subject = model.Subject, + Body = model.Body + }; + } + + public MessageInfoViewModel GetViewModel => new() + { + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + Subject = Subject, + Body = Body + }; + } +} diff --git a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs index 9b7b543..c9523e0 100644 --- a/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs +++ b/RenovationWork/RenovationWorkDatabaseImplement/RenovationWorkDatabase.cs @@ -26,7 +26,7 @@ namespace RenovationWorkDatabaseImplement } public virtual DbSet Components { set; get; } - + public virtual DbSet Messages { set; get; } public virtual DbSet Repairs { set; get; } public virtual DbSet Implementers { set; get; } public virtual DbSet RepairComponents { set; get; } diff --git a/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs b/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs index 4a642b0..5d78951 100644 --- a/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs +++ b/RenovationWork/RenovationWorkFileImplement/DataFileSingleton.cs @@ -21,11 +21,14 @@ namespace RenovationWorkFileImplement private readonly string RepairFileName = "Repair.xml"; private readonly string ClientFileName = "Client.xml"; private readonly string ImplementerFileName = "Implementer.xml"; + private readonly string MessageInfoFileName = "MessageInfo.xml"; + public List Clients { get; private set; } public List Components { get; private set; } public List Implementers { get; private set; } public List Orders { get; private set; } + public List Messages { get; private set; } public List Repairs { get; private set; } @@ -45,6 +48,7 @@ namespace RenovationWorkFileImplement public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, OrderFileName, "Clients", x => x.GetXElement); public void SaveImplementers() => SaveData(Implementers, ImplementerFileName, "Implementers", x => x.GetXElement); + public void SaveMessages() => SaveData(Orders, MessageInfoFileName, "Messages", x => x.GetXElement); private DataFileSingleton() @@ -54,6 +58,8 @@ namespace RenovationWorkFileImplement 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)!)!; + } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/RenovationWork/RenovationWorkFileImplement/Implements/MessageInfoStorage.cs b/RenovationWork/RenovationWorkFileImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..78e91e4 --- /dev/null +++ b/RenovationWork/RenovationWorkFileImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,59 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkFileImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataFileSingleton _source; + public MessageInfoStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + if (model.MessageId != null) + { + return _source.Messages.FirstOrDefault(x => x.MessageId == model.MessageId)?.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + 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/RenovationWork/RenovationWorkFileImplement/Models/MessageInfo.cs b/RenovationWork/RenovationWorkFileImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..3850c25 --- /dev/null +++ b/RenovationWork/RenovationWorkFileImplement/Models/MessageInfo.cs @@ -0,0 +1,81 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace RenovationWorkFileImplement.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/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs b/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs index 01d7069..905f584 100644 --- a/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs +++ b/RenovationWork/RenovationWorkListImplement/DataListSingleton.cs @@ -15,6 +15,7 @@ namespace RenovationWorkListImplement public List Orders { get; set; } public List Repair { get; set; } public List Clients { get; set; } + public List Messages { get; set; } public List Implementers { get; set; } private DataListSingleton() { @@ -22,6 +23,7 @@ namespace RenovationWorkListImplement Orders = new List(); Repair = new List(); Clients = new List(); + Messages = new List(); Implementers = new List(); } public static DataListSingleton GetInstance() diff --git a/RenovationWork/RenovationWorkListImplement/Implements/MessageInfoStorage.cs b/RenovationWork/RenovationWorkListImplement/Implements/MessageInfoStorage.cs new file mode 100644 index 0000000..3785ca3 --- /dev/null +++ b/RenovationWork/RenovationWorkListImplement/Implements/MessageInfoStorage.cs @@ -0,0 +1,67 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.SeatchModels; +using RenovationWorkContracts.StorageContracts; +using RenovationWorkContracts.ViewModels; +using RenovationWorkListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkListImplement.Implements +{ + public class MessageInfoStorage : IMessageInfoStorage + { + private readonly DataListSingleton _source; + public MessageInfoStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public MessageInfoViewModel? GetElement(MessageInfoSearchModel model) + { + foreach (var message in _source.Messages) + { + if (model.MessageId != null && model.MessageId.Equals(message.MessageId)) + return message.GetViewModel; + } + return null; + } + + public List GetFilteredList(MessageInfoSearchModel model) + { + List result = new(); + foreach (var item in _source.Messages) + { + if (item.ClientId.HasValue && item.ClientId == model.ClientId) + { + result.Add(item.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + List result = new(); + foreach (var item in _source.Messages) + { + result.Add(item.GetViewModel); + } + return result; + } + + 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/RenovationWork/RenovationWorkListImplement/Models/MessageInfo.cs b/RenovationWork/RenovationWorkListImplement/Models/MessageInfo.cs new file mode 100644 index 0000000..89b5c61 --- /dev/null +++ b/RenovationWork/RenovationWorkListImplement/Models/MessageInfo.cs @@ -0,0 +1,46 @@ +using RenovationWorkContracts.BindingModels; +using RenovationWorkContracts.ViewModels; +using RenovationWorkDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RenovationWorkListImplement.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 MessageInfoViewModel GetViewModel => new() + { + Body = Body, + Subject = Subject, + ClientId = ClientId, + MessageId = MessageId, + SenderName = SenderName, + DateDelivery = DateDelivery, + }; + } +} diff --git a/RenovationWork/RenovationWorkRestApi/Controllers/ClientController.cs b/RenovationWork/RenovationWorkRestApi/Controllers/ClientController.cs index 24fe4a8..a2c49bb 100644 --- a/RenovationWork/RenovationWorkRestApi/Controllers/ClientController.cs +++ b/RenovationWork/RenovationWorkRestApi/Controllers/ClientController.cs @@ -14,10 +14,14 @@ namespace RenovationWorkRestApi.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 +69,22 @@ namespace RenovationWorkRestApi.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/RenovationWork/RenovationWorkRestApi/Program.cs b/RenovationWork/RenovationWorkRestApi/Program.cs index 314aaac..73b8b1c 100644 --- a/RenovationWork/RenovationWorkRestApi/Program.cs +++ b/RenovationWork/RenovationWorkRestApi/Program.cs @@ -1,5 +1,7 @@ using Microsoft.OpenApi.Models; using RenovationWorkBusinessLogic.BusinessLogic; +using RenovationWorkBusinessLogic.MailWorker; +using RenovationWorkContracts.BindingModels; using RenovationWorkContracts.BusinessLogicsContracts; using RenovationWorkContracts.StorageContracts; using RenovationWorkDatabaseImplement.Implements; @@ -12,11 +14,17 @@ builder.Logging.AddLog4Net("log4net.config"); // Add services to the container. 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 @@ -28,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/RenovationWork/RenovationWorkRestApi/appsettings.json b/RenovationWork/RenovationWorkRestApi/appsettings.json index 10f68b8..866b319 100644 --- a/RenovationWork/RenovationWorkRestApi/appsettings.json +++ b/RenovationWork/RenovationWorkRestApi/appsettings.json @@ -5,5 +5,12 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + + "SmtpClientHost": "smtp.gmail.com", + "SmtpClientPort": "587", + "PopHost": "pop.gmail.com", + "PopPort": "995", + "MailLogin": "roma.ismailov96@gmail.com", + "MailPassword": "lopl zigv bpms xvre" } diff --git a/RenovationWork/RenovationWorkView/App.config b/RenovationWork/RenovationWorkView/App.config new file mode 100644 index 0000000..db4e4b5 --- /dev/null +++ b/RenovationWork/RenovationWorkView/App.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormMain.Designer.cs b/RenovationWork/RenovationWorkView/FormMain.Designer.cs index ba1b856..a3bf022 100644 --- a/RenovationWork/RenovationWorkView/FormMain.Designer.cs +++ b/RenovationWork/RenovationWorkView/FormMain.Designer.cs @@ -45,6 +45,7 @@ this.OrderReadyButton = new System.Windows.Forms.Button(); this.IssuedOrderButton = new System.Windows.Forms.Button(); this.UpdateListButton = new System.Windows.Forms.Button(); + this.письмаToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.MenuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); this.SuspendLayout(); @@ -55,7 +56,8 @@ this.MenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.СправочникиToolStripMenuItem, this.отчетыToolStripMenuItem, - this.запускРаботыToolStripMenuItem}); + this.запускРаботыToolStripMenuItem, + this.письмаToolStripMenuItem}); this.MenuStrip.Location = new System.Drawing.Point(0, 0); this.MenuStrip.Name = "MenuStrip"; this.MenuStrip.Padding = new System.Windows.Forms.Padding(7, 3, 0, 3); @@ -217,6 +219,11 @@ this.UpdateListButton.UseVisualStyleBackColor = true; this.UpdateListButton.Click += new System.EventHandler(this.UpdateListButton_Click); // + // письмаToolStripMenuItem + // + this.письмаToolStripMenuItem.Name = "письмаToolStripMenuItem"; + this.письмаToolStripMenuItem.Size = new System.Drawing.Size(77, 24); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); @@ -261,5 +268,6 @@ private ToolStripMenuItem КлиентыToolStripMenuItem; private ToolStripMenuItem исполнителиToolStripMenuItem; private ToolStripMenuItem запускРаботыToolStripMenuItem; + private ToolStripMenuItem письмаToolStripMenuItem; } } \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/FormMain.cs b/RenovationWork/RenovationWorkView/FormMain.cs index abe4107..15b6f2f 100644 --- a/RenovationWork/RenovationWorkView/FormMain.cs +++ b/RenovationWork/RenovationWorkView/FormMain.cs @@ -256,5 +256,6 @@ namespace PrecastConcretePlantView _workProcess.DoWork((Program.ServiceProvider?.GetService(typeof(IImplementerLogic)) as IImplementerLogic)!, _orderLogic); MessageBox.Show("Процесс обработки запущен", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information); } + } } diff --git a/RenovationWork/RenovationWorkView/Program.cs b/RenovationWork/RenovationWorkView/Program.cs index f1f8fc5..c006752 100644 --- a/RenovationWork/RenovationWorkView/Program.cs +++ b/RenovationWork/RenovationWorkView/Program.cs @@ -9,6 +9,8 @@ using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; using RenovationWorkBusinessLogic.OfficePackage.Implements; using RenovationWorkBusinessLogic.OfficePackage; +using RenovationWorkBusinessLogic.MailWorker; +using RenovationWorkContracts.BindingModels; namespace RenovationWorkView { @@ -24,8 +26,33 @@ namespace RenovationWorkView 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(_serviceProvider.GetRequiredService()); } + + private static void ConfigureServices(ServiceCollection services) { services.AddLogging(option => @@ -39,6 +66,8 @@ namespace RenovationWorkView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); @@ -47,6 +76,10 @@ namespace RenovationWorkView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + + services.AddTransient(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); @@ -65,5 +98,6 @@ namespace RenovationWorkView services.AddTransient(); services.AddTransient(); } + private static void MailCheck(object obj) => ServiceProvider?.GetService()?.MailCheck(); } } \ No newline at end of file diff --git a/RenovationWork/RenovationWorkView/RenovationWorkView.csproj b/RenovationWork/RenovationWorkView/RenovationWorkView.csproj index 4ab67d5..791fa02 100644 --- a/RenovationWork/RenovationWorkView/RenovationWorkView.csproj +++ b/RenovationWork/RenovationWorkView/RenovationWorkView.csproj @@ -38,6 +38,9 @@ + + Always + Always