2024-07-04 11:02:45 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2023-10-09 12:20:00 +05:00
|
|
|
|
using AsbCloudApp.Data.User;
|
2023-06-21 11:44:04 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2023-10-12 16:33:30 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2023-06-21 11:44:04 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// контроллер с контактной информацией по скважине
|
|
|
|
|
/// </summary>
|
2023-10-13 16:58:27 +05:00
|
|
|
|
[Route("api/well/{idWell}/[controller]")]
|
2023-06-21 11:44:04 +05:00
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public class WellContactController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IWellContactService wellContactsRepository;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
|
|
|
|
public WellContactController(IWellContactService wellContactsRepository, IWellService wellService)
|
|
|
|
|
{
|
|
|
|
|
this.wellContactsRepository = wellContactsRepository;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// получение списка типов контактов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-16 11:48:59 +05:00
|
|
|
|
[HttpGet("type")]
|
2023-06-21 11:44:04 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<CompanyTypeDto>), (int)System.Net.HttpStatusCode.OK)]
|
2023-10-13 16:58:27 +05:00
|
|
|
|
public async Task<IActionResult> GetTypesAsync(CancellationToken token)
|
2023-06-21 11:44:04 +05:00
|
|
|
|
{
|
2023-10-13 16:58:27 +05:00
|
|
|
|
var result = await wellContactsRepository.GetTypesAsync(token);
|
2023-06-21 11:44:04 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-10-12 16:33:30 +05:00
|
|
|
|
/// Получение контактов по типу контакта и ключу скважины
|
2023-06-21 11:44:04 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">ключ скважины</param>
|
|
|
|
|
/// <param name="contactTypeId">тип контакта</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-16 11:48:59 +05:00
|
|
|
|
[HttpGet("type/{contactTypeId}")]
|
2023-10-12 16:33:30 +05:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> GetAllAsync(int idWell, int contactTypeId, CancellationToken token)
|
2023-06-21 11:44:04 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-10-13 16:58:27 +05:00
|
|
|
|
var result = await wellContactsRepository.GetAllAsync(idWell, contactTypeId, token);
|
2023-10-12 16:33:30 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение контакта по ключу
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">ключ скважины</param>
|
|
|
|
|
/// <param name="id">ключ контакта</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-13 16:58:27 +05:00
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[ProducesResponseType(typeof(ContactDto), (int)System.Net.HttpStatusCode.OK)]
|
2023-10-12 16:33:30 +05:00
|
|
|
|
public async Task<IActionResult> GetAsync(int idWell, int id, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-10-13 16:58:27 +05:00
|
|
|
|
var result = await wellContactsRepository.GetAsync(idWell, id, token);
|
2023-06-21 11:44:04 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 12:20:00 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// добавление нового контакта
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="contactDto">контакт</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost]
|
2023-10-12 16:33:30 +05:00
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2023-10-09 12:20:00 +05:00
|
|
|
|
public async Task<IActionResult> InsertAsync(
|
2023-10-12 16:33:30 +05:00
|
|
|
|
[FromBody] ContactDto contactDto,
|
|
|
|
|
CancellationToken token)
|
2023-10-09 12:20:00 +05:00
|
|
|
|
{
|
2023-10-12 16:33:30 +05:00
|
|
|
|
if (!await CanUserAccessToWellAsync(contactDto.IdWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-10-13 16:58:27 +05:00
|
|
|
|
var result = await wellContactsRepository.InsertAsync(contactDto, token);
|
2023-10-09 12:20:00 +05:00
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 11:44:04 +05:00
|
|
|
|
/// <summary>
|
2023-10-12 16:33:30 +05:00
|
|
|
|
/// изменение контакта
|
2023-06-21 11:44:04 +05:00
|
|
|
|
/// </summary>
|
2023-10-12 16:33:30 +05:00
|
|
|
|
/// <param name="contactDto">контакт</param>
|
2023-06-21 11:44:04 +05:00
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-10-12 16:33:30 +05:00
|
|
|
|
[HttpPut]
|
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> UpdateAsync(
|
|
|
|
|
[FromBody] ContactDto contactDto,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(contactDto.IdWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-10-13 16:58:27 +05:00
|
|
|
|
var result = await wellContactsRepository.UpdateAsync(contactDto, token);
|
2023-10-12 16:33:30 +05:00
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление контакта
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">ключ скважины</param>
|
|
|
|
|
/// <param name="id">id контакта</param>
|
|
|
|
|
/// <param name="token">Токен отмены задачи</param>
|
|
|
|
|
/// <returns>Количество удаленных из БД строк</returns>
|
2023-10-13 16:58:27 +05:00
|
|
|
|
[HttpDelete("{id}")]
|
2023-06-21 11:44:04 +05:00
|
|
|
|
[Permission]
|
2023-06-21 11:51:16 +05:00
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
2023-10-12 16:33:30 +05:00
|
|
|
|
public async Task<IActionResult> DeleteAsync(int idWell, int id, CancellationToken token)
|
2023-06-21 11:44:04 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2023-10-16 11:48:59 +05:00
|
|
|
|
var result = await wellContactsRepository.DeleteAsync(idWell, id, token);
|
2023-10-12 16:33:30 +05:00
|
|
|
|
|
2023-06-21 11:44:04 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 14:55:05 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Скопировать контакты в другую скважину
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell">ключ скважины, откуда копировать контакты</param>
|
|
|
|
|
/// <param name="idWellTarget">ключ скважины, куда копировать контакты</param>
|
|
|
|
|
/// <param name="contactIds"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
2024-07-25 15:38:14 +05:00
|
|
|
|
[HttpPost("copy/{idWellTarget}")]
|
2024-07-25 14:55:05 +05:00
|
|
|
|
[Permission]
|
|
|
|
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
|
|
|
|
public async Task<IActionResult> CopyAsync(
|
|
|
|
|
int idWell,
|
|
|
|
|
int idWellTarget,
|
|
|
|
|
[FromBody] IEnumerable<int> contactIds,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
if (!await CanUserAccessToWellAsync(idWellTarget, token).ConfigureAwait(false))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var result = await wellContactsRepository.CopyAsync(idWell, idWellTarget, contactIds, token);
|
|
|
|
|
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 11:44:04 +05:00
|
|
|
|
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
2023-10-13 16:58:27 +05:00
|
|
|
|
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token);
|
2023-06-21 11:44:04 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|