2021-08-27 17:55:22 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
2021-08-11 12:11:21 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
2021-08-28 20:00:04 +05:00
|
|
|
|
[Route("api/well/{idWell}/measure")]
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public class MeasureController : ControllerBase
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
2021-08-31 09:52:15 +05:00
|
|
|
|
private readonly IMeasureService measureService;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
2021-08-31 09:52:15 +05:00
|
|
|
|
public MeasureController(IMeasureService measureService, IWellService wellService)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
2021-08-31 09:52:15 +05:00
|
|
|
|
this.measureService = measureService;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-27 17:55:22 +05:00
|
|
|
|
[Route("categories")]
|
|
|
|
|
public async Task<IActionResult> GetCategoriesAsync([FromRoute] int idWell, CancellationToken token = default)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
2021-08-27 17:55:22 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-31 09:52:15 +05:00
|
|
|
|
var result = await measureService.GetCategoriesAsync(token).ConfigureAwait(false);
|
2021-08-27 17:55:22 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-28 22:34:57 +05:00
|
|
|
|
[Route("last/{idCategory}")]
|
|
|
|
|
public async Task<IActionResult> GetLastAsync([FromRoute] int idWell, [FromRoute] int idCategory, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-31 09:52:15 +05:00
|
|
|
|
var result = await measureService.GetLastAsync(idWell, idCategory, token).ConfigureAwait(false);
|
2021-08-28 22:34:57 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-22 12:31:37 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// История замеров по скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
2021-12-24 15:50:30 +05:00
|
|
|
|
/// <param name="idCategory">Категория скважины. Не обязательный параметр.</param>
|
2021-12-22 12:31:37 +05:00
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-08-28 22:34:57 +05:00
|
|
|
|
[HttpGet]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-12-24 15:50:30 +05:00
|
|
|
|
[Route("history")]
|
2021-12-22 12:31:37 +05:00
|
|
|
|
public async Task<IActionResult> GetHisoryAsync([FromRoute] int idWell, int? idCategory = null, CancellationToken token = default)
|
2021-08-27 17:55:22 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
2021-08-24 10:59:10 +05:00
|
|
|
|
return Forbid();
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
2021-12-07 13:43:47 +05:00
|
|
|
|
var result = await measureService.GetHisoryAsync(idWell, idCategory, token).ConfigureAwait(false);
|
2021-08-03 17:55:28 +05:00
|
|
|
|
return Ok(result);
|
2021-08-02 11:39:39 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-28 20:00:04 +05:00
|
|
|
|
public async Task<IActionResult> InsertAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token = default)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
2021-08-27 17:55:22 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
2021-08-31 09:52:15 +05:00
|
|
|
|
var result = await measureService.InsertAsync(idWell, data, token).ConfigureAwait(false);
|
2021-08-27 17:55:22 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-28 20:00:04 +05:00
|
|
|
|
public async Task<IActionResult> UpdateAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token = default)
|
2021-08-27 17:55:22 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
2021-08-24 10:59:10 +05:00
|
|
|
|
return Forbid();
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
2021-08-31 09:52:15 +05:00
|
|
|
|
var result = await measureService.UpdateAsync(idWell, data, token).ConfigureAwait(false);
|
2021-08-27 17:55:22 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
2022-01-19 11:42:26 +05:00
|
|
|
|
[Permission]
|
2021-08-31 09:52:15 +05:00
|
|
|
|
[Route("history/{idData}")]
|
2021-08-27 17:55:22 +05:00
|
|
|
|
public async Task<IActionResult> MarkAsDeleteAsync([FromRoute] int idWell, [FromRoute] int idData, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2021-08-31 09:52:15 +05:00
|
|
|
|
var result = await measureService.MarkAsDeleteAsync(idWell, idData, token).ConfigureAwait(false);
|
2021-08-27 17:55:22 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
|
|
|
|
idWell, token).ConfigureAwait(false);
|
2021-08-02 11:39:39 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|