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 Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace AsbCloudWebApi.Controllers; /// /// Уведомления /// [ApiController] [Authorize] [Route("api/notification")] public class NotificationController : ControllerBase { private readonly INotificationService notificationService; private readonly INotificationRepository notificationRepository; public NotificationController(INotificationService notificationService, INotificationRepository notificationRepository) { this.notificationService = notificationService; this.notificationRepository = notificationRepository; } /// /// Отправка уведомления /// /// Id пользователя /// Id категории уведомления /// Заголовок уведомления /// Сообщение уведомления /// Время жизни уведомления /// Способ отправки уведомления /// /// [HttpPost] [Route("send")] public async Task SendAsync([Required] int idUser, [Required] int idNotificationCategory, [Required] string title, [Required] string message, [Required] TimeSpan timeToLife, [Required] NotificationTransport notificationTransport, CancellationToken cancellationToken) { await notificationService.NotifyAsync(idUser, idNotificationCategory, title, message, timeToLife, notificationTransport, cancellationToken); return Ok(); } /// /// Обновление уведомления /// /// Id уведомления /// Прочитано ли уведомление /// /// [HttpPut] [Route("update")] public async Task UpdateAsync([Required] int idNotification, [Required] bool isRead, CancellationToken cancellationToken) { await notificationService.UpdateNotificationAsync(idNotification, isRead, cancellationToken); return Ok(); } /// /// Получение уведомления по Id /// /// Id уведомления /// /// [HttpGet] [Route("get/{idNotification}")] [ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)] public async Task 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); } /// /// Получение списка уведомлений /// /// Параметры запроса /// /// [HttpGet] [Route("getList")] [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] [Route("delete")] public async Task DeleteAsync([Required] int idNotification, CancellationToken cancellationToken) { await notificationRepository.DeleteAsync(idNotification, cancellationToken); return Ok(); } }