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); } }