2021-08-16 14:19:43 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
2021-08-13 17:25:06 +05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2021-08-16 14:19:43 +05:00
|
|
|
|
/// Контроллер вручную внесенных операций на скважине
|
2021-08-13 17:25:06 +05:00
|
|
|
|
/// </summary>
|
2021-08-16 14:19:43 +05:00
|
|
|
|
[Route("api/wellOperations")]
|
2021-08-13 17:25:06 +05:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class WellOperationController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellOperationService operationService;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
|
|
|
|
public WellOperationController(IWellOperationService operationService, IWellService wellService)
|
|
|
|
|
{
|
|
|
|
|
this.operationService = operationService;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 14:19:43 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает список имен типов операций на скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-08-13 17:25:06 +05:00
|
|
|
|
[HttpGet]
|
2021-08-16 14:19:43 +05:00
|
|
|
|
[Route("types")]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<string>), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-17 09:20:31 +05:00
|
|
|
|
public IActionResult GetTypes()
|
2021-08-16 14:19:43 +05:00
|
|
|
|
{
|
2021-08-17 09:20:31 +05:00
|
|
|
|
var result = operationService.GetCategories();
|
2021-08-16 14:19:43 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает весь список операций на скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="skip">Для пагинации кол-во записей пропустить</param>
|
|
|
|
|
/// <param name="take">Для пагинации кол-во записей</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
|
|
|
|
/// <returns>Список операций на скважине</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(PaginationContainer<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
|
2021-08-13 17:25:06 +05:00
|
|
|
|
public async Task<IActionResult> GetAllAsync(int idWell, int skip = 0, int take = 32,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await operationService.GetAllByWellIdAsync(idWell, skip, take, token).ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-16 14:19:43 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращает нужную операцию на скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="idOperation">id нужной операции</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
|
|
|
|
/// <returns>Нужную операцию на скважине</returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{idOperation}")]
|
|
|
|
|
[ProducesResponseType(typeof(WellOperationDto), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> GetAsync(int idWell, int idOperation,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await operationService.GetAsync(idOperation, token).ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавляет новые операции на скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="values">Данные о добавляемых операциях</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
|
|
|
|
/// <returns>Количество добавленых в БД строк</returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<WellOperationDto>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> InsertRangeAsync(int idWell, [FromBody] IEnumerable<WellOperationDto> values,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await operationService.InsertRangeAsync(idWell, values, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновляет выбранную операцию на скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="idOperation">id выбраной операции</param>
|
|
|
|
|
/// <param name="value">Новые данные для выбраной операции</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
|
|
|
|
/// <returns>Количество обновленных в БД строк</returns>
|
|
|
|
|
[HttpPut("{idOperation}")]
|
|
|
|
|
[ProducesResponseType(typeof(WellOperationDto), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> UpdateAsync(int idWell, int idOperation,
|
|
|
|
|
[FromBody] WellOperationDto value, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await operationService.UpdateAsync(idWell, idOperation, value, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаляет выбраную операцию на скважине
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
|
/// <param name="idOperation">id выбраной операции</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
|
|
|
|
/// <returns>Количество удаленных из БД строк</returns>
|
|
|
|
|
[HttpDelete("{idOperation}")]
|
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> DeleteAsync(int idWell, int idOperation, CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell,
|
|
|
|
|
token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await operationService.DeleteAsync(new int[] { idOperation }, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-13 17:25:06 +05:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|