forked from ddrilling/AsbCloudServer
106 lines
4.0 KiB
C#
106 lines
4.0 KiB
C#
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")]
|
|
public class MeasureController: ControllerBase
|
|
{
|
|
private readonly IMeasureService lastDataService;
|
|
private readonly IWellService wellService;
|
|
|
|
public MeasureController(IMeasureService lastDataService, IWellService wellService)
|
|
{
|
|
this.lastDataService = lastDataService;
|
|
this.wellService = wellService;
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("categories")]
|
|
public async Task<IActionResult> GetCategoriesAsync([FromRoute] int idWell, CancellationToken token = default)
|
|
{
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
return Forbid();
|
|
|
|
var result = await lastDataService.GetCategoriesAsync(token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("lastAll")]
|
|
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);
|
|
}
|
|
|
|
[HttpGet]
|
|
[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 lastDataService.GetLastAsync(idWell, idCategory, token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("history/{idCategory}")]
|
|
public async Task<IActionResult> GetHisoryAsync([FromRoute] int idWell, [FromRoute] int idCategory, CancellationToken token = default)
|
|
{
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
return Forbid();
|
|
|
|
var result = await lastDataService.GetHisoryAsync(idWell, idCategory, token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> InsertAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token = default)
|
|
{
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
return Forbid();
|
|
|
|
var result = await lastDataService.InsertAsync(idWell, data, token).ConfigureAwait(false);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPut]
|
|
public async Task<IActionResult> UpdateAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token = default)
|
|
{
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
return Forbid();
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|