forked from ddrilling/AsbCloudServer
Рефакторинг
1. Поменял время жизни для отправителей уведомлений. 2. Добавил метод в контроллере для получения уведомления по Id. 3. Поправил хаб уведомлений 4. Небольшие фиксы в репозитории и сервисах
This commit is contained in:
parent
b51ec44c35
commit
83ee280fbb
@ -3,12 +3,12 @@ using AsbCloudApp.Data;
|
||||
namespace AsbCloudApp.Services.Notifications;
|
||||
|
||||
/// <summary>
|
||||
/// Сервис для работы с отправителями уведомлений
|
||||
/// Сервис для работы с отправителями уведомлений
|
||||
/// </summary>
|
||||
public interface INotificationSenderManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Метод получения нужного отправителя уведомлений
|
||||
/// Метод получения нужного отправителя уведомлений
|
||||
/// </summary>
|
||||
/// <param name="notificationTransport"></param>
|
||||
/// <returns></returns>
|
||||
|
@ -20,7 +20,7 @@ public class NotificationRepository : CrudCacheRepositoryBase<NotificationDto, N
|
||||
=> 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<NotificationDto, N
|
||||
return result;
|
||||
|
||||
result.Items = await query
|
||||
.OrderBy(x => x.SentDate)
|
||||
.SortBy(request.SortFields)
|
||||
.SkipTake(request.Skip, request.Take)
|
||||
.Include(x => x.NotificationCategory)
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение уведомления по Id
|
||||
/// </summary>
|
||||
/// <param name="idNotification">Id уведомления</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[Route("get/{idNotification}")]
|
||||
[ProducesResponseType(typeof(NotificationDto), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение списка уведомлений
|
||||
/// </summary>
|
||||
|
@ -22,7 +22,7 @@ namespace AsbCloudWebApi
|
||||
{
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.MapType<TimeSpan>(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("00:00:00") });
|
||||
c.MapType<TimeSpan>(() => new OpenApiSchema { Type = "string", Example = new OpenApiString("1.00:00:00") });
|
||||
c.MapType<DateOnly>(() => new OpenApiSchema { Type = "string", Format = "date" });
|
||||
c.MapType<JsonValue>(() => new OpenApiSchema {
|
||||
AnyOf = new OpenApiSchema[]
|
||||
@ -111,8 +111,8 @@ namespace AsbCloudWebApi
|
||||
|
||||
public static void AddNotificationSenders(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<INotificationSenderManager, NotificationSenderManager>();
|
||||
services.AddTransient<INotificationSender, SignalRNotificationSender>();
|
||||
services.AddSingleton<INotificationSenderManager, NotificationSenderManager>();
|
||||
services.AddSingleton<INotificationSender, SignalRNotificationSender>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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<NotificationHub> notificationHubContext;
|
||||
private readonly INotificationRepository notificationRepository;
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public SignalRNotificationSender(IConnectionManager connectionManager,
|
||||
IHubContext<NotificationHub> 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<INotificationRepository>();
|
||||
|
||||
if (notificationRepository != null)
|
||||
{
|
||||
await notificationRepository.UpdateAsync(notification, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
await notificationRepository.UpdateAsync(notification,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
public async Task SendRangeAsync(IEnumerable<NotificationDto> notifications,
|
||||
|
Loading…
Reference in New Issue
Block a user