DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/NotificationController.cs
Степанов Дмитрий Александрович 5f459b79b8 Изменил отправку уведомлений через SignalR
1. Добавил отправку всех неотправленных уведомлений и кол-во непрочитаннах уведомлений при первом подключении
2. При изменении статуса прочтения уведомления, клиенту отправляется информация о том сколько непрочитанных уведомлений ещё есть.
3. Добавил объект NotificationMessage, который отправляется клиенту.
4. Сделал небольшой рефакторинг
2023-08-04 09:47:22 +05:00

153 lines
4.6 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services.Notifications;
using AsbCloudWebApi.SignalR.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// Уведомления
/// </summary>
[ApiController]
[Authorize]
[Route("api/notification")]
public class NotificationController : ControllerBase
{
private readonly NotificationService notificationService;
private readonly INotificationRepository notificationRepository;
private readonly NotificationPublisher notificationPublisher;
public NotificationController(NotificationService notificationService,
INotificationRepository notificationRepository,
NotificationPublisher notificationPublisher)
{
this.notificationService = notificationService;
this.notificationRepository = notificationRepository;
this.notificationPublisher = notificationPublisher;
}
/// <summary>
/// Отправка уведомления
/// </summary>
/// <param name="request">Параметры запроса</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> SendAsync(NotifyRequest request, CancellationToken cancellationToken)
{
await notificationService.NotifyAsync(request, cancellationToken);
return Ok();
}
/// <summary>
/// Обновление уведомления
/// </summary>
/// <param name="idNotification">Id уведомления</param>
/// <param name="isRead">Прочитано ли уведомление</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPut]
public async Task<IActionResult> UpdateAsync([Required] int idNotification,
[Required] bool isRead,
CancellationToken cancellationToken)
{
var idUser = User.GetUserId();
if (!idUser.HasValue)
return Forbid();
await notificationService.UpdateAsync(idNotification,
isRead,
cancellationToken);
await notificationPublisher.PublishCountUnreadAsync(idUser.Value, cancellationToken);
return Ok();
}
/// <summary>
/// Получение уведомления по Id
/// </summary>
/// <param name="idNotification">Id уведомления</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet]
[Route("{idNotification}")]
[ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync([Required] int idNotification,
CancellationToken cancellationToken)
{
var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken);
if (notification is null)
{
return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(idNotification),
"Уведомление не найдено"));
}
return Ok(notification);
}
/// <summary>
/// Получение списка уведомлений
/// </summary>
/// <param name="request">Параметры запроса</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(PaginationContainer<NotificationDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetListAsync([FromQuery] NotificationRequest request,
CancellationToken cancellationToken)
{
int? idUser = User.GetUserId();
if (!idUser.HasValue)
return Forbid();
var result = await notificationRepository.GetNotificationsAsync(idUser.Value,
request,
cancellationToken);
return Ok(result);
}
/// <summary>
/// Удаление уведомления
/// </summary>
/// <param name="idNotification">Id уведомления</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpDelete]
[Route("{idNotification}")]
public async Task<IActionResult> DeleteAsync([Required] int idNotification,
CancellationToken cancellationToken)
{
await notificationRepository.DeleteAsync(idNotification,
cancellationToken);
return Ok();
}
/// <summary>
/// Удаление уведомлений
/// </summary>
/// <param name="request">Параметры запроса</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpDelete]
public async Task<IActionResult> DeleteAsync(NotificationDeleteRequest request,
CancellationToken cancellationToken)
{
await notificationRepository.DeleteAsync(request, cancellationToken);
return Ok();
}
}