ProcessMapController Add SirnalR notifications on modification. MethodName = "ProcessMapChanged"

This commit is contained in:
ngfrolov 2023-05-29 17:50:20 +05:00
parent bf9a1d50f4
commit 85983e2870
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7

View File

@ -1,17 +1,17 @@
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudWebApi.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// РТК
/// </summary>
@ -21,18 +21,23 @@ namespace AsbCloudWebApi.Controllers
public class ProcessMapController : CrudWellRelatedController<ProcessMapPlanDto, IProcessMapPlanRepository>
{
private readonly ITelemetryService telemetryService;
private readonly IHubContext<TelemetryHub> telemetryHubContext;
private readonly IProcessMapReportMakerService processMapReportService;
private readonly IProcessMapReportService processMapService;
private const string SirnalRMethodGetDataName = "ProcessMapChanged";
public ProcessMapController(
IWellService wellService,
IProcessMapPlanRepository repository,
IProcessMapReportMakerService processMapReportService,
IProcessMapReportService processMapService,
ITelemetryService telemetryService)
ITelemetryService telemetryService,
IHubContext<TelemetryHub> telemetryHubContext)
: base(wellService, repository)
{
this.telemetryService = telemetryService;
this.telemetryHubContext = telemetryHubContext;
this.processMapReportService = processMapReportService;
this.processMapService = processMapService;
@ -56,8 +61,7 @@ namespace AsbCloudWebApi.Controllers
if (idWell is null)
return BadRequest($"Wrong uid {uid}");
var dto = Enumerable.Empty<ProcessMapPlanDto>();
return Ok(dto);
throw new NotImplementedException();
}
/// <summary>
@ -136,7 +140,10 @@ namespace AsbCloudWebApi.Controllers
public override async Task<ActionResult<int>> InsertAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
{
value.IdUser = User.GetUserId() ?? -1;
return await base.InsertAsync(value, token);
var result = await base.InsertAsync(value, token);
if (result.Value > 0)
await NotifyUsersBySignalR(value.IdWell, token);
return result;
}
/// <summary>
@ -149,8 +156,17 @@ namespace AsbCloudWebApi.Controllers
public override async Task<ActionResult<int>> UpdateAsync([FromBody] ProcessMapPlanDto value, CancellationToken token)
{
value.IdUser = User.GetUserId() ?? -1;
return await base.UpdateAsync(value, token);
var result = await base.UpdateAsync(value, token);
if (result.Value > 0)
await NotifyUsersBySignalR(value.IdWell, token);
return result;
}
private async Task NotifyUsersBySignalR(int idWell, CancellationToken token)
{
var dtos = await service.GetAllAsync(idWell, null, token);
_ = Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}")
.SendAsync(SirnalRMethodGetDataName, dtos), CancellationToken.None);
}
}
}