using System;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
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 способа отправки уведомления
/// Id категории уведомления
/// Заголовок уведомления
/// Сообщение уведомления
/// Время жизни уведомления
///
///
[HttpPost]
[Route("send")]
public async Task SendAsync([Required] int idUser,
[Required] int idNotificationTransport,
[Required] int idNotificationCategory,
[Required] string title,
[Required] string subject,
[Required] TimeSpan timeToLife,
CancellationToken cancellationToken)
{
await notificationService.SendNotificationAsync(idUser,
idNotificationTransport,
idNotificationCategory,
title,
subject,
timeToLife,
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 способа доставки уведомления
///
///
[HttpGet]
[Route("getList")]
[ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)]
public async Task GetListAsync(int? skip,
int? take,
[Required] int idNotificationTransport,
CancellationToken cancellationToken)
{
int? idUser = User.GetUserId();
if (!idUser.HasValue)
return Forbid();
var result = await notificationRepository.GetNotificationsAsync(skip,
take,
idUser.Value,
idNotificationTransport,
cancellationToken);
return Ok(result);
}
///
/// Метод удаления уведомления
///
/// Id уведомления
///
///
[HttpDelete]
[Route("delete")]
public async Task DeleteAsync([Required] int idNotification,
CancellationToken cancellationToken)
{
await notificationRepository.DeleteAsync(idNotification,
cancellationToken);
return Ok();
}
}