diff --git a/AsbCloudApp/Repositories/INotificationRepository.cs b/AsbCloudApp/Repositories/INotificationRepository.cs index 0bbdffbd..0b5db967 100644 --- a/AsbCloudApp/Repositories/INotificationRepository.cs +++ b/AsbCloudApp/Repositories/INotificationRepository.cs @@ -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; /// public interface INotificationRepository : ICrudRepository { - /// - /// Получение уведомлений по параметрам - /// - /// - /// - /// - /// - Task> GetNotificationsAsync(int idUser, - NotificationRequest request, - CancellationToken cancellationToken); + /// + /// Получение уведомлений по параметрам + /// + /// + /// + /// + /// + Task> GetNotificationsAsync(int idUser, + NotificationRequest request, + CancellationToken cancellationToken); + + /// + /// Получение количества непрочтенных уведомлений + /// + /// + /// + /// + Task GetUnreadCountAsync(int idUser, + CancellationToken cancellationToken); } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/NotificationRepository.cs b/AsbCloudInfrastructure/Repository/NotificationRepository.cs index c3302c95..15014675 100644 --- a/AsbCloudInfrastructure/Repository/NotificationRepository.cs +++ b/AsbCloudInfrastructure/Repository/NotificationRepository.cs @@ -54,7 +54,18 @@ public class NotificationRepository : CrudRepositoryBase BuildQuery(int idUser, + + public async Task 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 BuildQuery(int idUser, NotificationRequest request) { var query = dbContext.Notifications diff --git a/AsbCloudWebApi/Controllers/NotificationController.cs b/AsbCloudWebApi/Controllers/NotificationController.cs index 3646647d..00ab0a24 100644 --- a/AsbCloudWebApi/Controllers/NotificationController.cs +++ b/AsbCloudWebApi/Controllers/NotificationController.cs @@ -111,13 +111,33 @@ public class NotificationController : ControllerBase return Ok(result); } - /// - /// Удаление уведомления - /// - /// Id уведомления - /// - /// - [HttpDelete] + /// + /// Получение количества непрочитанных уведомлений + /// + /// + /// + [HttpGet] + [Route("unreadNotificationCount")] + [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] + public async Task GetUnreadCountAsync(CancellationToken cancellationToken) + { + int? idUser = User.GetUserId(); + + if (!idUser.HasValue) + return Forbid(); + + var result = await notificationRepository.GetUnreadCountAsync(idUser.Value, cancellationToken); + + return Ok(result); + } + + /// + /// Удаление уведомления + /// + /// Id уведомления + /// + /// + [HttpDelete] [Route("delete")] public async Task DeleteAsync([Required] int idNotification, CancellationToken cancellationToken)