DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WellCompositeController.cs
ngfrolov 6512a7e752 doc
2022-06-16 17:37:10 +05:00

72 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
/// <summary>
/// Композитная скважина
/// </summary>
[Route("api/well/{idWell}/composite")]
[ApiController]
[Authorize]
public class WellCompositeController : ControllerBase
{
private readonly IWellCompositeService wellCompositeService;
private readonly IWellService wellService;
public WellCompositeController(IWellCompositeService wellCompositeService, IWellService wellService)
{
this.wellCompositeService = wellCompositeService;
this.wellService = wellService;
}
/// <summary>
/// Получает композитную скважину для скважины
/// </summary>
/// <param name="idWell">id скважины для которой собрана композитная скважина</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(IEnumerable<WellCompositeDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellCompositeService.GetAsync(idWell, token).ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Создает или заменяет композитную скважину для скважины с idWell
/// </summary>
/// <param name="idWell">id скважины для которой собрана композитная скважина</param>
/// <param name="wellComposites">Секции композитной скважины</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[Permission]
public async Task<IActionResult> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token = default)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellCompositeService.SaveAsync(idWell, wellComposites, 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);
}
}
}