forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
83ee280fbb
1. Поменял время жизни для отправителей уведомлений. 2. Добавил метод в контроллере для получения уведомления по Id. 3. Поправил хаб уведомлений 4. Небольшие фиксы в репозитории и сервисах
148 lines
4.5 KiB
C#
148 lines
4.5 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// Уведомления
|
|
/// </summary>
|
|
[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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Отправка уведомления
|
|
/// </summary>
|
|
/// <param name="idUser">Id пользователя</param>
|
|
/// <param name="idNotificationCategory">Id категории уведомления</param>
|
|
/// <param name="title">Заголовок уведомления</param>
|
|
/// <param name="message">Сообщение уведомления</param>
|
|
/// <param name="timeToLife">Время жизни уведомления</param>
|
|
/// <param name="notificationTransport">Способ отправки уведомления</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Route("send")]
|
|
public async Task<IActionResult> 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Обновление уведомления
|
|
/// </summary>
|
|
/// <param name="idNotification">Id уведомления</param>
|
|
/// <param name="isRead">Прочитано ли уведомление</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpPut]
|
|
[Route("update")]
|
|
public async Task<IActionResult> UpdateAsync([Required] int idNotification,
|
|
[Required] bool isRead,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await notificationService.UpdateNotificationAsync(idNotification,
|
|
isRead,
|
|
cancellationToken);
|
|
|
|
return Ok();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение уведомления по Id
|
|
/// </summary>
|
|
/// <param name="idNotification">Id уведомления</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("get/{idNotification}")]
|
|
[ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)]
|
|
public async Task<IActionResult> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение списка уведомлений
|
|
/// </summary>
|
|
/// <param name="request">Параметры запроса</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("getList")]
|
|
[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]
|
|
[Route("delete")]
|
|
public async Task<IActionResult> DeleteAsync([Required] int idNotification,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await notificationRepository.DeleteAsync(idNotification,
|
|
cancellationToken);
|
|
|
|
return Ok();
|
|
}
|
|
} |