Рефакторинг

This commit is contained in:
Olga Nemt 2023-12-21 12:31:25 +05:00
parent 371ff37d2e
commit 9671f15523
2 changed files with 32 additions and 49 deletions

View File

@ -1,39 +1,51 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Exceptions; using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories; using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Net.Mail; using System.Net.Mail;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Background namespace AsbCloudInfrastructure.Background
{ {
/// <summary>
/// Класс для отправки email
/// </summary>
internal class WorkToSendEmail : Work internal class WorkToSendEmail : Work
{ {
private NotificationDto notification; private NotificationDto notification;
private string sender; private string sender;
private string smtpPassword; private string smtpPassword;
private string smtpServer; private string smtpServer;
private bool IsConfigured;
public WorkToSendEmail(string workId, NotificationDto notification, string sender, string smtpPassword, string smtpServer) : base(workId) public WorkToSendEmail(string workId, NotificationDto notification, IConfiguration configuration) : base(workId)
{ {
this.notification = notification; this.notification = notification;
this.sender = sender; sender = configuration.GetValue("email:sender", string.Empty);
this.smtpPassword = smtpPassword; smtpPassword = configuration.GetValue("email:password", string.Empty);
this.smtpServer = smtpServer; smtpServer = configuration.GetValue("email:smtpServer", string.Empty);
var configError = string.IsNullOrEmpty(sender) ||
string.IsNullOrEmpty(smtpPassword) ||
string.IsNullOrEmpty(smtpServer);
IsConfigured = !configError;
} }
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token) protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
{ {
if (!IsConfigured)
{
Trace.TraceWarning("smtp is not configured");
return;
}
var notificationRepository = services.GetRequiredService<INotificationRepository>(); var notificationRepository = services.GetRequiredService<INotificationRepository>();
var userRepository = services.GetRequiredService<IUserRepository>(); var userRepository = services.GetRequiredService<IUserRepository>();

View File

@ -1,19 +1,11 @@
using System; using AsbCloudApp.Data;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services.Notifications; using AsbCloudApp.Services.Notifications;
using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Background;
using AsbCloudInfrastructure.Background.PeriodicWorks;
using DocumentFormat.OpenXml.Presentation;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.Email namespace AsbCloudInfrastructure.Services.Email
{ {
@ -21,45 +13,24 @@ namespace AsbCloudInfrastructure.Services.Email
public class EmailNotificationTransportService : INotificationTransportService public class EmailNotificationTransportService : INotificationTransportService
{ {
private readonly BackgroundWorker backgroundWorker; private readonly BackgroundWorker backgroundWorker;
private readonly bool IsConfigured;
private readonly string sender;
private readonly string smtpServer;
private readonly string smtpPassword;
private IConfiguration configuration; private IConfiguration configuration;
public EmailNotificationTransportService(BackgroundWorker backgroundWorker, public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
IConfiguration configuration) IConfiguration configuration)
{ {
sender = configuration.GetValue("email:sender", string.Empty); this.configuration = configuration;
smtpPassword = configuration.GetValue("email:password", string.Empty);
smtpServer = configuration.GetValue("email:smtpServer", string.Empty);
var configError = string.IsNullOrEmpty(sender) ||
string.IsNullOrEmpty(smtpPassword) ||
string.IsNullOrEmpty(smtpServer);
IsConfigured = !configError;
this.backgroundWorker = backgroundWorker; this.backgroundWorker = backgroundWorker;
} }
public int IdTransportType => 1; public int IdTransportType => 1;
public Task SendAsync(NotificationDto notification, CancellationToken cancellationToken) public Task SendAsync(NotificationDto notification, CancellationToken cancellationToken)
{ {
//if (!IsConfigured)
//{
// Trace.TraceWarning("smtp is not configured");
// return Task.CompletedTask;
//}
var workId = MakeWorkId(notification.IdUser, notification.Title, notification.Message); var workId = MakeWorkId(notification.IdUser, notification.Title, notification.Message);
if (!backgroundWorker.Works.Any(w=>w.Id==workId)) if (!backgroundWorker.Works.Any(w => w.Id == workId))
{ {
var work = new WorkToSendEmail(workId, notification, sender, smtpPassword, smtpServer); var work = new WorkToSendEmail(workId, notification, configuration);
backgroundWorker.Enqueue(work); backgroundWorker.Enqueue(work);
} }
return Task.CompletedTask; return Task.CompletedTask;
@ -72,7 +43,7 @@ namespace AsbCloudInfrastructure.Services.Email
return Task.WhenAll(tasks); return Task.WhenAll(tasks);
} }
private static string MakeWorkId(int idUser, string subject, string content) private static string MakeWorkId(int idUser, string subject, string content)
{ {
var hash = idUser.GetHashCode(); var hash = idUser.GetHashCode();