Метод, отдающий количество непрочитанных уведомлений

This commit is contained in:
Olga Nemt 2023-07-28 11:37:27 +05:00
parent bbc42208c2
commit af0d6e2d30
3 changed files with 60 additions and 20 deletions

View File

@ -1,8 +1,8 @@
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudApp.Repositories;
@ -11,14 +11,23 @@ namespace AsbCloudApp.Repositories;
/// </summary>
public interface INotificationRepository : ICrudRepository<NotificationDto>
{
/// <summary>
/// Получение уведомлений по параметрам
/// </summary>
/// <param name="idUser"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<PaginationContainer<NotificationDto>> GetNotificationsAsync(int idUser,
NotificationRequest request,
CancellationToken cancellationToken);
/// <summary>
/// Получение уведомлений по параметрам
/// </summary>
/// <param name="idUser"></param>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<PaginationContainer<NotificationDto>> GetNotificationsAsync(int idUser,
NotificationRequest request,
CancellationToken cancellationToken);
/// <summary>
/// Получение количества непрочтенных уведомлений
/// </summary>
/// <param name="idUser"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<int> GetUnreadCountAsync(int idUser,
CancellationToken cancellationToken);
}

View File

@ -54,7 +54,18 @@ public class NotificationRepository : CrudRepositoryBase<NotificationDto, Notifi
return result;
}
private IQueryable<Notification> BuildQuery(int idUser,
public async Task<int> GetUnreadCountAsync(int idUser, CancellationToken cancellationToken)
{
var count = await dbContext.Notifications
.Where(n => n.ReadDate == null)
.Where(n => n.IdUser == idUser)
.CountAsync(cancellationToken);
return count;
}
private IQueryable<Notification> BuildQuery(int idUser,
NotificationRequest request)
{
var query = dbContext.Notifications

View File

@ -111,13 +111,33 @@ public class NotificationController : ControllerBase
return Ok(result);
}
/// <summary>
/// Удаление уведомления
/// </summary>
/// <param name="idNotification">Id уведомления</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpDelete]
/// <summary>
/// Получение количества непрочитанных уведомлений
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet]
[Route("unreadNotificationCount")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetUnreadCountAsync(CancellationToken cancellationToken)
{
int? idUser = User.GetUserId();
if (!idUser.HasValue)
return Forbid();
var result = await notificationRepository.GetUnreadCountAsync(idUser.Value, 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)