DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WellSectionController.cs

62 lines
2.2 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using AsbCloudApp.Data;
2021-08-02 14:45:13 +05:00
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2021-08-10 14:36:35 +05:00
using System.Threading;
using System.Threading.Tasks;
2021-08-02 14:45:13 +05:00
namespace AsbCloudWebApi.Controllers
{
2021-08-12 11:52:23 +05:00
/// <summary>
/// Контроллер секций скважины
/// </summary>
2021-08-10 14:36:35 +05:00
[Route("api/well/{idWell}/sections")]
2021-08-02 14:45:13 +05:00
[ApiController]
[Authorize]
2021-08-10 14:36:35 +05:00
public class WellSectionController : ControllerBase
2021-08-02 14:45:13 +05:00
{
2021-08-10 17:43:13 +05:00
private readonly IWellSectionService sectionsService;
private readonly IWellService wellService;
2021-08-02 14:45:13 +05:00
2021-08-10 17:43:13 +05:00
public WellSectionController(IWellSectionService sectionsService, IWellService wellService)
2021-08-10 14:36:35 +05:00
{
2021-08-10 17:43:13 +05:00
this.sectionsService = sectionsService;
this.wellService = wellService;
2021-08-02 14:45:13 +05:00
}
2021-08-13 12:33:05 +05:00
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<WellSectionDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetSectionsByWellIdAsync(int idWell,
CancellationToken token = default)
2021-08-10 14:36:35 +05:00
{
if(!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
2021-08-10 17:43:13 +05:00
return Forbid();
var result = await sectionsService.GetSectionsByWellIdAsync(idWell, token)
.ConfigureAwait(false);
2021-08-10 14:36:35 +05:00
return Ok(result);
}
2021-08-10 16:37:36 +05:00
[HttpGet]
[Route("{idSection}")]
2021-08-12 11:52:23 +05:00
[ProducesResponseType(typeof(WellSectionDto), (int)System.Net.HttpStatusCode.OK)]
2021-08-12 11:50:04 +05:00
public async Task<IActionResult> GetAsync(int idWell, int idSection,
CancellationToken token = default)
2021-08-10 16:37:36 +05:00
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
2021-08-10 17:43:13 +05:00
return Forbid();
var result = await sectionsService.GetSectionByWellIdAsync(idSection, token).ConfigureAwait(false);
2021-08-10 16:37:36 +05:00
return Ok(result);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
2021-08-10 17:43:13 +05:00
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
2021-08-10 17:43:13 +05:00
}
2021-08-02 14:45:13 +05:00
}
}