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-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-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> GetAllAsync(int idWell, int skip = 0, int take = 32,
|
|
|
|
|
CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-08-11 17:26:02 +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-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> GetAsync(int idSection, int idWell,
|
|
|
|
|
CancellationToken token = default)
|
2021-08-10 16:37:36 +05:00
|
|
|
|
{
|
2021-08-11 17:26:02 +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-11 16:54:42 +05:00
|
|
|
|
public async Task<IActionResult> InsertAsync(int idWell, [FromBody] IEnumerable<WellSectionDto> values,
|
|
|
|
|
CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-08-11 17:26:02 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
2021-08-10 17:43:13 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await sectionsService.InsertRangeAsync(values, idWell, token).ConfigureAwait(false);
|
2021-08-10 14:36:35 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
2021-08-11 12:11:21 +05:00
|
|
|
|
public async Task<IActionResult> PutAsync(int id, int idWell, [FromBody] WellSectionDto value, CancellationToken token = default)
|
2021-08-10 14:36:35 +05:00
|
|
|
|
{
|
2021-08-11 17:26:02 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
2021-08-10 17:43:13 +05:00
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await sectionsService.UpdateAsync(value, id, idWell, token).ConfigureAwait(false);
|
2021-08-10 14:36:35 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
2021-08-11 12:11:21 +05:00
|
|
|
|
public async Task<IActionResult> DeleteAsync(int id, int idWell, CancellationToken token = default)
|
2021-08-02 14:45:13 +05:00
|
|
|
|
{
|
2021-08-11 17:26:02 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
2021-08-10 17:43:13 +05:00
|
|
|
|
|
|
|
|
|
var result = await sectionsService.DeleteAsync(new int[] { id }, token).ConfigureAwait(false);
|
2021-08-02 14:45:13 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
2021-08-10 17:43:13 +05:00
|
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
|
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
2021-08-10 17:43:13 +05:00
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-08-11 17:26:02 +05:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|