using AsbCloudApp.Data;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers.SAUB;
///
/// Сообщения панели бурильщика
///
[Route("api/well/{idWell}/message")]
[ApiController]
public class MessageController : ControllerBase
{
private readonly IMessageService messageService;
private readonly IWellService wellService;
public MessageController(IMessageService messageService, IWellService wellService)
{
this.messageService = messageService;
this.wellService = wellService;
}
///
/// Выдает список сообщений по скважине
///
/// id скважины
/// Параметры запроса
/// Токен для отмены задачи
/// список сообщений по скважине
[HttpGet]
[Permission]
[ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public async Task GetMessagesAsync(
[FromRoute] int idWell,
[FromQuery] MessageRequestBase request,
CancellationToken token)
{
if (!await UserHasAccesToWellAsync(idWell, token))
return Forbid();
if (request.Take > 1024)
return this.ValidationBadRequest(nameof(request.Take), "limit mast be less then 1024");
var requestToService = new MessageRequest(request, idWell);
var result = await messageService.GetMessagesAsync(
requestToService,
token)
.ConfigureAwait(false);
if (result is null || result.Count == 0)
return NoContent();
return Ok(result);
}
///
/// Выдает список сообщений по скважине
///
/// id скважины
/// Токен для отмены задачи
/// список сообщений по скважине
[HttpGet("datesRange")]
[Permission]
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetMessagesDateRangeAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
idWell, token).ConfigureAwait(false);
if (!isCompanyOwnsWell)
return Forbid();
var wellMessagesDatesRange = wellService.GetDatesRange(idWell);
return Ok(wellMessagesDatesRange);
}
protected async Task UserHasAccesToWellAsync(int idWell, CancellationToken token)
{
var idCompany = User.GetCompanyId();
if (idCompany is not null &&
await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token)
.ConfigureAwait(false))
return true;
return false;
}
}