DD.WellWorkover.Cloud/AsbCloudWebApi/SignalR/ReportsHub.cs
2024-08-19 10:01:07 +05:00

60 lines
2.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
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>
{
private readonly IWellService wellService;
private readonly IReportService reportService;
public ReportsHub(IWellService wellService, IReportService reportService)
{
this.wellService = wellService;
this.reportService = reportService;
}
/// <summary>
/// Добавление в группу, отправка данных о формировании отчета
/// </summary>
/// <param name="groupName">группа</param>
/// <param name="idWell">ключ скважины</param>
/// <param name="request">параметры запроса</param>
/// <returns></returns>
public async Task CreateReport(string groupName, int idWell, ReportParametersRequest request)
{
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);
}
}