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

109 lines
4.1 KiB
C#
Raw Normal View History

2021-08-27 17:55:22 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
[ApiController]
[Authorize]
[Route("api/well/{idWell}/measure")]
2022-04-11 18:00:34 +05:00
public class MeasureController : ControllerBase
{
private readonly IMeasureService measureService;
private readonly IWellService wellService;
public MeasureController(IMeasureService measureService, IWellService wellService)
{
this.measureService = measureService;
this.wellService = wellService;
}
[HttpGet]
[Permission]
2021-08-27 17:55:22 +05:00
[Route("categories")]
public async Task<IActionResult> GetCategoriesAsync([FromRoute] int idWell, CancellationToken token = default)
{
2021-08-27 17:55:22 +05:00
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await measureService.GetCategoriesAsync(token).ConfigureAwait(false);
2021-08-27 17:55:22 +05:00
return Ok(result);
}
[HttpGet]
[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();
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>
/// <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]
[Permission]
[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))
return Forbid();
var result = await measureService.GetHisoryAsync(idWell, idCategory, token).ConfigureAwait(false);
return Ok(result);
}
[HttpPost]
[Permission]
public async Task<IActionResult> InsertAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token = default)
{
2021-08-27 17:55:22 +05:00
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await measureService.InsertAsync(idWell, data, token).ConfigureAwait(false);
2021-08-27 17:55:22 +05:00
return Ok(result);
}
[HttpPut]
[Permission]
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))
return Forbid();
var result = await measureService.UpdateAsync(idWell, data, token).ConfigureAwait(false);
2021-08-27 17:55:22 +05:00
return Ok(result);
}
[HttpDelete]
[Permission]
[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();
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);
}
}
}