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 IUserRepository userRepository; private readonly NotificationService notificationService; private readonly INotificationRepository notificationRepository; public NotificationController(IUserRepository userRepository, NotificationService notificationService, INotificationRepository notificationRepository) { this.userRepository = userRepository; this.notificationService = notificationService; this.notificationRepository = notificationRepository; } /// /// Отправка уведомления /// /// Id пользователя /// Id категории уведомления. Допустимые: 1 /// Заголовок уведомления /// Сообщение уведомления /// Id типа доставки уведомления. Допустимые: 0, 1 /// /// [HttpPost] [Route("send")] public async Task SendAsync([Required] int idUser, [Required] [Range(minimum: 1, maximum: 1, ErrorMessage = "Id категории уведомления недоступно. Допустимые: 1")] int idNotificationCategory, [Required] string title, [Required] string message, [Required] [Range(minimum: 0, maximum: 1, ErrorMessage = "Id способа отправки уведомления недоступно. Допустимые: 0, 1")] int idTransportType, CancellationToken cancellationToken) { var user = await userRepository.GetOrDefaultAsync(idUser, cancellationToken) ?? throw new ArgumentInvalidException("Пользователь не найден", nameof(idUser)); await notificationService.NotifyAsync(new NotifyRequest { IdUser = user.Id, UserEmail = user.Email, IdNotificationCategory = idNotificationCategory, Title = title, Message = message, IdTransportType = idTransportType }, 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(); } }