diff --git a/AsbCloudApp/Repositories/INotificationRepository.cs b/AsbCloudApp/Repositories/INotificationRepository.cs index 0b5db967..60cfb161 100644 --- a/AsbCloudApp/Repositories/INotificationRepository.cs +++ b/AsbCloudApp/Repositories/INotificationRepository.cs @@ -11,16 +11,25 @@ namespace AsbCloudApp.Repositories; /// public interface INotificationRepository : ICrudRepository { - /// - /// Получение уведомлений по параметрам - /// - /// - /// - /// - /// - Task> GetNotificationsAsync(int idUser, - NotificationRequest request, - CancellationToken cancellationToken); + /// + /// Получение уведомлений по параметрам + /// + /// + /// + /// + /// + Task> GetNotificationsAsync(int idUser, + NotificationRequest request, + CancellationToken cancellationToken); + + /// + /// Удаление уведомлений по параметрам + /// + /// + /// + /// + Task DeleteAsync(NotificationDeleteRequest request, + CancellationToken cancellationToken); /// /// Получение количества непрочтенных уведомлений diff --git a/AsbCloudApp/Requests/NotificationDeleteRequest.cs b/AsbCloudApp/Requests/NotificationDeleteRequest.cs new file mode 100644 index 00000000..ec2026f3 --- /dev/null +++ b/AsbCloudApp/Requests/NotificationDeleteRequest.cs @@ -0,0 +1,24 @@ +using System; + +namespace AsbCloudApp.Requests; + +/// +/// Запрос для удаления уведомлений +/// +public class NotificationDeleteRequest +{ + /// + /// Идентификатор категории + /// + public int? IdCategory { get; set; } + + /// + /// Меньше или равно дате отправки + /// + public DateTime? LtSentDate { get; set; } + + /// + /// Меньше или равно дате прочтения + /// + public DateTime? LtReadDate { get; set; } +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/NotificationRepository.cs b/AsbCloudInfrastructure/Repository/NotificationRepository.cs index 15014675..97091217 100644 --- a/AsbCloudInfrastructure/Repository/NotificationRepository.cs +++ b/AsbCloudInfrastructure/Repository/NotificationRepository.cs @@ -54,15 +54,32 @@ public class NotificationRepository : CrudRepositoryBase DeleteAsync(NotificationDeleteRequest request, CancellationToken cancellationToken) + { + var query = dbContext.Notifications.AsQueryable(); + + if (request.IdCategory.HasValue) + query = query.Where(n => n.IdNotificationCategory == request.IdCategory.Value); + + if (request.LtSentDate.HasValue) + query = query.Where(n => n.SentDate <= request.LtSentDate.Value); + + if (request.LtReadDate.HasValue) + query = query.Where(n => n.ReadDate <= request.LtReadDate.Value); + + dbContext.Notifications.RemoveRange(query); + + return dbContext.SaveChangesAsync(cancellationToken); + } 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); + var count = await dbContext.Notifications + .Where(n => n.ReadDate == null) + .Where(n => n.IdUser == idUser) + .CountAsync(cancellationToken); - return count; + return count; } private IQueryable BuildQuery(int idUser, diff --git a/AsbCloudWebApi/Controllers/NotificationController.cs b/AsbCloudWebApi/Controllers/NotificationController.cs index 00ab0a24..13766201 100644 --- a/AsbCloudWebApi/Controllers/NotificationController.cs +++ b/AsbCloudWebApi/Controllers/NotificationController.cs @@ -36,7 +36,6 @@ public class NotificationController : ControllerBase /// /// [HttpPost] - [Route("send")] public async Task SendAsync(NotifyRequest request, CancellationToken cancellationToken) { await notificationService.NotifyAsync(request, cancellationToken); @@ -52,7 +51,6 @@ public class NotificationController : ControllerBase /// /// [HttpPut] - [Route("update")] public async Task UpdateAsync([Required] int idNotification, [Required] bool isRead, CancellationToken cancellationToken) @@ -71,7 +69,7 @@ public class NotificationController : ControllerBase /// /// [HttpGet] - [Route("get/{idNotification}")] + [Route("{idNotification}")] [ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync([Required] int idNotification, CancellationToken cancellationToken) @@ -94,7 +92,6 @@ public class NotificationController : ControllerBase /// /// [HttpGet] - [Route("getList")] [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] public async Task GetListAsync([FromQuery] NotificationRequest request, CancellationToken cancellationToken) @@ -111,6 +108,23 @@ public class NotificationController : ControllerBase return Ok(result); } + /// + /// Удаление уведомления + /// + /// Id уведомления + /// + /// + [HttpDelete] + [Route("{idNotification}")] + public async Task DeleteAsync([Required] int idNotification, + CancellationToken cancellationToken) + { + await notificationRepository.DeleteAsync(idNotification, + cancellationToken); + + return Ok(); + } + /// /// Получение количества непрочитанных уведомлений /// @@ -118,7 +132,7 @@ public class NotificationController : ControllerBase /// [HttpGet] [Route("unreadNotificationCount")] - [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] + [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task GetUnreadCountAsync(CancellationToken cancellationToken) { int? idUser = User.GetUserId(); @@ -132,18 +146,16 @@ public class NotificationController : ControllerBase } /// - /// Удаление уведомления + /// Удаление уведомлений /// - /// Id уведомления + /// Параметры запроса /// /// [HttpDelete] - [Route("delete")] - public async Task DeleteAsync([Required] int idNotification, + public async Task DeleteAsync(NotificationDeleteRequest request, CancellationToken cancellationToken) { - await notificationRepository.DeleteAsync(idNotification, - cancellationToken); + await notificationRepository.DeleteAsync(request, cancellationToken); return Ok(); }