DD.WellWorkover.Cloud/AsbCloudWebApi/SignalR/ReportsHub.cs

60 lines
2.0 KiB
C#
Raw Normal View History

using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Background;
using AsbCloudWebApi.SignalR.Clients;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.SignalR;
// SignalR manual:
// https://docs.microsoft.com/ru-ru/aspnet/core/signalr/introduction?view=aspnetcore-5.0
//[SignalRHub]
/// <summary>
/// ReportsHub
/// </summary>
[Authorize]
public class ReportsHub : BaseHub<IReportHubClient>
{
2024-08-19 10:01:07 +05:00
private readonly IWellService wellService;
private readonly IReportService reportService;
public ReportsHub(IWellService wellService, IReportService reportService)
{
this.wellService = wellService;
this.reportService = reportService;
}
/// <summary>
2024-08-19 10:01:07 +05:00
/// Добавление в группу, отправка данных о формировании отчета
/// </summary>
2024-08-19 10:01:07 +05:00
/// <param name="groupName">группа</param>
/// <param name="idWell">ключ скважины</param>
/// <param name="request">параметры запроса</param>
/// <returns></returns>
public async Task CreateReport(string groupName, int idWell, ReportParametersRequest request)
{
2024-08-19 10:01:07 +05:00
var idUser = Context.User?.GetUserId();
var idCompany = Context.User?.GetCompanyId();
if ((idCompany is null) || (idUser is null))
return;
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, CancellationToken.None))
return;
await base.AddToGroup(groupName);
void HandleReportProgressAsync(object progress, string id) =>
Task.Run(async () =>
{
await Clients.Group(groupName)
.GetReportProgress(progress, CancellationToken.None);
}, CancellationToken.None);
var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser, request, HandleReportProgressAsync);
}
}