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

98 lines
3.6 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
2021-04-07 18:01:56 +05:00
[Route("api/well")]
[ApiController]
[Authorize]
public class WellController : ControllerBase
{
private readonly IWellService wellService;
public WellController(IWellService wellService)
{
this.wellService = wellService;
}
/// <summary>
/// Возвращает список доступных скважин
/// </summary>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Список скважин</returns>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<WellDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetWellsAsync(CancellationToken token = default)
{
2021-07-21 15:22:58 +05:00
var idCompany = User.GetCompanyId();
2021-07-21 15:22:58 +05:00
if (idCompany is null)
{
return NoContent();
}
var wells = await wellService.GetWellsByCompanyAsync((int)idCompany,
token).ConfigureAwait(false);
if (wells is null || !wells.Any())
return NoContent();
return Ok(wells);
}
/// <summary>
/// Возвращает информацию о требуемой скважине
/// </summary>
/// <param name="idWell"> Id требуемой скважины </param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns>Информация о требуемой скважине </returns>
[HttpGet("getWell")]
[ProducesResponseType(typeof(WellDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
2021-10-20 17:55:50 +05:00
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync(idCompany??default, idWell, token).ConfigureAwait(false))
return NoContent();
var well = await wellService.GetAsync(idWell,
token).ConfigureAwait(false);
return Ok(well);
}
/// <summary>
/// Редактирует указанные поля скважины
/// </summary>
/// <param name="idWell"> Id скважины </param>
/// <param name="dto"> Объект параметров скважины.
/// IdWellType: 1 - Наклонно-направленная, 2 - Горизонтальная.
/// State: 0 - Неизвестно, 1 - В работе, 2 - Завершена.</param>
/// <param name="token"> Токен отмены задачи </param>
/// <returns></returns>
[HttpPut]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
2021-12-21 11:52:53 +05:00
public async Task<IActionResult> UpdateWellAsync(int idWell, WellDto dto,
CancellationToken token = default)
{
var idCompany = User.GetCompanyId();
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false))
return Forbid();
2021-12-21 11:52:53 +05:00
var result = await wellService.UpdateAsync(idWell, dto, token)
.ConfigureAwait(false);
return Ok(result);
}
}
}