DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/ProcessMapWellboreDevelopmentController.cs
Степанов Дмитрий 67113878a3 Рефакторинг
1. Добавил проверку при удалении РТК
2. Сделал небольшой рефакторинг контроллеров
2023-10-05 09:33:38 +05:00

107 lines
4.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using Microsoft.AspNetCore.Authorization;
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// Проработка скважины
/// </summary>
public class ProcessMapWellboreDevelopmentController : CrudWellRelatedController<ProcessMapWellboreDevelopmentDto,
IProcessMapWellboreDevelopmentRepository>
{
private readonly IUserRepository userRepository;
private readonly IProcessMapWellboreDevelopmentService processMapWellboreDevelopmentService;
public ProcessMapWellboreDevelopmentController(IWellService wellService,
IProcessMapWellboreDevelopmentRepository processMapWellboreDevelopmentRepository,
IUserRepository userRepository,
IProcessMapWellboreDevelopmentService processMapWellboreDevelopmentService)
: base(wellService, processMapWellboreDevelopmentRepository)
{
this.userRepository = userRepository;
this.processMapWellboreDevelopmentService = processMapWellboreDevelopmentService;
}
/// <summary>
/// Добавить запись проработки
/// </summary>
/// <param name="value"></param>
/// <param name="token"></param>
/// <returns></returns>
public override async Task<ActionResult<int>> InsertAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token)
{
value.IdUser = User.GetUserId()
?? throw new ForbidException("Неизвестный пользователь");
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, token);
return await processMapWellboreDevelopmentService.InsertAsync(value, token);
}
/// <summary>
/// Обновить запись проработки
/// </summary>
/// <param name="value"></param>
/// <param name="token"></param>
/// <returns></returns>
public override async Task<ActionResult<int>> UpdateAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token)
{
value.IdUser = User.GetUserId()
?? throw new ForbidException("Неизвестный пользователь");
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, token);
return await processMapWellboreDevelopmentService.UpdateAsync(value, token);
}
public override async Task<ActionResult<int>> DeleteAsync(int id, CancellationToken token)
{
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(id, token);
return await base.DeleteAsync(id, token);
}
/// <summary>
/// Возвращает проработки по uid телеметрии
/// </summary>
/// <param name="uid">Уникальный ключ телеметрии</param>
/// <param name="updateFrom">Необязательный параметр. Начальная дата</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("telemetry/{uid}")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<ProcessMapWellboreDevelopmentDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken)
{
var dto = await processMapWellboreDevelopmentService.GetByTelemetryAsync(uid, updateFrom,
cancellationToken);
return Ok(dto);
}
private async Task AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(int idWell, CancellationToken cancellationToken)
{
var idUser = User.GetUserId();
var idCompany = User.GetCompanyId();
if (!idCompany.HasValue || !idUser.HasValue)
throw new ForbidException("Неизвестный пользователь");
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken)
?? throw new ForbidException($"Скважины с {idWell} не существует");
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
throw new ForbidException("Нет доступа к скважине");
if (well.IdState == 2 && !userRepository.HasPermission(idUser.Value, "ProcessMap.editCompletedWell"))
throw new ForbidException("Недостаточно прав для редактирования РТК завершённой скважины");
}
}