forked from ddrilling/AsbCloudServer
Рефакторинг
This commit is contained in:
parent
371ff37d2e
commit
9671f15523
@ -1,39 +1,51 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Exceptions;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Background
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для отправки email
|
||||
/// </summary>
|
||||
internal class WorkToSendEmail : Work
|
||||
{
|
||||
private NotificationDto notification;
|
||||
private string sender;
|
||||
private string smtpPassword;
|
||||
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.sender = sender;
|
||||
this.smtpPassword = smtpPassword;
|
||||
this.smtpServer = smtpServer;
|
||||
sender = configuration.GetValue("email:sender", string.Empty);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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 userRepository = services.GetRequiredService<IUserRepository>();
|
||||
|
||||
|
@ -1,19 +1,11 @@
|
||||
using System;
|
||||
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.Data;
|
||||
using AsbCloudApp.Services.Notifications;
|
||||
using AsbCloudInfrastructure.Background;
|
||||
using AsbCloudInfrastructure.Background.PeriodicWorks;
|
||||
using DocumentFormat.OpenXml.Presentation;
|
||||
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
|
||||
{
|
||||
@ -21,45 +13,24 @@ namespace AsbCloudInfrastructure.Services.Email
|
||||
public class EmailNotificationTransportService : INotificationTransportService
|
||||
{
|
||||
private readonly BackgroundWorker backgroundWorker;
|
||||
private readonly bool IsConfigured;
|
||||
private readonly string sender;
|
||||
private readonly string smtpServer;
|
||||
private readonly string smtpPassword;
|
||||
private IConfiguration configuration;
|
||||
|
||||
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
|
||||
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
sender = configuration.GetValue("email:sender", string.Empty);
|
||||
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.configuration = configuration;
|
||||
this.backgroundWorker = backgroundWorker;
|
||||
}
|
||||
|
||||
public int IdTransportType => 1;
|
||||
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
@ -72,7 +43,7 @@ namespace AsbCloudInfrastructure.Services.Email
|
||||
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
|
||||
private static string MakeWorkId(int idUser, string subject, string content)
|
||||
{
|
||||
var hash = idUser.GetHashCode();
|
||||
|
Loading…
Reference in New Issue
Block a user