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")]
|
|
|
|
|
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]
|
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]
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-10-05 18:02:02 +05:00
|
|
|
|
[Route("history")]
|
2021-08-28 22:34:57 +05:00
|
|
|
|
public async Task<IActionResult> GetHisoryAsync([FromRoute] int idWell, [FromRoute] int idCategory, 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-10-05 18:02:02 +05:00
|
|
|
|
var result = await measureService.GetHisoryAsync(idWell, token).ConfigureAwait(false);
|
2021-08-03 17:55:28 +05:00
|
|
|
|
return Ok(result);
|
2021-08-02 11:39:39 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
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]
|
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]
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|