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-28 20:00:04 +05:00
|
|
|
|
private readonly IMeasureService lastDataService;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
2021-08-28 20:00:04 +05:00
|
|
|
|
public MeasureController(IMeasureService lastDataService, IWellService wellService)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
|
|
|
|
this.lastDataService = lastDataService;
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
var result = await lastDataService.GetCategoriesAsync(token).ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("last")]
|
|
|
|
|
public async Task<IActionResult> GetAllLastAsync([FromRoute] int idWell, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await lastDataService.GetAllLastAsync(idWell, token).ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
2021-08-27 17:55:22 +05:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{idCategory}/history")]
|
|
|
|
|
public async Task<IActionResult> GetHisoryAsync([FromRoute] int idWell, [FromQuery] int idCategory, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
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-27 17:55:22 +05:00
|
|
|
|
var result = await lastDataService.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]
|
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-27 17:55:22 +05:00
|
|
|
|
var result = await lastDataService.InsertAsync(idWell, data, token).ConfigureAwait(false);
|
|
|
|
|
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-27 17:55:22 +05:00
|
|
|
|
var result = await lastDataService.UpdateAsync(idWell, data, token).ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
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 lastDataService.MarkAsDeleteAsync(idWell, idData, token).ConfigureAwait(false);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|