forked from ddrilling/AsbCloudServer
Степанов Дмитрий
55ca21c31e
1. Удалил зависимость от AsbCloudDb в проекте AsbCloudApp 2. Убрал лишние константы 3. Сделал реализацию для IdState у NotificationDto 4. Удалил атрибут Range у IdTransportType. Проверка на существование сервиса транспорта находится в методе GetTransportService в NotificationService
118 lines
4.8 KiB
C#
118 lines
4.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using AsbCloudApp.Data;
|
||
using AsbCloudApp.Exceptions;
|
||
using AsbCloudApp.Repositories;
|
||
using AsbCloudApp.Requests;
|
||
|
||
namespace AsbCloudApp.Services.Notifications;
|
||
|
||
/// <summary>
|
||
/// Сервис для работы с уведомлениями
|
||
/// </summary>
|
||
public class NotificationService
|
||
{
|
||
private readonly ICrudRepository<NotificationCategoryDto> notificationCategoryRepository;
|
||
private readonly INotificationRepository notificationRepository;
|
||
private readonly IEnumerable<INotificationTransportService> notificationTransportServices;
|
||
|
||
/// <summary>
|
||
/// Сервис для работы с уведомлениями
|
||
/// </summary>
|
||
/// <param name="notificationCategoryRepository"></param>
|
||
/// <param name="notificationRepository"></param>
|
||
/// <param name="notificationTransportServices"></param>
|
||
public NotificationService(ICrudRepository<NotificationCategoryDto> notificationCategoryRepository,
|
||
INotificationRepository notificationRepository,
|
||
IEnumerable<INotificationTransportService> notificationTransportServices)
|
||
{
|
||
this.notificationCategoryRepository = notificationCategoryRepository;
|
||
this.notificationRepository = notificationRepository;
|
||
this.notificationTransportServices = notificationTransportServices;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Отправка нового уведомления
|
||
/// </summary>
|
||
/// <param name="request"></param>
|
||
/// <param name="cancellationToken"></param>
|
||
public async Task NotifyAsync(NotifyRequest request,
|
||
CancellationToken cancellationToken)
|
||
{
|
||
var notificationCategory = await notificationCategoryRepository.GetOrDefaultAsync(request.IdNotificationCategory, cancellationToken)
|
||
?? throw new ArgumentInvalidException("Категория уведомления не найдена", nameof(request.IdNotificationCategory));
|
||
|
||
var notification = new NotificationDto
|
||
{
|
||
IdUser = request.IdUser,
|
||
RegistrationDate = DateTime.UtcNow,
|
||
IdNotificationCategory = notificationCategory.Id,
|
||
Title = request.Title,
|
||
Message = request.Message,
|
||
IdTransportType = request.IdTransportType,
|
||
};
|
||
|
||
notification.Id = await notificationRepository.InsertAsync(notification, cancellationToken);
|
||
notification.NotificationCategory = notificationCategory;
|
||
|
||
var notificationTransportService = GetTransportService(request.IdTransportType);
|
||
|
||
await notificationTransportService.SendAsync(notification, cancellationToken);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Обновление уведомления
|
||
/// </summary>
|
||
/// <param name="idNotification"></param>
|
||
/// <param name="isRead"></param>
|
||
/// <param name="cancellationToken"></param>
|
||
/// <returns></returns>
|
||
public async Task UpdateAsync(int idNotification,
|
||
bool isRead,
|
||
CancellationToken cancellationToken)
|
||
{
|
||
var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken)
|
||
?? throw new ArgumentInvalidException("Уведомление не найдено", nameof(idNotification));
|
||
|
||
if(isRead && !notification.SentDate.HasValue)
|
||
throw new ArgumentInvalidException("Уведомление не может быть прочитано", nameof(isRead));
|
||
|
||
notification.ReadDate = isRead ? DateTime.UtcNow : null;
|
||
|
||
await notificationRepository.UpdateAsync(notification,
|
||
cancellationToken);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Отправка уведомлений, которые не были отправлены
|
||
/// </summary>
|
||
/// <param name="idUser"></param>
|
||
/// <param name="request"></param>
|
||
/// <param name="cancellationToken"></param>
|
||
/// <returns></returns>
|
||
public async Task RenotifyAsync(int idUser, CancellationToken cancellationToken)
|
||
{
|
||
const int idTransportType = 0;
|
||
|
||
var notifications = await notificationRepository.GetAllAsync(idUser, false,
|
||
idTransportType,
|
||
cancellationToken);
|
||
|
||
var notificationTransportService = GetTransportService(idTransportType);
|
||
|
||
await notificationTransportService.SendRangeAsync(notifications,
|
||
cancellationToken);
|
||
}
|
||
|
||
private INotificationTransportService GetTransportService(int idTransportType)
|
||
{
|
||
var notificationTransportService = notificationTransportServices
|
||
.FirstOrDefault(s => s.IdTransportType == idTransportType)
|
||
?? throw new ArgumentInvalidException("Доставщик уведомлений не найден", nameof(idTransportType));
|
||
|
||
return notificationTransportService;
|
||
}
|
||
} |