2023-09-15 11:26:33 +05:00
|
|
|
|
using System;
|
2023-07-10 16:56:55 +05:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data;
|
2023-07-12 13:31:55 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2023-07-10 16:56:55 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-07-11 19:07:57 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudApp.Services.Notifications;
|
2023-08-04 09:47:22 +05:00
|
|
|
|
using AsbCloudWebApi.SignalR.Services;
|
2023-07-10 16:56:55 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Уведомления
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
2023-08-08 12:21:09 +05:00
|
|
|
|
[Route("api/[controller]")]
|
2023-09-15 11:26:33 +05:00
|
|
|
|
[Obsolete("Это тестовый контроллер, его нельзя использовать на фронте.")]
|
2023-07-10 16:56:55 +05:00
|
|
|
|
public class NotificationController : ControllerBase
|
|
|
|
|
{
|
2024-07-04 11:02:45 +05:00
|
|
|
|
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("{idNotification}")]
|
|
|
|
|
[ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)]
|
2023-09-28 16:25:29 +05:00
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
|
|
|
|
public async Task<IActionResult> GetAsync([Required] int idNotification,
|
2024-07-04 11:02:45 +05:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken);
|
|
|
|
|
if (notification is null)
|
2023-09-28 17:50:58 +05:00
|
|
|
|
return this.ValidationBadRequest(nameof(idNotification), "Уведомление не найдено");
|
2023-07-12 13:31:55 +05:00
|
|
|
|
|
2023-09-28 16:25:29 +05:00
|
|
|
|
return Ok(notification);
|
2024-07-04 11:02:45 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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("{idNotification}")]
|
|
|
|
|
public async Task<IActionResult> DeleteAsync([Required] int idNotification,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await notificationRepository.DeleteAsync(idNotification,
|
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-07-28 11:42:10 +05:00
|
|
|
|
/// Удаление уведомлений
|
2023-07-28 11:37:27 +05:00
|
|
|
|
/// </summary>
|
2023-07-28 11:42:10 +05:00
|
|
|
|
/// <param name="request">Параметры запроса</param>
|
2023-07-28 11:37:27 +05:00
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
2024-07-04 11:02:45 +05:00
|
|
|
|
public async Task<IActionResult> DeleteAsync(NotificationDeleteRequest request,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await notificationRepository.DeleteAsync(request, cancellationToken);
|
2023-07-10 16:56:55 +05:00
|
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2023-07-10 16:56:55 +05:00
|
|
|
|
}
|