DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/NotificationService.cs
Степанов Дмитрий Александрович 96786b1be7 Сервисы для уведомлений
1. Добавил репозиторий для уведомлений
2. Добавил сервисы для уведомлений
2023-07-10 16:56:55 +05:00

71 lines
2.1 KiB
C#

using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services;
public class NotificationService : INotificationService
{
private readonly INotificationSendingQueueService notificationSendingQueueService;
private readonly INotificationRepository notificationRepository;
public NotificationService(INotificationSendingQueueService notificationSendingQueueService,
INotificationRepository notificationRepository)
{
this.notificationSendingQueueService = notificationSendingQueueService;
this.notificationRepository = notificationRepository;
}
public async Task SendNotificationAsync(int idUser,
int idNotificationTransport,
int idNotificationCategory,
string title,
string subject,
TimeSpan timeToLife,
CancellationToken cancellationToken)
{
NotificationDto notification = new()
{
IdUser = idUser,
IdNotificationTransport = idNotificationTransport,
IdNotificationCategory = idNotificationCategory,
Title = title,
Subject = subject,
TimeToLife = timeToLife
};
await notificationRepository.InsertAsync(notification,
cancellationToken);
notificationSendingQueueService.Enqueue(notification);
}
public async Task UpdateNotificationAsync(int idNotification,
bool isRead,
CancellationToken cancellationToken)
{
var notification = await notificationRepository.GetOrDefaultAsync(idNotification,
cancellationToken) ?? throw new ArgumentInvalidException("Уведомление не найдено",
nameof(idNotification));
notification.IsRead = isRead;
await notificationRepository.UpdateAsync(notification,
cancellationToken);
}
public async Task ResendNotificationAsync(int idUser,
int idNotificationTransport,
CancellationToken cancellationToken)
{
var notifications = await notificationRepository.GetUnsentNotificationsAsync(idUser,
idNotificationTransport,
cancellationToken);
notificationSendingQueueService.EnqueueRange(notifications);
}
}