DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/SetpointsController.cs

163 lines
6.5 KiB
C#
Raw 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.Services;
using AsbCloudApp.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
namespace AsbCloudWebApi.Controllers
{
[ApiController]
[Authorize]
public class SetpointsController : ControllerBase
{
private readonly ISetpointsService setpointsService;
private readonly IWellService wellService;
private const int ObsolescenceSecMin = 30;
private const int ObsolescenceSecMax = 6 * 60 * 60;
public SetpointsController(ISetpointsService setpointsService, IWellService wellService)
{
this.setpointsService = setpointsService;
this.wellService = wellService;
}
/// <summary>
/// Добавляет запрос на изменение заданий панели оператора.
/// </summary>
/// <param name="idWell"></param>
/// <param name="setpoints"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("api/well/{idWell}/setpoints")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> InsertAsync(int idWell, SetpointsRequestDto setpoints, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
setpoints.IdAuthor = idUser ?? -1;
setpoints.IdWell = idWell;
setpoints.IdState = 1;
if (setpoints is null
|| setpoints.ObsolescenceSec > ObsolescenceSecMax
|| setpoints.ObsolescenceSec < ObsolescenceSecMin)
return BadRequest("Wrong ObsolescenceSec");
if (!setpoints.Setpoints.Any())
return BadRequest("Wrong Setpoints count");
var result = await setpointsService.InsertAsync(setpoints, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает список запросов на изменение уставок.
/// </summary>
/// <param name="idWell"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("api/well/{idWell}/setpoints")]
[ProducesResponseType(typeof(IEnumerable<SetpointsRequestDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetByIdWellAsync([FromRoute] int idWell, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
var result = await setpointsService.GetAsync(idWell, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Отчет о принятии, или отклонении уставок оператором.
/// После уставка будет не изменяемой.
/// </summary>
/// <param name="uid"></param>
/// <param name="id"></param>
/// <param name="setpointsRequestDto">можно передать только новый state eg.: {state:3} - принято</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPut("api/telemetry/{uid}/setpoints/{id}")]
[ProducesResponseType(typeof(SetpointsRequestDto), (int)System.Net.HttpStatusCode.OK)]
[AllowAnonymous]
public async Task<IActionResult> UpdateByTelemetryUidAsync([FromRoute] string uid, int id, SetpointsRequestDto setpointsRequestDto, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
var result = await setpointsService.UpdateStateAsync(uid, id, setpointsRequestDto, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Получает запросы на изменение уставок панели.
/// !!SIDE EFFECT: изменяет состояние запросов уставок на отправлено, это не позволит удалить эти уставки после отправки на панель.
/// </summary>
/// <param name="uid"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("api/telemetry/{uid}/setpoints")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<SetpointsRequestDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetByTelemetryUidAsync([FromRoute] string uid, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
var result = await setpointsService.GetForPanelAsync(uid, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Пробуем удалить запрос на изменение уставок. Это будет выполнено, если запрос еще не был отправлен.
/// </summary>
/// <param name="idWell"></param>
/// <param name="id"></param>
/// <param name="token"></param>
/// <returns>1 - удалено, <= 0 - не удалено</returns>
[HttpDelete("api/well/{idWell}/setpoints/{id}")]
public async Task<IActionResult> TryDeleteByIdWellAsync(int idWell, int id, CancellationToken token = default)
{
int? idCompany = User.GetCompanyId();
int? idUser = User.GetUserId();
if (idCompany is null || idUser is null)
return Forbid();
var result = await setpointsService.TryDelete(idWell, id, token)
.ConfigureAwait(false);
return Ok(result);
}
}
}