2024-07-04 11:02:45 +05:00
|
|
|
using AsbCloudApp.Data;
|
2023-02-02 11:08:25 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2021-04-23 10:21:25 +05:00
|
|
|
using AsbCloudApp.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-11 16:54:42 +05:00
|
|
|
using System.Threading;
|
2021-08-11 12:11:21 +05:00
|
|
|
using System.Threading.Tasks;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
namespace AsbCloudWebApi.Controllers.SAUB
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
2022-06-16 17:37:10 +05:00
|
|
|
/// <summary>
|
|
|
|
/// Сообщения панели бурильщика
|
|
|
|
/// </summary>
|
2021-08-17 09:39:05 +05:00
|
|
|
[Route("api/well/{idWell}/message")]
|
2021-04-23 10:21:25 +05:00
|
|
|
[ApiController]
|
|
|
|
public class MessageController : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly IMessageService messageService;
|
2021-05-14 17:02:29 +05:00
|
|
|
private readonly IWellService wellService;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2021-05-14 17:02:29 +05:00
|
|
|
public MessageController(IMessageService messageService, IWellService wellService)
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
|
|
|
this.messageService = messageService;
|
2021-05-14 17:02:29 +05:00
|
|
|
this.wellService = wellService;
|
2021-04-23 10:21:25 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Выдает список сообщений по скважине
|
|
|
|
/// </summary>
|
2021-07-27 14:43:30 +05:00
|
|
|
/// <param name="idWell">id скважины</param>
|
2023-02-02 11:08:25 +05:00
|
|
|
/// <param name="request">Параметры запроса</param>
|
2021-08-11 16:54:42 +05:00
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
2021-04-23 10:21:25 +05:00
|
|
|
/// <returns>список сообщений по скважине</returns>
|
|
|
|
[HttpGet]
|
2022-01-19 11:42:26 +05:00
|
|
|
[Permission]
|
2021-04-23 10:21:25 +05:00
|
|
|
[ProducesResponseType(typeof(PaginationContainer<MessageDto>), (int)System.Net.HttpStatusCode.OK)]
|
2023-09-28 17:42:30 +05:00
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
|
2023-02-02 11:08:25 +05:00
|
|
|
public async Task<IActionResult> GetMessagesAsync(
|
|
|
|
[FromRoute] int idWell,
|
|
|
|
[FromQuery] MessageRequestBase request,
|
2023-07-03 14:33:34 +05:00
|
|
|
CancellationToken token)
|
2021-04-23 10:21:25 +05:00
|
|
|
{
|
2023-02-02 11:08:25 +05:00
|
|
|
|
|
|
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
if (request.Take > 1024)
|
2023-09-28 17:42:30 +05:00
|
|
|
return this.ValidationBadRequest(nameof(request.Take), "limit mast be less then 1024");
|
2021-04-23 10:21:25 +05:00
|
|
|
|
2023-02-02 11:08:25 +05:00
|
|
|
var requestToService = new MessageRequest(request, idWell);
|
|
|
|
|
|
|
|
var result = await messageService.GetMessagesAsync(
|
|
|
|
requestToService,
|
|
|
|
token)
|
|
|
|
.ConfigureAwait(false);
|
2021-07-02 15:02:56 +05:00
|
|
|
|
|
|
|
if (result is null || result.Count == 0)
|
|
|
|
return NoContent();
|
|
|
|
|
2021-04-23 10:21:25 +05:00
|
|
|
return Ok(result);
|
|
|
|
}
|
2021-05-13 17:50:37 +05:00
|
|
|
|
2021-08-11 16:54:42 +05:00
|
|
|
/// <summary>
|
|
|
|
/// Выдает список сообщений по скважине
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="idWell">id скважины</param>
|
|
|
|
/// <param name="token">Токен для отмены задачи</param>
|
|
|
|
/// <returns>список сообщений по скважине</returns>
|
2023-08-08 12:21:09 +05:00
|
|
|
[HttpGet("datesRange")]
|
2022-01-19 11:42:26 +05:00
|
|
|
[Permission]
|
2021-05-14 17:02:29 +05:00
|
|
|
[ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)]
|
2023-07-03 14:33:34 +05:00
|
|
|
public async Task<IActionResult> GetMessagesDateRangeAsync(int idWell, CancellationToken token)
|
2021-05-13 17:50:37 +05:00
|
|
|
{
|
2021-07-21 15:22:58 +05:00
|
|
|
int? idCompany = User.GetCompanyId();
|
2021-05-14 17:02:29 +05:00
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
if (idCompany is null)
|
2021-06-28 10:31:18 +05:00
|
|
|
return Forbid();
|
2021-05-14 17:02:29 +05:00
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
2021-08-11 17:26:02 +05:00
|
|
|
idWell, token).ConfigureAwait(false);
|
2021-07-21 15:29:19 +05:00
|
|
|
|
2021-07-21 15:22:58 +05:00
|
|
|
if (!isCompanyOwnsWell)
|
2021-05-14 17:02:29 +05:00
|
|
|
return Forbid();
|
2022-04-11 18:00:34 +05:00
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
var wellMessagesDatesRange = wellService.GetDatesRange(idWell);
|
2021-05-13 17:50:37 +05:00
|
|
|
|
|
|
|
return Ok(wellMessagesDatesRange);
|
|
|
|
}
|
2023-02-02 11:08:25 +05:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-04-23 10:21:25 +05:00
|
|
|
}
|
|
|
|
}
|