From 593ecd9c08cc29f53a5b4262d0ec59b2887360d0 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Fri, 21 Apr 2023 00:14:44 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BB=207-=D0=B9=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=BE=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ClientController.cs | 1 + .../BusinessLogic/MessageInfoLogic.cs | 8 ++++---- .../MailWorker/AbstractMailWorker.cs | 11 +++++++---- .../MailWorker/MailKitWorker.cs | 4 +++- .../Views/Home/Enter.cshtml | 2 +- .../SearchModels/MessageInfoSearchModel.cs | 4 ---- .../ViewModels/MessageInfoViewModel.cs | 1 - .../Implements/MessageInfoStorage.cs | 13 ++----------- .../Models/Client.cs | 3 +++ .../Models/MessageInfo.cs | 3 ++- 10 files changed, 23 insertions(+), 27 deletions(-) diff --git a/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs b/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs index ed8ad14..c088727 100644 --- a/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs +++ b/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs @@ -74,6 +74,7 @@ namespace BlacksmithWorkshopRestApi.Controllers } } + [HttpGet] public List? GetMessages(int clientId) { try diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/MessageInfoLogic.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/MessageInfoLogic.cs index 4ed90c1..c2630f9 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/MessageInfoLogic.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/BusinessLogic/MessageInfoLogic.cs @@ -26,7 +26,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic public List? ReadList(MessageInfoSearchModel? model) { - _logger.LogInformation("ReadList. SenderName:{SenderName}. MessageId:{MessageId}", model?.SenderName, model?.MessageId); + _logger.LogInformation("ReadList. MessageId:{MessageId}", model?.MessageId); //list хранит весь список в случае, если model пришло со значением null на вход метода var list = model == null ? _messageInfoStorage.GetFullList() : _messageInfoStorage.GetFilteredList(model); @@ -44,7 +44,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic public bool Create(MessageInfoBindingModel model) { - CheckModel(model); + //CheckModel(model); if (_messageInfoStorage.Insert(model) == null) { @@ -57,7 +57,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic } //проверка входного аргумента для методов Insert, Update и Delete - private void CheckModel(MessageInfoBindingModel model, bool withParams = true) + /*private void CheckModel(MessageInfoBindingModel model, bool withParams = true) { if (model == null) { @@ -109,6 +109,6 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogic { throw new InvalidOperationException("Аккаунт с таким логином уже есть"); } - } + }*/ } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs index 18505c3..e284113 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/AbstractMailWorker.cs @@ -25,12 +25,15 @@ namespace BlacksmithWorkshopBusinessLogic.MailWorker private readonly IMessageInfoLogic _messageInfoLogic; + private readonly IClientLogic _clientLogic; + private readonly ILogger _logger; - public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) + public AbstractMailWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) { _logger = logger; _messageInfoLogic = messageInfoLogic; + _clientLogic = clientLogic; } public void MailConfig(MailConfigBindingModel config) @@ -91,14 +94,14 @@ namespace BlacksmithWorkshopBusinessLogic.MailWorker foreach (var mail in list) { + mail.ClientId = _clientLogic.ReadElement(new() { Email = mail.SenderName })?.Id; + _messageInfoLogic.Create(mail); } } protected abstract Task SendMailAsync(MailSendInfoBindingModel info); - protected abstract Task> - - ReceiveMailAsync(); + protected abstract Task> ReceiveMailAsync(); } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs index 884c8e8..4a75dad 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopBusinessLogic/MailWorker/MailKitWorker.cs @@ -15,7 +15,8 @@ namespace BlacksmithWorkshopBusinessLogic.MailWorker { public class MailKitWorker : AbstractMailWorker { - public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic) : base(logger, messageInfoLogic) { } + public MailKitWorker(ILogger logger, IMessageInfoLogic messageInfoLogic, IClientLogic clientLogic) + : base(logger, messageInfoLogic, clientLogic) { } protected override async Task SendMailAsync(MailSendInfoBindingModel info) { @@ -31,6 +32,7 @@ namespace BlacksmithWorkshopBusinessLogic.MailWorker objMailMessage.Body = info.Text; objMailMessage.SubjectEncoding = Encoding.UTF8; objMailMessage.BodyEncoding = Encoding.UTF8; + objSmtpClient.UseDefaultCredentials = false; objSmtpClient.EnableSsl = true; objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Enter.cshtml b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Enter.cshtml index affc9d4..5618938 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Enter.cshtml +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Enter.cshtml @@ -9,7 +9,7 @@
Логин:
-
diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs index 70dac9b..47c87aa 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs @@ -11,9 +11,5 @@ namespace BlacksmithWorkshopContracts.SearchModels public string? MessageId { get; set; } public int? ClientId { get; set; } - - public string? SenderName { get; set; } - - public DateTime? DateDelivery { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs index 6e62d95..5933def 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/ViewModels/MessageInfoViewModel.cs @@ -20,7 +20,6 @@ namespace BlacksmithWorkshopContracts.ViewModels [DisplayName("Дата отправки")] public DateTime DateDelivery { get; set; } = DateTime.Now; - [DisplayName("Заголовок")] public string Subject { get; set; } = string.Empty; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs index 718c19b..684884f 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -32,15 +32,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { using var context = new BlacksmithWorkshopDatabase(); - if (!model.ClientId.HasValue) - { - return context.Messages - .Select(x => x.GetViewModel) - .ToList(); - } - return context.Messages - .Where(x => x.ClientId == model.ClientId) + .Where(x => x.ClientId.HasValue && x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); } @@ -68,9 +61,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements context.Messages.Add(newMessage); context.SaveChanges(); - return context.Messages - .FirstOrDefault(x => x.MessageId == newMessage.MessageId) - ?.GetViewModel; + return newMessage.GetViewModel; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs index 118cf94..a2120cb 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Client.cs @@ -28,6 +28,9 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [ForeignKey("ClientId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List MessageInfos { get; set; } = new(); + public static Client? Create(ClientBindingModel model) { if (model == null) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs index 9b8f245..780cba1 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/MessageInfo.cs @@ -15,7 +15,6 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [Key] public string MessageId { get; private set; } = string.Empty; - [Required] public int? ClientId { get; private set; } [Required] @@ -30,6 +29,8 @@ namespace BlacksmithWorkshopDatabaseImplement.Models [Required] public string Body { get; private set; } = string.Empty; + public virtual Client? Client { get; set; } + public static MessageInfo? Create(MessageInfoBindingModel model) { if (model == null)