diff --git a/AsbCloudApp/Services/Notifications/INotificationSenderManager.cs b/AsbCloudApp/Services/Notifications/INotificationSenderManager.cs
index 228308cd..b2cc8254 100644
--- a/AsbCloudApp/Services/Notifications/INotificationSenderManager.cs
+++ b/AsbCloudApp/Services/Notifications/INotificationSenderManager.cs
@@ -3,12 +3,12 @@ using AsbCloudApp.Data;
namespace AsbCloudApp.Services.Notifications;
///
-///
+/// Сервис для работы с отправителями уведомлений
///
public interface INotificationSenderManager
{
///
- ///
+ /// Метод получения нужного отправителя уведомлений
///
///
///
diff --git a/AsbCloudInfrastructure/Repository/NotificationRepository.cs b/AsbCloudInfrastructure/Repository/NotificationRepository.cs
index df6f4222..ed6c3f63 100644
--- a/AsbCloudInfrastructure/Repository/NotificationRepository.cs
+++ b/AsbCloudInfrastructure/Repository/NotificationRepository.cs
@@ -20,7 +20,7 @@ public class NotificationRepository : CrudCacheRepositoryBase dbSet.AsNoTracking()
.Include(n => n.NotificationCategory);
- public NotificationRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
+ public NotificationRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache)
: base(dbContext, memoryCache, MakeQueryNotification)
{
}
@@ -60,6 +60,7 @@ public class NotificationRepository : CrudCacheRepositoryBase x.SentDate)
.SortBy(request.SortFields)
.SkipTake(request.Skip, request.Take)
.Include(x => x.NotificationCategory)
diff --git a/AsbCloudInfrastructure/Services/Notifications/NotificationService.cs b/AsbCloudInfrastructure/Services/Notifications/NotificationService.cs
index bfa055d6..6d68ad41 100644
--- a/AsbCloudInfrastructure/Services/Notifications/NotificationService.cs
+++ b/AsbCloudInfrastructure/Services/Notifications/NotificationService.cs
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
+using AsbCloudApp.Services;
using AsbCloudApp.Services.Notifications;
namespace AsbCloudInfrastructure.Services.Notifications;
@@ -28,7 +29,7 @@ public class NotificationService : INotificationService
NotificationTransport notificationTransport,
CancellationToken cancellationToken)
{
- NotificationDto notification = new()
+ var notification = new NotificationDto()
{
IdUser = idUser,
IdNotificationCategory = idNotificationCategory,
@@ -39,12 +40,12 @@ public class NotificationService : INotificationService
NotificationState = NotificationState.Registered
};
- await notificationRepository.InsertAsync(notification,
+ notification.Id = await notificationRepository.InsertAsync(notification,
cancellationToken);
var notificationSender = notificationSenderManager.GetOrDefault(notificationTransport);
- if(notificationSender is null)
+ if(notificationSender is null)
throw new ArgumentInvalidException("Метод отправки уведомления не найден", nameof(notificationTransport));
await notificationSender.SendAsync(notification, cancellationToken);
diff --git a/AsbCloudWebApi/Controllers/NotificationController.cs b/AsbCloudWebApi/Controllers/NotificationController.cs
index d9834779..b490db73 100644
--- a/AsbCloudWebApi/Controllers/NotificationController.cs
+++ b/AsbCloudWebApi/Controllers/NotificationController.cs
@@ -3,6 +3,7 @@ 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;
@@ -81,6 +82,29 @@ public class NotificationController : ControllerBase
return Ok();
}
+ ///
+ /// Получение уведомления по Id
+ ///
+ /// Id уведомления
+ ///
+ ///
+ [HttpGet]
+ [Route("get/{idNotification}")]
+ [ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)]
+ public async Task 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);
+ }
+
///
/// Получение списка уведомлений
///
diff --git a/AsbCloudWebApi/DependencyInjection.cs b/AsbCloudWebApi/DependencyInjection.cs
index 5d50561a..846ffb23 100644
--- a/AsbCloudWebApi/DependencyInjection.cs
+++ b/AsbCloudWebApi/DependencyInjection.cs
@@ -22,7 +22,7 @@ namespace AsbCloudWebApi
{
services.AddSwaggerGen(c =>
{
- c.MapType(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("00:00:00") });
+ c.MapType(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("1.00:00:00") });
c.MapType(() => new OpenApiSchema { Type = "string", Format = "date" });
c.MapType(() => new OpenApiSchema {
AnyOf = new OpenApiSchema[]
@@ -111,8 +111,8 @@ namespace AsbCloudWebApi
public static void AddNotificationSenders(this IServiceCollection services)
{
- services.AddScoped();
- services.AddTransient();
+ services.AddSingleton();
+ services.AddSingleton();
}
}
}
diff --git a/AsbCloudWebApi/SignalR/NotificationHub.cs b/AsbCloudWebApi/SignalR/NotificationHub.cs
index 99983f14..2e378de9 100644
--- a/AsbCloudWebApi/SignalR/NotificationHub.cs
+++ b/AsbCloudWebApi/SignalR/NotificationHub.cs
@@ -1,3 +1,4 @@
+using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
@@ -22,14 +23,28 @@ public class NotificationHub : BaseHub
public async Task OnConnected(int idUser)
{
- string connectionId = Context.ConnectionId;
+ try
+ {
+ string connectionId = Context.ConnectionId;
- connectionManager.AddConnection(idUser, connectionId);
+ connectionManager.AddConnection(idUser, connectionId);
- await notificationService.ResendNotificationAsync(idUser,
- NotificationTransport.SignalR,
- CancellationToken.None);
+ await notificationService.ResendNotificationAsync(idUser,
+ NotificationTransport.SignalR,
+ CancellationToken.None);
- await base.OnConnectedAsync();
+ await base.OnConnectedAsync();
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine(e);
+ }
+ }
+
+ public Task OnDisconnected(int idUser)
+ {
+ connectionManager.RemoveConnection(idUser);
+
+ return base.OnDisconnectedAsync(null);
}
}
\ No newline at end of file
diff --git a/AsbCloudWebApi/SignalR/Services/SignalRNotificationSender.cs b/AsbCloudWebApi/SignalR/Services/SignalRNotificationSender.cs
index bf7c226e..3c277d60 100644
--- a/AsbCloudWebApi/SignalR/Services/SignalRNotificationSender.cs
+++ b/AsbCloudWebApi/SignalR/Services/SignalRNotificationSender.cs
@@ -7,6 +7,7 @@ using AsbCloudApp.Repositories;
using AsbCloudApp.Services.Notifications;
using AsbCloudWebApi.SignalR.ConnectionManager;
using Microsoft.AspNetCore.SignalR;
+using Microsoft.Extensions.DependencyInjection;
namespace AsbCloudWebApi.SignalR.Services;
@@ -14,15 +15,15 @@ public class SignalRNotificationSender : INotificationSender
{
private readonly IConnectionManager connectionManager;
private readonly IHubContext notificationHubContext;
- private readonly INotificationRepository notificationRepository;
+ private readonly IServiceProvider serviceProvider;
public SignalRNotificationSender(IConnectionManager connectionManager,
IHubContext notificationHubContext,
- INotificationRepository notificationRepository)
+ IServiceProvider serviceProvider)
{
this.connectionManager = connectionManager;
this.notificationHubContext = notificationHubContext;
- this.notificationRepository = notificationRepository;
+ this.serviceProvider = serviceProvider;
}
public NotificationTransport NotificationTransport => NotificationTransport.SignalR;
@@ -36,17 +37,25 @@ public class SignalRNotificationSender : INotificationSender
if (!string.IsNullOrWhiteSpace(connectionId))
{
+ string message = $"IdNotification: {notification.Id}";
+
await notificationHubContext.Clients.Client(connectionId)
.SendAsync(method,
- notification,
+ message,
cancellationToken);
notification.SentDate = DateTime.UtcNow;
notification.NotificationState = NotificationState.Sent;
+
+ var scope = serviceProvider.CreateScope();
+
+ var notificationRepository = scope.ServiceProvider.GetService();
+
+ if (notificationRepository != null)
+ {
+ await notificationRepository.UpdateAsync(notification, cancellationToken);
+ }
}
-
- await notificationRepository.UpdateAsync(notification,
- cancellationToken);
}
public async Task SendRangeAsync(IEnumerable notifications,