using AsbCloudApp.Data; using AsbCloudApp.Data.User; using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Repository; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { /// /// контроллер с контактной информацией по скважине /// [ApiController] [Route("api/[controller]")] [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("api/well/{idWell}/contacts/types")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetTypesAsync(int idWell, CancellationToken token) { var result = await wellContactsRepository.GetTypesAsync(idWell, token).ConfigureAwait(false); return Ok(result); } /// /// Получение контактов по типу контакта и ключу скважины /// /// ключ скважины /// тип контакта /// /// [HttpGet("api/well/{idWell}/contactType/{contactTypeId}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetAllAsync(int idWell, int contactTypeId, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellContactsRepository.GetAllAsync(idWell, contactTypeId, token).ConfigureAwait(false); return Ok(result); } /// /// Получение контакта по ключу /// /// ключ скважины /// ключ контакта /// /// [HttpGet("api/well/{idWell}/contact/{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(typeof(IEnumerable), (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).ConfigureAwait(false); return Ok(result); } /// /// добавление нового контакта /// /// контакт /// /// [HttpPost] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] 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) .ConfigureAwait(false); return Ok(result); } /// /// изменение контакта /// /// контакт /// /// [HttpPut] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] 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) .ConfigureAwait(false); return Ok(result); } /// /// Удаление контакта /// /// ключ скважины /// id контакта /// Токен отмены задачи /// Количество удаленных из БД строк [HttpDelete("api/well/{idWell}/contact/{id}")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status403Forbidden)] public async Task DeleteAsync(int idWell, int id, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await wellContactsRepository.DeleteAsync(id, token) .ConfigureAwait(false); 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).ConfigureAwait(false); } } }