From 3ac3fded5ba8af897aa4da45415a83af945f435e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 27 Jul 2023 17:33:20 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE=D1=80=D0=B8=D0=BD=D0=B3=20?= =?UTF-8?q?=D1=83=D0=B2=D0=B5=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Убрал глаголы из Route в контроллере. 2. Создал метод удаления уведомлений по параметрам --- .../Repositories/INotificationRepository.cs | 9 +++++++ .../Requests/NotificationDeleteRequest.cs | 24 +++++++++++++++++++ .../Repository/NotificationRepository.cs | 18 ++++++++++++++ .../Controllers/NotificationController.cs | 22 +++++++++++++---- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 AsbCloudApp/Requests/NotificationDeleteRequest.cs diff --git a/AsbCloudApp/Repositories/INotificationRepository.cs b/AsbCloudApp/Repositories/INotificationRepository.cs index 0bbdffbd..d469b120 100644 --- a/AsbCloudApp/Repositories/INotificationRepository.cs +++ b/AsbCloudApp/Repositories/INotificationRepository.cs @@ -21,4 +21,13 @@ public interface INotificationRepository : ICrudRepository Task> GetNotificationsAsync(int idUser, NotificationRequest request, CancellationToken cancellationToken); + + /// + /// Удаление уведомлений по параметрам + /// + /// + /// + /// + Task DeleteAsync(NotificationDeleteRequest request, + CancellationToken cancellationToken); } \ No newline at end of file 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 c3302c95..fecbfa26 100644 --- a/AsbCloudInfrastructure/Repository/NotificationRepository.cs +++ b/AsbCloudInfrastructure/Repository/NotificationRepository.cs @@ -54,6 +54,24 @@ 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); + } + private IQueryable BuildQuery(int idUser, NotificationRequest request) { diff --git a/AsbCloudWebApi/Controllers/NotificationController.cs b/AsbCloudWebApi/Controllers/NotificationController.cs index 3646647d..066ddd99 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) @@ -118,7 +115,7 @@ public class NotificationController : ControllerBase /// /// [HttpDelete] - [Route("delete")] + [Route("{idNotification}")] public async Task DeleteAsync([Required] int idNotification, CancellationToken cancellationToken) { @@ -127,4 +124,19 @@ public class NotificationController : ControllerBase return Ok(); } + + /// + /// Удаление уведомлений + /// + /// Параметры запроса + /// + /// + [HttpDelete] + public async Task DeleteAsync(NotificationDeleteRequest request, + CancellationToken cancellationToken) + { + await notificationRepository.DeleteAsync(request, cancellationToken); + + return Ok(); + } } \ No newline at end of file