forked from ddrilling/AsbCloudServer
174 lines
7.0 KiB
C#
174 lines
7.0 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// контроллер с контактной информацией по скважине
|
||
/// </summary>
|
||
[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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// получение списка типов контактов
|
||
/// </summary>
|
||
/// <param name="idWell">ключ скважины</param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("api/well/{idWell}/contacts/types")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
[ProducesResponseType(typeof(IEnumerable<CompanyTypeDto>), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> GetTypesAsync(int idWell, CancellationToken token)
|
||
{
|
||
var result = await wellContactsRepository.GetTypesAsync(idWell, token).ConfigureAwait(false);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение контактов по типу контакта и ключу скважины
|
||
/// </summary>
|
||
/// <param name="idWell">ключ скважины</param>
|
||
/// <param name="contactTypeId">тип контакта</param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("api/well/{idWell}/contactType/{contactTypeId}")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
|
||
public async Task<IActionResult> 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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение контакта по ключу
|
||
/// </summary>
|
||
/// <param name="idWell">ключ скважины</param>
|
||
/// <param name="id">ключ контакта</param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("api/well/{idWell}/contact/{id}")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
[ProducesResponseType(typeof(IEnumerable<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();
|
||
|
||
var result = await wellContactsRepository.GetAsync(idWell, id, token).ConfigureAwait(false);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// добавление нового контакта
|
||
/// </summary>
|
||
/// <param name="contactDto">контакт</param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[Permission]
|
||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
public async Task<IActionResult> 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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// изменение контакта
|
||
/// </summary>
|
||
/// <param name="contactDto">контакт</param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpPut]
|
||
[Permission]
|
||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
public async Task<IActionResult> 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);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// Удаление контакта
|
||
/// </summary>
|
||
/// <param name="idWell">ключ скважины</param>
|
||
/// <param name="id">id контакта</param>
|
||
/// <param name="token">Токен отмены задачи</param>
|
||
/// <returns>Количество удаленных из БД строк</returns>
|
||
[HttpDelete("api/well/{idWell}/contact/{id}")]
|
||
[Permission]
|
||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
public async Task<IActionResult> 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<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
|
||
{
|
||
int? idCompany = User.GetCompanyId();
|
||
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||
idWell, token).ConfigureAwait(false);
|
||
|
||
|
||
}
|
||
|
||
}
|
||
}
|