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

161 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
/// <summary>
/// контроллер по работе с faq-вопросами
/// </summary>
[Route("api/well/{idWell}/faq")]
[ApiController]
[Authorize]
public class FaqController : ControllerBase
{
private readonly IFaqRepository faqRepository;
private readonly IWellService wellService;
public FaqController(IFaqRepository faqRepository, IWellService wellService)
{
this.faqRepository = faqRepository;
this.wellService = wellService;
}
/// <summary>
/// получение списка faq-вопросов
/// </summary>
/// <param name="idWell"></param>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Permission]
[ProducesResponseType(typeof(IEnumerable<FaqDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync(
[FromRoute] int idWell,
[FromQuery] FaqRequestBase request,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
var requestToRepository = new FaqRequest(request, idWell);
var result = await faqRepository.GetAllAsync(
requestToRepository,
token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// добавление нового вопроса
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="faqDto">вопрос</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpPost]
[Permission]
[ProducesResponseType(typeof(FaqDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> InsertAsync(
[Range(1, int.MaxValue, ErrorMessage = "Id скважины не может быть меньше 1")] int idWell,
[FromBody] FaqDto faqDto,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
faqDto.IdWell = idWell;
faqDto.IdAuthorQuestion = User.GetUserId()!.Value;
var result = await faqRepository.InsertAsync(faqDto, token)
.ConfigureAwait(false);
return Ok(result);
}
[HttpPut]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> UpdateAsync(int idWell, [FromBody] FaqDto faqDto, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
faqDto.IdAuthorAnswer = User.GetUserId()!.Value;
var result = await faqRepository.UpdateAsync(faqDto, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Объединение 2 вопросов в один
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="sourceIds">ключи вопросов, подлежащих объединению</param>
/// <param name="mergeTexts">настройка, объединять ли текст 2-х вопросов в один или нет</param>
/// <param name="token"></param>
/// <returns></returns>
/// <exception cref="ArgumentInvalidException"></exception>
[HttpGet("merge")]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> MergeAsync(
int idWell,
[FromQuery] IEnumerable<int> sourceIds,
[FromQuery] bool mergeTexts,
CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
return Forbid();
if (sourceIds.Count() < 2)
throw new ArgumentInvalidException("Count of questions must be more than 1", nameof(sourceIds));
var result = await faqRepository.MergeAsync(sourceIds, mergeTexts, token)
.ConfigureAwait(false);
return Ok(result);
}
/// <summary>
/// Помечает вопрос как удаленный
/// </summary>
/// <param name="idWell">id скважины</param>
/// <param name="id">id вопроса</param>
/// <param name="token">Токен отмены задачи</param>
/// <returns>Количество удаленных из БД строк</returns>
[HttpPut("{id}")]
[Permission]
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> MarkAsDeletedAsync(int idWell, int id, CancellationToken token)
{
if (!await CanUserAccessToWellAsync(idWell,
token).ConfigureAwait(false))
return Forbid();
var result = await faqRepository.MarkAsDeletedAsync(idWell, 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);
}
}
#nullable disable
}