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

105 lines
4.2 KiB
C#
Raw Normal View History

2021-08-02 14:45:13 +05:00
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2021-08-10 17:43:13 +05:00
using System.Collections.Generic;
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
}
[HttpGet]
2021-08-13 12:33:05 +05:00
[Route("types")]
[ProducesResponseType(typeof(string[]), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetTypesAsync(CancellationToken token = default)
{
var result = await sectionsService.GetTypesAsync(token).ConfigureAwait(false);
return Ok(result);
}
[HttpGet]
[ProducesResponseType(typeof(PaginationContainer<WellSectionDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync(int idWell, int skip = 0, int take = 32,
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.GetAllByWellIdAsync(idWell, skip, take, 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.GetAsync(idSection, token).ConfigureAwait(false);
2021-08-10 16:37:36 +05:00
return Ok(result);
}
2021-08-10 14:36:35 +05:00
[HttpPost]
2021-08-12 11:52:23 +05:00
[ProducesResponseType(typeof(IEnumerable<WellSectionDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> InsertAsync(int idWell, [FromBody] IEnumerable<WellSectionDto> values,
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();
2021-08-12 11:50:04 +05:00
var result = await sectionsService.InsertRangeAsync(idWell, values, token).ConfigureAwait(false);
2021-08-10 14:36:35 +05:00
return Ok(result);
}
2021-08-12 11:50:04 +05:00
[HttpPut("{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> UpdateAsync(int idWell, int idSection, [FromBody] WellSectionDto value, 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();
2021-08-12 11:50:04 +05:00
var result = await sectionsService.UpdateAsync(idWell, idSection, value, token).ConfigureAwait(false);
2021-08-10 14:36:35 +05:00
return Ok(result);
}
2021-08-12 11:50:04 +05:00
[HttpDelete("{idSection}")]
2021-08-12 11:52:23 +05:00
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
2021-08-13 12:33:05 +05:00
public async Task<IActionResult> DeleteAsync(int idWell, int idItem, CancellationToken token = default)
2021-08-02 14:45:13 +05:00
{
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))
return Forbid();
2021-08-10 17:43:13 +05:00
2021-08-13 12:33:05 +05:00
var result = await sectionsService.DeleteAsync(new int[] { idItem }, token).ConfigureAwait(false);
2021-08-02 14:45:13 +05:00
return Ok(result);
}
2021-08-10 17:43:13 +05:00
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
}
}