2023-12-13 18:06:48 +05:00
|
|
|
|
using System;
|
2023-12-04 17:07:32 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Data.ProcessMaps;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
2023-12-13 18:06:48 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-12-04 17:07:32 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
2023-12-13 18:06:48 +05:00
|
|
|
|
namespace AsbCloudWebApi.Controllers.ProcessMaps;
|
2023-12-04 17:07:32 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструкция скважины - план
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/well/{idWell:int}/[controller]")]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class WellSectionPlanController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellService wellService;
|
2023-12-13 18:06:48 +05:00
|
|
|
|
private readonly IWellSectionPlanRepository wellSectionPlanRepository;
|
2023-12-04 17:07:32 +05:00
|
|
|
|
private readonly ICrudRepository<WellSectionTypeDto> wellSectionRepository;
|
|
|
|
|
|
2023-12-13 18:06:48 +05:00
|
|
|
|
public WellSectionPlanController(IWellService wellService,
|
|
|
|
|
IWellSectionPlanRepository wellSectionPlanRepository,
|
|
|
|
|
ICrudRepository<WellSectionTypeDto> wellSectionRepository)
|
2023-12-04 17:07:32 +05:00
|
|
|
|
{
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
this.wellSectionPlanRepository = wellSectionPlanRepository;
|
2023-12-13 18:06:48 +05:00
|
|
|
|
this.wellSectionRepository = wellSectionRepository;
|
2023-12-04 17:07:32 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: так же следует вынести в базовый контроллер
|
|
|
|
|
private int IdUser
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var idUser = User.GetUserId();
|
|
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
|
throw new ForbidException("Неизвестный пользователь");
|
|
|
|
|
|
|
|
|
|
return idUser.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить секцию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Идентификатор скважины</param>
|
|
|
|
|
/// <param name="wellSection">Секция скважины - план</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<IActionResult> InsertAsync(int idWell, WellSectionPlanDto wellSection, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
wellSection.IdWell = idWell;
|
|
|
|
|
wellSection.IdUser = IdUser;
|
|
|
|
|
|
|
|
|
|
await CheckIsExistsWellSectionTypeAsync(wellSection.IdSectionType, cancellationToken);
|
|
|
|
|
|
|
|
|
|
await AssertUserAccessToWell(idWell, cancellationToken);
|
|
|
|
|
|
2023-12-13 18:06:48 +05:00
|
|
|
|
var wellSectionId = await wellSectionPlanRepository.InsertAsync(wellSection, cancellationToken);
|
2023-12-04 17:07:32 +05:00
|
|
|
|
|
|
|
|
|
return Ok(wellSectionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Обновить секцию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Идентификатор скважины</param>
|
|
|
|
|
/// <param name="wellSection">Секция скважины - план</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPut]
|
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<IActionResult> UpdateAsync(int idWell, WellSectionPlanDto wellSection, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
wellSection.IdWell = idWell;
|
|
|
|
|
wellSection.IdUser = IdUser;
|
2023-12-13 18:06:48 +05:00
|
|
|
|
wellSection.LastUpdateDate = DateTimeOffset.UtcNow;
|
|
|
|
|
|
2023-12-04 17:07:32 +05:00
|
|
|
|
await CheckIsExistsWellSectionTypeAsync(wellSection.IdSectionType, cancellationToken);
|
|
|
|
|
|
|
|
|
|
await AssertUserAccessToWell(idWell, cancellationToken);
|
|
|
|
|
|
2023-12-13 18:06:48 +05:00
|
|
|
|
var wellSectionId = await wellSectionPlanRepository.UpdateAsync(wellSection, cancellationToken);
|
2023-12-04 17:07:32 +05:00
|
|
|
|
|
|
|
|
|
if (wellSectionId == ICrudRepository<WellSectionPlanDto>.ErrorIdNotFound)
|
|
|
|
|
return this.ValidationBadRequest(nameof(wellSection.Id), $"Секции скважины с Id: {wellSection.Id} не существует");
|
|
|
|
|
|
|
|
|
|
return Ok(wellSectionId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить типы секций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Идентификатор скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-12-06 17:55:19 +05:00
|
|
|
|
[HttpGet("wellSectionTypes")]
|
2023-12-04 17:07:32 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<WellSectionTypeDto>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<IActionResult> GetWellSectionTypesAsync(int idWell, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserAccessToWell(idWell, cancellationToken);
|
|
|
|
|
|
2023-12-13 18:06:48 +05:00
|
|
|
|
var wellSectionTypes = await wellSectionPlanRepository.GetWellSectionTypesAsync(idWell, cancellationToken);
|
2023-12-04 17:07:32 +05:00
|
|
|
|
|
|
|
|
|
if (!wellSectionTypes.Any())
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
return Ok(wellSectionTypes);
|
|
|
|
|
}
|
2023-12-06 17:55:19 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить список секций
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Идентификатор скважины</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<WellSectionPlanDto>), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<IActionResult> GetPlanWellSectionsAsync(int idWell, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserAccessToWell(idWell, cancellationToken);
|
|
|
|
|
|
|
|
|
|
var planWellSections = await wellSectionPlanRepository.GetByIdWellAsync(idWell, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (!planWellSections.Any())
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
return Ok(planWellSections);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 17:07:32 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удалить секцию
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">Идентификатор скважины</param>
|
|
|
|
|
/// <param name="id">Идентификатор плановой секции</param>
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
public async Task<IActionResult> DeleteAsync(int idWell, int id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await AssertUserAccessToWell(idWell, cancellationToken);
|
|
|
|
|
|
|
|
|
|
var deletedWellSectionPlanCount = await wellSectionPlanRepository.DeleteAsync(id, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return Ok(deletedWellSectionPlanCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: нужно создать базовый контроллер связанный со скважиной и вынести этот метод туда. Данный метод много где дублируется
|
|
|
|
|
private async Task AssertUserAccessToWell(int idWell, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (!idCompany.HasValue || !await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
|
|
|
|
|
throw new ForbidException("Нет доступа к скважине");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: тоже нужно вынести в базовый контроллер
|
|
|
|
|
private async Task CheckIsExistsWellSectionTypeAsync(int idWellSectionType, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_ = await wellSectionRepository.GetOrDefaultAsync(idWellSectionType, cancellationToken)
|
|
|
|
|
?? throw new ArgumentInvalidException(nameof(ProcessMapPlanWellDrillingDto.IdWellSectionType),
|
|
|
|
|
$"Тип секции с Id: {idWellSectionType} не найден");
|
|
|
|
|
}
|
|
|
|
|
}
|