using System; 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; /// /// Уведомления /// [ApiController] [Authorize] [Route("api/[controller]")] [Obsolete("Это тестовый контроллер, его нельзя использовать на фронте.")] 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; } /// /// Отправка уведомления /// /// Параметры запроса /// /// [HttpPost] public async Task SendAsync(NotifyRequest request, CancellationToken cancellationToken) { await notificationService.NotifyAsync(request, cancellationToken); return Ok(); } /// /// Обновление уведомления /// /// Id уведомления /// Прочитано ли уведомление /// /// [HttpPut] public async Task 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(); } /// /// Получение уведомления по Id /// /// Id уведомления /// /// [HttpGet("{idNotification}")] [ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] public async Task GetAsync([Required] int idNotification, CancellationToken cancellationToken) { var notification = await notificationRepository.GetOrDefaultAsync(idNotification, cancellationToken); if (notification is null) return this.ValidationBadRequest(nameof(idNotification), "Уведомление не найдено"); return Ok(notification); } /// /// Получение списка уведомлений /// /// Параметры запроса /// /// [HttpGet] [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] public async Task 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); } /// /// Удаление уведомления /// /// Id уведомления /// /// [HttpDelete("{idNotification}")] public async Task DeleteAsync([Required] int idNotification, CancellationToken cancellationToken) { await notificationRepository.DeleteAsync(idNotification, cancellationToken); return Ok(); } /// /// Удаление уведомлений /// /// Параметры запроса /// /// [HttpDelete] public async Task DeleteAsync(NotificationDeleteRequest request, CancellationToken cancellationToken) { await notificationRepository.DeleteAsync(request, cancellationToken); return Ok(); } }