using AsbCloudApp.Data;
using AsbCloudApp.Data.User;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers;
///
/// контроллер с контактной информацией по скважине
///
[Route("api/well/{idWell}/[controller]")]
[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;
}
///
/// получение списка типов контактов
///
///
///
[HttpGet("type")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetTypesAsync(CancellationToken token)
{
var result = await wellContactsRepository.GetTypesAsync(token);
return Ok(result);
}
///
/// Получение контактов по типу контакта и ключу скважины
///
/// ключ скважины
/// тип контакта
///
///
[HttpGet("type/{contactTypeId}")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetByTypeAsync(int idWell, int contactTypeId, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var request = new WellContactRequest()
{
IdsWells = new int[] { idWell },
ContactTypeId = contactTypeId
};
var result = await wellContactsRepository.GetAllAsync(request, token);
return Ok(result);
}
///
/// Получение контактов по массиву ключей скважины
///
/// ключи скважин
///
///
[HttpGet("/api/well/[controller]")]
[ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)]
public async Task GetAllAsync([FromQuery] IEnumerable idsWells, CancellationToken token)
{
foreach(var idWell in idsWells)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
}
var request = new WellContactRequest()
{
IdsWells = idsWells
};
var result = await wellContactsRepository.GetAllAsync(request, token);
return Ok(result);
}
///
/// Получение контакта по ключу
///
/// ключ скважины
/// ключ контакта
///
///
[HttpGet("{id}")]
[ProducesResponseType(typeof(ContactDto), (int)System.Net.HttpStatusCode.OK)]
public async Task GetAsync(int idWell, int id, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.GetAsync(idWell, id, token);
return Ok(result);
}
///
/// добавление нового контакта
///
/// контакт
///
///
[HttpPost]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task InsertAsync(
[FromBody] ContactDto contactDto,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(contactDto.IdWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.InsertAsync(contactDto, token);
return Ok(result);
}
///
/// изменение контакта
///
/// контакт
///
///
[HttpPut]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task UpdateAsync(
[FromBody] ContactDto contactDto,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(contactDto.IdWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.UpdateAsync(contactDto, token);
return Ok(result);
}
///
/// Удаление контакта
///
/// ключ скважины
/// id контакта
/// Токен отмены задачи
/// Количество удаленных из БД строк
[HttpDelete("{id}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task DeleteAsync(int idWell, int id, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var result = await wellContactsRepository.DeleteAsync(idWell, id, token);
return Ok(result);
}
///
/// Скопировать контакты в другую скважину
///
/// ключ скважины, откуда копировать контакты
/// ключ скважины, куда копировать контакты
///
///
///
[HttpPost("copy/{idWellTarget}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task CopyAsync(
int idWell,
int idWellTarget,
[FromBody] IEnumerable 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);
}
private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token);
}
}