DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/Email/EmailNotificationTransportService.cs

49 lines
1.5 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services.Notifications;
using AsbCloudInfrastructure.Background;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services.Email
{
public class EmailNotificationTransportService : INotificationTransportService
{
private readonly IConfiguration configuration;
private readonly BackgroundWorker backgroundWorker;
public EmailNotificationTransportService(BackgroundWorker backgroundWorker,
IConfiguration configuration)
{
this.configuration = configuration;
this.backgroundWorker = backgroundWorker;
}
public int IdTransportType => 1;
public Task SendAsync(NotificationDto notification, CancellationToken cancellationToken)
{
var work = new WorkToSendEmail(notification, configuration);
if (!backgroundWorker.Works.Any(w => w.Id == work.Id))
{
backgroundWorker.Enqueue(work);
}
return Task.CompletedTask;
}
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications, CancellationToken cancellationToken)
{
var tasks = notifications
.Select(notification => SendAsync(notification, cancellationToken));
return Task.WhenAll(tasks);
}
}
}