forked from ddrilling/AsbCloudServer
102 lines
3.6 KiB
C#
102 lines
3.6 KiB
C#
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Requests;
|
|
using AsbCloudApp.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudWebApi.Controllers.SAUB
|
|
{
|
|
/// <summary>
|
|
/// Сообщения панели бурильщика
|
|
/// </summary>
|
|
[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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Выдает список сообщений по скважине
|
|
/// </summary>
|
|
/// <param name="idWell">id скважины</param>
|
|
/// <param name="request">Параметры запроса</param>
|
|
/// <param name="token">Токен для отмены задачи</param>
|
|
/// <returns>список сообщений по скважине</returns>
|
|
[HttpGet]
|
|
[Permission]
|
|
[ProducesResponseType(typeof(PaginationContainer<MessageDto>), (int)System.Net.HttpStatusCode.OK)]
|
|
public async Task<IActionResult> GetMessagesAsync(
|
|
[FromRoute] int idWell,
|
|
[FromQuery] MessageRequestBase request,
|
|
CancellationToken token = default)
|
|
{
|
|
|
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
|
return Forbid();
|
|
|
|
if (request.Take > 1024)
|
|
return BadRequest("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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Выдает список сообщений по скважине
|
|
/// </summary>
|
|
/// <param name="idWell">id скважины</param>
|
|
/// <param name="token">Токен для отмены задачи</param>
|
|
/// <returns>список сообщений по скважине</returns>
|
|
[HttpGet]
|
|
[Route("datesRange")]
|
|
[Permission]
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
|
public async Task<IActionResult> GetMessagesDateRangeAsync(int idWell, CancellationToken token = default)
|
|
{
|
|
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<bool> 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;
|
|
}
|
|
}
|
|
}
|