DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/WellContactController.cs

208 lines
7.4 KiB
C#
Raw Permalink Normal View History

using AsbCloudApp.Data;
2023-10-09 12:20:00 +05:00
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;
2024-08-19 10:01:07 +05:00
namespace AsbCloudWebApi.Controllers;
/// <summary>
/// контроллер с контактной информацией по скважине
/// </summary>
[Route("api/well/{idWell}/[controller]")]
[ApiController]
[Authorize]
public class WellContactController : ControllerBase
{
2024-08-19 10:01:07 +05:00
private readonly IWellContactService wellContactsRepository;
private readonly IWellService wellService;
public WellContactController(IWellContactService wellContactsRepository, IWellService wellService)
{
this.wellContactsRepository = wellContactsRepository;
this.wellService = wellService;
}
/// <summary>
2024-08-19 10:01:07 +05:00
/// получение списка типов контактов
/// </summary>
2024-08-19 10:01:07 +05:00
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("type")]
[ProducesResponseType(typeof(IEnumerable<CompanyTypeDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetTypesAsync(CancellationToken token)
{
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.GetTypesAsync(token);
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Получение контактов по типу контакта и ключу скважины
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="contactTypeId">тип контакта</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("type/{contactTypeId}")]
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetByTypeAsync(int idWell, int contactTypeId, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var request = new WellContactRequest()
{
2024-08-19 10:01:07 +05:00
IdsWells = new int[] { idWell },
ContactTypeId = contactTypeId
};
var result = await wellContactsRepository.GetAllAsync(request, token);
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Получение контактов по массиву ключей скважины
/// </summary>
/// <param name="idsWells">ключи скважин</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("/api/well/[controller]")]
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync([FromQuery] IEnumerable<int> idsWells, CancellationToken token)
{
foreach(var idWell in idsWells)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
}
2024-08-19 10:01:07 +05:00
var request = new WellContactRequest()
{
2024-08-19 10:01:07 +05:00
IdsWells = idsWells
};
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.GetAllAsync(request, token);
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Получение контакта по ключу
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="id">ключ контакта</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{id}")]
[ProducesResponseType(typeof(ContactDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAsync(int idWell, int id, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.GetAsync(idWell, id, token);
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// добавление нового контакта
/// </summary>
/// <param name="contactDto">контакт</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> InsertAsync(
[FromBody] ContactDto contactDto,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(contactDto.IdWell, token).ConfigureAwait(false))
return Forbid();
2023-10-09 12:20:00 +05:00
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.InsertAsync(contactDto, token);
2023-10-09 12:20:00 +05:00
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// изменение контакта
/// </summary>
/// <param name="contactDto">контакт</param>
/// <param name="token"></param>
/// <returns></returns>
[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();
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.UpdateAsync(contactDto, token);
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Удаление контакта
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="id">id контакта</param>
/// <param name="token">Токен отмены задачи</param>
/// <returns>Количество удаленных из БД строк</returns>
[HttpDelete("{id}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> DeleteAsync(int idWell, int id, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.DeleteAsync(idWell, id, token);
2024-08-19 10:01:07 +05:00
return Ok(result);
}
2024-08-19 10:01:07 +05:00
/// <summary>
/// Скопировать контакты в другую скважину
/// </summary>
/// <param name="idWell">ключ скважины, откуда копировать контакты</param>
/// <param name="idWellTarget">ключ скважины, куда копировать контакты</param>
/// <param name="contactIds"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost("copy/{idWellTarget}")]
[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();
2024-08-19 10:01:07 +05:00
if (!await CanUserAccessToWellAsync(idWellTarget, token).ConfigureAwait(false))
return Forbid();
2024-08-19 10:01:07 +05:00
var result = await wellContactsRepository.CopyAsync(idWell, idWellTarget, contactIds, token);
2024-08-19 10:01:07 +05:00
return Ok(result);
}
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
{
int? idCompany = User.GetCompanyId();
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token);
}
}