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 measureService; private readonly IWellService wellService; public MeasureController(IMeasureService measureService, IWellService wellService) { this.measureService = measureService; this.wellService = wellService; } [HttpGet("categories")] [Permission] public async Task GetCategoriesAsync([FromRoute] int idWell, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await measureService.GetCategoriesAsync(token).ConfigureAwait(false); return Ok(result); } [HttpGet("last/{idCategory}")] [Permission] public async Task GetLastAsync([FromRoute] int idWell, [FromRoute] int idCategory, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await measureService.GetLastOrDefaultAsync(idWell, idCategory, token).ConfigureAwait(false); return Ok(result); } /// /// История замеров по скважине /// /// /// Категория скважины. Не обязательный параметр. /// /// [HttpGet("history")] [Permission] public async Task GetHisoryAsync([FromRoute] int idWell, CancellationToken token, int? idCategory = null) { 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 InsertAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await measureService.InsertAsync(idWell, data, token).ConfigureAwait(false); return Ok(result); } [HttpPut] [Permission] public async Task UpdateAsync([FromRoute] int idWell, MeasureDto data, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await measureService.UpdateAsync(idWell, data, token).ConfigureAwait(false); return Ok(result); } [HttpDelete("history/{idData}")] [Permission] public async Task MarkAsDeleteAsync([FromRoute] int idWell, [FromRoute] int idData, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await measureService.MarkAsDeleteAsync(idWell, idData, token).ConfigureAwait(false); return Ok(result); } private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token) { int? idCompany = User.GetCompanyId(); return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); } }